Skip to content

Commit 1750328

Browse files
feat: Untyped param (#1001)
* changes * change * tests * tests * changes * change * lint * lint --------- Co-authored-by: surbhigarg92 <[email protected]>
1 parent 2bf0319 commit 1750328

File tree

7 files changed

+48
-63
lines changed

7 files changed

+48
-63
lines changed

google/cloud/spanner_v1/database.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,6 @@ def execute_partitioned_dml(
648648
if params is not None:
649649
from google.cloud.spanner_v1.transaction import Transaction
650650

651-
if param_types is None:
652-
raise ValueError("Specify 'param_types' when passing 'params'.")
653651
params_pb = Transaction._make_params_pb(params, param_types)
654652
else:
655653
params_pb = {}

google/cloud/spanner_v1/snapshot.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,6 @@ def execute_sql(
410410
raise ValueError("Transaction ID pending.")
411411

412412
if params is not None:
413-
if param_types is None:
414-
raise ValueError("Specify 'param_types' when passing 'params'.")
415413
params_pb = Struct(
416414
fields={key: _make_value_pb(value) for key, value in params.items()}
417415
)
@@ -646,8 +644,6 @@ def partition_query(
646644
raise ValueError("Transaction not started.")
647645

648646
if params is not None:
649-
if param_types is None:
650-
raise ValueError("Specify 'param_types' when passing 'params'.")
651647
params_pb = Struct(
652648
fields={key: _make_value_pb(value) for (key, value) in params.items()}
653649
)

google/cloud/spanner_v1/transaction.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,9 @@ def _make_params_pb(params, param_types):
276276
If ``params`` is None but ``param_types`` is not None.
277277
"""
278278
if params is not None:
279-
if param_types is None:
280-
raise ValueError("Specify 'param_types' when passing 'params'.")
281279
return Struct(
282280
fields={key: _make_value_pb(value) for key, value in params.items()}
283281
)
284-
else:
285-
if param_types is not None:
286-
raise ValueError("Specify 'params' when passing 'param_types'.")
287282

288283
return {}
289284

tests/system/test_session_api.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
"jsonb_array",
9191
)
9292

93+
QUERY_ALL_TYPES_COLUMNS = LIVE_ALL_TYPES_COLUMNS[1:17:2]
94+
9395
AllTypesRowData = collections.namedtuple("AllTypesRowData", LIVE_ALL_TYPES_COLUMNS)
9496
AllTypesRowData.__new__.__defaults__ = tuple([None for colum in LIVE_ALL_TYPES_COLUMNS])
9597
EmulatorAllTypesRowData = collections.namedtuple(
@@ -211,6 +213,17 @@
211213
PostGresAllTypesRowData(pkey=309, jsonb_array=[JSON_1, JSON_2, None]),
212214
)
213215

216+
QUERY_ALL_TYPES_DATA = (
217+
123,
218+
False,
219+
BYTES_1,
220+
SOME_DATE,
221+
1.4142136,
222+
"VALUE",
223+
SOME_TIME,
224+
NUMERIC_1,
225+
)
226+
214227
if _helpers.USE_EMULATOR:
215228
ALL_TYPES_COLUMNS = EMULATOR_ALL_TYPES_COLUMNS
216229
ALL_TYPES_ROWDATA = EMULATOR_ALL_TYPES_ROWDATA
@@ -475,6 +488,39 @@ def test_batch_insert_or_update_then_query(sessions_database):
475488
sd._check_rows_data(rows)
476489

477490

491+
def test_batch_insert_then_read_wo_param_types(
492+
sessions_database, database_dialect, not_emulator
493+
):
494+
sd = _sample_data
495+
496+
with sessions_database.batch() as batch:
497+
batch.delete(ALL_TYPES_TABLE, sd.ALL)
498+
batch.insert(ALL_TYPES_TABLE, ALL_TYPES_COLUMNS, ALL_TYPES_ROWDATA)
499+
500+
with sessions_database.snapshot(multi_use=True) as snapshot:
501+
for column_type, value in list(
502+
zip(QUERY_ALL_TYPES_COLUMNS, QUERY_ALL_TYPES_DATA)
503+
):
504+
placeholder = (
505+
"$1" if database_dialect == DatabaseDialect.POSTGRESQL else "@value"
506+
)
507+
sql = (
508+
"SELECT * FROM "
509+
+ ALL_TYPES_TABLE
510+
+ " WHERE "
511+
+ column_type
512+
+ " = "
513+
+ placeholder
514+
)
515+
param = (
516+
{"p1": value}
517+
if database_dialect == DatabaseDialect.POSTGRESQL
518+
else {"value": value}
519+
)
520+
rows = list(snapshot.execute_sql(sql, params=param))
521+
assert len(rows) == 1
522+
523+
478524
def test_batch_insert_w_commit_timestamp(sessions_database, not_postgres):
479525
table = "users_history"
480526
columns = ["id", "commit_ts", "name", "email", "deleted"]
@@ -1930,8 +1976,8 @@ def _check_sql_results(
19301976
database,
19311977
sql,
19321978
params,
1933-
param_types,
1934-
expected,
1979+
param_types=None,
1980+
expected=None,
19351981
order=True,
19361982
recurse_into_lists=True,
19371983
):

tests/unit/test_database.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,10 +1136,6 @@ def _execute_partitioned_dml_helper(
11361136
def test_execute_partitioned_dml_wo_params(self):
11371137
self._execute_partitioned_dml_helper(dml=DML_WO_PARAM)
11381138

1139-
def test_execute_partitioned_dml_w_params_wo_param_types(self):
1140-
with self.assertRaises(ValueError):
1141-
self._execute_partitioned_dml_helper(dml=DML_W_PARAM, params=PARAMS)
1142-
11431139
def test_execute_partitioned_dml_w_params_and_param_types(self):
11441140
self._execute_partitioned_dml_helper(
11451141
dml=DML_W_PARAM, params=PARAMS, param_types=PARAM_TYPES

tests/unit/test_snapshot.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -868,16 +868,6 @@ def test_execute_sql_other_error(self):
868868
attributes=dict(BASE_ATTRIBUTES, **{"db.statement": SQL_QUERY}),
869869
)
870870

871-
def test_execute_sql_w_params_wo_param_types(self):
872-
database = _Database()
873-
session = _Session(database)
874-
derived = self._makeDerived(session)
875-
876-
with self.assertRaises(ValueError):
877-
derived.execute_sql(SQL_QUERY_WITH_PARAM, PARAMS)
878-
879-
self.assertNoSpans()
880-
881871
def _execute_sql_helper(
882872
self,
883873
multi_use,
@@ -1397,18 +1387,6 @@ def test_partition_query_other_error(self):
13971387
attributes=dict(BASE_ATTRIBUTES, **{"db.statement": SQL_QUERY}),
13981388
)
13991389

1400-
def test_partition_query_w_params_wo_param_types(self):
1401-
database = _Database()
1402-
session = _Session(database)
1403-
derived = self._makeDerived(session)
1404-
derived._multi_use = True
1405-
derived._transaction_id = TXN_ID
1406-
1407-
with self.assertRaises(ValueError):
1408-
list(derived.partition_query(SQL_QUERY_WITH_PARAM, PARAMS))
1409-
1410-
self.assertNoSpans()
1411-
14121390
def test_partition_query_single_use_raises(self):
14131391
with self.assertRaises(ValueError):
14141392
self._partition_query_helper(multi_use=False, w_txn=True)

tests/unit/test_transaction.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -471,20 +471,6 @@ def test_commit_w_incorrect_tag_dictionary_error(self):
471471
with self.assertRaises(ValueError):
472472
self._commit_helper(request_options=request_options)
473473

474-
def test__make_params_pb_w_params_wo_param_types(self):
475-
session = _Session()
476-
transaction = self._make_one(session)
477-
478-
with self.assertRaises(ValueError):
479-
transaction._make_params_pb(PARAMS, None)
480-
481-
def test__make_params_pb_wo_params_w_param_types(self):
482-
session = _Session()
483-
transaction = self._make_one(session)
484-
485-
with self.assertRaises(ValueError):
486-
transaction._make_params_pb(None, PARAM_TYPES)
487-
488474
def test__make_params_pb_w_params_w_param_types(self):
489475
from google.protobuf.struct_pb2 import Struct
490476
from google.cloud.spanner_v1._helpers import _make_value_pb
@@ -510,16 +496,6 @@ def test_execute_update_other_error(self):
510496
with self.assertRaises(RuntimeError):
511497
transaction.execute_update(DML_QUERY)
512498

513-
def test_execute_update_w_params_wo_param_types(self):
514-
database = _Database()
515-
database.spanner_api = self._make_spanner_api()
516-
session = _Session(database)
517-
transaction = self._make_one(session)
518-
transaction._transaction_id = self.TRANSACTION_ID
519-
520-
with self.assertRaises(ValueError):
521-
transaction.execute_update(DML_QUERY_WITH_PARAM, PARAMS)
522-
523499
def _execute_update_helper(
524500
self,
525501
count=0,

0 commit comments

Comments
 (0)