From 051bfbbaf771529e33ba46c21e1b3486ab43008f Mon Sep 17 00:00:00 2001 From: Oleg Ovcharuk Date: Wed, 21 Aug 2024 12:22:10 +0300 Subject: [PATCH 1/3] Add huge select example to basic example v2 --- examples/basic_example_v2/basic_example.py | 20 +++++++++++++++++++ .../basic_example_v2/basic_example_async.py | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/examples/basic_example_v2/basic_example.py b/examples/basic_example_v2/basic_example.py index 52d580bd..d6da8087 100644 --- a/examples/basic_example_v2/basic_example.py +++ b/examples/basic_example_v2/basic_example.py @@ -183,6 +183,25 @@ def callee(session: ydb.QuerySessionSync): return pool.retry_operation_sync(callee) +def huge_select(pool: ydb.QuerySessionPool, path: str): + def callee(session: ydb.QuerySessionSync): + query = f""" + PRAGMA TablePathPrefix("{path}"); + SELECT * from episodes; + """ + + with session.transaction().execute( + query, + commit_tx=True, + ) as result_sets: + print("\n> Huge SELECT call") + for result_set in result_sets: + for row in result_set.rows: + print("episode title:", row.title, ", air date:", row.air_date) + + return pool.retry_operation_sync(callee) + + def drop_tables(pool: ydb.QuerySessionPool, path: str): print("\nCleaning up existing tables...") pool.execute_with_retries(DropTablesQuery.format(path)) @@ -287,3 +306,4 @@ def run(endpoint, database, path): explicit_transaction_control(pool, full_path, 2, 6, 1) select_with_parameters(pool, full_path, 2, 6, 1) + huge_select(pool, full_path) diff --git a/examples/basic_example_v2/basic_example_async.py b/examples/basic_example_v2/basic_example_async.py index 2bb5cab6..be977471 100644 --- a/examples/basic_example_v2/basic_example_async.py +++ b/examples/basic_example_v2/basic_example_async.py @@ -185,6 +185,25 @@ async def callee(session: ydb.aio.QuerySessionAsync): return await pool.retry_operation_async(callee) +async def huge_select(pool: ydb.aio.QuerySessionPoolAsync, path: str): + async def callee(session: ydb.aio.QuerySessionAsync): + query = f""" + PRAGMA TablePathPrefix("{path}"); + SELECT * from episodes; + """ + + async with await session.transaction().execute( + query, + commit_tx=True, + ) as result_sets: + print("\n> Huge SELECT call") + async for result_set in result_sets: + for row in result_set.rows: + print("episode title:", row.title, ", air date:", row.air_date) + + return await pool.retry_operation_async(callee) + + async def drop_tables(pool: ydb.aio.QuerySessionPoolAsync, path: str): print("\nCleaning up existing tables...") await pool.execute_with_retries(DropTablesQuery.format(path)) @@ -289,3 +308,4 @@ async def run(endpoint, database, path): await explicit_transaction_control(pool, full_path, 2, 6, 1) await select_with_parameters(pool, full_path, 2, 6, 1) + await huge_select(pool, full_path) From f07ad801d2b844e05546a25332f3c89d0c8e7eb7 Mon Sep 17 00:00:00 2001 From: Oleg Ovcharuk Date: Wed, 21 Aug 2024 12:44:30 +0300 Subject: [PATCH 2/3] Remove PragmaTablePrefix and add Declare to example --- examples/basic_example_v2/__main__.py | 5 +- examples/basic_example_v2/basic_example.py | 124 ++++++----------- .../basic_example_v2/basic_example_async.py | 125 ++++++------------ 3 files changed, 81 insertions(+), 173 deletions(-) diff --git a/examples/basic_example_v2/__main__.py b/examples/basic_example_v2/__main__.py index 9c68c00d..95ecf99e 100644 --- a/examples/basic_example_v2/__main__.py +++ b/examples/basic_example_v2/__main__.py @@ -11,9 +11,8 @@ formatter_class=argparse.RawDescriptionHelpFormatter, description="""\033[92mYDB basic example.\x1b[0m\n""", ) - parser.add_argument("-d", "--database", help="Name of the database to use", default="/local") parser.add_argument("-e", "--endpoint", help="Endpoint url to use", default="grpc://localhost:2136") - parser.add_argument("-p", "--path", default="") + parser.add_argument("-d", "--database", help="Name of the database to use", default="/local") parser.add_argument("-v", "--verbose", default=False, action="store_true") parser.add_argument("-m", "--mode", default="sync", help="Mode of example: sync or async") @@ -28,7 +27,6 @@ run_sync( args.endpoint, args.database, - args.path, ) elif args.mode == "async": print("Running async example") @@ -36,7 +34,6 @@ run_async( args.endpoint, args.database, - args.path, ) ) else: diff --git a/examples/basic_example_v2/basic_example.py b/examples/basic_example_v2/basic_example.py index d6da8087..ed05554b 100644 --- a/examples/basic_example_v2/basic_example.py +++ b/examples/basic_example_v2/basic_example.py @@ -1,26 +1,15 @@ # -*- coding: utf-8 -*- -import posixpath import ydb import basic_example_data -# Table path prefix allows to put the working tables into the specific directory -# inside the YDB database. Putting `PRAGMA TablePathPrefix("some/path")` -# at the beginning of the query allows to reference the tables through -# their names "under" the specified directory. -# -# TablePathPrefix has to be defined as an absolute path, which has to be started -# with the current database location. -# -# https://ydb.tech/ru/docs/yql/reference/syntax/pragma#table-path-prefix - -DropTablesQuery = """PRAGMA TablePathPrefix("{}"); + +DropTablesQuery = """ DROP TABLE IF EXISTS series; DROP TABLE IF EXISTS seasons; DROP TABLE IF EXISTS episodes; """ -FillDataQuery = """PRAGMA TablePathPrefix("{}"); - +FillDataQuery = """ DECLARE $seriesData AS List 0: - full_path = paths_to_create.pop(-1) - driver.scheme_client.make_directory(full_path) - - -def run(endpoint, database, path): +def run(endpoint, database): with ydb.Driver( endpoint=endpoint, database=database, @@ -284,26 +247,19 @@ def run(endpoint, database, path): driver.wait(timeout=5, fail_fast=True) with ydb.QuerySessionPool(driver) as pool: + drop_tables(pool) - ensure_path_exists(driver, database, path) - - # absolute path - prefix to the table's names, - # including the database location - full_path = posixpath.join(database, path) - - drop_tables(pool, full_path) - - create_tables(pool, full_path) + create_tables(pool) - fill_tables_with_data(pool, full_path) + fill_tables_with_data(pool) - select_simple(pool, full_path) + select_simple(pool) - upsert_simple(pool, full_path) + upsert_simple(pool) - select_with_parameters(pool, full_path, 2, 3, 7) - select_with_parameters(pool, full_path, 2, 3, 8) + select_with_parameters(pool, 2, 3, 7) + select_with_parameters(pool, 2, 3, 8) - explicit_transaction_control(pool, full_path, 2, 6, 1) - select_with_parameters(pool, full_path, 2, 6, 1) - huge_select(pool, full_path) + explicit_transaction_control(pool, 2, 6, 1) + select_with_parameters(pool, 2, 6, 1) + huge_select(pool) diff --git a/examples/basic_example_v2/basic_example_async.py b/examples/basic_example_v2/basic_example_async.py index be977471..e35fbabc 100644 --- a/examples/basic_example_v2/basic_example_async.py +++ b/examples/basic_example_v2/basic_example_async.py @@ -1,26 +1,15 @@ # -*- coding: utf-8 -*- -import posixpath import ydb import basic_example_data -# Table path prefix allows to put the working tables into the specific directory -# inside the YDB database. Putting `PRAGMA TablePathPrefix("some/path")` -# at the beginning of the query allows to reference the tables through -# their names "under" the specified directory. -# -# TablePathPrefix has to be defined as an absolute path, which has to be started -# with the current database location. -# -# https://ydb.tech/ru/docs/yql/reference/syntax/pragma#table-path-prefix - -DropTablesQuery = """PRAGMA TablePathPrefix("{}"); + +DropTablesQuery = """ DROP TABLE IF EXISTS series; DROP TABLE IF EXISTS seasons; DROP TABLE IF EXISTS episodes; """ -FillDataQuery = """PRAGMA TablePathPrefix("{}"); - +FillDataQuery = """ DECLARE $seriesData AS List 0: - full_path = paths_to_create.pop(-1) - await driver.scheme_client.make_directory(full_path) - - -async def run(endpoint, database, path): +async def run(endpoint, database): async with ydb.aio.Driver( endpoint=endpoint, database=database, @@ -286,26 +248,19 @@ async def run(endpoint, database, path): await driver.wait(timeout=5, fail_fast=True) async with ydb.aio.QuerySessionPoolAsync(driver) as pool: + await drop_tables(pool) - await ensure_path_exists(driver, database, path) - - # absolute path - prefix to the table's names, - # including the database location - full_path = posixpath.join(database, path) - - await drop_tables(pool, full_path) - - await create_tables(pool, full_path) + await create_tables(pool) - await fill_tables_with_data(pool, full_path) + await fill_tables_with_data(pool) - await select_simple(pool, full_path) + await select_simple(pool) - await upsert_simple(pool, full_path) + await upsert_simple(pool) - await select_with_parameters(pool, full_path, 2, 3, 7) - await select_with_parameters(pool, full_path, 2, 3, 8) + await select_with_parameters(pool, 2, 3, 7) + await select_with_parameters(pool, 2, 3, 8) - await explicit_transaction_control(pool, full_path, 2, 6, 1) - await select_with_parameters(pool, full_path, 2, 6, 1) - await huge_select(pool, full_path) + await explicit_transaction_control(pool, 2, 6, 1) + await select_with_parameters(pool, 2, 6, 1) + await huge_select(pool) From cc67aeece8088c0fbceee1c9a926a649933e9ede Mon Sep 17 00:00:00 2001 From: Oleg Ovcharuk Date: Wed, 21 Aug 2024 12:59:49 +0300 Subject: [PATCH 3/3] review fixes --- examples/basic_example_v2/basic_example_async.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/basic_example_v2/basic_example_async.py b/examples/basic_example_v2/basic_example_async.py index e35fbabc..944ffe7c 100644 --- a/examples/basic_example_v2/basic_example_async.py +++ b/examples/basic_example_v2/basic_example_async.py @@ -138,9 +138,7 @@ async def select_with_parameters(pool: ydb.aio.QuerySessionPoolAsync, series_id, # In most cases it's better to use transaction control settings in session.transaction # calls instead to avoid additional hops to YDB cluster and allow more efficient # execution of queries. -async def explicit_transaction_control( - pool: ydb.aio.QuerySessionPoolAsync, series_id, season_id, episode_id -): +async def explicit_transaction_control(pool: ydb.aio.QuerySessionPoolAsync, series_id, season_id, episode_id): async def callee(session: ydb.aio.QuerySessionAsync): query = """ DECLARE $seriesId AS Int64;