From 16606e46a96250ba1340ed076cc4a302f888f738 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 3 Jan 2023 09:25:25 -0800 Subject: [PATCH] Fix back quoted alias of FROM subquery (#1189) (#1208) * Unquote from subquery alias Signed-off-by: Chen Dai * Add comparison test case Signed-off-by: Chen Dai * Add more comparison test case Signed-off-by: Chen Dai * Update doc to remove limitations Signed-off-by: Chen Dai Signed-off-by: Chen Dai (cherry picked from commit 91ef34d396d400aec0db5cad6cfdd9e665ddb9d9) Co-authored-by: Chen Dai --- docs/user/limitations/limitations.rst | 17 --------------- .../resources/correctness/bugfixes/550.txt | 2 ++ .../opensearch/sql/sql/parser/AstBuilder.java | 3 ++- .../sql/sql/parser/AstBuilderTest.java | 21 +++++++++++++++++++ 4 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 integ-test/src/test/resources/correctness/bugfixes/550.txt diff --git a/docs/user/limitations/limitations.rst b/docs/user/limitations/limitations.rst index d6e763a73d..8ce75a0e25 100644 --- a/docs/user/limitations/limitations.rst +++ b/docs/user/limitations/limitations.rst @@ -34,23 +34,6 @@ Aggregation over expression is not supported for now. You can only apply aggrega Here's a link to the Github issue - [Issue #288](https://github.com/opendistro-for-elasticsearch/sql/issues/288). -Limitations on Subqueries -========================= - -Subqueries in the FROM clause ------------------------------ - -Subquery in the `FROM` clause in this format: `SELECT outer FROM (SELECT inner)` is supported only when the query is merged into one query. For example, the following query is supported:: - - SELECT t.f, t.d - FROM ( - SELECT FlightNum as f, DestCountry as d - FROM opensearch_dashboards_sample_data_flights - WHERE OriginCountry = 'US') t - -But, if the outer query has `GROUP BY` or `ORDER BY`, then it's not supported. - - Limitations on JOINs ==================== diff --git a/integ-test/src/test/resources/correctness/bugfixes/550.txt b/integ-test/src/test/resources/correctness/bugfixes/550.txt new file mode 100644 index 0000000000..902e3b0d20 --- /dev/null +++ b/integ-test/src/test/resources/correctness/bugfixes/550.txt @@ -0,0 +1,2 @@ +SELECT ABS(`flights`.`AvgTicketPrice`) FROM (SELECT `AvgTicketPrice` FROM `opensearch_dashboards_sample_data_flights`) AS `flights` GROUP BY ABS(`flights`.`AvgTicketPrice`) +SELECT `b`.`Origin`, `b`.`avgPrice` FROM (SELECT `a`.`Origin` AS `Origin`, AVG(`AvgTicketPrice`) AS `avgPrice` FROM (SELECT `Origin`, `AvgTicketPrice` FROM `opensearch_dashboards_sample_data_flights` WHERE `FlightDelay` = True) AS `a` GROUP BY `a`.`Origin`) AS `b` ORDER BY `b`.`avgPrice` DESC diff --git a/sql/src/main/java/org/opensearch/sql/sql/parser/AstBuilder.java b/sql/src/main/java/org/opensearch/sql/sql/parser/AstBuilder.java index 6edce6eb15..02a7f7d1b9 100644 --- a/sql/src/main/java/org/opensearch/sql/sql/parser/AstBuilder.java +++ b/sql/src/main/java/org/opensearch/sql/sql/parser/AstBuilder.java @@ -170,7 +170,8 @@ public UnresolvedPlan visitTableAsRelation(TableAsRelationContext ctx) { @Override public UnresolvedPlan visitSubqueryAsRelation(SubqueryAsRelationContext ctx) { - return new RelationSubquery(visit(ctx.subquery), ctx.alias().getText()); + String subqueryAlias = StringUtils.unquoteIdentifier(ctx.alias().getText()); + return new RelationSubquery(visit(ctx.subquery), subqueryAlias); } @Override diff --git a/sql/src/test/java/org/opensearch/sql/sql/parser/AstBuilderTest.java b/sql/src/test/java/org/opensearch/sql/sql/parser/AstBuilderTest.java index 2aed4f2834..fafb143291 100644 --- a/sql/src/test/java/org/opensearch/sql/sql/parser/AstBuilderTest.java +++ b/sql/src/test/java/org/opensearch/sql/sql/parser/AstBuilderTest.java @@ -518,6 +518,27 @@ public void can_build_from_subquery() { ); } + @Test + public void can_build_from_subquery_with_backquoted_alias() { + assertEquals( + project( + relationSubquery( + project( + relation("test"), + alias("firstname", qualifiedName("firstname"), "firstName")), + "a"), + alias("a.firstName", qualifiedName("a", "firstName")) + ), + buildAST( + "SELECT a.firstName " + + "FROM ( " + + " SELECT `firstname` AS `firstName` " + + " FROM `test` " + + ") AS `a`" + ) + ); + } + @Test public void can_build_show_all_tables() { assertEquals(