From d577d8c756e773646cd559bce3aa1bd0e1a05515 Mon Sep 17 00:00:00 2001 From: Alex Qyoun-ae <4062971+MazterQyou@users.noreply.github.com> Date: Tue, 22 Apr 2025 15:51:35 +0400 Subject: [PATCH] fix(cubesql): Realias expressions when normalizing columns Signed-off-by: Alex Qyoun-ae <4062971+MazterQyou@users.noreply.github.com> --- .../cubesql/src/compile/rewrite/converter.rs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/rust/cubesql/cubesql/src/compile/rewrite/converter.rs b/rust/cubesql/cubesql/src/compile/rewrite/converter.rs index 03b6a5cc26554..2fd8b9fbbdf6c 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/converter.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/converter.rs @@ -46,7 +46,7 @@ use datafusion::{ error::DataFusionError, logical_plan::{ build_join_schema, build_table_udf_schema, exprlist_to_fields, - exprlist_to_fields_from_schema, normalize_cols, + exprlist_to_fields_from_schema, normalize_col as df_normalize_col, plan::{Aggregate, Extension, Filter, Join, Projection, Sort, TableUDFs, Window}, replace_col_to_expr, Column, CrossJoin, DFField, DFSchema, DFSchemaRef, Distinct, EmptyRelation, Expr, ExprRewritable, ExprRewriter, GroupingSet, Like, Limit, LogicalPlan, @@ -2441,3 +2441,29 @@ fn replace_qualified_col_with_flat_name_if_missing( }) .collect::, _>>() } + +/// Recursively normalize all Column expressions in a list of expression trees +fn normalize_cols( + exprs: impl IntoIterator>, + plan: &LogicalPlan, +) -> Result, CubeError> { + exprs + .into_iter() + .map(|e| normalize_col(e.into(), plan)) + .collect() +} + +/// Recursively call [`df_normalize_col`] on all Column expressions +/// in the `expr` expression tree, realiasing the expressions if the name is different. +fn normalize_col(expr: Expr, plan: &LogicalPlan) -> Result { + if let Expr::Alias(_, _) = expr { + return Ok(df_normalize_col(expr, plan)?); + } + let original_expr_name = expr_name(&expr)?; + let mut normalized_expr = df_normalize_col(expr, plan)?; + let normalized_expr_name = expr_name(&normalized_expr)?; + if original_expr_name != normalized_expr_name { + normalized_expr = normalized_expr.alias(&original_expr_name); + } + Ok(normalized_expr) +}