This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathbinary_compiler.py
More file actions
246 lines (203 loc) · 9.3 KB
/
binary_compiler.py
File metadata and controls
246 lines (203 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import bigframes_vendored.constants as constants
import sqlglot.expressions as sge
from bigframes import dtypes
from bigframes import operations as ops
from bigframes.core.compile.sqlglot.expressions.op_registration import OpRegistration
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr
BINARY_OP_REGISTRATION = OpRegistration()
def compile(op: ops.BinaryOp, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return BINARY_OP_REGISTRATION[op](op, left, right)
# TODO: add parenthesize for operators
@BINARY_OP_REGISTRATION.register(ops.add_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
if left.dtype == dtypes.STRING_DTYPE and right.dtype == dtypes.STRING_DTYPE:
# String addition
return sge.Concat(expressions=[left.expr, right.expr])
if dtypes.is_numeric(left.dtype) and dtypes.is_numeric(right.dtype):
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.Add(this=left_expr, expression=right_expr)
if (
dtypes.is_time_or_date_like(left.dtype)
and right.dtype == dtypes.TIMEDELTA_DTYPE
):
left_expr = left.expr
if left.dtype == dtypes.DATE_DTYPE:
left_expr = sge.Cast(this=left_expr, to="DATETIME")
return sge.TimestampAdd(
this=left_expr, expression=right.expr, unit=sge.Var(this="MICROSECOND")
)
if (
dtypes.is_time_or_date_like(right.dtype)
and left.dtype == dtypes.TIMEDELTA_DTYPE
):
right_expr = right.expr
if right.dtype == dtypes.DATE_DTYPE:
right_expr = sge.Cast(this=right_expr, to="DATETIME")
return sge.TimestampAdd(
this=right_expr, expression=left.expr, unit=sge.Var(this="MICROSECOND")
)
if left.dtype == dtypes.TIMEDELTA_DTYPE and right.dtype == dtypes.TIMEDELTA_DTYPE:
return sge.Add(this=left.expr, expression=right.expr)
raise TypeError(
f"Cannot add type {left.dtype} and {right.dtype}. {constants.FEEDBACK_LINK}"
)
@BINARY_OP_REGISTRATION.register(ops.eq_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.EQ(this=left_expr, expression=right_expr)
@BINARY_OP_REGISTRATION.register(ops.eq_null_match_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE and right.dtype != dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE and left.dtype != dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
sentinel = sge.convert("$NULL_SENTINEL$")
left_coalesce = sge.Coalesce(
this=sge.Cast(this=left_expr, to="STRING"), expressions=[sentinel]
)
right_coalesce = sge.Coalesce(
this=sge.Cast(this=right_expr, to="STRING"), expressions=[sentinel]
)
return sge.EQ(this=left_coalesce, expression=right_coalesce)
@BINARY_OP_REGISTRATION.register(ops.div_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
result = sge.func("IEEE_DIVIDE", left_expr, right_expr)
if left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right.dtype):
return sge.Cast(this=sge.Floor(this=result), to="INT64")
else:
return result
@BINARY_OP_REGISTRATION.register(ops.ge_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.GTE(this=left_expr, expression=right_expr)
@BINARY_OP_REGISTRATION.register(ops.gt_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.GT(this=left_expr, expression=right_expr)
@BINARY_OP_REGISTRATION.register(ops.JSONSet)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.func("JSON_SET", left.expr, sge.convert(op.json_path), right.expr)
@BINARY_OP_REGISTRATION.register(ops.lt_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.LT(this=left_expr, expression=right_expr)
@BINARY_OP_REGISTRATION.register(ops.le_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.LTE(this=left_expr, expression=right_expr)
@BINARY_OP_REGISTRATION.register(ops.mul_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
result = sge.Mul(this=left_expr, expression=right_expr)
if (dtypes.is_numeric(left.dtype) and right.dtype == dtypes.TIMEDELTA_DTYPE) or (
left.dtype == dtypes.TIMEDELTA_DTYPE and dtypes.is_numeric(right.dtype)
):
return sge.Cast(this=sge.Floor(this=result), to="INT64")
else:
return result
@BINARY_OP_REGISTRATION.register(ops.ne_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.NEQ(this=left_expr, expression=right_expr)
@BINARY_OP_REGISTRATION.register(ops.sub_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
if dtypes.is_numeric(left.dtype) and dtypes.is_numeric(right.dtype):
left_expr = left.expr
if left.dtype == dtypes.BOOL_DTYPE:
left_expr = sge.Cast(this=left_expr, to="INT64")
right_expr = right.expr
if right.dtype == dtypes.BOOL_DTYPE:
right_expr = sge.Cast(this=right_expr, to="INT64")
return sge.Sub(this=left_expr, expression=right_expr)
if (
dtypes.is_time_or_date_like(left.dtype)
and right.dtype == dtypes.TIMEDELTA_DTYPE
):
left_expr = left.expr
if left.dtype == dtypes.DATE_DTYPE:
left_expr = sge.Cast(this=left_expr, to="DATETIME")
return sge.TimestampSub(
this=left_expr, expression=right.expr, unit=sge.Var(this="MICROSECOND")
)
if dtypes.is_time_or_date_like(left.dtype) and dtypes.is_time_or_date_like(
right.dtype
):
left_expr = left.expr
if left.dtype == dtypes.DATE_DTYPE:
left_expr = sge.Cast(this=left_expr, to="DATETIME")
right_expr = right.expr
if right.dtype == dtypes.DATE_DTYPE:
right_expr = sge.Cast(this=right_expr, to="DATETIME")
return sge.TimestampDiff(
this=left_expr, expression=right_expr, unit=sge.Var(this="MICROSECOND")
)
if left.dtype == dtypes.TIMEDELTA_DTYPE and right.dtype == dtypes.TIMEDELTA_DTYPE:
return sge.Sub(this=left.expr, expression=right.expr)
raise TypeError(
f"Cannot subtract type {left.dtype} and {right.dtype}. {constants.FEEDBACK_LINK}"
)
@BINARY_OP_REGISTRATION.register(ops.obj_make_ref_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.func("OBJ.MAKE_REF", left.expr, right.expr)