Skip to content

Commit 68e93ac

Browse files
authored
Merge pull request #27 from ydb-platform/add-query-stats
add support for query stats in scan query
2 parents 9d85630 + 261ac2a commit 68e93ac

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.0 ##
2+
3+
* support query stats in scan query
4+
15
## 2.2.0 ##
26

37
* allow to refer endpoints by node id

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setuptools.setup(
66
name="ydb",
7-
version="2.3.1",
7+
version="2.4.0",
88
description="YDB Python library",
99
author="Yandex LLC",
1010
author_email="[email protected]",

tests/aio/test_async_iter_stream.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,52 @@ async def test_read_table(driver, database):
3232
i += 1
3333

3434

35+
@pytest.mark.asyncio
36+
async def test_scan_query_with_query_stats(driver, database):
37+
description = (
38+
ydb.TableDescription()
39+
.with_primary_keys("key1")
40+
.with_columns(
41+
ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)),
42+
ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)),
43+
)
44+
)
45+
46+
session = await driver.table_client.session().create()
47+
await session.create_table(
48+
database + "/test_scan_query_with_query_stats", description
49+
)
50+
await session.transaction(ydb.SerializableReadWrite()).execute(
51+
"""INSERT INTO `test_scan_query_with_query_stats` (`key1`, `value`) VALUES (1, "hello_world"), (2, "2")""",
52+
commit_tx=True,
53+
)
54+
55+
expected_res = [{"key1": 1, "value": "hello_world"}, {"key1": 2, "value": "2"}]
56+
i = 0
57+
async for resp in await driver.table_client.scan_query(
58+
"select * from `test_scan_query_with_query_stats` order by key1"
59+
):
60+
for row in resp.result_set.rows:
61+
assert row == expected_res[i]
62+
i += 1
63+
64+
i = 0
65+
settings = ydb.ScanQuerySettings().with_collect_stats(
66+
ydb.QueryStatsCollectionMode.FULL
67+
)
68+
last_response = None
69+
async for resp in await driver.table_client.scan_query(
70+
"select * from `test_scan_query_with_query_stats` order by key1",
71+
settings=settings,
72+
):
73+
last_response = resp
74+
for row in resp.result_set.rows:
75+
assert row == expected_res[i]
76+
i += 1
77+
78+
assert len(last_response.query_stats.query_phases) > 0
79+
80+
3581
@pytest.mark.asyncio
3682
async def test_read_shard_table(driver, database):
3783
session: ydb.ISession = await driver.table_client.session().create()

ydb/table.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,11 +1027,29 @@ def with_lazy_result_sets(self, enabled):
10271027
class ScanQueryResult(object):
10281028
def __init__(self, result, table_client_settings):
10291029
self._result = result
1030+
self.query_stats = result.query_stats
10301031
self.result_set = convert.ResultSet.from_message(
10311032
self._result.result_set, table_client_settings
10321033
)
10331034

10341035

1036+
@enum.unique
1037+
class QueryStatsCollectionMode(enum.IntEnum):
1038+
NONE = _apis.ydb_table.QueryStatsCollection.Mode.STATS_COLLECTION_NONE
1039+
BASIC = _apis.ydb_table.QueryStatsCollection.Mode.STATS_COLLECTION_BASIC
1040+
FULL = _apis.ydb_table.QueryStatsCollection.Mode.STATS_COLLECTION_FULL
1041+
1042+
1043+
class ScanQuerySettings(settings_impl.BaseRequestSettings):
1044+
def __init__(self):
1045+
super(ScanQuerySettings, self).__init__()
1046+
self.collect_stats = None
1047+
1048+
def with_collect_stats(self, collect_stats_mode):
1049+
self.collect_stats = collect_stats_mode
1050+
return self
1051+
1052+
10351053
class ScanQuery(object):
10361054
def __init__(self, yql_text, parameters_types):
10371055
self.yql_text = yql_text
@@ -1047,10 +1065,16 @@ def _scan_query_request_factory(query, parameters=None, settings=None):
10471065
if not isinstance(query, ScanQuery):
10481066
query = ScanQuery(query, {})
10491067
parameters = {} if parameters is None else parameters
1068+
collect_stats = getattr(
1069+
settings,
1070+
"collect_stats",
1071+
_apis.ydb_table.QueryStatsCollection.Mode.STATS_COLLECTION_NONE,
1072+
)
10501073
return _apis.ydb_table.ExecuteScanQueryRequest(
10511074
mode=_apis.ydb_table.ExecuteScanQueryRequest.Mode.MODE_EXEC,
10521075
query=_apis.ydb_table.Query(yql_text=query.yql_text),
10531076
parameters=convert.parameters_to_pb(query.parameters_types, parameters),
1077+
collect_stats=collect_stats,
10541078
)
10551079

10561080

ydb/ydb_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = "2.3.1"
1+
VERSION = "2.4.0"

0 commit comments

Comments
 (0)