23
23
24
24
from bigframes import operations as ops
25
25
from bigframes .core .compile .constants import UNIT_TO_US_CONVERSION_FACTORS
26
+ import bigframes .core .compile .sqlglot .expressions .constants as constants
26
27
from bigframes .core .compile .sqlglot .expressions .op_registration import OpRegistration
27
28
from bigframes .core .compile .sqlglot .expressions .typed_expr import TypedExpr
28
29
29
- _NAN = sge .Cast (this = sge .convert ("NaN" ), to = "FLOAT64" )
30
- _INF = sge .Cast (this = sge .convert ("Infinity" ), to = "FLOAT64" )
31
-
32
- # Approx Highest number you can pass in to EXP function and get a valid FLOAT64 result
33
- # FLOAT64 has 11 exponent bits, so max values is about 2**(2**10)
34
- # ln(2**(2**10)) == (2**10)*ln(2) ~= 709.78, so EXP(x) for x>709.78 will overflow.
35
- _FLOAT64_EXP_BOUND = sge .convert (709.78 )
36
-
37
30
UNARY_OP_REGISTRATION = OpRegistration ()
38
31
39
32
@@ -52,7 +45,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
52
45
ifs = [
53
46
sge .If (
54
47
this = expr .expr < sge .convert (1 ),
55
- true = _NAN ,
48
+ true = constants . _NAN ,
56
49
)
57
50
],
58
51
default = sge .func ("ACOSH" , expr .expr ),
@@ -65,7 +58,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
65
58
ifs = [
66
59
sge .If (
67
60
this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
68
- true = _NAN ,
61
+ true = constants . _NAN ,
69
62
)
70
63
],
71
64
default = sge .func ("ACOS" , expr .expr ),
@@ -78,7 +71,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
78
71
ifs = [
79
72
sge .If (
80
73
this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
81
- true = _NAN ,
74
+ true = constants . _NAN ,
82
75
)
83
76
],
84
77
default = sge .func ("ASIN" , expr .expr ),
@@ -101,7 +94,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
101
94
ifs = [
102
95
sge .If (
103
96
this = sge .func ("ABS" , expr .expr ) > sge .convert (1 ),
104
- true = _NAN ,
97
+ true = constants . _NAN ,
105
98
)
106
99
],
107
100
default = sge .func ("ATANH" , expr .expr ),
@@ -177,7 +170,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
177
170
ifs = [
178
171
sge .If (
179
172
this = sge .func ("ABS" , expr .expr ) > sge .convert (709.78 ),
180
- true = _INF ,
173
+ true = constants . _INF ,
181
174
)
182
175
],
183
176
default = sge .func ("COSH" , expr .expr ),
@@ -222,8 +215,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
222
215
return sge .Case (
223
216
ifs = [
224
217
sge .If (
225
- this = expr .expr > _FLOAT64_EXP_BOUND ,
226
- true = _INF ,
218
+ this = expr .expr > constants . _FLOAT64_EXP_BOUND ,
219
+ true = constants . _INF ,
227
220
)
228
221
],
229
222
default = sge .func ("EXP" , expr .expr ),
@@ -235,8 +228,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
235
228
return sge .Case (
236
229
ifs = [
237
230
sge .If (
238
- this = expr .expr > _FLOAT64_EXP_BOUND ,
239
- true = _INF ,
231
+ this = expr .expr > constants . _FLOAT64_EXP_BOUND ,
232
+ true = constants . _INF ,
240
233
)
241
234
],
242
235
default = sge .func ("EXP" , expr .expr ),
@@ -403,7 +396,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
403
396
ifs = [
404
397
sge .If (
405
398
this = expr .expr < sge .convert (0 ),
406
- true = _NAN ,
399
+ true = constants . _NAN ,
407
400
)
408
401
],
409
402
default = sge .Ln (this = expr .expr ),
@@ -416,7 +409,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
416
409
ifs = [
417
410
sge .If (
418
411
this = expr .expr < sge .convert (0 ),
419
- true = _NAN ,
412
+ true = constants . _NAN ,
420
413
)
421
414
],
422
415
default = sge .Log (this = expr .expr , expression = sge .convert (10 )),
@@ -429,7 +422,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
429
422
ifs = [
430
423
sge .If (
431
424
this = expr .expr < sge .convert (- 1 ),
432
- true = _NAN ,
425
+ true = constants . _NAN ,
433
426
)
434
427
],
435
428
default = sge .Ln (this = sge .convert (1 ) + expr .expr ),
@@ -512,7 +505,7 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
512
505
ifs = [
513
506
sge .If (
514
507
this = expr .expr < sge .convert (0 ),
515
- true = _NAN ,
508
+ true = constants . _NAN ,
516
509
)
517
510
],
518
511
default = sge .Sqrt (this = expr .expr ),
@@ -534,8 +527,8 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
534
527
return sge .Case (
535
528
ifs = [
536
529
sge .If (
537
- this = sge .func ("ABS" , expr .expr ) > _FLOAT64_EXP_BOUND ,
538
- true = sge .func ("SIGN" , expr .expr ) * _INF ,
530
+ this = sge .func ("ABS" , expr .expr ) > constants . _FLOAT64_EXP_BOUND ,
531
+ true = sge .func ("SIGN" , expr .expr ) * constants . _INF ,
539
532
)
540
533
],
541
534
default = sge .func ("SINH" , expr .expr ),
0 commit comments