Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Fix back quoted alias of FROM subquery #1208

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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