Skip to content

Commit 32f9b79

Browse files
authored
fix(cubesql): Realias expressions when normalizing columns (#9498)
1 parent cd7e9fd commit 32f9b79

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

rust/cubesql/cubesql/src/compile/rewrite/converter.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use datafusion::{
4646
error::DataFusionError,
4747
logical_plan::{
4848
build_join_schema, build_table_udf_schema, exprlist_to_fields,
49-
exprlist_to_fields_from_schema, normalize_cols,
49+
exprlist_to_fields_from_schema, normalize_col as df_normalize_col,
5050
plan::{Aggregate, Extension, Filter, Join, Projection, Sort, TableUDFs, Window},
5151
replace_col_to_expr, Column, CrossJoin, DFField, DFSchema, DFSchemaRef, Distinct,
5252
EmptyRelation, Expr, ExprRewritable, ExprRewriter, GroupingSet, Like, Limit, LogicalPlan,
@@ -2441,3 +2441,29 @@ fn replace_qualified_col_with_flat_name_if_missing(
24412441
})
24422442
.collect::<Result<Vec<_>, _>>()
24432443
}
2444+
2445+
/// Recursively normalize all Column expressions in a list of expression trees
2446+
fn normalize_cols(
2447+
exprs: impl IntoIterator<Item = impl Into<Expr>>,
2448+
plan: &LogicalPlan,
2449+
) -> Result<Vec<Expr>, CubeError> {
2450+
exprs
2451+
.into_iter()
2452+
.map(|e| normalize_col(e.into(), plan))
2453+
.collect()
2454+
}
2455+
2456+
/// Recursively call [`df_normalize_col`] on all Column expressions
2457+
/// in the `expr` expression tree, realiasing the expressions if the name is different.
2458+
fn normalize_col(expr: Expr, plan: &LogicalPlan) -> Result<Expr, CubeError> {
2459+
if let Expr::Alias(_, _) = expr {
2460+
return Ok(df_normalize_col(expr, plan)?);
2461+
}
2462+
let original_expr_name = expr_name(&expr)?;
2463+
let mut normalized_expr = df_normalize_col(expr, plan)?;
2464+
let normalized_expr_name = expr_name(&normalized_expr)?;
2465+
if original_expr_name != normalized_expr_name {
2466+
normalized_expr = normalized_expr.alias(&original_expr_name);
2467+
}
2468+
Ok(normalized_expr)
2469+
}

0 commit comments

Comments
 (0)