Skip to content

Commit 87fd788

Browse files
committed
combine null ops into generic_ops files
1 parent de5a12c commit 87fd788

File tree

15 files changed

+80
-211
lines changed

15 files changed

+80
-211
lines changed

bigframes/core/compile/compiled.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import pyarrow as pa
3131

3232
from bigframes.core import utils
33-
import bigframes.core.compile.aggregate_compiler as agg_compiler
3433
import bigframes.core.compile.googlesql
34+
import bigframes.core.compile.ibis_compiler.aggregate_compiler as agg_compiler
3535
import bigframes.core.compile.ibis_compiler.scalar_op_compiler as op_compilers
3636
import bigframes.core.compile.ibis_types
3737
import bigframes.core.expression as ex

bigframes/core/compile/ibis_compiler/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@
2020

2121
from __future__ import annotations
2222

23-
import bigframes.core.compile.ibis_compiler.operations.generic_ops.isnull_op # noqa: F401
24-
import bigframes.core.compile.ibis_compiler.operations.generic_ops.notnull_op # noqa: F401
23+
import bigframes.core.compile.ibis_compiler.operations.generic_ops # noqa: F401
2524
import bigframes.core.compile.ibis_compiler.scalar_op_registry # noqa: F401
File renamed without changes.

bigframes/core/compile/ibis_compiler/operations/generic_ops/isnull_op.py renamed to bigframes/core/compile/ibis_compiler/operations/generic_ops.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
BigFrames -> Ibis compilation for the operations in bigframes.operations.generic_ops.
17+
18+
Please keep implementations in sequential order by op name.
19+
"""
20+
1521
from __future__ import annotations
1622

1723
from bigframes_vendored.ibis.expr import types as ibis_types
1824

1925
from bigframes.core.compile.ibis_compiler import scalar_op_compiler
20-
from bigframes.operations.generic_ops import isnull_op
26+
from bigframes.operations import generic_ops
27+
28+
register_unary_op = scalar_op_compiler.scalar_op_compiler.register_unary_op
29+
30+
31+
@register_unary_op(generic_ops.notnull_op)
32+
def notnull_op_impl(x: ibis_types.Value):
33+
return x.notnull()
2134

2235

23-
@scalar_op_compiler.scalar_op_compiler.register_unary_op(isnull_op.isnull_op)
24-
def _ibis_isnull_op_impl(x: ibis_types.Value):
36+
@register_unary_op(generic_ops.isnull_op)
37+
def isnull_op_impl(x: ibis_types.Value):
2538
return x.isnull()

bigframes/core/compile/ibis_compiler/operations/generic_ops/__init__.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

bigframes/core/compile/ibis_compiler/operations/generic_ops/notnull_op.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

bigframes/core/compile/polars/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020

2121
import warnings
2222

23+
# The ops imports appear first so that the implementations can be registered.
24+
# polars shouldn't be needed at import time, as register is a no-op if polars
25+
# isn't installed.
26+
import bigframes.core.compile.polars.operations.generic_ops # noqa: F401
27+
2328
try:
2429
import polars # noqa
2530

2631
from bigframes.core.compile.polars.compiler import PolarsCompiler
27-
import bigframes.core.compile.polars.operations.generic_ops.isnull_op # noqa: F401
28-
import bigframes.core.compile.polars.operations.generic_ops.notnull_op # noqa: F401
2932

3033
__all__ = ["PolarsCompiler"]
3134
except Exception as exc:

bigframes/core/compile/polars/compiler.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import functools
1818
import itertools
1919
import operator
20-
from typing import cast, Literal, Optional, Sequence, Tuple, TYPE_CHECKING
20+
from typing import cast, Literal, Optional, Sequence, Tuple, Type, TYPE_CHECKING
2121

2222
import pandas as pd
2323

@@ -46,6 +46,27 @@
4646
except Exception:
4747
polars_installed = False
4848

49+
50+
def register_op(op: Type):
51+
"""Register a compilation from BigFrames to Ibis.
52+
53+
This decorator can be used, even if Polars is not installed.
54+
55+
Args:
56+
op: The type of the operator the wrapped function compiles.
57+
"""
58+
59+
def decorator(func):
60+
if polars_installed:
61+
# Ignore the type because compile_op is a generic Callable, so
62+
# register isn't available according to mypy.
63+
return PolarsExpressionCompiler.compile_op.register(op)(func) # type: ignore
64+
else:
65+
return func
66+
67+
return decorator
68+
69+
4970
if polars_installed:
5071
_DTYPE_MAPPING = {
5172
# Direct mappings

bigframes/core/compile/polars/operations/generic_ops/notnull_op.py renamed to bigframes/core/compile/polars/operations/generic_ops.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,36 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
"""
16+
BigFrames -> Polars compilation for the operations in bigframes.operations.generic_ops.
17+
18+
Please keep implementations in sequential order by op name.
19+
"""
20+
1521
from __future__ import annotations
1622

1723
from typing import TYPE_CHECKING
1824

1925
import bigframes.core.compile.polars.compiler as polars_compiler
20-
from bigframes.operations.generic_ops import notnull_op
26+
from bigframes.operations import generic_ops
2127

2228
if TYPE_CHECKING:
2329
import polars as pl
2430

2531

26-
def _polars_notnull_op_impl(
32+
@polars_compiler.register_op(generic_ops.NotNullOp)
33+
def notnull_op_impl(
2734
compiler: polars_compiler.PolarsExpressionCompiler,
28-
op: notnull_op.NotNullOp,
35+
op: generic_ops.NotNullOp, # type: ignore
2936
input: pl.Expr,
3037
) -> pl.Expr:
3138
return input.is_not_null()
3239

3340

34-
if hasattr(polars_compiler, "PolarsExpressionCompiler"):
35-
# TODO(https://github.com/python/mypy/issues/13040): remove `type: ignore`
36-
# when mypy can better handle singledispatch.
37-
polars_compiler.PolarsExpressionCompiler.compile_op.register( # type: ignore
38-
notnull_op.NotNullOp, _polars_notnull_op_impl
39-
)
41+
@polars_compiler.register_op(generic_ops.IsNullOp)
42+
def isnull_op_impl(
43+
compiler: polars_compiler.PolarsExpressionCompiler,
44+
op: generic_ops.IsNullOp, # type: ignore
45+
input: pl.Expr,
46+
) -> pl.Expr:
47+
return input.is_null()

bigframes/core/compile/polars/operations/generic_ops/__init__.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)