-
Notifications
You must be signed in to change notification settings - Fork 63
chore: implement ai.generate_bool in SQLGlot compiler #2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5814727
chore: implement ai.generate_bool in SQLGlot compiler
sycai 4ded421
Merge branch 'main' into sycai_ai_gen_bool_sqlglot
sycai 1c47302
fix lint
sycai eab92f9
fix test
sycai fbd5eb8
Merge branch 'main' into sycai_ai_gen_bool_sqlglot
sycai 5d3e971
Merge branch 'main' into sycai_ai_gen_bool_sqlglot
sycai 530b6b6
Merge branch 'main' into sycai_ai_gen_bool_sqlglot
sycai fb6c364
add comment on sge.JSON
sycai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # 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 sqlglot.expressions as sge | ||
|
|
||
| from bigframes import operations as ops | ||
| from bigframes.core.compile.sqlglot import scalar_compiler | ||
| from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr | ||
|
|
||
| register_nary_op = scalar_compiler.scalar_op_compiler.register_nary_op | ||
|
|
||
|
|
||
| @register_nary_op(ops.AIGenerateBool, pass_op=True) | ||
| def _(*exprs: TypedExpr, op: ops.AIGenerateBool) -> sge.Expression: | ||
|
|
||
| prompt: list[str | sge.Expression] = [] | ||
| column_ref_idx = 0 | ||
|
|
||
| for elem in op.prompt_context: | ||
| if elem is None: | ||
| prompt.append(exprs[column_ref_idx].expr) | ||
| else: | ||
| prompt.append(sge.Literal.string(elem)) | ||
|
|
||
| args = [sge.Kwarg(this="prompt", expression=sge.Tuple(expressions=prompt))] | ||
|
|
||
| args.append( | ||
| sge.Kwarg(this="connection_id", expression=sge.Literal.string(op.connection_id)) | ||
| ) | ||
|
|
||
| if op.endpoint is not None: | ||
| args.append( | ||
| sge.Kwarg(this="endpoint", expression=sge.Literal.string(op.endpoint)) | ||
| ) | ||
|
|
||
| args.append( | ||
| sge.Kwarg( | ||
| this="request_type", expression=sge.Literal.string(op.request_type.upper()) | ||
| ) | ||
| ) | ||
|
|
||
| if op.model_params is not None: | ||
| args.append( | ||
| sge.Kwarg( | ||
| this="model_params", | ||
| # sge.JSON requires a newer SQLGlot version than 23.6.3. | ||
| # PARSE_JSON won't work as the function requires a JSON literal. | ||
| expression=sge.JSON(this=sge.Literal.string(op.model_params)), | ||
| ) | ||
| ) | ||
|
|
||
| return sge.func("AI.GENERATE_BOOL", *args) | ||
18 changes: 18 additions & 0 deletions
18
...unit/core/compile/sqlglot/expressions/snapshots/test_ai_ops/test_ai_generate_bool/out.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| WITH `bfcte_0` AS ( | ||
| SELECT | ||
| `string_col` AS `bfcol_0` | ||
| FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` | ||
| ), `bfcte_1` AS ( | ||
| SELECT | ||
| *, | ||
| AI.GENERATE_BOOL( | ||
| prompt => (`bfcol_0`, ' is the same as ', `bfcol_0`), | ||
| connection_id => 'test_connection_id', | ||
| endpoint => 'gemini-2.5-flash', | ||
| request_type => 'SHARED' | ||
| ) AS `bfcol_1` | ||
| FROM `bfcte_0` | ||
| ) | ||
| SELECT | ||
| `bfcol_1` AS `result` | ||
| FROM `bfcte_1` |
18 changes: 18 additions & 0 deletions
18
.../sqlglot/expressions/snapshots/test_ai_ops/test_ai_generate_bool_with_model_param/out.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| WITH `bfcte_0` AS ( | ||
| SELECT | ||
| `string_col` AS `bfcol_0` | ||
| FROM `bigframes-dev`.`sqlglot_test`.`scalar_types` | ||
| ), `bfcte_1` AS ( | ||
| SELECT | ||
| *, | ||
| AI.GENERATE_BOOL( | ||
| prompt => (`bfcol_0`, ' is the same as ', `bfcol_0`), | ||
| connection_id => 'test_connection_id', | ||
| request_type => 'SHARED', | ||
| model_params => JSON '{}' | ||
| ) AS `bfcol_1` | ||
| FROM `bfcte_0` | ||
| ) | ||
| SELECT | ||
| `bfcol_1` AS `result` | ||
| FROM `bfcte_1` |
67 changes: 67 additions & 0 deletions
67
tests/unit/core/compile/sqlglot/expressions/test_ai_ops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # 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. | ||
|
|
||
| import json | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from bigframes import dataframe | ||
| from bigframes import operations as ops | ||
| from bigframes.testing import utils | ||
|
|
||
| pytest.importorskip("pytest_snapshot") | ||
|
|
||
|
|
||
| def test_ai_generate_bool(scalar_types_df: dataframe.DataFrame, snapshot): | ||
| col_name = "string_col" | ||
|
|
||
| op = ops.AIGenerateBool( | ||
| prompt_context=(None, " is the same as ", None), | ||
| connection_id="test_connection_id", | ||
| endpoint="gemini-2.5-flash", | ||
| request_type="shared", | ||
| model_params=None, | ||
| ) | ||
|
|
||
| sql = utils._apply_unary_ops( | ||
| scalar_types_df, [op.as_expr(col_name, col_name)], ["result"] | ||
| ) | ||
|
|
||
| snapshot.assert_match(sql, "out.sql") | ||
|
|
||
|
|
||
| def test_ai_generate_bool_with_model_param( | ||
| scalar_types_df: dataframe.DataFrame, snapshot | ||
| ): | ||
| if sys.version_info < (3, 10): | ||
| pytest.skip( | ||
| "Skip test because SQLGLot cannot compile model params to JSON at this env." | ||
| ) | ||
|
|
||
| col_name = "string_col" | ||
|
|
||
| op = ops.AIGenerateBool( | ||
| prompt_context=(None, " is the same as ", None), | ||
| connection_id="test_connection_id", | ||
| endpoint=None, | ||
| request_type="shared", | ||
| model_params=json.dumps(dict()), | ||
| ) | ||
|
|
||
| sql = utils._apply_unary_ops( | ||
| scalar_types_df, [op.as_expr(col_name, col_name)], ["result"] | ||
| ) | ||
|
|
||
| snapshot.assert_match(sql, "out.sql") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.