Skip to content

Commit 49e0cac

Browse files
dai-chengithub-actions[bot]
authored andcommitted
Fix back quoted alias of FROM subquery (#1189)
* Unquote from subquery alias Signed-off-by: Chen Dai <[email protected]> * Add comparison test case Signed-off-by: Chen Dai <[email protected]> * Add more comparison test case Signed-off-by: Chen Dai <[email protected]> * Update doc to remove limitations Signed-off-by: Chen Dai <[email protected]> Signed-off-by: Chen Dai <[email protected]> (cherry picked from commit 91ef34d)
1 parent 13474e3 commit 49e0cac

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

docs/user/limitations/limitations.rst

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,6 @@ Aggregation over expression is not supported for now. You can only apply aggrega
3434
Here's a link to the Github issue - [Issue #288](https://github.com/opendistro-for-elasticsearch/sql/issues/288).
3535

3636

37-
Limitations on Subqueries
38-
=========================
39-
40-
Subqueries in the FROM clause
41-
-----------------------------
42-
43-
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::
44-
45-
SELECT t.f, t.d
46-
FROM (
47-
SELECT FlightNum as f, DestCountry as d
48-
FROM opensearch_dashboards_sample_data_flights
49-
WHERE OriginCountry = 'US') t
50-
51-
But, if the outer query has `GROUP BY` or `ORDER BY`, then it's not supported.
52-
53-
5437
Limitations on JOINs
5538
====================
5639

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT ABS(`flights`.`AvgTicketPrice`) FROM (SELECT `AvgTicketPrice` FROM `opensearch_dashboards_sample_data_flights`) AS `flights` GROUP BY ABS(`flights`.`AvgTicketPrice`)
2+
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

sql/src/main/java/org/opensearch/sql/sql/parser/AstBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ public UnresolvedPlan visitTableAsRelation(TableAsRelationContext ctx) {
170170

171171
@Override
172172
public UnresolvedPlan visitSubqueryAsRelation(SubqueryAsRelationContext ctx) {
173-
return new RelationSubquery(visit(ctx.subquery), ctx.alias().getText());
173+
String subqueryAlias = StringUtils.unquoteIdentifier(ctx.alias().getText());
174+
return new RelationSubquery(visit(ctx.subquery), subqueryAlias);
174175
}
175176

176177
@Override

sql/src/test/java/org/opensearch/sql/sql/parser/AstBuilderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,27 @@ public void can_build_from_subquery() {
518518
);
519519
}
520520

521+
@Test
522+
public void can_build_from_subquery_with_backquoted_alias() {
523+
assertEquals(
524+
project(
525+
relationSubquery(
526+
project(
527+
relation("test"),
528+
alias("firstname", qualifiedName("firstname"), "firstName")),
529+
"a"),
530+
alias("a.firstName", qualifiedName("a", "firstName"))
531+
),
532+
buildAST(
533+
"SELECT a.firstName "
534+
+ "FROM ( "
535+
+ " SELECT `firstname` AS `firstName` "
536+
+ " FROM `test` "
537+
+ ") AS `a`"
538+
)
539+
);
540+
}
541+
521542
@Test
522543
public void can_build_show_all_tables() {
523544
assertEquals(

0 commit comments

Comments
 (0)