Skip to content

Apache DataFusion SQL Query Engine

refactor: share the REPLACE and validation steps of the wildcard arms in project_with_validation (#26312)

Yunus Ozenc6ab9b2a3dcf
diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rs
index fe97ab8752..332559f92b 100644
--- a/datafusion/expr/src/logical_plan/builder.rs
+++ b/datafusion/expr/src/logical_plan/builder.rs
@@ -2200,58 +2200,31 @@ fn project_with_validation(
let mut normalizer = ColumnNormalizer::new(&plan);
let columnizer = Columnizer::new(&plan);
for (e, validate) in expr {
- let e = e.into();
- match e {
+ let mut push_validated = |e: Expr| -> Result<()> {
+ if validate {
+ projected_expr.push(columnizer.columnize(normalizer.normalize(e)?)?)
+ } else {
+ projected_expr.push(e)
+ }
+ Ok(())
+ };
+ match e.into() {
SelectExpr::Wildcard(opt) => {
has_wildcard = true;
let expanded = expand_wildcard(plan.schema(), &plan, Some(&opt))?;
-
- // If there is a REPLACE statement, replace that column with the given
- // replace expression. Column name remains the same.
- let expanded = if let Some(replace) = opt.replace {
- replace_columns(expanded, &replace)?
- } else {
- expanded
- };
-
- for e in expanded {
- if validate {
- projected_expr
- .push(columnizer.columnize(normalizer.normalize(e)?)?)
- } else {
- projected_expr.push(e)
- }
- }
+ apply_replace(expanded, opt.replace.as_ref())?
+ .into_iter()
+ .try_for_each(push_validated)?;
}
SelectExpr::QualifiedWildcard(table_ref, opt) => {
has_wildcard = true;
let expanded =
expand_qualified_wildcard(&table_ref, plan.schema(), Some(&opt))?;
-
- // If there is a REPLACE statement, replace that column with the given
- // replace expression. Column name remains the same.
- let expanded = if let Some(replace) = opt.replace {
- replace_columns(expanded, &replace)?
- } else {
- expanded
- };
-
- for e in expanded {
- if validate {
- projected_expr
- .push(columnizer.columnize(normalizer.normalize(e)?)?)
- } else {
- projected_expr.push(e)
- }
- }
- }
- SelectExpr::Expression(e) => {
- if validate {
- projected_expr.push(columnizer.columnize(normalizer.normalize(e)?)?)
- } else {
- projected_expr.push(e)
- }
+ apply_replace(expanded, opt.replace.as_ref())?
+ .into_iter()
+ .try_for_each(push_validated)?;
}
+ SelectExpr::Expression(e) => push_validated(e)?,
}
}
@@ -2279,6 +2252,18 @@ fn project_with_validation(
Projection::try_new(projected_expr, Arc::new(plan)).map(LogicalPlan::Projection)
}
+/// Applies a wildcard's REPLACE statement, if it has one, to the columns the
+/// wildcard expanded to.
+fn apply_replace(
+ expanded: Vec<Expr>,
+ replace: Option<&PlannedReplaceSelectItem>,
+) -> Result<Vec<Expr>> {
+ match replace {
+ Some(replace) => replace_columns(expanded, replace),
+ None => Ok(expanded),
+ }
+}
+
/// If there is a REPLACE statement in the projected expression in the form of
/// "REPLACE (some_column_within_an_expr AS some_column)", this function replaces
/// that column with the given replace expression. Column name remains the same.