This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
ObjectFieldSelectIT.java
127 lines (107 loc) Β· 4.04 KB
/
ObjectFieldSelectIT.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/
package com.amazon.opendistroforelasticsearch.sql.esintgtest;
import static com.amazon.opendistroforelasticsearch.sql.esintgtest.TestsConstants.TEST_INDEX_DEEP_NESTED;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.rows;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.schema;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifyDataRows;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifySchema;
import com.amazon.opendistroforelasticsearch.sql.utils.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
/**
* Integration test for Elasticsearch object field (and nested field).
* This class is focused on simple SELECT-FROM query to ensure right column
* number and value is returned.
*/
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"));
// Nested field is absent in ES Get Field Mapping response either hence "object" used
verifySchema(response, schema("projects", null, "object"));
// 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"));
// Expect the entire list of values is returned just like a nested field
verifyDataRows(response,
rows(new JSONArray(
"[\n"
+ " {\"id\": 1},\n"
+ " {\"id\": 2}\n"
+ "]")
)
);
}
@Test
public void testSelectObjectFieldOfArrayValuesInnerFields() {
JSONObject response = new JSONObject(query("SELECT accounts.id FROM %s"));
// We don't support flatten object field of list value so expect null returned
verifyDataRows(response, rows(JSONObject.NULL));
}
private String query(String sql) {
return executeQuery(
StringUtils.format(sql, TEST_INDEX_DEEP_NESTED),
"jdbc"
);
}
}