-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Adding Spanner STRUCT param samples #1519
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import argparse | ||
|
||
from google.cloud import spanner | ||
from google.cloud.spanner_v1 import param_types | ||
|
||
|
||
# [START spanner_create_database] | ||
|
@@ -598,6 +599,164 @@ def query_data_with_timestamp(instance_id, database_id): | |
# [END spanner_query_data_with_timestamp_column] | ||
|
||
|
||
# [START spanner_write_data_for_struct_queries] | ||
def write_struct_data(instance_id, database_id): | ||
"""Inserts sample data that can be used to test STRUCT parameters in queries. | ||
""" | ||
spanner_client = spanner.Client() | ||
instance = spanner_client.instance(instance_id) | ||
database = instance.database(database_id) | ||
|
||
with database.batch() as batch: | ||
batch.insert( | ||
table='Singers', | ||
columns=('SingerId', 'FirstName', 'LastName',), | ||
values=[ | ||
(6, u'Elena', u'Campbell'), | ||
(7, u'Gabriel', u'Wright'), | ||
(8, u'Benjamin', u'Martinez'), | ||
(9, u'Hannah', u'Harris')]) | ||
|
||
print('Inserted sample data for STRUCT queries') | ||
# [END spanner_write_data_for_struct_queries] | ||
|
||
|
||
def query_with_struct(instance_id, database_id): | ||
""" | ||
Query a table using STRUCT parameters. | ||
""" | ||
# [START spanner_create_struct_with_data] | ||
record_type = param_types.Struct([ | ||
param_types.StructField('FirstName', param_types.STRING), | ||
param_types.StructField('LastName', param_types.STRING) | ||
]) | ||
record_value = ('Elena', 'Campbell') | ||
# [END spanner_create_struct_with_data] | ||
|
||
# [START spanner_query_data_with_struct] | ||
spanner_client = spanner.Client() | ||
instance = spanner_client.instance(instance_id) | ||
|
||
database = instance.database(database_id) | ||
|
||
with database.snapshot() as snapshot: | ||
results = snapshot.execute_sql( | ||
"SELECT SingerId FROM Singers WHERE " | ||
"(FirstName, LastName) = @name", | ||
params={'name': record_value}, | ||
param_types={'name': record_type}) | ||
|
||
for row in results: | ||
print(u'SingerId: {}'.format(*row)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this is indented too far. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
# [END spanner_query_data_with_struct] | ||
|
||
|
||
def query_with_array_of_struct(instance_id, database_id): | ||
""" | ||
Query a table using an array of STRUCT parameters. | ||
""" | ||
# [START spanner_create_user_defined_struct] | ||
name_type = param_types.Struct([ | ||
param_types.StructField('FirstName', param_types.STRING), | ||
param_types.StructField('LastName', param_types.STRING) | ||
]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this is indented one too far. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
# [END spanner_create_user_defined_struct] | ||
|
||
# [START spanner_create_array_of_struct_with_data] | ||
band_members = [("Elena", "Campbell"), | ||
("Gabriel", "Wright"), | ||
("Benjamin", "Martinez")] | ||
# [END spanner_create_array_of_struct_with_data] | ||
|
||
# [START spanner_query_data_with_array_of_struct] | ||
spanner_client = spanner.Client() | ||
instance = spanner_client.instance(instance_id) | ||
database = instance.database(database_id) | ||
|
||
with database.snapshot() as snapshot: | ||
results = snapshot.execute_sql( | ||
"SELECT SingerId FROM Singers WHERE " | ||
"STRUCT<FirstName STRING, LastName STRING>" | ||
"(FirstName, LastName) IN UNNEST(@names)", | ||
params={'names': band_members}, | ||
param_types={'names': param_types.Array(name_type)}) | ||
|
||
for row in results: | ||
print(u'SingerId: {}'.format(*row)) | ||
# [END spanner_query_data_with_array_of_struct] | ||
|
||
|
||
# [START spanner_field_access_on_struct_parameters] | ||
def query_struct_field(instance_id, database_id): | ||
""" | ||
Query a table using field access on a STRUCT parameter. | ||
""" | ||
spanner_client = spanner.Client() | ||
instance = spanner_client.instance(instance_id) | ||
database = instance.database(database_id) | ||
|
||
name_type = param_types.Struct([ | ||
param_types.StructField('FirstName', param_types.STRING), | ||
param_types.StructField('LastName', param_types.STRING) | ||
]) | ||
|
||
with database.snapshot() as snapshot: | ||
results = snapshot.execute_sql( | ||
"SELECT SingerId FROM Singers " | ||
"WHERE FirstName = @name.FirstName", | ||
params={'name': ("Elena", "Campbell")}, | ||
param_types={'name': name_type}) | ||
|
||
for row in results: | ||
print(u'SingerId: {}'.format(*row)) | ||
# [START spanner_field_access_on_struct_parameters] | ||
|
||
|
||
# [START spanner_field_access_on_nested_struct_parameters] | ||
def query_nested_struct_field(instance_id, database_id): | ||
""" | ||
Query a table using nested field access on a STRUCT parameter. | ||
""" | ||
spanner_client = spanner.Client() | ||
instance = spanner_client.instance(instance_id) | ||
database = instance.database(database_id) | ||
|
||
song_info_type = param_types.Struct([ | ||
param_types.StructField('SongName', param_types.STRING), | ||
param_types.StructField( | ||
'ArtistNames', param_types.Array( | ||
param_types.Struct([ | ||
param_types.StructField( | ||
'FirstName', param_types.STRING), | ||
param_types.StructField( | ||
'LastName', param_types.STRING) | ||
]) | ||
) | ||
) | ||
]) | ||
|
||
song_info = ('Imagination', [('Elena', 'Campbell'), ('Hannah', 'Harris')]) | ||
|
||
with database.snapshot() as snapshot: | ||
results = snapshot.execute_sql( | ||
"SELECT SingerId, @song_info.SongName " | ||
"FROM Singers WHERE " | ||
"STRUCT<FirstName STRING, LastName STRING>" | ||
"(FirstName, LastName) " | ||
"IN UNNEST(@song_info.ArtistNames)", | ||
params={ | ||
'song_info': song_info | ||
}, | ||
param_types={ | ||
'song_info': song_info_type | ||
} | ||
) | ||
|
||
for row in results: | ||
print(u'SingerId: {} SongName: {}'.format(*row)) | ||
# [END spanner_field_access_on_nested_struct_parameters] | ||
|
||
|
||
if __name__ == '__main__': # noqa: C901 | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
|
@@ -644,6 +803,14 @@ def query_data_with_timestamp(instance_id, database_id): | |
'update_data_with_timestamp', help=update_data_with_timestamp.__doc__) | ||
subparsers.add_parser( | ||
'query_data_with_timestamp', help=query_data_with_timestamp.__doc__) | ||
subparsers.add_parser('write_struct_data', help=write_struct_data.__doc__) | ||
subparsers.add_parser('query_with_struct', help=query_with_struct.__doc__) | ||
subparsers.add_parser( | ||
'query_with_array_of_struct', help=query_with_array_of_struct.__doc__) | ||
subparsers.add_parser( | ||
'query_struct_field', help=query_struct_field.__doc__) | ||
subparsers.add_parser( | ||
'query_nested_struct_field', help=query_nested_struct_field.__doc__) | ||
|
||
args = parser.parse_args() | ||
|
||
|
@@ -689,3 +856,13 @@ def query_data_with_timestamp(instance_id, database_id): | |
update_data_with_timestamp(args.instance_id, args.database_id) | ||
elif args.command == 'query_data_with_timestamp': | ||
query_data_with_timestamp(args.instance_id, args.database_id) | ||
elif args.command == 'write_struct_data': | ||
write_struct_data(args.instance_id, args.database_id) | ||
elif args.command == 'query_with_struct': | ||
query_with_struct(args.instance_id, args.database_id) | ||
elif args.command == 'query_with_array_of_struct': | ||
query_with_array_of_struct(args.instance_id, args.database_id) | ||
elif args.command == 'query_struct_field': | ||
query_struct_field(args.instance_id, args.database_id) | ||
elif args.command == 'query_nested_struct_field': | ||
query_nested_struct_field(args.instance_id, args.database_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: docstrings are inconsistent in terms of spacing/line breaks between functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, done.