From e5dfd175952f121d17e3e51b4187d8192108c7fb Mon Sep 17 00:00:00 2001 From: Chloe Date: Wed, 9 Dec 2020 10:46:53 -0800 Subject: [PATCH] Fix select all from subquery issue (#902) * fix select all from subquery issue * added a test case in comparison test --- .../sql/analysis/Analyzer.java | 2 +- .../sql/ast/dsl/AstDSL.java | 5 ++++ .../sql/analysis/AnalyzerTest.java | 29 ++++++++++++++++++- .../resources/correctness/bugfixes/901.txt | 4 +++ .../correctness/queries/subquries.txt | 3 +- 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 integ-test/src/test/resources/correctness/bugfixes/901.txt diff --git a/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/Analyzer.java b/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/Analyzer.java index c811bd9f6a..c497284c2b 100644 --- a/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/Analyzer.java +++ b/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/analysis/Analyzer.java @@ -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 diff --git a/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/dsl/AstDSL.java b/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/dsl/AstDSL.java index 35a6e35f00..24c6ca8116 100644 --- a/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/dsl/AstDSL.java +++ b/core/src/main/java/com/amazon/opendistroforelasticsearch/sql/ast/dsl/AstDSL.java @@ -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; @@ -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); } diff --git a/core/src/test/java/com/amazon/opendistroforelasticsearch/sql/analysis/AnalyzerTest.java b/core/src/test/java/com/amazon/opendistroforelasticsearch/sql/analysis/AnalyzerTest.java index 0548cb683d..f62b2ec67d 100644 --- a/core/src/test/java/com/amazon/opendistroforelasticsearch/sql/analysis/AnalyzerTest.java +++ b/core/src/test/java/com/amazon/opendistroforelasticsearch/sql/analysis/AnalyzerTest.java @@ -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() { @@ -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. */ diff --git a/integ-test/src/test/resources/correctness/bugfixes/901.txt b/integ-test/src/test/resources/correctness/bugfixes/901.txt new file mode 100644 index 0000000000..fe3cdbb86d --- /dev/null +++ b/integ-test/src/test/resources/correctness/bugfixes/901.txt @@ -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 \ No newline at end of file diff --git a/integ-test/src/test/resources/correctness/queries/subquries.txt b/integ-test/src/test/resources/correctness/queries/subquries.txt index 8c19d6538c..de7a35e3da 100644 --- a/integ-test/src/test/resources/correctness/queries/subquries.txt +++ b/integ-test/src/test/resources/correctness/queries/subquries.txt @@ -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 \ No newline at end of file +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 \ No newline at end of file