Skip to content

Commit a801a67

Browse files
committed
typed parameters
1 parent 792ee57 commit a801a67

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

examples/query-service/basic_example.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ def main():
1616

1717
pool = ydb.QuerySessionPool(driver)
1818

19-
print("=" * 50)
20-
print("DELETE TABLE IF EXISTS")
21-
pool.execute_with_retries("DROP TABLE IF EXISTS example")
19+
# print("=" * 50)
20+
# print("DELETE TABLE IF EXISTS")
21+
# pool.execute_with_retries("drop table if exists example")
2222

23-
print("=" * 50)
24-
print("CREATE TABLE")
25-
pool.execute_with_retries("CREATE TABLE example(key UInt64, value String, PRIMARY KEY (key))")
23+
# print("=" * 50)
24+
# print("CREATE TABLE")
25+
# pool.execute_with_retries("CREATE TABLE example(key UInt64, value String, PRIMARY KEY (key))")
2626

27-
pool.execute_with_retries("INSERT INTO example (key, value) VALUES (1, 'onepieceisreal')")
27+
# pool.execute_with_retries("INSERT INTO example (key, value) VALUES (1, 'onepieceisreal')")
2828

2929
def callee(session):
3030
print("=" * 50)
@@ -82,6 +82,26 @@ def callee(session):
8282

8383
pool.retry_operation_sync(callee)
8484

85+
query_print = """
86+
select $a
87+
"""
88+
89+
def callee(session: ydb.QuerySessionSync):
90+
print("=" * 50)
91+
print("Check typed parameters")
92+
93+
values = [1, 1.0, True, "text"]
94+
95+
for value in values:
96+
print(f"value: {value}")
97+
with session.transaction().execute(
98+
query=query_print, parameters={'$a': value}, commit_tx=True
99+
) as results:
100+
for result_set in results:
101+
print(f"rows: {str(result_set.rows)}")
102+
103+
pool.retry_operation_sync(callee)
104+
85105

86106
if __name__ == "__main__":
87107
main()

ydb/_grpc/grpcwrapper/ydb_query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
ServerStatus,
1919
)
2020

21+
from ... import convert
22+
2123

2224
@dataclass
2325
class CreateSessionResponse(IFromProto):
@@ -176,5 +178,5 @@ def to_proto(self) -> ydb_query_pb2.ExecuteQueryRequest:
176178
exec_mode=self.exec_mode,
177179
stats_mode=self.stats_mode,
178180
concurrent_result_sets=self.concurrent_result_sets,
179-
parameters=self.parameters,
181+
parameters=convert.query_parameters_to_pb(self.parameters),
180182
)

ydb/convert.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,39 @@ def parameters_to_pb(parameters_types, parameters_values):
281281
return param_values_pb
282282

283283

284+
def query_parameters_to_pb(parameters):
285+
if parameters is None or not parameters:
286+
return {}
287+
288+
parameters_types = {}
289+
parameters_values = {}
290+
for name, value in parameters.items():
291+
if isinstance(value, tuple):
292+
parameters_values[name] = value[0]
293+
parameters_types[name] = value[1]
294+
else:
295+
parameters_values[name] = value
296+
parameters_types[name] = _primitive_type_from_python_native(value)
297+
298+
return parameters_to_pb(parameters_types, parameters_values)
299+
300+
301+
_from_python_type_map = {
302+
int: types.PrimitiveType.Int64,
303+
float: types.PrimitiveType.Float,
304+
dict: types.PrimitiveType.Json,
305+
bool: types.PrimitiveType.Bool,
306+
str: types.PrimitiveType.Utf8,
307+
}
308+
309+
310+
def _primitive_type_from_python_native(value):
311+
t = type(value)
312+
if t not in _from_python_type_map:
313+
return types.PrimitiveType.Int64
314+
return _from_python_type_map[t]
315+
316+
284317
def _unwrap_optionality(column):
285318
c_type = column.type
286319
current_type = c_type.WhichOneof("type")

0 commit comments

Comments
 (0)