Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix select all from subquery issue (#902)
Browse files Browse the repository at this point in the history
* fix select all from subquery issue

* added a test case in comparison test
  • Loading branch information
chloe-zh authored and joshuali925 committed Dec 9, 2020
1 parent e58969b commit 9aacadf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public LogicalPlan visitRelation(Relation node, AnalysisContext context) {
@Override
public LogicalPlan visitRelationSubquery(RelationSubquery node, AnalysisContext context) {
LogicalPlan subquery = analyze(node.getChild().get(0), context);
context.push();
// inherit the parent environment to keep the subquery fields in current environment
TypeEnvironment curEnv = context.peek();

// Put subquery alias in index namespace so the qualifier can be removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.amazon.opendistroforelasticsearch.sql.ast.expression.AggregateFunction;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Alias;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AllFields;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.And;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Argument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Case;
Expand Down Expand Up @@ -263,6 +264,10 @@ public static UnresolvedArgument unresolvedArg(String argName, UnresolvedExpress
return new UnresolvedArgument(argName, argValue);
}

public AllFields allFields() {
return AllFields.of();
}

public Field field(UnresolvedExpression field) {
return new Field((QualifiedName) field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public void window_function() {
/**
* SELECT name FROM (
* SELECT name, age FROM test
* ) AS a.
* ) AS schema.
*/
@Test
public void from_subquery() {
Expand All @@ -418,6 +418,33 @@ public void from_subquery() {
);
}

/**
* SELECT * FROM (
* SELECT name FROM test
* ) AS schema.
*/
@Test
public void select_all_from_subquery() {
assertAnalyzeEqual(
LogicalPlanDSL.project(
LogicalPlanDSL.project(
LogicalPlanDSL.relation("schema"),
DSL.named("string_value", DSL.ref("string_value", STRING))),
DSL.named("string_value", DSL.ref("string_value", STRING))
),
AstDSL.project(
AstDSL.relationSubquery(
AstDSL.project(
AstDSL.relation("schema"),
AstDSL.alias("string_value", AstDSL.qualifiedName("string_value"))
),
"schema"
),
AstDSL.allFields()
)
);
}

/**
* SELECT name, AVG(age) FROM test GROUP BY name.
*/
Expand Down
4 changes: 4 additions & 0 deletions integ-test/src/test/resources/correctness/bugfixes/901.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT * FROM (SELECT Origin, Dest, AvgTicketPrice FROM kibana_sample_data_flights) AS flights
SELECT * FROM (SELECT Origin AS origin FROM kibana_sample_data_flights) AS flights
SELECT * FROM (SELECT * FROM (SELECT Origin FROM kibana_sample_data_flights) AS flights) AS f
SELECT Origin FROM (SELECT * FROM kibana_sample_data_flights) AS flights
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ SELECT Origin, Dest FROM (SELECT * FROM kibana_sample_data_flights WHERE AvgTick
SELECT Origin, MIN(AvgTicketPrice) FROM (SELECT * FROM kibana_sample_data_flights) AS flights GROUP BY Origin ORDER BY MAX(AvgTicketPrice)
SELECT Origin FROM (SELECT Origin, AvgTicketPrice FROM kibana_sample_data_flights) AS flights GROUP BY Origin HAVING MIN(AvgTicketPrice) > 500
SELECT avg_price FROM (SELECT AVG(AvgTicketPrice) AS avg_price FROM kibana_sample_data_flights) AS flights
SELECT Dest FROM (SELECT Dest, OriginWeather FROM (SELECT Dest, OriginWeather, AvgTicketPrice FROM (SELECT Dest, Origin, OriginWeather, AvgTicketPrice FROM kibana_sample_data_flights WHERE Origin = 'Zurich Airport') AS flights_data WHERE AvgTicketPrice < 10000) AS flights WHERE OriginWeather = 'Clear') AS f
SELECT Dest FROM (SELECT Dest, OriginWeather FROM (SELECT Dest, OriginWeather, AvgTicketPrice FROM (SELECT Dest, Origin, OriginWeather, AvgTicketPrice FROM kibana_sample_data_flights WHERE Origin = 'Zurich Airport') AS flights_data WHERE AvgTicketPrice < 10000) AS flights WHERE OriginWeather = 'Clear') AS f
SELECT * FROM (SELECT Origin FROM kibana_sample_data_flights) AS flights

0 comments on commit 9aacadf

Please sign in to comment.