|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import typing |
| 18 | + |
| 19 | +# Direct imports from bigframes |
| 20 | +from bigframes import dtypes |
| 21 | +from bigframes.operations import base_ops |
| 22 | +import bigframes.operations.type as op_typing |
| 23 | + |
| 24 | +# Imports for Ibis compilation |
| 25 | +from bigframes_vendored.ibis.expr import types as ibis_types |
| 26 | + |
| 27 | +# Imports for Polars compilation |
| 28 | +try: |
| 29 | + import polars as pl |
| 30 | +except ImportError: |
| 31 | + # Polars is optional, error will be raised elsewhere if user tries to use it. |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +# Definitions of IsNullOp and NotNullOp operations |
| 36 | +IsNullOp = base_ops.create_unary_op( |
| 37 | + name="isnull", |
| 38 | + type_signature=op_typing.FixedOutputType( |
| 39 | + lambda x: True, dtypes.BOOL_DTYPE, description="nullable" |
| 40 | + ), |
| 41 | +) |
| 42 | +isnull_op = IsNullOp() |
| 43 | + |
| 44 | +NotNullOp = base_ops.create_unary_op( |
| 45 | + name="notnull", |
| 46 | + type_signature=op_typing.FixedOutputType( |
| 47 | + lambda x: True, dtypes.BOOL_DTYPE, description="nullable" |
| 48 | + ), |
| 49 | +) |
| 50 | +notnull_op = NotNullOp() |
| 51 | + |
| 52 | +# Ibis Scalar Op Implementations |
| 53 | +def _ibis_isnull_op_impl(x: ibis_types.Value): |
| 54 | + return x.isnull() |
| 55 | + |
| 56 | +def _ibis_notnull_op_impl(x: ibis_types.Value): |
| 57 | + return x.notnull() |
| 58 | + |
| 59 | + |
| 60 | +# Polars Expression Implementations |
| 61 | +def _polars_isnull_op_impl(op: IsNullOp, input: pl.Expr) -> pl.Expr: |
| 62 | + return input.is_null() |
| 63 | + |
| 64 | +def _polars_notnull_op_impl(op: NotNullOp, input: pl.Expr) -> pl.Expr: |
| 65 | + return input.is_not_null() |
| 66 | + |
| 67 | +__all__ = [ |
| 68 | + "IsNullOp", |
| 69 | + "isnull_op", |
| 70 | + "NotNullOp", |
| 71 | + "notnull_op", |
| 72 | + "_ibis_isnull_op_impl", |
| 73 | + "_ibis_notnull_op_impl", |
| 74 | + "_polars_isnull_op_impl", |
| 75 | + "_polars_notnull_op_impl", |
| 76 | +] |
0 commit comments