Skip to content

Commit

Permalink
Fix back quoted alias of FROM subquery (opensearch-project#1189)
Browse files Browse the repository at this point in the history
* Unquote from subquery alias

Signed-off-by: Chen Dai <daichen@amazon.com>

* Add comparison test case

Signed-off-by: Chen Dai <daichen@amazon.com>

* Add more comparison test case

Signed-off-by: Chen Dai <daichen@amazon.com>

* Update doc to remove limitations

Signed-off-by: Chen Dai <daichen@amazon.com>

Signed-off-by: Chen Dai <daichen@amazon.com>
  • Loading branch information
dai-chen authored Dec 23, 2022
1 parent 0a02009 commit 91ef34d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
17 changes: 0 additions & 17 deletions docs/user/limitations/limitations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
====================

Expand Down
2 changes: 2 additions & 0 deletions integ-test/src/test/resources/correctness/bugfixes/550.txt
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 91ef34d

Please sign in to comment.