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

Fix select all from subquery issue #902

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
3 changes: 3 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,3 @@
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
chloe-zh marked this conversation as resolved.
Show resolved Hide resolved
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