Skip to content

Commit 27ab028

Browse files
perf: Compilation no longer bounded by recursion (#1464)
1 parent 9a65e83 commit 27ab028

File tree

4 files changed

+252
-244
lines changed

4 files changed

+252
-244
lines changed

bigframes/core/bigframe_node.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
COLUMN_SET = frozenset[identifiers.ColumnId]
3434

35+
T = typing.TypeVar("T")
36+
3537

3638
@dataclasses.dataclass(frozen=True)
3739
class Field:
@@ -382,3 +384,14 @@ def bottom_up(
382384
results[node] = result
383385

384386
return results[self]
387+
388+
def reduce_up(self, reduction: Callable[[BigFrameNode, Tuple[T, ...]], T]) -> T:
389+
"""Apply a bottom-up reduction to the tree."""
390+
results: dict[BigFrameNode, T] = {}
391+
for node in list(self.iter_nodes_topo()):
392+
# child nodes have already been transformed
393+
child_results = tuple(results[child] for child in node.child_nodes)
394+
result = reduction(node, child_results)
395+
results[node] = result
396+
397+
return results[self]

bigframes/core/compile/api.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@
2525
import bigframes.core.ordering
2626
import bigframes.core.schema
2727

28-
_STRICT_COMPILER = compiler.Compiler(strict=True)
29-
3028

3129
class SQLCompiler:
32-
def __init__(self, strict: bool = True):
33-
self._compiler = compiler.Compiler(strict=strict)
34-
3530
def compile(
3631
self,
3732
node: bigframes.core.nodes.BigFrameNode,
@@ -41,7 +36,7 @@ def compile(
4136
) -> str:
4237
"""Compile node into sql where rows are sorted with ORDER BY."""
4338
# If we are ordering the query anyways, compiling the slice as a limit is probably a good idea.
44-
return self._compiler.compile_sql(node, ordered=ordered, limit=limit)
39+
return compiler.compile_sql(node, ordered=ordered, limit=limit)
4540

4641
def compile_raw(
4742
self,
@@ -50,16 +45,16 @@ def compile_raw(
5045
str, Sequence[bigquery.SchemaField], bigframes.core.ordering.RowOrdering
5146
]:
5247
"""Compile node into sql that exposes all columns, including hidden ordering-only columns."""
53-
return self._compiler.compile_raw(node)
48+
return compiler.compile_raw(node)
5449

5550

5651
def test_only_ibis_inferred_schema(node: bigframes.core.nodes.BigFrameNode):
5752
"""Use only for testing paths to ensure ibis inferred schema does not diverge from bigframes inferred schema."""
5853
import bigframes.core.schema
5954

60-
node = _STRICT_COMPILER._replace_unsupported_ops(node)
55+
node = compiler._replace_unsupported_ops(node)
6156
node, _ = rewrite.pull_up_order(node, order_root=False)
62-
ir = _STRICT_COMPILER.compile_node(node)
57+
ir = compiler.compile_node(node)
6358
items = tuple(
6459
bigframes.core.schema.SchemaItem(name, ir.get_column_type(ibis_id))
6560
for name, ibis_id in zip(node.schema.names, ir.column_ids)

0 commit comments

Comments
 (0)