|
| 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 | +from typing import Sequence |
| 18 | + |
| 19 | +import sqlglot.expressions as sge |
| 20 | + |
| 21 | +from bigframes import operations as ops |
| 22 | +from bigframes.core.compile.sqlglot import scalar_compiler |
| 23 | +from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr |
| 24 | + |
| 25 | +register_nary_op = scalar_compiler.scalar_op_compiler.register_nary_op |
| 26 | + |
| 27 | + |
| 28 | +@register_nary_op(ops.AIGenerateBool, pass_op=True) |
| 29 | +def _(*exprs: TypedExpr, op: ops.AIGenerateBool) -> sge.Expression: |
| 30 | + |
| 31 | + prompt: list[str | sge.Expression] = [] |
| 32 | + column_ref_idx = 0 |
| 33 | + |
| 34 | + for elem in op.prompt_context: |
| 35 | + if elem is None: |
| 36 | + prompt.append(exprs[column_ref_idx].expr) |
| 37 | + else: |
| 38 | + prompt.append(sge.Literal.string(elem)) |
| 39 | + |
| 40 | + args = [sge.Kwarg(this="prompt", expression=sge.Tuple(expressions=prompt))] |
| 41 | + |
| 42 | + args.append( |
| 43 | + sge.Kwarg(this="connection_id", expression=sge.Literal.string(op.connection_id)) |
| 44 | + ) |
| 45 | + |
| 46 | + if op.endpoint is not None: |
| 47 | + args.append( |
| 48 | + sge.Kwarg(this="endpoint", expression=sge.Literal.string(op.endpoint)) |
| 49 | + ) |
| 50 | + |
| 51 | + args.append( |
| 52 | + sge.Kwarg( |
| 53 | + this="request_type", expression=sge.Literal.string(op.request_type.upper()) |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + if op.model_params is not None: |
| 58 | + args.append( |
| 59 | + sge.Kwarg( |
| 60 | + this="model_params", |
| 61 | + expression=sge.JSON(this=sge.Literal.string(op.model_params)), |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + return sge.func("AI.GENERATE_BOOL", *args) |
0 commit comments