@@ -203,7 +203,6 @@ def from_union(
203203 def select (
204204 self ,
205205 selected_cols : tuple [tuple [str , sge .Expression ], ...],
206- squash_selections : bool = True ,
207206 ) -> SQLGlotIR :
208207 selections = [
209208 sge .Alias (
@@ -213,15 +212,6 @@ def select(
213212 for id , expr in selected_cols
214213 ]
215214
216- # If squashing is enabled, we try to simplify the selections
217- # by checking if the new selections are simply aliases of the
218- # original columns.
219- if squash_selections :
220- new_selections = _squash_selections (self .expr .expressions , selections )
221- if new_selections != []:
222- new_expr = self .expr .select (* new_selections , append = False )
223- return SQLGlotIR (expr = new_expr , uid_gen = self .uid_gen )
224-
225215 new_expr = self ._encapsulate_as_cte ().select (* selections , append = False )
226216 return SQLGlotIR (expr = new_expr , uid_gen = self .uid_gen )
227217
@@ -361,63 +351,3 @@ def _table(table: bigquery.TableReference) -> sge.Table:
361351 db = sg .to_identifier (table .dataset_id , quoted = True ),
362352 catalog = sg .to_identifier (table .project , quoted = True ),
363353 )
364-
365-
366- def _squash_selections (
367- old_expr : list [sge .Expression ], new_expr : list [sge .Alias ]
368- ) -> list [sge .Alias ]:
369- """
370- TODO: Reanble this function to optimize the SQL.
371- Simplifies the select column expressions if existing (old_expr) and
372- new (new_expr) selected columns are both simple aliases of column definitions.
373-
374- Example:
375- old_expr: [A AS X, B AS Y]
376- new_expr: [X AS P, Y AS Q]
377- Result: [A AS P, B AS Q]
378- """
379- old_alias_map : typing .Dict [str , str ] = {}
380- for selected in old_expr :
381- column_alias_pair = _get_column_alias_pair (selected )
382- if column_alias_pair is None :
383- return []
384- else :
385- old_alias_map [column_alias_pair [1 ]] = column_alias_pair [0 ]
386-
387- new_selected_cols : typing .List [sge .Alias ] = []
388- for selected in new_expr :
389- column_alias_pair = _get_column_alias_pair (selected )
390- if column_alias_pair is None or column_alias_pair [0 ] not in old_alias_map :
391- return []
392- else :
393- new_alias_expr = sge .Alias (
394- this = sge .ColumnDef (
395- this = sge .to_identifier (
396- old_alias_map [column_alias_pair [0 ]], quoted = True
397- )
398- ),
399- alias = sg .to_identifier (column_alias_pair [1 ], quoted = True ),
400- )
401- new_selected_cols .append (new_alias_expr )
402- return new_selected_cols
403-
404-
405- def _get_column_alias_pair (
406- expr : sge .Expression ,
407- ) -> typing .Optional [typing .Tuple [str , str ]]:
408- """Checks if an expression is a simple alias of a column definition
409- (e.g., "column_name AS alias_name").
410- If it is, returns a tuple containing the alias name and original column name.
411- Returns `None` otherwise.
412- """
413- if not isinstance (expr , sge .Alias ):
414- return None
415- if not isinstance (expr .this , sge .ColumnDef ):
416- return None
417-
418- column_def_expr : sge .ColumnDef = expr .this
419- if not isinstance (column_def_expr .this , sge .Identifier ):
420- return None
421-
422- original_identifier : sge .Identifier = column_def_expr .this
423- return (original_identifier .this , expr .alias )
0 commit comments