Skip to content

Commit 2285d0f

Browse files
committed
Merge branch 'main' into shuowei-anywidget-deferred-mode
2 parents 0bb9426 + d02d32f commit 2285d0f

File tree

7 files changed

+83
-33
lines changed

7 files changed

+83
-33
lines changed

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

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,19 @@ def _(expr: TypedExpr) -> sge.Expression:
9393
def _(expr: TypedExpr) -> sge.Expression:
9494
return sge.Case(
9595
ifs=[
96+
# |x| < 1: The standard formula
97+
sge.If(
98+
this=sge.func("ABS", expr.expr) < sge.convert(1),
99+
true=sge.func("ATANH", expr.expr),
100+
),
101+
# |x| > 1: Returns NaN
96102
sge.If(
97103
this=sge.func("ABS", expr.expr) > sge.convert(1),
98104
true=constants._NAN,
99-
)
105+
),
100106
],
101-
default=sge.func("ATANH", expr.expr),
107+
# |x| = 1: Returns Infinity or -Infinity
108+
default=sge.Mul(this=constants._INF, expression=expr.expr),
102109
)
103110

104111

@@ -145,15 +152,11 @@ def _(expr: TypedExpr) -> sge.Expression:
145152

146153
@register_unary_op(ops.expm1_op)
147154
def _(expr: TypedExpr) -> sge.Expression:
148-
return sge.Case(
149-
ifs=[
150-
sge.If(
151-
this=expr.expr > constants._FLOAT64_EXP_BOUND,
152-
true=constants._INF,
153-
)
154-
],
155-
default=sge.func("EXP", expr.expr),
156-
) - sge.convert(1)
155+
return sge.If(
156+
this=expr.expr > constants._FLOAT64_EXP_BOUND,
157+
true=constants._INF,
158+
false=sge.func("EXP", expr.expr) - sge.convert(1),
159+
)
157160

158161

159162
@register_unary_op(ops.floor_op)
@@ -166,11 +169,22 @@ def _(expr: TypedExpr) -> sge.Expression:
166169
return sge.Case(
167170
ifs=[
168171
sge.If(
169-
this=expr.expr <= sge.convert(0),
172+
this=sge.Is(this=expr.expr, expression=sge.Null()),
173+
true=sge.null(),
174+
),
175+
# |x| > 0: The standard formula
176+
sge.If(
177+
this=expr.expr > sge.convert(0),
178+
true=sge.Ln(this=expr.expr),
179+
),
180+
# |x| < 0: Returns NaN
181+
sge.If(
182+
this=expr.expr < sge.convert(0),
170183
true=constants._NAN,
171-
)
184+
),
172185
],
173-
default=sge.Ln(this=expr.expr),
186+
# |x| == 0: Returns -Infinity
187+
default=constants._NEG_INF,
174188
)
175189

176190

@@ -179,11 +193,22 @@ def _(expr: TypedExpr) -> sge.Expression:
179193
return sge.Case(
180194
ifs=[
181195
sge.If(
182-
this=expr.expr <= sge.convert(0),
196+
this=sge.Is(this=expr.expr, expression=sge.Null()),
197+
true=sge.null(),
198+
),
199+
# |x| > 0: The standard formula
200+
sge.If(
201+
this=expr.expr > sge.convert(0),
202+
true=sge.Log(this=sge.convert(10), expression=expr.expr),
203+
),
204+
# |x| < 0: Returns NaN
205+
sge.If(
206+
this=expr.expr < sge.convert(0),
183207
true=constants._NAN,
184-
)
208+
),
185209
],
186-
default=sge.Log(this=expr.expr, expression=sge.convert(10)),
210+
# |x| == 0: Returns -Infinity
211+
default=constants._NEG_INF,
187212
)
188213

189214

@@ -192,11 +217,22 @@ def _(expr: TypedExpr) -> sge.Expression:
192217
return sge.Case(
193218
ifs=[
194219
sge.If(
195-
this=expr.expr <= sge.convert(-1),
220+
this=sge.Is(this=expr.expr, expression=sge.Null()),
221+
true=sge.null(),
222+
),
223+
# Domain: |x| > -1 (The standard formula)
224+
sge.If(
225+
this=expr.expr > sge.convert(-1),
226+
true=sge.Ln(this=sge.convert(1) + expr.expr),
227+
),
228+
# Out of Domain: |x| < -1 (Returns NaN)
229+
sge.If(
230+
this=expr.expr < sge.convert(-1),
196231
true=constants._NAN,
197-
)
232+
),
198233
],
199-
default=sge.Ln(this=sge.convert(1) + expr.expr),
234+
# Boundary: |x| == -1 (Returns -Infinity)
235+
default=constants._NEG_INF,
200236
)
201237

