Skip to content

Update code after docs review #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 41 additions & 55 deletions examples/basic_example_v2/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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`)
)
"""
)


Expand Down
96 changes: 41 additions & 55 deletions examples/basic_example_v2/basic_example_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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`)
)
"""
)


Expand Down
7 changes: 7 additions & 0 deletions tests/query/test_query_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
3 changes: 2 additions & 1 deletion ydb/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand Down
Loading