Skip to content

Commit a853d92

Browse files
committed
chore: fix sqlglot compiler misses the columns of SqlDataSource
1 parent 44e0ffd commit a853d92

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

bigframes/core/compile/sqlglot/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ def compile_readlocal(
202202
@_compile_node.register
203203
def compile_readtable(node: sql_nodes.SqlDataSource, child: sqlglot_ir.SQLGlotIR):
204204
table_obj = node.source.table
205+
columns = () if node.is_star_selection else node.source.schema.names
205206
return sqlglot_ir.SQLGlotIR.from_table(
206207
table_obj.project_id,
207208
table_obj.dataset_id,
208209
table_obj.table_id,
209210
uid_gen=child.uid_gen,
211+
columns=columns,
210212
sql_predicate=node.source.sql_predicate,
211213
system_time=node.source.at_time,
212214
)

bigframes/core/compile/sqlglot/sqlglot_ir.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def from_table(
178178
dataset_id: str,
179179
table_id: str,
180180
uid_gen: guid.SequentialUIDGenerator,
181+
columns: typing.Sequence[str] = (),
181182
sql_predicate: typing.Optional[str] = None,
182183
system_time: typing.Optional[datetime.datetime] = None,
183184
) -> SQLGlotIR:
@@ -187,9 +188,8 @@ def from_table(
187188
project_id (str): The project ID of the BigQuery table.
188189
dataset_id (str): The dataset ID of the BigQuery table.
189190
table_id (str): The table ID of the BigQuery table.
190-
col_names (typing.Sequence[str]): The names of the columns to select.
191-
alias_names (typing.Sequence[str]): The aliases for the selected columns.
192191
uid_gen (guid.SequentialUIDGenerator): A generator for unique identifiers.
192+
columns (typing.Sequence[str]): The names of the columns to select.
193193
sql_predicate (typing.Optional[str]): An optional SQL predicate for filtering.
194194
system_time (typing.Optional[str]): An optional system time for time-travel queries.
195195
"""
@@ -210,14 +210,22 @@ def from_table(
210210
version=version,
211211
alias=sql.identifier(table_alias),
212212
)
213-
if sql_predicate:
213+
214+
if not columns and not sql_predicate:
215+
return cls.from_expr(expr=table_expr, uid_gen=uid_gen)
216+
217+
if len(columns) > 0:
218+
select_cols = [sql.identifier(col) for col in columns]
219+
select_expr = sge.Select().select(*select_cols).from_(table_expr)
220+
else:
214221
select_expr = sge.Select().select(sge.Star()).from_(table_expr)
222+
223+
if sql_predicate:
215224
select_expr = select_expr.where(
216225
sg.parse_one(sql_predicate, dialect=sql.base.DIALECT), append=False
217226
)
218-
return cls.from_expr(expr=select_expr, uid_gen=uid_gen)
219227

220-
return cls.from_expr(expr=table_expr, uid_gen=uid_gen)
228+
return cls.from_expr(expr=select_expr, uid_gen=uid_gen)
221229

222230
@classmethod
223231
def from_cte_ref(

bigframes/core/sql_nodes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def fields(self) -> Sequence[nodes.Field]:
4545
for source_id in self.source.schema.names
4646
)
4747

48+
@property
49+
def is_star_selection(self) -> bool:
50+
return tuple(self.source.schema.names) == tuple(
51+
field.name for field in self.source.table.physical_schema
52+
)
53+
4854
@property
4955
def variables_introduced(self) -> int:
5056
# This operation only renames variables, doesn't actually create new ones
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
SELECT
2-
*
2+
`rowindex`,
3+
`int64_col`,
4+
`string_col`
35
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` AS `bft_0`
46
WHERE
57
`rowindex` > 0 AND `string_col` IN ('Hello, World!')

0 commit comments

Comments
 (0)