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 69
Expand file tree
/
Copy pathbinary_compiler.py
More file actions
125 lines (105 loc) · 4.8 KB
/
binary_compiler.py
File metadata and controls
125 lines (105 loc) · 4.8 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
# 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.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.ge_op)
def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
return sge.GTE(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)