Skip to content

Commit 31ee2bd

Browse files
authored
feat(spanner): add samples for NUMERIC type (GoogleCloudPlatform#4643)
* feat(spanner) add samples for NUMERIC support * fix: fix typo * test: reorder tests to ensure required samples run first * fix: fix another typo * test: remove unneeded asserts Co-authored-by: larkee <[email protected]>
1 parent a133b58 commit 31ee2bd

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

spanner/cloud-client/snippets.py

+80
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import argparse
2424
import base64
2525
import datetime
26+
import decimal
2627

2728
from google.cloud import spanner
2829
from google.cloud.spanner_v1 import param_types
@@ -658,6 +659,57 @@ def query_data_with_timestamp(instance_id, database_id):
658659
# [END spanner_query_data_with_timestamp_column]
659660

660661

662+
# [START spanner_add_numeric_column]
663+
def add_numeric_column(instance_id, database_id):
664+
""" Adds a new NUMERIC column to the Venues table in the example database.
665+
"""
666+
spanner_client = spanner.Client()
667+
instance = spanner_client.instance(instance_id)
668+
669+
database = instance.database(database_id)
670+
671+
operation = database.update_ddl([
672+
'ALTER TABLE Venues ADD COLUMN Revenue NUMERIC'])
673+
674+
print('Waiting for operation to complete...')
675+
operation.result(120)
676+
677+
print('Altered table "Venue" on database {} on instance {}.'.format(
678+
database_id, instance_id))
679+
# [END spanner_add_numeric_column]
680+
681+
682+
# [START spanner_update_data_with_numeric_column]
683+
def update_data_with_numeric(instance_id, database_id):
684+
"""Updates Venues tables in the database with the NUMERIC
685+
column.
686+
687+
This updates the `Revenue` column which must be created before
688+
running this sample. You can add the column by running the
689+
`add_numeric_column` sample or by running this DDL statement
690+
against your database:
691+
692+
ALTER TABLE Venues ADD COLUMN Revenue NUMERIC
693+
"""
694+
spanner_client = spanner.Client()
695+
instance = spanner_client.instance(instance_id)
696+
697+
database = instance.database(database_id)
698+
699+
with database.batch() as batch:
700+
batch.update(
701+
table='Venues',
702+
columns=('VenueId', 'Revenue'),
703+
values=[
704+
(4, decimal.Decimal("35000")),
705+
(19, decimal.Decimal("104500")),
706+
(42, decimal.Decimal("99999999999999999999999999999.99"))
707+
])
708+
709+
print('Updated data.')
710+
# [END spanner_update_data_with_numeric_column]
711+
712+
661713
# [START spanner_write_data_for_struct_queries]
662714
def write_struct_data(instance_id, database_id):
663715
"""Inserts sample data that can be used to test STRUCT parameters
@@ -1397,6 +1449,34 @@ def query_data_with_string(instance_id, database_id):
13971449
# [END spanner_query_with_string_parameter]
13981450

13991451

1452+
def query_data_with_numeric_parameter(instance_id, database_id):
1453+
"""Queries sample data using SQL with a NUMERIC parameter. """
1454+
# [START spanner_query_with_numeric_parameter]
1455+
# instance_id = "your-spanner-instance"
1456+
# database_id = "your-spanner-db-id"
1457+
spanner_client = spanner.Client()
1458+
instance = spanner_client.instance(instance_id)
1459+
database = instance.database(database_id)
1460+
1461+
example_numeric = decimal.Decimal("100000")
1462+
param = {
1463+
'revenue': example_numeric,
1464+
}
1465+
param_type = {
1466+
'revenue': param_types.NUMERIC
1467+
}
1468+
1469+
with database.snapshot() as snapshot:
1470+
results = snapshot.execute_sql(
1471+
'SELECT VenueId, Revenue FROM Venues '
1472+
'WHERE Revenue < @revenue',
1473+
params=param, param_types=param_type)
1474+
1475+
for row in results:
1476+
print(u"VenueId: {}, Revenue: {}".format(*row))
1477+
# [END spanner_query_with_numeric_parameter]
1478+
1479+
14001480
def query_data_with_timestamp_parameter(instance_id, database_id):
14011481
"""Queries sample data using SQL with a TIMESTAMP parameter. """
14021482
# [START spanner_query_with_timestamp_parameter]

spanner/cloud-client/snippets_test.py

+18
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,24 @@ def test_query_data_with_string(capsys):
362362
assert 'VenueId: 42, VenueName: Venue 42' in out
363363

364364

365+
def test_add_numeric_column(capsys):
366+
snippets.add_numeric_column(INSTANCE_ID, DATABASE_ID)
367+
out, _ = capsys.readouterr()
368+
assert 'Altered table "Venue" on database ' in out
369+
370+
371+
def test_update_data_with_numeric(capsys):
372+
snippets.update_data_with_numeric(INSTANCE_ID, DATABASE_ID)
373+
out, _ = capsys.readouterr()
374+
assert 'Updated data' in out
375+
376+
377+
def test_query_data_with_numeric_parameter(capsys):
378+
snippets.query_data_with_numeric_parameter(INSTANCE_ID, DATABASE_ID)
379+
out, _ = capsys.readouterr()
380+
assert 'VenueId: 4, Revenue: 35000' in out
381+
382+
365383
def test_query_data_with_timestamp_parameter(capsys):
366384
snippets.query_data_with_timestamp_parameter(INSTANCE_ID, DATABASE_ID)
367385
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)