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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix object/nested field select issue (#584)
* Fix object and deep object select issue * Fix same field added more than once bug * Add more IT cases * Add doctest * Add doctest * Add doctest * Update doctest with more examples * Prepare PR
- Loading branch information
Showing
17 changed files
with
426 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"city": {"name": "Seattle", "location": {"latitude": 10.5}}, "accounts": [{"id": 1}, {"id": 2}], "projects": [{"name": "AWS Redshift Spectrum querying"}, {"name": "AWS Redshift security"}, {"name": "AWS Aurora security"}] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"city": { | ||
"properties": { | ||
"name": { | ||
"type": "keyword" | ||
}, | ||
"location": { | ||
"properties": { | ||
"latitude": { | ||
"type": "double" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"account": { | ||
"properties": { | ||
"id": { | ||
"type": "keyword" | ||
} | ||
} | ||
}, | ||
"projects": { | ||
"type": "nested", | ||
"properties": { | ||
"name": { | ||
"type": "keyword" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
...t/src/test/java/com/amazon/opendistroforelasticsearch/sql/legacy/ObjectFieldSelectIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.legacy; | ||
|
||
import static com.amazon.opendistroforelasticsearch.sql.legacy.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.legacy.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" | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.