1414
1515from __future__ import annotations
1616
17+ import bigframes_vendored .constants as constants
1718import sqlglot .expressions as sge
1819
1920from bigframes import dtypes
@@ -35,8 +36,49 @@ def _(op, left: TypedExpr, right: TypedExpr) -> sge.Expression:
3536 # String addition
3637 return sge .Concat (expressions = [left .expr , right .expr ])
3738
38- # Numerical addition
39- return sge .Add (this = left .expr , expression = right .expr )
39+ if dtypes .is_numeric (left .dtype ) and dtypes .is_numeric (right .dtype ):
40+ left_expr = left .expr
41+ if left .dtype == dtypes .BOOL_DTYPE :
42+ left_expr = sge .Cast (this = left_expr , to = "INT64" )
43+ right_expr = right .expr
44+ if right .dtype == dtypes .BOOL_DTYPE :
45+ right_expr = sge .Cast (this = right_expr , to = "INT64" )
46+ return sge .Add (this = left_expr , expression = right_expr )
47+
48+ if (
49+ dtypes .is_datetime_like (left .dtype ) or dtypes .is_date_like (left .dtype )
50+ ) and right .dtype == dtypes .TIMEDELTA_DTYPE :
51+ left_expr = left .expr
52+ if left .dtype == dtypes .DATE_DTYPE :
53+ left_expr = sge .Cast (this = left_expr , to = "DATETIME" )
54+ return sge .TimestampAdd (
55+ this = left_expr , expression = right .expr , unit = sge .Var (this = "MICROSECOND" )
56+ )
57+ if (
58+ dtypes .is_datetime_like (right .dtype ) or dtypes .is_date_like (right .dtype )
59+ ) and left .dtype == dtypes .TIMEDELTA_DTYPE :
60+ right_expr = right .expr
61+ if right .dtype == dtypes .DATE_DTYPE :
62+ right_expr = sge .Cast (this = right_expr , to = "DATETIME" )
63+ return sge .TimestampAdd (
64+ this = right_expr , expression = left .expr , unit = sge .Var (this = "MICROSECOND" )
65+ )
66+
67+ if left .dtype == dtypes .DATE_DTYPE and right .dtype == dtypes .INT_DTYPE :
68+ return sge .DateAdd (
69+ this = left .expr , expression = right .expr , unit = sge .Var (this = "DAY" )
70+ )
71+
72+ if right .dtype == dtypes .DATE_DTYPE and left .dtype == dtypes .INT_DTYPE :
73+ return sge .DateAdd (
74+ this = right .expr , expression = left .expr , unit = sge .Var (this = "DAY" )
75+ )
76+ if left .dtype == dtypes .TIMEDELTA_DTYPE and right .dtype == dtypes .TIMEDELTA_DTYPE :
77+ return sge .Add (this = left .expr , expression = right .expr )
78+
79+ raise TypeError (
80+ f"Cannot add type { left .dtype } and { right .dtype } . { constants .FEEDBACK_LINK } "
81+ )
4082
4183
4284@BINARY_OP_REGISTRATION .register (ops .ge_op )
0 commit comments