-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix subquery alias #1067
Merged
Merged
fix subquery alias #1067
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -236,34 +236,22 @@ impl LogicalPlanBuilder { | |
Ok(Self::from(table_scan)) | ||
} | ||
|
||
/// Apply a projection. | ||
/// | ||
/// # Errors | ||
/// This function errors under any of the following conditions: | ||
/// * Two or more expressions have the same name | ||
/// * An invalid expression is used (e.g. a `sort` expression) | ||
/// Apply a projection without alias. | ||
pub fn project(&self, expr: impl IntoIterator<Item = Expr>) -> Result<Self> { | ||
let input_schema = self.plan.schema(); | ||
let mut projected_expr = vec![]; | ||
for e in expr { | ||
match e { | ||
Expr::Wildcard => { | ||
projected_expr.extend(expand_wildcard(input_schema, &self.plan)?) | ||
} | ||
_ => projected_expr | ||
.push(columnize_expr(normalize_col(e, &self.plan)?, input_schema)), | ||
} | ||
} | ||
|
||
validate_unique_names("Projections", projected_expr.iter(), input_schema)?; | ||
|
||
let schema = DFSchema::new(exprlist_to_fields(&projected_expr, input_schema)?)?; | ||
self.project_with_alias(expr, None) | ||
} | ||
|
||
Ok(Self::from(LogicalPlan::Projection { | ||
expr: projected_expr, | ||
input: Arc::new(self.plan.clone()), | ||
schema: DFSchemaRef::new(schema), | ||
})) | ||
/// Apply a projection with alias | ||
pub fn project_with_alias( | ||
&self, | ||
expr: impl IntoIterator<Item = Expr>, | ||
alias: Option<String>, | ||
) -> Result<Self> { | ||
Ok(Self::from(project_with_alias( | ||
self.plan.clone(), | ||
expr, | ||
alias, | ||
)?)) | ||
} | ||
|
||
/// Apply a filter | ||
|
@@ -477,12 +465,9 @@ impl LogicalPlanBuilder { | |
let group_expr = normalize_cols(group_expr, &self.plan)?; | ||
let aggr_expr = normalize_cols(aggr_expr, &self.plan)?; | ||
let all_expr = group_expr.iter().chain(aggr_expr.iter()); | ||
|
||
validate_unique_names("Aggregations", all_expr.clone(), self.plan.schema())?; | ||
|
||
let aggr_schema = | ||
DFSchema::new(exprlist_to_fields(all_expr, self.plan.schema())?)?; | ||
|
||
Ok(Self::from(LogicalPlan::Aggregate { | ||
input: Arc::new(self.plan.clone()), | ||
group_expr, | ||
|
@@ -615,6 +600,41 @@ pub fn union_with_alias( | |
}) | ||
} | ||
|
||
/// Project with optional alias | ||
/// # Errors | ||
/// This function errors under any of the following conditions: | ||
/// * Two or more expressions have the same name | ||
/// * An invalid expression is used (e.g. a `sort` expression) | ||
pub fn project_with_alias( | ||
plan: LogicalPlan, | ||
expr: impl IntoIterator<Item = Expr>, | ||
alias: Option<String>, | ||
) -> Result<LogicalPlan> { | ||
let input_schema = plan.schema(); | ||
let mut projected_expr = vec![]; | ||
for e in expr { | ||
match e { | ||
Expr::Wildcard => { | ||
projected_expr.extend(expand_wildcard(input_schema, &plan)?) | ||
} | ||
_ => projected_expr | ||
.push(columnize_expr(normalize_col(e, &plan)?, input_schema)), | ||
} | ||
} | ||
validate_unique_names("Projections", projected_expr.iter(), input_schema)?; | ||
let input_schema = DFSchema::new(exprlist_to_fields(&projected_expr, input_schema)?)?; | ||
let schema = match alias { | ||
Some(ref alias) => input_schema.replace_qualifier(alias.as_str()), | ||
None => input_schema, | ||
}; | ||
Ok(LogicalPlan::Projection { | ||
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. 👍 |
||
expr: projected_expr, | ||
input: Arc::new(plan.clone()), | ||
schema: DFSchemaRef::new(schema), | ||
alias, | ||
}) | ||
} | ||
|
||
/// Resolves an `Expr::Wildcard` to a collection of `Expr::Column`'s. | ||
pub(crate) fn expand_wildcard( | ||
schema: &DFSchema, | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
👍