-
Notifications
You must be signed in to change notification settings - Fork 53
feat: Add isin local execution impl #1993
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
# limitations under the License. | ||
|
||
import dataclasses | ||
from typing import cast | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from bigframes import dtypes | ||
from bigframes.core import bigframe_node, expression | ||
|
@@ -316,6 +318,35 @@ def lower(self, expr: expression.OpExpression) -> expression.Expression: | |
return expr | ||
|
||
|
||
class LowerIsinOp(op_lowering.OpLoweringRule): | ||
@property | ||
def op(self) -> type[ops.ScalarOp]: | ||
return generic_ops.IsInOp | ||
|
||
def lower(self, expr: expression.OpExpression) -> expression.Expression: | ||
assert isinstance(expr.op, generic_ops.IsInOp) | ||
arg = expr.children[0] | ||
new_values = [] | ||
match_nulls = False | ||
for val in expr.op.values: | ||
# coercible, non-coercible | ||
# float NaN/inf should be treated as distinct from 'true' null values | ||
if cast(bool, pd.isna(val)) and not isinstance(val, float): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can fold this into one statement: match_nulls = cast(bool, pd.isna(val)) and not isinstance(val, float) and expr.op.match_nulls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do to the elif statement, that wouldn't quite be the same |
||
if expr.op.match_nulls: | ||
match_nulls = True | ||
elif dtypes.is_compatible(val, arg.output_type): | ||
new_values.append(val) | ||
else: | ||
pass | ||
|
||
new_isin = ops.IsInOp(tuple(new_values), match_nulls=False).as_expr(arg) | ||
if match_nulls: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So whether we match nulls partially depends on the last values in expr.op.values? Is that correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
return ops.coalesce_op.as_expr(new_isin, expression.const(True)) | ||
else: | ||
# polars propagates nulls, so need to coalesce to false | ||
return ops.coalesce_op.as_expr(new_isin, expression.const(False)) | ||
|
||
|
||
def _coerce_comparables( | ||
expr1: expression.Expression, | ||
expr2: expression.Expression, | ||
|
@@ -414,6 +445,7 @@ def _lower_cast(cast_op: ops.AsTypeOp, arg: expression.Expression): | |
LowerModRule(), | ||
LowerAsTypeRule(), | ||
LowerInvertOp(), | ||
LowerIsinOp(), | ||
) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -390,3 +390,35 @@ def test_engines_invert_op(scalars_array_value: array_value.ArrayValue, engine): | |
) | ||
|
||
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) | ||
|
||
|
||
@pytest.mark.parametrize("engine", ["polars", "bq"], indirect=True) | ||
def test_engines_isin_op(scalars_array_value: array_value.ArrayValue, engine): | ||
arr, col_ids = scalars_array_value.compute_values( | ||
[ | ||
ops.IsInOp((1, 2, 3)).as_expr(expression.deref("int64_col")), | ||
ops.IsInOp((None, 123456)).as_expr(expression.deref("int64_col")), | ||
ops.IsInOp((None, 123456), match_nulls=False).as_expr( | ||
expression.deref("int64_col") | ||
), | ||
ops.IsInOp((1.0, 2.0, 3.0)).as_expr(expression.deref("int64_col")), | ||
ops.IsInOp(("1.0", "2.0")).as_expr(expression.deref("int64_col")), | ||
ops.IsInOp(("1.0", 2.5, 3)).as_expr(expression.deref("int64_col")), | ||
ops.IsInOp(()).as_expr(expression.deref("int64_col")), | ||
ops.IsInOp((1, 2, 3, None)).as_expr(expression.deref("float64_col")), | ||
] | ||
) | ||
new_names = ( | ||
"int in ints", | ||
"int in ints w null", | ||
"int in ints w null wo match nulls", | ||
"int in floats", | ||
"int in strings", | ||
"int in mixed", | ||
"int in empty", | ||
"float in ints", | ||
) | ||
arr = arr.rename_columns( | ||
{old_name: new_names[i] for i, old_name in enumerate(col_ids)} | ||
) | ||
assert_equivalence_execution(arr.node, REFERENCE_ENGINE, engine) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: empty line above assertion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this change any existing behavior? Were we missing a test case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, it looks like we were handling this higher up. removed that code, and now the op itself is purely non-null. also added a test