-
Notifications
You must be signed in to change notification settings - Fork 1.9k
refactor code about query -> plan for subqueries
#4559
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -406,7 +406,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| query: Query, | ||
| planner_context: &mut PlannerContext, | ||
| ) -> Result<LogicalPlan> { | ||
| self.query_to_plan_with_alias(query, None, planner_context, None) | ||
| self.query_to_plan_with_schema(query, planner_context, None) | ||
| } | ||
|
|
||
| /// Generate a logical plan from a SQL subquery | ||
|
|
@@ -416,19 +416,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| planner_context: &mut PlannerContext, | ||
| outer_query_schema: &DFSchema, | ||
| ) -> Result<LogicalPlan> { | ||
| self.query_to_plan_with_alias( | ||
| query, | ||
| None, | ||
| planner_context, | ||
| Some(outer_query_schema), | ||
| ) | ||
| self.query_to_plan_with_schema(query, planner_context, Some(outer_query_schema)) | ||
| } | ||
|
|
||
| /// Generate a logic plan from an SQL query with optional alias | ||
| pub fn query_to_plan_with_alias( | ||
| /// Generate a logic plan from an SQL query. | ||
| /// It's implementation of `subquery_to_plan` and `query_to_plan`. | ||
| /// It shouldn't be invoked directly. | ||
| fn query_to_plan_with_schema( | ||
| &self, | ||
| query: Query, | ||
| alias: Option<String>, | ||
| planner_context: &mut PlannerContext, | ||
| outer_query_schema: Option<&DFSchema>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| ) -> Result<LogicalPlan> { | ||
|
|
@@ -452,12 +448,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| )))); | ||
| } | ||
| // create logical plan & pass backreferencing CTEs | ||
| let logical_plan = self.query_to_plan_with_alias( | ||
| *cte.query, | ||
| None, | ||
| &mut planner_context.clone(), | ||
| outer_query_schema, | ||
| )?; | ||
|
Comment on lines
-455
to
-460
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just |
||
| // CTE expr don't need extend outer_query_schema | ||
| let logical_plan = | ||
| self.query_to_plan(*cte.query, &mut planner_context.clone())?; | ||
|
|
||
| // Each `WITH` block can change the column names in the last | ||
| // projection (e.g. "WITH table(t1, t2) AS SELECT 1, 2"). | ||
|
|
@@ -467,21 +460,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| } | ||
| } | ||
| let plan = | ||
| self.set_expr_to_plan(*set_expr, alias, planner_context, outer_query_schema)?; | ||
| self.set_expr_to_plan(*set_expr, planner_context, outer_query_schema)?; | ||
| let plan = self.order_by(plan, query.order_by)?; | ||
| self.limit(plan, query.offset, query.limit) | ||
| } | ||
|
|
||
| fn set_expr_to_plan( | ||
| &self, | ||
| set_expr: SetExpr, | ||
| alias: Option<String>, | ||
| planner_context: &mut PlannerContext, | ||
| outer_query_schema: Option<&DFSchema>, | ||
| ) -> Result<LogicalPlan> { | ||
| match set_expr { | ||
| SetExpr::Select(s) => { | ||
| self.select_to_plan(*s, planner_context, alias, outer_query_schema) | ||
| self.select_to_plan(*s, planner_context, outer_query_schema) | ||
| } | ||
| SetExpr::Values(v) => self.sql_values_to_plan(v), | ||
| SetExpr::SetOperation { | ||
|
|
@@ -495,18 +487,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| SetQuantifier::Distinct | SetQuantifier::None => false, | ||
| }; | ||
|
|
||
| let left_plan = self.set_expr_to_plan( | ||
| *left, | ||
| None, | ||
| planner_context, | ||
| outer_query_schema, | ||
| )?; | ||
| let right_plan = self.set_expr_to_plan( | ||
| *right, | ||
| None, | ||
| planner_context, | ||
| outer_query_schema, | ||
| )?; | ||
| let left_plan = | ||
| self.set_expr_to_plan(*left, planner_context, outer_query_schema)?; | ||
| let right_plan = | ||
| self.set_expr_to_plan(*right, planner_context, outer_query_schema)?; | ||
| match (op, all) { | ||
| (SetOperator::Union, true) => LogicalPlanBuilder::from(left_plan) | ||
| .union(right_plan)? | ||
|
|
@@ -924,12 +908,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| TableFactor::Derived { | ||
| subquery, alias, .. | ||
| } => { | ||
| let logical_plan = self.query_to_plan_with_alias( | ||
| *subquery, | ||
| None, | ||
| planner_context, | ||
| outer_query_schema, | ||
| )?; | ||
| let logical_plan = self.query_to_plan(*subquery, planner_context)?; | ||
| (logical_plan, alias) | ||
| } | ||
| TableFactor::NestedJoin { | ||
|
|
@@ -1068,7 +1047,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| &self, | ||
| select: Select, | ||
| planner_context: &mut PlannerContext, | ||
| alias: Option<String>, | ||
| outer_query_schema: Option<&DFSchema>, | ||
| ) -> Result<LogicalPlan> { | ||
| // check for unsupported syntax first | ||
|
|
@@ -1238,13 +1216,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
| }; | ||
|
|
||
| // final projection | ||
| let mut plan = project(plan, select_exprs_post_aggr)?; | ||
| plan = match alias { | ||
| Some(alias) => { | ||
| LogicalPlan::SubqueryAlias(SubqueryAlias::try_new(plan, &alias)?) | ||
| } | ||
| None => plan, | ||
| }; | ||
| let plan = project(plan, select_exprs_post_aggr)?; | ||
|
|
||
| // process distinct clause | ||
| let plan = if select.distinct { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API makes a lot of sense -- thank you @jackwener