diff --git a/rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs b/rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs index 077b9ecb5ecf3..762e4b9f7a470 100644 --- a/rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs +++ b/rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs @@ -901,10 +901,19 @@ impl CubeScanWrapperNode { }; if !next_remapping.contains_key(&Column::from_name(&alias)) { next_remapping.insert(original_alias_key, Column::from_name(&alias)); + let original_relation = if let Expr::Column(column) = &original_expr { + if column.relation.is_some() { + &column.relation + } else { + &from_alias + } + } else { + &from_alias + }; next_remapping.insert( Column { name: original_alias.clone(), - relation: from_alias.clone(), + relation: original_relation.clone(), }, Column { name: alias.clone(),