Skip to content
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

feat: handle scalar sql functions in bqsync that reference only datas… #14

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Updates and improve testing and add tox actions on pull requests
- Update dependencies (dependabot) absl-py and bigquery
- Enhance head views to include streaming buffer
- For scalar SQL routines when syncing if fail with Bad Request attempt to use DDL instead of API to apply

1.0.5 April 10th 2023
- Adjust jinja dependency so will allow Jina2 version3
Expand Down
27 changes: 27 additions & 0 deletions src/bqtools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5822,6 +5822,33 @@ def create_destination_routine(copy_driver, routine_name, routine_input):
copy_driver.get_logger().exception(
f"Unable to create routine {routine_name} in {copy_driver.destination_project}.{copy_driver.destination_dataset} definition {routine_input['routine_definition']}"
)
if dstroutine_ref.type_ == "SCALAR_FUNCTION" and dstroutine_ref.language == "SQL":
copy_driver.get_logger().info("As scalar function and SQL attempting adding as query")
function_as_query = f"""CREATE OR REPLACE FUNCTION `{dstroutine_ref.project}.{dstroutine_ref.dataset_id}.{dstroutine_ref.routine_id}` ({",".join([arg.name + " " + arg.data_type.type_kind for arg in dstroutine_ref.arguments])}) AS
({dstroutine_ref.body})
{"RETURNS " + dstroutine_ref.return_type.type_kind if dstroutine_ref.return_type else ""}
OPTIONS (description="{dstroutine_ref.description if dstroutine_ref.description else ""}")"""
try:
for result in run_query(
copy_driver.query_client,
function_as_query,
copy_driver.get_logger(),
"Apply SQL scalar function",
location=copy_driver.destination_location,
callback_on_complete=copy_driver.update_job_stats,
labels=BQSYNCQUERYLABELS,
# ddl statements cannot use CMEK
query_cmek=None,
):
pass
copy_driver.get_logger().info(f"Running as query did work function {routine_name} in {copy_driver.destination_project}.{copy_driver.destination_dataset} created")
except Exception:
copy_driver.increment_routines_failed_sync()
copy_driver.get_logger().exception(
f"Unable to create routine using query {routine_name} in {copy_driver.destination_project}.{copy_driver.destination_dataset} definition {routine_input['routine_definition']}"
)
else:
copy_driver.increment_routines_failed_sync()
return
return dstroutine
else:
Expand Down