Skip to content

Commit

Permalink
Make use of to_query_parameters and BQ parametrized syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermo-carrasco committed Oct 26, 2018
1 parent ca18126 commit 6c32111
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions bigquery/google/cloud/bigquery/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@

import google.auth
from google.cloud import bigquery
from google.cloud.bigquery.dbapi._helpers import to_query_parameters


class Context(object):
Expand Down Expand Up @@ -276,17 +277,17 @@ def _cell_magic(line, query):
"""
args = magic_arguments.parse_argstring(_cell_magic, line)

if args.params is not None:
params = []
if params:
try:
params = ast.literal_eval(''.join(args.params))
params = to_query_parameters(ast.literal_eval(''.join(args.params)))
except Exception:
raise SyntaxError('--params is not a correctly formatted JSON string')

query = query.format(**params)

project = args.project or context.project
client = bigquery.Client(project=project, credentials=context.credentials)
job_config = bigquery.job.QueryJobConfig()
job_config.query_parameters = params
job_config.use_legacy_sql = args.use_legacy_sql
query_job = _run_query(client, query, job_config)

Expand Down
2 changes: 1 addition & 1 deletion bigquery/tests/unit/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def test_bigquery_magic_with_formatting_params():
magics.context.credentials = mock.create_autospec(
google.auth.credentials.Credentials, instance=True)

sql = 'SELECT {num} AS num'
sql = 'SELECT @num AS num'
result = pandas.DataFrame([17], columns=['num'])
assert 'myvariable' not in ip.user_ns

Expand Down

0 comments on commit 6c32111

Please sign in to comment.