Skip to content

Commit c0b5e68

Browse files
committed
preserve NA
1 parent 41ce94b commit c0b5e68

File tree

7 files changed

+40
-36
lines changed

7 files changed

+40
-36
lines changed

bigframes/core/compile/ibis_compiler/scalar_op_compiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@ def _convert_range_ordering_to_table_value(
270270
scalar_op_compiler = ExpressionCompiler()
271271

272272

273-
@scalar_op_compiler.register_unary_op(numeric_ops.isnanornull_op)
273+
@scalar_op_compiler.register_unary_op(numeric_ops.isnan_op)
274274
def isnanornull(arg):
275-
return arg.isnan() | arg.isnull()
275+
return arg.isnan()
276276

277277

278278
@scalar_op_compiler.register_unary_op(numeric_ops.isfinite_op)
279279
def isfinite(arg):
280-
return arg.isinf().not_() & arg.isnan().not_() & arg.isnull().not_()
280+
return arg.isinf().negate() & arg.isnan().negate()

bigframes/core/compile/polars/compiler.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,6 @@ def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
199199
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
200200
return input.ceil()
201201

202-
@compile_op.register(num_ops.IsNanOrNullOp)
203-
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
204-
return input.is_nan() | input.is_null()
205-
206-
@compile_op.register(num_ops.IsFiniteOp)
207-
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
208-
return input.is_finite()
209-
210202
@compile_op.register(num_ops.PosOp)
211203
def _(self, op: ops.ScalarOp, input: pl.Expr) -> pl.Expr:
212204
return input.__pos__()

bigframes/core/compile/polars/operations/numeric_ops.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,21 @@ def sqrt_op_impl(
8989
import polars as pl
9090

9191
return pl.when(input < 0).then(float("nan")).otherwise(input.sqrt())
92+
93+
94+
@polars_compiler.register_op(numeric_ops.IsNanOp)
95+
def is_nan_op_impl(
96+
compiler: polars_compiler.PolarsExpressionCompiler,
97+
op: numeric_ops.SqrtOp, # type: ignore
98+
input: pl.Expr,
99+
) -> pl.Expr:
100+
return input.is_nan()
101+
102+
103+
@polars_compiler.register_op(numeric_ops.IsFiniteOp)
104+
def is_finite_op_impl(
105+
compiler: polars_compiler.PolarsExpressionCompiler,
106+
op: numeric_ops.SqrtOp, # type: ignore
107+
input: pl.Expr,
108+
) -> pl.Expr:
109+
return input.is_finite()

bigframes/core/compile/sqlglot/expressions/numeric_ops.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import bigframes.core.compile.sqlglot.expressions.constants as constants
2323
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr
2424
import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler
25+
from bigframes.operations import numeric_ops
2526

2627
register_unary_op = scalar_compiler.scalar_op_compiler.register_unary_op
2728
register_binary_op = scalar_compiler.scalar_op_compiler.register_binary_op
@@ -408,6 +409,21 @@ def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
408409
)
409410

410411

412+
@register_unary_op(numeric_ops.isnan_op)
413+
def isnan(arg: TypedExpr) -> sge.Expression:
414+
return sge.IsNan(this=arg.expr)
415+
416+
417+
@register_unary_op(numeric_ops.isfinite_op)
418+
def isfinite(arg: TypedExpr) -> sge.Expression:
419+
return sge.Not(
420+
this=sge.Or(
421+
this=sge.IsInf(this=arg.expr),
422+
right=sge.IsNan(this=arg.expr),
423+
),
424+
)
425+
426+
411427
def _coerce_bool_to_int(typed_expr: TypedExpr) -> sge.Expression:
412428
"""Coerce boolean expression to integer."""
413429
if typed_expr.dtype == dtypes.BOOL_DTYPE:

bigframes/core/compile/sqlglot/scalar_compiler.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr
2222
import bigframes.core.compile.sqlglot.sqlglot_ir as ir
2323
import bigframes.core.expression as ex
24-
from bigframes.operations import numeric_ops
2524
import bigframes.operations as ops
2625

2726

@@ -181,24 +180,3 @@ def _register(
181180

182181
# Singleton compiler
183182
scalar_op_compiler = ScalarOpCompiler()
184-
185-
186-
@scalar_op_compiler.register_unary_op(numeric_ops.isnanornull_op)
187-
def isnanornull(arg: TypedExpr) -> sge.Expression:
188-
return sge.Or(
189-
this=sge.IsNan(this=arg.expr),
190-
right=sge.Is(this=arg.expr, expression=sge.Null()),
191-
)
192-
193-
194-
@scalar_op_compiler.register_unary_op(numeric_ops.isfinite_op)
195-
def isfinite(arg: TypedExpr) -> sge.Expression:
196-
return sge.Not(
197-
this=sge.Or(
198-
this=sge.Is(this=arg.expr, expression=sge.Null()),
199-
right=sge.Or(
200-
this=sge.IsInf(this=arg.expr),
201-
right=sge.IsNan(this=arg.expr),
202-
),
203-
),
204-
)

bigframes/operations/numeric_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,13 @@ def output_type(self, *input_types: dtypes.ExpressionType) -> dtypes.ExpressionT
349349
)
350350
unsafe_pow_op = UnsafePowOp()
351351

352-
IsNanOrNullOp = base_ops.create_unary_op(
352+
IsNanOp = base_ops.create_unary_op(
353353
name="isnanornull",
354354
type_signature=op_typing.FixedOutputType(
355355
dtypes.is_numeric, dtypes.BOOL_DTYPE, "numeric"
356356
),
357357
)
358-
isnanornull_op = IsNanOrNullOp()
358+
isnan_op = IsNanOp()
359359

360360
IsFiniteOp = base_ops.create_unary_op(
361361
name="isfinite",

bigframes/operations/numpy_op_maps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
np.ceil: numeric_ops.ceil_op,
4141
np.log1p: numeric_ops.log1p_op,
4242
np.expm1: numeric_ops.expm1_op,
43-
np.isnan: numeric_ops.isnanornull_op,
43+
np.isnan: numeric_ops.isnan_op,
4444
np.isfinite: numeric_ops.isfinite_op,
4545
}
4646

0 commit comments

Comments
 (0)