|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import sqlglot.expressions as sge |
| 18 | + |
| 19 | +from bigframes import dtypes |
| 20 | +from bigframes import operations as ops |
| 21 | +from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr |
| 22 | +import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler |
| 23 | + |
| 24 | +register_binary_op = scalar_compiler.scalar_op_compiler.register_binary_op |
| 25 | + |
| 26 | + |
| 27 | +@register_binary_op(ops.and_op) |
| 28 | +def _(left: TypedExpr, right: TypedExpr) -> sge.Expression: |
| 29 | + if left.dtype == dtypes.BOOL_DTYPE and right.dtype == dtypes.BOOL_DTYPE: |
| 30 | + return sge.And(this=left.expr, expression=right.expr) |
| 31 | + return sge.BitwiseAnd(this=left.expr, expression=right.expr) |
| 32 | + |
| 33 | + |
| 34 | +@register_binary_op(ops.or_op) |
| 35 | +def _(left: TypedExpr, right: TypedExpr) -> sge.Expression: |
| 36 | + if left.dtype == dtypes.BOOL_DTYPE and right.dtype == dtypes.BOOL_DTYPE: |
| 37 | + return sge.Or(this=left.expr, expression=right.expr) |
| 38 | + return sge.BitwiseOr(this=left.expr, expression=right.expr) |
| 39 | + |
| 40 | + |
| 41 | +@register_binary_op(ops.xor_op) |
| 42 | +def _(left: TypedExpr, right: TypedExpr) -> sge.Expression: |
| 43 | + if left.dtype == dtypes.BOOL_DTYPE and right.dtype == dtypes.BOOL_DTYPE: |
| 44 | + left_expr = sge.And(this=left.expr, expression=sge.Not(this=right.expr)) |
| 45 | + right_expr = sge.And(this=sge.Not(this=left.expr), expression=right.expr) |
| 46 | + return sge.Or(this=left_expr, expression=right_expr) |
| 47 | + return sge.BitwiseXor(this=left.expr, expression=right.expr) |
0 commit comments