diff --git a/examples/basic_example_v2/basic_example.py b/examples/basic_example_v2/basic_example.py index 3deed483..52d580bd 100644 --- a/examples/basic_example_v2/basic_example.py +++ b/examples/basic_example_v2/basic_example.py @@ -87,17 +87,15 @@ def fill_tables_with_data(pool: ydb.QuerySessionPool, path: str): def select_simple(pool: ydb.QuerySessionPool, path: str): print("\nCheck series table...") result_sets = pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); + f""" + PRAGMA TablePathPrefix("{path}"); SELECT series_id, title, release_date FROM series WHERE series_id = 1; - """.format( - path - ), + """, ) first_set = result_sets[0] for row in first_set.rows: @@ -117,27 +115,23 @@ def upsert_simple(pool: ydb.QuerySessionPool, path: str): print("\nPerforming UPSERT into episodes...") pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); + f""" + PRAGMA TablePathPrefix("{path}"); UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD"); - """.format( - path - ) + """ ) def select_with_parameters(pool: ydb.QuerySessionPool, path: str, series_id, season_id, episode_id): result_sets = pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); + f""" + PRAGMA TablePathPrefix("{path}"); SELECT title, air_date FROM episodes WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; - """.format( - path - ), + """, { "$seriesId": series_id, # could be defined implicit "$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple @@ -159,14 +153,12 @@ def select_with_parameters(pool: ydb.QuerySessionPool, path: str, series_id, sea # execution of queries. def explicit_transaction_control(pool: ydb.QuerySessionPool, path: str, series_id, season_id, episode_id): def callee(session: ydb.QuerySessionSync): - query = """ - PRAGMA TablePathPrefix("{}"); + query = f""" + PRAGMA TablePathPrefix("{path}"); UPDATE episodes SET air_date = CurrentUtcDate() WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; - """.format( - path - ) + """ # Get newly created transaction id tx = session.transaction(ydb.QuerySerializableReadWrite()).begin() @@ -199,52 +191,46 @@ def drop_tables(pool: ydb.QuerySessionPool, path: str): def create_tables(pool: ydb.QuerySessionPool, path: str): print("\nCreating table series...") pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); - CREATE table `series` ( - `series_id` Int64, - `title` Utf8, - `series_info` Utf8, - `release_date` Date, - PRIMARY KEY (`series_id`) - ) - """.format( - path + f""" + PRAGMA TablePathPrefix("{path}"); + CREATE table `series` ( + `series_id` Int64, + `title` Utf8, + `series_info` Utf8, + `release_date` Date, + PRIMARY KEY (`series_id`) ) + """ ) print("\nCreating table seasons...") pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); - CREATE table `seasons` ( - `series_id` Int64, - `season_id` Int64, - `title` Utf8, - `first_aired` Date, - `last_aired` Date, - PRIMARY KEY (`series_id`, `season_id`) - ) - """.format( - path + f""" + PRAGMA TablePathPrefix("{path}"); + CREATE table `seasons` ( + `series_id` Int64, + `season_id` Int64, + `title` Utf8, + `first_aired` Date, + `last_aired` Date, + PRIMARY KEY (`series_id`, `season_id`) ) + """ ) print("\nCreating table episodes...") pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); - CREATE table `episodes` ( - `series_id` Int64, - `season_id` Int64, - `episode_id` Int64, - `title` Utf8, - `air_date` Date, - PRIMARY KEY (`series_id`, `season_id`, `episode_id`) - ) - """.format( - path + f""" + PRAGMA TablePathPrefix("{path}"); + CREATE table `episodes` ( + `series_id` Int64, + `season_id` Int64, + `episode_id` Int64, + `title` Utf8, + `air_date` Date, + PRIMARY KEY (`series_id`, `season_id`, `episode_id`) ) + """ ) diff --git a/examples/basic_example_v2/basic_example_async.py b/examples/basic_example_v2/basic_example_async.py index 0966b592..2bb5cab6 100644 --- a/examples/basic_example_v2/basic_example_async.py +++ b/examples/basic_example_v2/basic_example_async.py @@ -87,17 +87,15 @@ async def fill_tables_with_data(pool: ydb.aio.QuerySessionPoolAsync, path: str): async def select_simple(pool: ydb.aio.QuerySessionPoolAsync, path: str): print("\nCheck series table...") result_sets = await pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); + f""" + PRAGMA TablePathPrefix("{path}"); SELECT series_id, title, release_date FROM series WHERE series_id = 1; - """.format( - path - ), + """, ) first_set = result_sets[0] for row in first_set.rows: @@ -117,27 +115,23 @@ async def upsert_simple(pool: ydb.aio.QuerySessionPoolAsync, path: str): print("\nPerforming UPSERT into episodes...") await pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); + f""" + PRAGMA TablePathPrefix("{path}"); UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD"); - """.format( - path - ) + """ ) async def select_with_parameters(pool: ydb.aio.QuerySessionPoolAsync, path: str, series_id, season_id, episode_id): result_sets = await pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); + f""" + PRAGMA TablePathPrefix("{path}"); SELECT title, air_date FROM episodes WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; - """.format( - path - ), + """, { "$seriesId": series_id, # could be defined implicit "$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple @@ -161,14 +155,12 @@ async def explicit_transaction_control( pool: ydb.aio.QuerySessionPoolAsync, path: str, series_id, season_id, episode_id ): async def callee(session: ydb.aio.QuerySessionAsync): - query = """ - PRAGMA TablePathPrefix("{}"); + query = f""" + PRAGMA TablePathPrefix("{path}"); UPDATE episodes SET air_date = CurrentUtcDate() WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; - """.format( - path - ) + """ # Get newly created transaction id tx = await session.transaction(ydb.QuerySerializableReadWrite()).begin() @@ -201,52 +193,46 @@ async def drop_tables(pool: ydb.aio.QuerySessionPoolAsync, path: str): async def create_tables(pool: ydb.aio.QuerySessionPoolAsync, path: str): print("\nCreating table series...") await pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); - CREATE table `series` ( - `series_id` Int64, - `title` Utf8, - `series_info` Utf8, - `release_date` Date, - PRIMARY KEY (`series_id`) - ) - """.format( - path + f""" + PRAGMA TablePathPrefix("{path}"); + CREATE table `series` ( + `series_id` Int64, + `title` Utf8, + `series_info` Utf8, + `release_date` Date, + PRIMARY KEY (`series_id`) ) + """ ) print("\nCreating table seasons...") await pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); - CREATE table `seasons` ( - `series_id` Int64, - `season_id` Int64, - `title` Utf8, - `first_aired` Date, - `last_aired` Date, - PRIMARY KEY (`series_id`, `season_id`) - ) - """.format( - path + f""" + PRAGMA TablePathPrefix("{path}"); + CREATE table `seasons` ( + `series_id` Int64, + `season_id` Int64, + `title` Utf8, + `first_aired` Date, + `last_aired` Date, + PRIMARY KEY (`series_id`, `season_id`) ) + """ ) print("\nCreating table episodes...") await pool.execute_with_retries( - """ - PRAGMA TablePathPrefix("{}"); - CREATE table `episodes` ( - `series_id` Int64, - `season_id` Int64, - `episode_id` Int64, - `title` Utf8, - `air_date` Date, - PRIMARY KEY (`series_id`, `season_id`, `episode_id`) - ) - """.format( - path + f""" + PRAGMA TablePathPrefix("{path}"); + CREATE table `episodes` ( + `series_id` Int64, + `season_id` Int64, + `episode_id` Int64, + `title` Utf8, + `air_date` Date, + PRIMARY KEY (`series_id`, `season_id`, `episode_id`) ) + """ ) diff --git a/tests/query/test_query_parameters.py b/tests/query/test_query_parameters.py index ff033311..4fccca0f 100644 --- a/tests/query/test_query_parameters.py +++ b/tests/query/test_query_parameters.py @@ -33,6 +33,13 @@ def test_select_implicit_str(pool: ydb.QuerySessionPool): assert expected_value == actual_value +def test_select_implicit_bytes(pool: ydb.QuerySessionPool): + expected_value = b"text" + res = pool.execute_with_retries(query, parameters={"$a": expected_value}) + actual_value = res[0].rows[0]["value"] + assert expected_value == actual_value + + def test_select_implicit_list(pool: ydb.QuerySessionPool): expected_value = [1, 2, 3] res = pool.execute_with_retries(query, parameters={"$a": expected_value}) diff --git a/ydb/convert.py b/ydb/convert.py index 63a5dbe4..f48e024a 100644 --- a/ydb/convert.py +++ b/ydb/convert.py @@ -304,9 +304,10 @@ def query_parameters_to_pb(parameters): _from_python_type_map = { int: types.PrimitiveType.Int64, - float: types.PrimitiveType.Float, + float: types.PrimitiveType.Double, bool: types.PrimitiveType.Bool, str: types.PrimitiveType.Utf8, + bytes: types.PrimitiveType.String, }