Skip to content

Commit 9357526

Browse files
committed
refactor(cubesql): Simplify aliases handling in Remapper
1 parent c759ebc commit 9357526

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -366,32 +366,25 @@ impl Remapper {
366366

367367
fn insert_new_alias(&mut self, original_column: &Column, new_alias: &String) {
368368
self.used_targets.insert(new_alias.clone());
369-
self.remapping.insert(
369+
370+
let target = match &self.from_alias {
371+
// When result of this remapper will be used with FROM alias, it should always be safe to use it in target
372+
Some(from_alias) => Column {
373+
name: new_alias.clone(),
374+
relation: Some(from_alias.clone()),
375+
},
376+
None => Column::from_name(new_alias),
377+
};
378+
379+
for source in [
380+
// This is not exactly safe to use any unqualified name as a source
381+
// But we rely here on following validation from DF:
382+
// If any column name is ambiguous, then it would not be used as unqualified column expression
383+
// And then this source column would not be used ever
370384
Column::from_name(&original_column.name),
371-
Column::from_name(new_alias),
372-
);
373-
if let Some(from_alias) = &self.from_alias {
374-
self.remapping.insert(
375-
Column {
376-
name: original_column.name.clone(),
377-
relation: Some(from_alias.clone()),
378-
},
379-
Column {
380-
name: new_alias.clone(),
381-
relation: Some(from_alias.clone()),
382-
},
383-
);
384-
if let Some(original_relation) = &original_column.relation {
385-
if original_relation != from_alias {
386-
self.remapping.insert(
387-
original_column.clone(),
388-
Column {
389-
name: new_alias.clone(),
390-
relation: Some(from_alias.clone()),
391-
},
392-
);
393-
}
394-
}
385+
original_column.clone(),
386+
] {
387+
self.remapping.insert(source, target.clone());
395388
}
396389
}
397390

rust/cubesql/cubesql/src/compile/test/test_wrapper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ async fn test_case_wrapper_alias_with_order() {
807807
.wrapped_sql
808808
.unwrap()
809809
.sql
810-
.contains("ORDER BY \"case_when_a_cust\""));
810+
.contains("ORDER BY \"a\".\"case_when_a_cust\""));
811811

812812
let physical_plan = query_plan.as_physical_plan().await.unwrap();
813813
println!(
@@ -935,7 +935,7 @@ async fn test_case_wrapper_ungrouped_sorted_aliased() {
935935
.unwrap()
936936
.sql
937937
// TODO test without depend on column name
938-
.contains("ORDER BY \"case_when"));
938+
.contains("ORDER BY \"a\".\"case_when"));
939939
}
940940

941941
#[tokio::test]

0 commit comments

Comments
 (0)