-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathObjectFieldSelectIT.java
More file actions
113 lines (94 loc) · 3.76 KB
/
Copy pathObjectFieldSelectIT.java
File metadata and controls
113 lines (94 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.legacy;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DEEP_NESTED;
import static org.opensearch.sql.util.Capability.STRUCT_PARENT_FIELD;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.schema;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
import static org.opensearch.sql.util.MatcherUtils.verifySchema;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.opensearch.sql.common.setting.Settings;
import org.opensearch.sql.legacy.utils.StringUtils;
import org.opensearch.sql.util.RequiresCapability;
/**
* Integration test for OpenSearch object field (and nested field). This class is focused on simple
* SELECT-FROM query to ensure right column number and value is returned.
*/
@RequiresCapability(STRUCT_PARENT_FIELD)
public class ObjectFieldSelectIT extends SQLIntegTestCase {
@Override
protected void init() throws Exception {
loadIndex(Index.DEEP_NESTED);
}
@Test
public void testSelectObjectFieldItself() {
JSONObject response = new JSONObject(query("SELECT city FROM %s"));
verifySchema(response, schema("city", null, "object"));
// Expect object field itself is returned in a single cell
verifyDataRows(
response,
rows(
new JSONObject(
"{\n"
+ " \"name\": \"Seattle\",\n"
+ " \"location\": {\"latitude\": 10.5}\n"
+ "}")));
}
@Test
public void testSelectObjectInnerFields() {
JSONObject response =
new JSONObject(query("SELECT city.location, city.location.latitude FROM %s"));
verifySchema(
response,
schema("city.location", null, "object"),
schema("city.location.latitude", null, "double"));
// Expect inner regular or object field returned in its single cell
verifyDataRows(response, rows(new JSONObject("{\"latitude\": 10.5}"), 10.5));
}
@Test
public void testSelectNestedFieldItself() {
JSONObject response = new JSONObject(query("SELECT projects FROM %s"));
verifySchema(response, schema("projects", null, "nested"));
// Expect nested field itself is returned in a single cell
verifyDataRows(
response,
rows(
new JSONArray(
"[\n"
+ " {\"name\": \"AWS Redshift Spectrum querying\"},\n"
+ " {\"name\": \"AWS Redshift security\"},\n"
+ " {\"name\": \"AWS Aurora security\"}\n"
+ "]")));
}
@Test
public void testSelectObjectFieldOfArrayValuesItself() {
JSONObject response = new JSONObject(query("SELECT accounts FROM %s"));
verifyDataRows(response, rows(new JSONArray("[{\"id\":1},{\"id\":2}]")));
}
@Test
public void testSelectObjectFieldOfArrayValuesItselfNoFieldTypeTolerance() throws Exception {
updateClusterSettings(
new ClusterSetting(PERSISTENT, Settings.Key.FIELD_TYPE_TOLERANCE.getKeyValue(), "false"));
try {
JSONObject response = new JSONObject(query("SELECT accounts FROM %s"));
verifyDataRows(response, rows(new JSONObject("{\"id\":1}")));
} finally {
updateClusterSettings(
new ClusterSetting(PERSISTENT, Settings.Key.FIELD_TYPE_TOLERANCE.getKeyValue(), "true"));
}
}
@Test
public void testSelectObjectFieldOfArrayValuesInnerFields() {
JSONObject response = new JSONObject(query("SELECT accounts.id FROM %s"));
// Only the first element of the list of is returned.
verifyDataRows(response, rows(1));
}
private String query(String sql) {
return executeQuery(StringUtils.format(sql, TEST_INDEX_DEEP_NESTED), "jdbc");
}
}