From b39f19dccfe4e8e3f99cd53bd6304135b842562c Mon Sep 17 00:00:00 2001 From: Andrei Naumov Date: Fri, 30 Aug 2024 12:00:00 +0300 Subject: [PATCH 1/3] Add recomended cached query usage example --- examples/basic_example_v1/basic_example.py | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/examples/basic_example_v1/basic_example.py b/examples/basic_example_v1/basic_example.py index e439a8c6..d09c644a 100644 --- a/examples/basic_example_v1/basic_example.py +++ b/examples/basic_example_v1/basic_example.py @@ -134,6 +134,54 @@ def callee(session): def select_prepared(pool, path, series_id, season_id, episode_id): + def callee(session): + query = """ + PRAGMA TablePathPrefix("{}"); + + DECLARE $seriesId AS Uint64; + DECLARE $seasonId AS Uint64; + DECLARE $episodeId AS Uint64; + + $format = DateTime::Format("%Y-%m-%d"); + SELECT + title, + $format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(air_date AS Int16))) AS Uint32))) AS air_date + FROM episodes + WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId; + """.format( + path + ) + + data_query = ydb.types.DataQuery( + query, + parameters_types={ + '$seriesId': ydb.types.PrimitiveType.Uint64, + '$seasonId': ydb.types.PrimitiveType.Uint64, + '$episodeId': ydb.types.PrimitiveType.Uint64, + } + ) + + result_sets = session.transaction(ydb.SerializableReadWrite()).execute( + data_query, + { + "$seriesId": series_id, + "$seasonId": season_id, + "$episodeId": episode_id, + }, + commit_tx=True, + settings=ydb.table.ExecDataQuerySettings().with_keep_in_cache(True), + ) + print("\n> select_prepared_transaction:") + for row in result_sets[0].rows: + print("episode title:", row.title, ", air date:", row.air_date) + + return result_sets[0] + + return pool.retry_operation_sync(callee) + + +# Prepared query with session-based cache (obsolete) +def select_prepared_session_based(pool, path, series_id, season_id, episode_id): def callee(session): query = """ PRAGMA TablePathPrefix("{}"); From 9f5650a52239351a3e5f1c30d51c5a968241341c Mon Sep 17 00:00:00 2001 From: Andrei Naumov Date: Fri, 30 Aug 2024 15:11:05 +0300 Subject: [PATCH 2/3] Fix style --- examples/basic_example_v1/basic_example.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/basic_example_v1/basic_example.py b/examples/basic_example_v1/basic_example.py index d09c644a..db4b86f7 100644 --- a/examples/basic_example_v1/basic_example.py +++ b/examples/basic_example_v1/basic_example.py @@ -155,10 +155,10 @@ def callee(session): data_query = ydb.types.DataQuery( query, parameters_types={ - '$seriesId': ydb.types.PrimitiveType.Uint64, - '$seasonId': ydb.types.PrimitiveType.Uint64, - '$episodeId': ydb.types.PrimitiveType.Uint64, - } + "$seriesId": ydb.types.PrimitiveType.Uint64, + "$seasonId": ydb.types.PrimitiveType.Uint64, + "$episodeId": ydb.types.PrimitiveType.Uint64, + }, ) result_sets = session.transaction(ydb.SerializableReadWrite()).execute( From 8ae452dbf1f4d3b2619eea9220abf15b62d23c8b Mon Sep 17 00:00:00 2001 From: Andrei Naumov Date: Tue, 3 Sep 2024 11:21:13 +0300 Subject: [PATCH 3/3] Improve example --- examples/basic_example_v1/basic_example.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/basic_example_v1/basic_example.py b/examples/basic_example_v1/basic_example.py index db4b86f7..54b9bb16 100644 --- a/examples/basic_example_v1/basic_example.py +++ b/examples/basic_example_v1/basic_example.py @@ -133,7 +133,7 @@ def callee(session): return pool.retry_operation_sync(callee) -def select_prepared(pool, path, series_id, season_id, episode_id): +def select_parametrized(pool, path, series_id, season_id, episode_id): def callee(session): query = """ PRAGMA TablePathPrefix("{}"); @@ -171,7 +171,7 @@ def callee(session): commit_tx=True, settings=ydb.table.ExecDataQuerySettings().with_keep_in_cache(True), ) - print("\n> select_prepared_transaction:") + print("\n> select_parametrized_transaction:") for row in result_sets[0].rows: print("episode title:", row.title, ", air date:", row.air_date) @@ -180,8 +180,8 @@ def callee(session): return pool.retry_operation_sync(callee) -# Prepared query with session-based cache (obsolete) -def select_prepared_session_based(pool, path, series_id, season_id, episode_id): +# Prepared query with session-based cache +def select_prepared(pool, path, series_id, season_id, episode_id): def callee(session): query = """ PRAGMA TablePathPrefix("{}"); @@ -386,5 +386,8 @@ def run(endpoint, database, path): select_prepared(pool, full_path, 2, 3, 7) select_prepared(pool, full_path, 2, 3, 8) + select_parametrized(pool, full_path, 2, 3, 9) + select_parametrized(pool, full_path, 2, 3, 10) + explicit_tcl(pool, full_path, 2, 6, 1) - select_prepared(pool, full_path, 2, 6, 1) + select_parametrized(pool, full_path, 2, 6, 1)