22
22
import sqlglot .expressions as sge
23
23
24
24
from bigframes import operations as ops
25
+ import bigframes .core .compile .sqlglot .expressions .constants as constants
25
26
from bigframes .core .compile .sqlglot .expressions .op_registration import OpRegistration
26
27
from bigframes .core .compile .sqlglot .expressions .typed_expr import TypedExpr
27
28
28
- _NAN = sge .Cast (this = sge .convert ("NaN" ), to = "FLOAT64" )
29
- _INF = sge .Cast (this = sge .convert ("Infinity" ), to = "FLOAT64" )
30
-
31
- # Approx Highest number you can pass in to EXP function and get a valid FLOAT64 result
32
- # FLOAT64 has 11 exponent bits, so max values is about 2**(2**10)
33
- # ln(2**(2**10)) == (2**10)*ln(2) ~= 709.78, so EXP(x) for x>709.78 will overflow.
34
- _FLOAT64_EXP_BOUND = sge .convert (709.78 )
35
-
36
29
UNARY_OP_REGISTRATION = OpRegistration ()
37
30
38
31
@@ -51,7 +44,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
51
44
ifs = [
52
45
sge .If (
53
46
this = expr .expr < sge .convert (1 ),
54
- true = _NAN ,
47
+ true = constants . _NAN ,
55
48
)
56
49
],
57
50
default = sge .func ("ACOSH" , expr .expr ),
@@ -64,7 +57,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
64
57
ifs = [
65
58
sge .If (
66
59
this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
67
- true = _NAN ,
60
+ true = constants . _NAN ,
68
61
)
69
62
],
70
63
default = sge .func ("ACOS" , expr .expr ),
@@ -77,7 +70,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
77
70
ifs = [
78
71
sge .If (
79
72
this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
80
- true = _NAN ,
73
+ true = constants . _NAN ,
81
74
)
82
75
],
83
76
default = sge .func ("ASIN" , expr .expr ),
@@ -100,7 +93,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
100
93
ifs = [
101
94
sge .If (
102
95
this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
103
- true = _NAN ,
96
+ true = constants . _NAN ,
104
97
)
105
98
],
106
99
default = sge .func ("ATANH" , expr .expr ),
@@ -176,7 +169,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
176
169
ifs = [
177
170
sge .If (
178
171
this = sge .func ("ABS" , expr .expr ) > sge .convert (709.78 ),
179
- true = _INF ,
172
+ true = constants . _INF ,
180
173
)
181
174
],
182
175
default = sge .func ("COSH" , expr .expr ),
@@ -221,8 +214,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
221
214
return sge .Case (
222
215
ifs = [
223
216
sge .If (
224
- this = expr .expr > _FLOAT64_EXP_BOUND ,
225
- true = _INF ,
217
+ this = expr .expr > constants . _FLOAT64_EXP_BOUND ,
218
+ true = constants . _INF ,
226
219
)
227
220
],
228
221
default = sge .func ("EXP" , expr .expr ),
@@ -234,8 +227,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
234
227
return sge .Case (
235
228
ifs = [
236
229
sge .If (
237
- this = expr .expr > _FLOAT64_EXP_BOUND ,
238
- true = _INF ,
230
+ this = expr .expr > constants . _FLOAT64_EXP_BOUND ,
231
+ true = constants . _INF ,
239
232
)
240
233
],
241
234
default = sge .func ("EXP" , expr .expr ),
@@ -382,7 +375,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
382
375
ifs = [
383
376
sge .If (
384
377
this = expr .expr < sge .convert (0 ),
385
- true = _NAN ,
378
+ true = constants . _NAN ,
386
379
)
387
380
],
388
381
default = sge .Ln (this = expr .expr ),
@@ -395,7 +388,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
395
388
ifs = [
396
389
sge .If (
397
390
this = expr .expr < sge .convert (0 ),
398
- true = _NAN ,
391
+ true = constants . _NAN ,
399
392
)
400
393
],
401
394
default = sge .Log (this = expr .expr , expression = sge .convert (10 )),
@@ -408,7 +401,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
408
401
ifs = [
409
402
sge .If (
410
403
this = expr .expr < sge .convert (- 1 ),
411
- true = _NAN ,
404
+ true = constants . _NAN ,
412
405
)
413
406
],
414
407
default = sge .Ln (this = sge .convert (1 ) + expr .expr ),
@@ -476,7 +469,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
476
469
ifs = [
477
470
sge .If (
478
471
this = expr .expr < sge .convert (0 ),
479
- true = _NAN ,
472
+ true = constants . _NAN ,
480
473
)
481
474
],
482
475
default = sge .Sqrt (this = expr .expr ),
@@ -523,8 +516,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
523
516
return sge .Case (
524
517
ifs = [
525
518
sge .If (
526
- this = sge .func ("ABS" , expr .expr ) > _FLOAT64_EXP_BOUND ,
527
- true = sge .func ("SIGN" , expr .expr ) * _INF ,
519
+ this = sge .func ("ABS" , expr .expr ) > constants . _FLOAT64_EXP_BOUND ,
520
+ true = sge .func ("SIGN" , expr .expr ) * constants . _INF ,
528
521
)
529
522
],
530
523
default = sge .func ("SINH" , expr .expr ),
0 commit comments