-
Notifications
You must be signed in to change notification settings - Fork 50
chore: refactor IsNullOp and NotNullOp logic to make scalar ops generation easier #1822
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 all commits
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 |
---|---|---|
|
@@ -228,18 +228,23 @@ def _register( | |
# Singleton compiler | ||
scalar_op_compiler = ScalarOpCompiler() | ||
|
||
# Registrations for operations defined in isnull_op.py | ||
from bigframes.operations import isnull_op, notnull_op | ||
from bigframes.operations.isnull_op import ( | ||
_ibis_isnull_op_impl, | ||
_ibis_notnull_op_impl, | ||
) | ||
|
||
### Unary Ops | ||
@scalar_op_compiler.register_unary_op(ops.isnull_op) | ||
def isnull_op_impl(x: ibis_types.Value): | ||
return x.isnull() | ||
|
||
@scalar_op_compiler.register_unary_op(isnull_op) | ||
def _scalar_isnull_op_impl_wrapper(x: ibis_types.Value): | ||
return _ibis_isnull_op_impl(x) | ||
|
||
@scalar_op_compiler.register_unary_op(ops.notnull_op) | ||
def notnull_op_impl(x: ibis_types.Value): | ||
return x.notnull() | ||
@scalar_op_compiler.register_unary_op(notnull_op) | ||
def _scalar_notnull_op_impl_wrapper(x: ibis_types.Value): | ||
return _ibis_notnull_op_impl(x) | ||
Comment on lines
+231
to
+244
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. Likewise, the registration should happen from bigframes/operations/isnull_op.py Perhaps with some delay to avoid circular imports? |
||
|
||
|
||
### Unary Ops | ||
@scalar_op_compiler.register_unary_op(ops.hash_op) | ||
def hash_op_impl(x: ibis_types.Value): | ||
return typing.cast(ibis_types.IntegerValue, x).hash() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2521,6 +2521,7 @@ def _filter_rows( | |
elif items is not None: | ||
# Behavior matches pandas 2.1+, older pandas versions would reindex | ||
block = self._block | ||
block = self._block | ||
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. Bad merge? |
||
block, mask_id = block.apply_unary_op( | ||
self._block.index_columns[0], ops.IsInOp(values=tuple(items)) | ||
) | ||
|
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. Let's create a scalar functions directory for this. Also "op" probably shouldn't be in the module name. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright 2023 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 __future__ import annotations | ||
|
||
import typing | ||
|
||
# Direct imports from bigframes | ||
from bigframes import dtypes | ||
from bigframes.operations import base_ops | ||
import bigframes.operations.type as op_typing | ||
|
||
# Imports for Ibis compilation | ||
from bigframes_vendored.ibis.expr import types as ibis_types | ||
|
||
# Imports for Polars compilation | ||
try: | ||
import polars as pl | ||
except ImportError: | ||
# Polars is optional, error will be raised elsewhere if user tries to use it. | ||
pass | ||
|
||
|
||
# Definitions of IsNullOp and NotNullOp operations | ||
IsNullOp = base_ops.create_unary_op( | ||
name="isnull", | ||
type_signature=op_typing.FixedOutputType( | ||
lambda x: True, dtypes.BOOL_DTYPE, description="nullable" | ||
), | ||
) | ||
isnull_op = IsNullOp() | ||
|
||
NotNullOp = base_ops.create_unary_op( | ||
name="notnull", | ||
type_signature=op_typing.FixedOutputType( | ||
lambda x: True, dtypes.BOOL_DTYPE, description="nullable" | ||
), | ||
) | ||
notnull_op = NotNullOp() | ||
|
||
# Ibis Scalar Op Implementations | ||
def _ibis_isnull_op_impl(x: ibis_types.Value): | ||
return x.isnull() | ||
|
||
def _ibis_notnull_op_impl(x: ibis_types.Value): | ||
return x.notnull() | ||
|
||
|
||
# Polars Expression Implementations | ||
def _polars_isnull_op_impl(op: IsNullOp, input: pl.Expr) -> pl.Expr: | ||
return input.is_null() | ||
|
||
def _polars_notnull_op_impl(op: NotNullOp, input: pl.Expr) -> pl.Expr: | ||
return input.is_not_null() | ||
|
||
__all__ = [ | ||
"IsNullOp", | ||
"isnull_op", | ||
"NotNullOp", | ||
"notnull_op", | ||
"_ibis_isnull_op_impl", | ||
"_ibis_notnull_op_impl", | ||
"_polars_isnull_op_impl", | ||
"_polars_notnull_op_impl", | ||
] |
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.
Ugh. I asked Jules not to do this. Instead, we should call polars's
compile_op
from bigframes.operations.isnull_op`