Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bigframes/core/compile/sqlglot/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ def compile_readlocal(node: nodes.ReadLocalNode, child: ir.SQLGlotIR) -> ir.SQLG
@_compile_node.register
def compile_readtable(node: nodes.ReadTableNode, child: ir.SQLGlotIR):
table = node.source.table
is_star_selection = node.is_star_selection
return ir.SQLGlotIR.from_table(
table.project_id,
table.dataset_id,
table.table_id,
col_names=[col.source_id for col in node.scan_list.items],
alias_names=[col.id.sql for col in node.scan_list.items],
is_star_selection=is_star_selection,
uid_gen=child.uid_gen,
sql_predicate=node.source.sql_predicate,
system_time=node.source.at_time,
Expand Down
5 changes: 4 additions & 1 deletion bigframes/core/compile/sqlglot/expressions/generic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def _(expr: TypedExpr, op: ops.MapOp) -> sge.Expression:

@register_unary_op(ops.notnull_op)
def _(expr: TypedExpr) -> sge.Expression:
return sge.Not(this=sge.Is(this=expr.expr, expression=sge.Null()))
return sge.Is(
this=sge.paren(expr.expr, copy=False),
expression=sg.not_(sge.Null(), copy=False),
)


@register_ternary_op(ops.where_op)
Expand Down
25 changes: 15 additions & 10 deletions bigframes/core/compile/sqlglot/sqlglot_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def from_table(
table_id: str,
col_names: typing.Sequence[str],
alias_names: typing.Sequence[str],
is_star_selection: bool,
uid_gen: guid.SequentialUIDGenerator,
sql_predicate: typing.Optional[str] = None,
system_time: typing.Optional[datetime.datetime] = None,
Expand All @@ -134,15 +135,7 @@ def from_table(
sql_predicate (typing.Optional[str]): An optional SQL predicate for filtering.
system_time (typing.Optional[str]): An optional system time for time-travel queries.
"""
selections = [
sge.Alias(
this=sge.to_identifier(col_name, quoted=cls.quoted),
alias=sge.to_identifier(alias_name, quoted=cls.quoted),
)
if col_name != alias_name
else sge.to_identifier(col_name, quoted=cls.quoted)
for col_name, alias_name in zip(col_names, alias_names)
]

version = (
sge.Version(
this="TIMESTAMP",
Expand All @@ -158,7 +151,19 @@ def from_table(
catalog=sg.to_identifier(project_id, quoted=cls.quoted),
version=version,
)
select_expr = sge.Select().select(*selections).from_(table_expr)
if is_star_selection:
select_expr = sge.Select().select(sge.Star()).from_(table_expr)
else:
selections = [
sge.Alias(
this=sge.to_identifier(col_name, quoted=cls.quoted),
alias=sge.to_identifier(alias_name, quoted=cls.quoted),
)
if col_name != alias_name
else sge.to_identifier(col_name, quoted=cls.quoted)
for col_name, alias_name in zip(col_names, alias_names)
]
select_expr = sge.Select().select(*selections).from_(table_expr)
if sql_predicate:
select_expr = select_expr.where(
sg.parse_one(sql_predicate, dialect="bigquery"), append=False
Expand Down
6 changes: 6 additions & 0 deletions bigframes/core/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,12 @@ def order_ambiguous(self) -> bool:
def explicitly_ordered(self) -> bool:
return self.source.ordering is not None

@property
def is_star_selection(self) -> bool:
physical_names = tuple(item.name for item in self.source.table.physical_schema)
scan_names = tuple(item.source_id for item in self.scan_list.items)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scan list item id must equal source_id as well for this to work as intended

return physical_names == scan_names

@functools.cached_property
def variables_introduced(self) -> int:
return len(self.scan_list.items) + 1
Expand Down
9 changes: 4 additions & 5 deletions bigframes/core/rewrite/select_pullup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ def pull_up_source_ids(node: nodes.ReadTableNode) -> nodes.BigFrameNode:
if all(id.sql == source_id for id, source_id in node.scan_list.items):
return node
else:
source_ids = sorted(
set(scan_item.source_id for scan_item in node.scan_list.items)
)
new_scan_list = nodes.ScanList.from_items(
[
nodes.ScanItem(identifiers.ColumnId(source_id), source_id)
for source_id in source_ids
nodes.ScanItem(
identifiers.ColumnId(scan_item.source_id), scan_item.source_id
)
for scan_item in node.scan_list.items
Comment on lines +59 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what we really want is to order by the underlying physical schema?

]
)
new_source = dataclasses.replace(node, scan_list=new_scan_list)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`bytes_col`,
`date_col`,
`datetime_col`,
`duration_col`,
`float64_col`,
`geography_col`,
`int64_col`,
`int64_too`,
`numeric_col`,
`rowindex`,
`rowindex_2`,
`string_col`,
`time_col`,
`timestamp_col`
*
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`bytes_col`,
`date_col`,
`datetime_col`,
`duration_col`,
`float64_col`,
`geography_col`,
`int64_col`,
`int64_too`,
`numeric_col`,
`rowindex`,
`rowindex_2`,
`string_col`,
`time_col`,
`timestamp_col`
*
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`duration_col`,
`int64_col`
`int64_col`,
`duration_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ WITH `bfcte_0` AS (
), `bfcte_1` AS (
SELECT
*,
NOT `int64_col` IS NULL AS `bfcol_4`
(
`int64_col`
) IS NOT NULL AS `bfcol_4`
FROM `bfcte_0`
), `bfcte_2` AS (
SELECT
Expand All @@ -28,7 +30,9 @@ WITH `bfcte_0` AS (
), `bfcte_4` AS (
SELECT
*,
NOT `int64_col` IS NULL AS `bfcol_10`
(
`int64_col`
) IS NOT NULL AS `bfcol_10`
FROM `bfcte_3`
), `bfcte_5` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`duration_col`,
`int64_col`
`int64_col`,
`duration_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`float64_col`,
`int64_col`,
`float64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
WITH `bfcte_0` AS (
SELECT
`date_col`,
`duration_col`,
`rowindex`,
`timestamp_col`
`timestamp_col`,
`duration_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`,
`float64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`datetime_col`,
`float64_col`,
`numeric_col`,
`float64_col`,
`time_col`,
`timestamp_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`float64_col`,
`int64_col`,
`float64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`float64_col`,
`int64_col`,
`int64_too`
`int64_too`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WITH `bfcte_0` AS (
SELECT
`float64_col`,
`int64_col`,
`float64_col`,
`string_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ WITH `bfcte_0` AS (
), `bfcte_1` AS (
SELECT
*,
NOT `float64_col` IS NULL AS `bfcol_1`
(
`float64_col`
) IS NOT NULL AS `bfcol_1`
FROM `bfcte_0`
)
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`bytes_col`,
`date_col`,
`datetime_col`,
`duration_col`,
`float64_col`,
`geography_col`,
`int64_col`,
`int64_too`,
`numeric_col`,
`rowindex`,
`rowindex_2`,
`string_col`,
`time_col`,
`timestamp_col`
*
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WITH `bfcte_0` AS (
SELECT
`bool_col`,
`float64_col`,
`int64_col`
`int64_col`,
`float64_col`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
Expand Down
Loading
Loading