202238

@@ -608,7 +644,7 @@ def isfinite(arg: TypedExpr) -> sge.Expression:
608644
return sge.Not(
609645
this=sge.Or(
610646
this=sge.IsInf(this=arg.expr),
611-
right=sge.IsNan(this=arg.expr),
647+
expression=sge.IsNan(this=arg.expr),
612648
),
613649
)
614650

bigframes/core/compile/sqlglot/sqlglot_ir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def _literal(value: typing.Any, dtype: dtypes.Dtype) -> sge.Expression:
674674
expressions=[_literal(value=v, dtype=value_type) for v in value]
675675
)
676676
return values if len(value) > 0 else _cast(values, sqlglot_type)
677-
elif pd.isna(value):
677+
elif pd.isna(value) or (isinstance(value, pa.Scalar) and not value.is_valid):
678678
return _cast(sge.Null(), sqlglot_type)
679679
elif dtype == dtypes.JSON_DTYPE:
680680
return sge.ParseJSON(this=sge.convert(str(value)))

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_arctanh/out.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ WITH `bfcte_0` AS (
66
SELECT
77
*,
88
CASE
9+
WHEN ABS(`float64_col`) < 1
10+
THEN ATANH(`float64_col`)
911
WHEN ABS(`float64_col`) > 1
1012
THEN CAST('NaN' AS FLOAT64)
11-
ELSE ATANH(`float64_col`)
13+
ELSE CAST('Infinity' AS FLOAT64) * `float64_col`
1214
END AS `bfcol_1`
1315
FROM `bfcte_0`
1416
)

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_expm1/out.sql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ WITH `bfcte_0` AS (
55
), `bfcte_1` AS (
66
SELECT
77
*,
8-
CASE
9-
WHEN `float64_col` > 709.78
10-
THEN CAST('Infinity' AS FLOAT64)
11-
ELSE EXP(`float64_col`)
12-
END - 1 AS `bfcol_1`
8+
IF(`float64_col` > 709.78, CAST('Infinity' AS FLOAT64), EXP(`float64_col`) - 1) AS `bfcol_1`
139
FROM `bfcte_0`
1410
)
1511
SELECT

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_ln/out.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ WITH `bfcte_0` AS (
55
), `bfcte_1` AS (
66
SELECT
77
*,
8-
CASE WHEN `float64_col` <= 0 THEN CAST('NaN' AS FLOAT64) ELSE LN(`float64_col`) END AS `bfcol_1`
8+
CASE
9+
WHEN `float64_col` IS NULL
10+
THEN NULL
11+
WHEN `float64_col` > 0
12+
THEN LN(`float64_col`)
13+
WHEN `float64_col` < 0
14+
THEN CAST('NaN' AS FLOAT64)
15+
ELSE CAST('-Infinity' AS FLOAT64)
16+
END AS `bfcol_1`
917
FROM `bfcte_0`
1018
)
1119
SELECT

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_log10/out.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ WITH `bfcte_0` AS (
66
SELECT
77
*,
88
CASE
9-
WHEN `float64_col` <= 0
9+
WHEN `float64_col` IS NULL
10+
THEN NULL
11+
WHEN `float64_col` > 0
12+
THEN LOG(`float64_col`, 10)
13+
WHEN `float64_col` < 0
1014
THEN CAST('NaN' AS FLOAT64)
11-
ELSE LOG(10, `float64_col`)
15+
ELSE CAST('-Infinity' AS FLOAT64)
1216
END AS `bfcol_1`
1317
FROM `bfcte_0`
1418
)

tests/unit/core/compile/sqlglot/expressions/snapshots/test_numeric_ops/test_log1p/out.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ WITH `bfcte_0` AS (
66
SELECT
77
*,
88
CASE
9-
WHEN `float64_col` <= -1
9+
WHEN `float64_col` IS NULL
10+
THEN NULL
11+
WHEN `float64_col` > -1
12+
THEN LN(1 + `float64_col`)
13+
WHEN `float64_col` < -1
1014
THEN CAST('NaN' AS FLOAT64)
11-
ELSE LN(1 + `float64_col`)
15+
ELSE CAST('-Infinity' AS FLOAT64)
1216
END AS `bfcol_1`
1317
FROM `bfcte_0`
1418
)

0 commit comments

Comments
 (0)