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

spanner: add query options versioning samples #3093

Merged
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
2 changes: 1 addition & 1 deletion spanner/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-spanner==1.13.0
google-cloud-spanner==1.15.0
futures==3.3.0; python_version < "3"
53 changes: 53 additions & 0 deletions spanner/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,49 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
# [END spanner_query_with_timestamp_parameter]


def query_data_with_query_options(instance_id, database_id):
"""Queries sample data using SQL with query options."""
# [START spanner_query_with_query_options]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
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 VenueId, VenueName, LastUpdateTime FROM Venues',
query_options={'optimizer_version': '1'}
)

for row in results:
print(u"VenueId: {}, VenueName: {}, LastUpdateTime: {}".format(
*row))
# [END spanner_query_with_query_options]


def create_client_with_query_options(instance_id, database_id):
"""Create a client with query options."""
# [START spanner_create_client_with_query_options]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
spanner_client = spanner.Client(
query_options={'optimizer_version': '1'}
)
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

with database.snapshot() as snapshot:
results = snapshot.execute_sql(
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues'
)

for row in results:
print(u"VenueId: {}, VenueName: {}, LastUpdateTime: {}".format(
*row))
# [END spanner_create_client_with_query_options]


if __name__ == '__main__': # noqa: C901
parser = argparse.ArgumentParser(
description=__doc__,
Expand Down Expand Up @@ -1506,6 +1549,12 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
subparsers.add_parser(
'query_data_with_timestamp_parameter',
help=query_data_with_timestamp_parameter.__doc__)
subparsers.add_parser(
'query_data_with_query_options',
help=query_data_with_query_options.__doc__)
subparsers.add_parser(
'create_client_with_query_options',
help=create_client_with_query_options.__doc__)

args = parser.parse_args()

Expand Down Expand Up @@ -1607,3 +1656,7 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
query_data_with_string(args.instance_id, args.database_id)
elif args.command == 'query_data_with_timestamp_parameter':
query_data_with_timestamp_parameter(args.instance_id, args.database_id)
elif args.command == 'query_data_with_query_options':
query_data_with_query_options(args.instance_id, args.database_id)
elif args.command == 'create_client_with_query_options':
create_client_with_query_options(args.instance_id, args.database_id)
16 changes: 16 additions & 0 deletions spanner/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,19 @@ def test_query_data_with_timestamp_parameter(capsys):
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out


def test_query_data_with_query_options(capsys):
snippets.query_data_with_query_options(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out


def test_create_client_with_query_options(capsys):
snippets.create_client_with_query_options(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out