@@ -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
0 commit comments