Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9e0a0c

Browse filesBrowse files
larkeeskuruppu
andauthored
spanner: add query options versioning samples (GoogleCloudPlatform#3093)
* spanner: add query versioning samples * update test assertions * Fixed the region tags. * Removed extra whitespace. * update required spanner version Co-authored-by: larkee <larkee@users.noreply.github.com> Co-authored-by: skuruppu <skuruppu@google.com>
1 parent f4ff21a commit a9e0a0c
Copy full SHA for a9e0a0c

File tree

Expand file treeCollapse file tree

3 files changed

+70
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+70
-1
lines changed

‎spanner/cloud-client/requirements.txt

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-spanner==1.13.0
1+
google-cloud-spanner==1.15.0
22
futures==3.3.0; python_version < "3"

‎spanner/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets.py
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,49 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
13921392
# [END spanner_query_with_timestamp_parameter]
13931393

13941394

1395+
def query_data_with_query_options(instance_id, database_id):
1396+
"""Queries sample data using SQL with query options."""
1397+
# [START spanner_query_with_query_options]
1398+
# instance_id = "your-spanner-instance"
1399+
# database_id = "your-spanner-db-id"
1400+
spanner_client = spanner.Client()
1401+
instance = spanner_client.instance(instance_id)
1402+
database = instance.database(database_id)
1403+
1404+
with database.snapshot() as snapshot:
1405+
results = snapshot.execute_sql(
1406+
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues',
1407+
query_options={'optimizer_version': '1'}
1408+
)
1409+
1410+
for row in results:
1411+
print(u"VenueId: {}, VenueName: {}, LastUpdateTime: {}".format(
1412+
*row))
1413+
# [END spanner_query_with_query_options]
1414+
1415+
1416+
def create_client_with_query_options(instance_id, database_id):
1417+
"""Create a client with query options."""
1418+
# [START spanner_create_client_with_query_options]
1419+
# instance_id = "your-spanner-instance"
1420+
# database_id = "your-spanner-db-id"
1421+
spanner_client = spanner.Client(
1422+
query_options={'optimizer_version': '1'}
1423+
)
1424+
instance = spanner_client.instance(instance_id)
1425+
database = instance.database(database_id)
1426+
1427+
with database.snapshot() as snapshot:
1428+
results = snapshot.execute_sql(
1429+
'SELECT VenueId, VenueName, LastUpdateTime FROM Venues'
1430+
)
1431+
1432+
for row in results:
1433+
print(u"VenueId: {}, VenueName: {}, LastUpdateTime: {}".format(
1434+
*row))
1435+
# [END spanner_create_client_with_query_options]
1436+
1437+
13951438
if __name__ == '__main__': # noqa: C901
13961439
parser = argparse.ArgumentParser(
13971440
description=__doc__,
@@ -1506,6 +1549,12 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
15061549
subparsers.add_parser(
15071550
'query_data_with_timestamp_parameter',
15081551
help=query_data_with_timestamp_parameter.__doc__)
1552+
subparsers.add_parser(
1553+
'query_data_with_query_options',
1554+
help=query_data_with_query_options.__doc__)
1555+
subparsers.add_parser(
1556+
'create_client_with_query_options',
1557+
help=create_client_with_query_options.__doc__)
15091558

15101559
args = parser.parse_args()
15111560

@@ -1607,3 +1656,7 @@ def query_data_with_timestamp_parameter(instance_id, database_id):
16071656
query_data_with_string(args.instance_id, args.database_id)
16081657
elif args.command == 'query_data_with_timestamp_parameter':
16091658
query_data_with_timestamp_parameter(args.instance_id, args.database_id)
1659+
elif args.command == 'query_data_with_query_options':
1660+
query_data_with_query_options(args.instance_id, args.database_id)
1661+
elif args.command == 'create_client_with_query_options':
1662+
create_client_with_query_options(args.instance_id, args.database_id)

‎spanner/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets_test.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,19 @@ def test_query_data_with_timestamp_parameter(capsys):
358358
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
359359
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
360360
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out
361+
362+
363+
def test_query_data_with_query_options(capsys):
364+
snippets.query_data_with_query_options(INSTANCE_ID, DATABASE_ID)
365+
out, _ = capsys.readouterr()
366+
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
367+
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
368+
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out
369+
370+
371+
def test_create_client_with_query_options(capsys):
372+
snippets.create_client_with_query_options(INSTANCE_ID, DATABASE_ID)
373+
out, _ = capsys.readouterr()
374+
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
375+
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
376+
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.