From 5e5f48559ec060ed6b018515f33977dee0ada397 Mon Sep 17 00:00:00 2001 From: Peng Huo Date: Thu, 9 Apr 2020 09:58:56 -0700 Subject: [PATCH] Bug fix, support subquery in from doesn't have alias (#418) --- .../sql/parser/SubQueryParser.java | 5 ++-- .../sql/esintgtest/SubqueryIT.java | 26 +++++++++++++++++++ .../unittest/parser/SubQueryParserTest.java | 15 +++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/sql/parser/SubQueryParser.java b/src/main/java/com/amazon/opendistroforelasticsearch/sql/parser/SubQueryParser.java index ddf1ad4ead..7b0e19c947 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/sql/parser/SubQueryParser.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/sql/parser/SubQueryParser.java @@ -71,8 +71,9 @@ private Select pushSelect(List selectItems, Select subquerySelect * for example, subquerySelectItem is "SUM(emp.empno) as TEMP", * and final select list is TEMP. then return true. */ - if (fieldAliasRewriter.containsKey(field.getAlias())) { - field.setAlias(fieldAliasRewriter.get(field.getAlias()).apply(field.getAlias())); + String fieldIdentifier = Strings.isNullOrEmpty(field.getAlias()) ? field.getName() : field.getAlias(); + if (fieldAliasRewriter.containsKey(fieldIdentifier)) { + field.setAlias(fieldAliasRewriter.get(fieldIdentifier).apply(fieldIdentifier)); } else { fieldIterator.remove(); } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SubqueryIT.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SubqueryIT.java index 0c10d7ee84..b006b7a229 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SubqueryIT.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SubqueryIT.java @@ -36,6 +36,10 @@ import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.hitAll; import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvInt; import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.kvString; +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 static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.core.Is.is; @@ -353,4 +357,26 @@ public void selectFromSubqueryCountAndSum() throws IOException { assertThat(result.query("/aggregations/count/value"), equalTo(1000)); assertThat(result.query("/aggregations/balance/value"), equalTo(25714837.0)); } + + @Test + public void selectFromSubqueryWithoutAliasShouldPass() throws IOException { + JSONObject response = executeJdbcRequest( + StringUtils.format( + "SELECT a.firstname AS my_first, a.lastname AS my_last, a.age AS my_age " + + "FROM (SELECT firstname, lastname, age " + + "FROM %s " + + "WHERE age = 40 and account_number = 291) AS a", + TEST_INDEX_ACCOUNT)); + + verifySchema(response, + schema("firstname", "my_first", "text"), + schema("lastname", "my_last", "text"), + schema("age", "my_age", "long")); + verifyDataRows(response, + rows("Lynn", "Pollard", 40)); + } + + private JSONObject executeJdbcRequest(String query) { + return new JSONObject(executeQuery(query, "jdbc")); + } } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/parser/SubQueryParserTest.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/parser/SubQueryParserTest.java index fd05397ecd..cd0c1168c8 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/parser/SubQueryParserTest.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/parser/SubQueryParserTest.java @@ -46,6 +46,21 @@ public void selectFromSubqueryShouldPass() throws SqlParseException { assertEquals("balance1", select.getFields().get(1).getAlias()); } + @Test + public void selectFromSubqueryWithoutAliasShouldPass() throws SqlParseException { + Select select = parseSelect( + StringUtils.format( + "SELECT t.age as finalAge, t.balance as finalBalance " + + "FROM (SELECT age, balance FROM %s/account) t", + TEST_INDEX_ACCOUNT)); + + assertEquals(2, select.getFields().size()); + assertEquals("age", select.getFields().get(0).getName()); + assertEquals("finalAge", select.getFields().get(0).getAlias()); + assertEquals("balance", select.getFields().get(1).getName()); + assertEquals("finalBalance", select.getFields().get(1).getAlias()); + } + @Test public void selectFromSubqueryShouldIgnoreUnusedField() throws SqlParseException { Select select = parseSelect(