Skip to content

perf: Compilation no longer bounded by recursion #1464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions bigframes/core/bigframe_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

COLUMN_SET = frozenset[identifiers.ColumnId]

T = typing.TypeVar("T")


@dataclasses.dataclass(frozen=True)
class Field:
Expand Down Expand Up @@ -382,3 +384,14 @@ def bottom_up(
results[node] = result

return results[self]

def reduce_up(self, reduction: Callable[[BigFrameNode, Tuple[T, ...]], T]) -> T:
"""Apply a bottom-up reduction to the tree."""
results: dict[BigFrameNode, T] = {}
for node in list(self.iter_nodes_topo()):
# child nodes have already been transformed
child_results = tuple(results[child] for child in node.child_nodes)
result = reduction(node, child_results)
Copy link
Contributor

Choose a reason for hiding this comment

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

The bottom_up and reduce_up are similar but the transformed methods APIs are different. Do we have a way to merge them into one? If not, could you please add docstring to reduce_up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In theory, bottom_up could be implemented in terms of reduce_up, but will leave separate for now, maybe they'll have somewhat different optimizations or extra arguments later on. Added a brief description

results[node] = result

return results[self]
13 changes: 4 additions & 9 deletions bigframes/core/compile/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@
import bigframes.core.ordering
import bigframes.core.schema

_STRICT_COMPILER = compiler.Compiler(strict=True)


class SQLCompiler:
def __init__(self, strict: bool = True):
self._compiler = compiler.Compiler(strict=strict)

def compile(
self,
node: bigframes.core.nodes.BigFrameNode,
Expand All @@ -41,7 +36,7 @@ def compile(
) -> str:
"""Compile node into sql where rows are sorted with ORDER BY."""
# If we are ordering the query anyways, compiling the slice as a limit is probably a good idea.
return self._compiler.compile_sql(node, ordered=ordered, limit=limit)
return compiler.compile_sql(node, ordered=ordered, limit=limit)

def compile_raw(
self,
Expand All @@ -50,16 +45,16 @@ def compile_raw(
str, Sequence[bigquery.SchemaField], bigframes.core.ordering.RowOrdering
]:
"""Compile node into sql that exposes all columns, including hidden ordering-only columns."""
return self._compiler.compile_raw(node)
return compiler.compile_raw(node)


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

node = _STRICT_COMPILER._replace_unsupported_ops(node)
node = compiler._replace_unsupported_ops(node)
node, _ = rewrite.pull_up_order(node, order_root=False)
ir = _STRICT_COMPILER.compile_node(node)
ir = compiler.compile_node(node)
items = tuple(
bigframes.core.schema.SchemaItem(name, ir.get_column_type(ibis_id))
for name, ibis_id in zip(node.schema.names, ir.column_ids)
Expand Down
Loading