Skip to content

Commit

Permalink
BigQuery: array query parameter sample
Browse files Browse the repository at this point in the history
Run a parameterized query using an array value as a parameter.
  • Loading branch information
tswast committed Dec 19, 2016
1 parent d9f7f20 commit 547041e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
67 changes: 52 additions & 15 deletions bigquery/cloud-client/sync_query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,68 @@ def sync_query_named_params(corpus, min_word_count):
print_results(query_results)


def sync_query_array_params(gender, states):
client = bigquery.Client()
query_results = client.run_sync_query(
"""SELECT name, sum(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE gender = @gender
AND state IN UNNEST(@states)
GROUP BY name
ORDER BY count DESC
LIMIT 10;
""",
query_parameters=(
bigquery.ScalarQueryParameter('gender', 'STRING', gender),
bigquery.ArrayQueryParameter('states', 'STRING', states)))
query_results.use_legacy_sql = False
query_results.run()
print_results(query_results)


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
subparsers = parser.add_subparsers(dest='sample', help='samples')
named_parser = subparsers.add_parser(
'named',
help='Run a query with named parameters.')
named_parser.add_argument(
'corpus',
help='Corpus to search from Shakespeare dataset.')
parser.add_argument(
named_parser.add_argument(
'min_word_count',
help='Minimum count of words to query.',
type=int)

params_type_parser = parser.add_mutually_exclusive_group(required=False)
params_type_parser.add_argument(
'--use-named-params',
dest='use_named_params',
action='store_true')
params_type_parser.add_argument(
'--use-positional-params',
dest='use_named_params',
action='store_false')
parser.set_defaults(use_named_params=False)
positional_parser = subparsers.add_parser(
'positional',
help='Run a query with positional parameters.')
positional_parser.add_argument(
'corpus',
help='Corpus to search from Shakespeare dataset.')
positional_parser.add_argument(
'min_word_count',
help='Minimum count of words to query.',
type=int)
array_parser = subparsers.add_parser(
'array',
help='Run a query with an array parameter.')
array_parser.add_argument(
'gender',
choices=['F', 'M'],
help='Gender of baby in the Social Security baby names database.')
array_parser.add_argument(
'states',
help='U.S. States to consider for popular baby names.',
nargs='+')
args = parser.parse_args()

if args.use_named_params:
if args.sample == 'named':
sync_query_named_params(args.corpus, args.min_word_count)
else:
elif args.sample == 'positional':
sync_query_positional_params(args.corpus, args.min_word_count)
elif args.sample == 'array':
sync_query_array_params(args.gender, args.states)
else:
print('Unexpected value for sample')
8 changes: 8 additions & 0 deletions bigquery/cloud-client/sync_query_params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
import sync_query_params


def test_sync_query_array_params(cloud_config, capsys):
sync_query_params.sync_query_array_params(
gender='M',
states=['WA', 'WI', 'WV', 'WY'])
out, _ = capsys.readouterr()
assert 'James' in out


def test_sync_query_named_params(cloud_config, capsys):
sync_query_params.sync_query_named_params(
corpus='romeoandjuliet',
Expand Down

0 comments on commit 547041e

Please sign in to comment.