Skip to content

Commit 95ab809

Browse files
authored
Merge pull request #203 error on truncated result by default
2 parents 3545a78 + a123228 commit 95ab809

File tree

6 files changed

+110
-1
lines changed

6 files changed

+110
-1
lines changed

tests/aio/test_tx.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,46 @@ async def check_transaction(s: ydb.aio.table.Session):
156156
assert rs[0].rows[0].cnt == 1
157157

158158
await pool.retry_operation(check_transaction)
159+
160+
161+
@pytest.mark.asyncio
162+
async def test_truncated_response(driver, table_name, table_path):
163+
column_types = ydb.BulkUpsertColumns().add_column("id", ydb.PrimitiveType.Int64)
164+
165+
rows = []
166+
167+
rows_count = 1100
168+
for i in range(rows_count):
169+
rows.append({"id": i})
170+
171+
await driver.table_client.bulk_upsert(table_path, rows, column_types)
172+
173+
table_client = driver.table_client # default table client with driver's settings
174+
s = table_client.session()
175+
await s.create()
176+
t = s.transaction()
177+
with pytest.raises(ydb.TruncatedResponseError):
178+
await t.execute("SELECT * FROM %s" % table_name)
179+
180+
181+
@pytest.mark.asyncio
182+
async def test_truncated_response_allow(driver, table_name, table_path):
183+
column_types = ydb.BulkUpsertColumns().add_column("id", ydb.PrimitiveType.Int64)
184+
185+
rows = []
186+
187+
rows_count = 1100
188+
for i in range(rows_count):
189+
rows.append({"id": i})
190+
191+
await driver.table_client.bulk_upsert(table_path, rows, column_types)
192+
193+
table_client = ydb.TableClient(
194+
driver, ydb.TableClientSettings().with_allow_truncated_result(True)
195+
)
196+
s = table_client.session()
197+
await s.create()
198+
t = s.transaction()
199+
result = await t.execute("SELECT * FROM %s" % table_name)
200+
assert result[0].truncated
201+
assert len(result[0].rows) == 1000

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def create_table(s):
132132
return table_name
133133

134134

135+
@pytest.fixture()
136+
def table_path(database, table_name) -> str:
137+
return database + "/" + table_name
138+
139+
135140
@pytest.fixture()
136141
def topic_consumer():
137142
return "fixture-consumer"

tests/table/test_tx.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,46 @@ def check_transaction(s: ydb.table.Session):
148148
assert rs[0].rows[0].cnt == 1
149149

150150
pool.retry_operation_sync(check_transaction)
151+
152+
153+
def test_truncated_response(driver_sync, table_name, table_path):
154+
column_types = ydb.BulkUpsertColumns().add_column("id", ydb.PrimitiveType.Int64)
155+
156+
rows = []
157+
158+
rows_count = 1100
159+
for i in range(rows_count):
160+
rows.append({"id": i})
161+
162+
driver_sync.table_client.bulk_upsert(table_path, rows, column_types)
163+
164+
table_client = (
165+
driver_sync.table_client
166+
) # default table client with driver's settings
167+
s = table_client.session()
168+
s.create()
169+
t = s.transaction()
170+
with pytest.raises(ydb.TruncatedResponseError):
171+
t.execute("SELECT * FROM %s" % table_name)
172+
173+
174+
def test_truncated_response_allow(driver_sync, table_name, table_path):
175+
column_types = ydb.BulkUpsertColumns().add_column("id", ydb.PrimitiveType.Int64)
176+
177+
rows = []
178+
179+
rows_count = 1100
180+
for i in range(rows_count):
181+
rows.append({"id": i})
182+
183+
driver_sync.table_client.bulk_upsert(table_path, rows, column_types)
184+
185+
table_client = ydb.TableClient(
186+
driver_sync, ydb.TableClientSettings().with_allow_truncated_result(True)
187+
)
188+
s = table_client.session()
189+
s.create()
190+
t = s.transaction()
191+
result = t.execute("SELECT * FROM %s" % table_name)
192+
assert result[0].truncated
193+
assert len(result[0].rows) == 1000

ydb/convert.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,5 +489,13 @@ def __init__(self, result_sets_pb, table_client_settings=None):
489489
_ResultSet.from_message if not make_lazy else _ResultSet.lazy_from_message
490490
)
491491
for result_set in result_sets_pb:
492-
result_sets.append(initializer(result_set, table_client_settings))
492+
result_set = initializer(result_set, table_client_settings)
493+
if (
494+
result_set.truncated
495+
and not table_client_settings._allow_truncated_result
496+
):
497+
raise issues.TruncatedResponseError(
498+
"Response for the request was truncated by server"
499+
)
500+
result_sets.append(result_set)
493501
super(ResultSets, self).__init__(result_sets)

ydb/issues.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def __init__(self, message, issues=None):
5252
self.message = message
5353

5454

55+
class TruncatedResponseError(Error):
56+
status = None
57+
58+
5559
class ConnectionError(Error):
5660
status = None
5761

ydb/table.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ def __init__(self):
10021002
self._native_json_in_result_sets = False
10031003
self._native_interval_in_result_sets = False
10041004
self._native_timestamp_in_result_sets = False
1005+
self._allow_truncated_result = False
10051006

10061007
def with_native_timestamp_in_result_sets(self, enabled):
10071008
# type:(bool) -> ydb.TableClientSettings
@@ -1038,6 +1039,11 @@ def with_lazy_result_sets(self, enabled):
10381039
self._make_result_sets_lazy = enabled
10391040
return self
10401041

1042+
def with_allow_truncated_result(self, enabled):
1043+
# type:(bool) -> ydb.TableClientSettings
1044+
self._allow_truncated_result = enabled
1045+
return self
1046+
10411047

10421048
class ScanQueryResult(object):
10431049
def __init__(self, result, table_client_settings):

0 commit comments

Comments
 (0)