Skip to content

feat: Hybrid engine local join support #1900

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 1 commit into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions bigframes/core/compile/polars/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,14 @@ def compile_offsets(self, node: nodes.PromoteOffsetsNode):
def compile_join(self, node: nodes.JoinNode):
left = self.compile_node(node.left_child)
right = self.compile_node(node.right_child)
left_on = [l_name.id.sql for l_name, _ in node.conditions]
right_on = [r_name.id.sql for _, r_name in node.conditions]

left_on = []
right_on = []
for left_ex, right_ex in node.conditions:
left_ex, right_ex = lowering._coerce_comparables(left_ex, right_ex)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does polars treat NULL as different each other (similar to pandas or BQ)? I am wondering if Polars needs a _join_condition like compiler too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Polars df.join actually has a very helpful join_nulls (nulls_equal in more recent versions) argument that does exactly what we need

left_on.append(self.expr_compiler.compile_expression(left_ex))
right_on.append(self.expr_compiler.compile_expression(right_ex))

if node.type == "right":
return self._ordered_join(
right, left, "left", right_on, left_on, node.joins_nulls
Expand All @@ -502,8 +508,8 @@ def _ordered_join(
left_frame: pl.LazyFrame,
right_frame: pl.LazyFrame,
how: Literal["inner", "outer", "left", "cross"],
left_on: Sequence[str],
right_on: Sequence[str],
left_on: Sequence[pl.Expr],
right_on: Sequence[pl.Expr],
join_nulls: bool,
):
if how == "right":
Expand Down
1 change: 1 addition & 0 deletions bigframes/session/polars_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
nodes.AggregateNode,
nodes.FilterNode,
nodes.ConcatNode,
nodes.JoinNode,
)

_COMPATIBLE_SCALAR_OPS = (
Expand Down
90 changes: 90 additions & 0 deletions tests/system/small/engines/test_join.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Literal

import pytest

from bigframes import operations as ops
from bigframes.core import array_value, expression, ordering
from bigframes.session import polars_executor
from bigframes.testing.engine_utils import assert_equivalence_execution

pytest.importorskip("polars")

# Polars used as reference as its fast and local. Generally though, prefer gbq engine where they disagree.
REFERENCE_ENGINE = polars_executor.PolarsExecutor()


@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
@pytest.mark.parametrize("join_type", ["left", "inner", "right", "outer"])
def test_engines_join_on_key(
scalars_array_value: array_value.ArrayValue,
engine,
join_type: Literal["inner", "outer", "left", "right"],
):
result, _ = scalars_array_value.relational_join(
scalars_array_value, conditions=(("int64_col", "int64_col"),), type=join_type
)

assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine)


@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
@pytest.mark.parametrize("join_type", ["left", "inner", "right", "outer"])
def test_engines_join_on_coerced_key(
scalars_array_value: array_value.ArrayValue,
engine,
join_type: Literal["inner", "outer", "left", "right"],
):
result, _ = scalars_array_value.relational_join(
scalars_array_value, conditions=(("int64_col", "float64_col"),), type=join_type
)

assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine)


@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
@pytest.mark.parametrize("join_type", ["left", "inner", "right", "outer"])
def test_engines_join_multi_key(
scalars_array_value: array_value.ArrayValue,
engine,
join_type: Literal["inner", "outer", "left", "right"],
):
l_input = scalars_array_value.order_by([ordering.ascending_over("float64_col")])
l_input, l_join_cols = scalars_array_value.compute_values(
[
ops.mod_op.as_expr("int64_col", expression.const(2)),
ops.invert_op.as_expr("bool_col"),
]
)
r_input, r_join_cols = scalars_array_value.compute_values(
[ops.mod_op.as_expr("int64_col", expression.const(3)), expression.const(True)]
)

conditions = tuple((l_col, r_col) for l_col, r_col in zip(l_join_cols, r_join_cols))

result, _ = l_input.relational_join(r_input, conditions=conditions, type=join_type)

assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine)


@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True)
def test_engines_cross_join(
scalars_array_value: array_value.ArrayValue,
engine,
):
result, _ = scalars_array_value.relational_join(scalars_array_value, type="cross")

assert_equivalence_execution(result.node, REFERENCE_ENGINE, engine)