Skip to content

Commit ea849e0

Browse files
authored
feat: support job_config at query level (#1167)
* feat: support job_cofig at query level * add unit test * remove breakpoint
1 parent 1840951 commit ea849e0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

sqlalchemy_bigquery/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,12 @@ def _add_default_dataset_to_job_config(job_config, project_id, dataset_id):
11241124

11251125
job_config.default_dataset = "{}.{}".format(project_id, dataset_id)
11261126

1127+
def do_execute(self, cursor, statement, parameters, context=None):
1128+
kwargs = {}
1129+
if context is not None and context.execution_options.get("job_config"):
1130+
kwargs["job_config"] = context.execution_options.get("job_config")
1131+
cursor.execute(statement, parameters, **kwargs)
1132+
11271133
def create_connect_args(self, url):
11281134
(
11291135
project_id,

tests/unit/test_sqlalchemy_bigquery.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,24 @@ def test_setting_user_supplied_client_skips_creating_client(
240240
)
241241
assert result == ([], {})
242242
assert not mock_create_bigquery_client.called
243+
244+
245+
def test_do_execute():
246+
# Ensures the do_execute() method overrides that of the parent class.
247+
import sqlalchemy_bigquery # noqa
248+
from sqlalchemy_bigquery.base import BigQueryExecutionContext
249+
250+
job_config_kwargs = {}
251+
job_config_kwargs["use_query_cache"] = False
252+
job_config = bigquery.QueryJobConfig(**job_config_kwargs)
253+
execution_options = {"job_config": job_config}
254+
context = mock.MagicMock(spec=BigQueryExecutionContext)
255+
type(context).execution_options = mock.PropertyMock(return_value=execution_options)
256+
257+
cursor = mock.MagicMock()
258+
259+
sqlalchemy_bigquery.BigQueryDialect().do_execute(
260+
cursor, sqlalchemy.text("SELECT 'a' AS `1`"), mock.MagicMock(), context=context
261+
)
262+
263+
assert cursor.execute.call_args.kwargs["job_config"] is job_config

0 commit comments

Comments
 (0)