Skip to content

Commit 53e86eb

Browse files
authored
Merge pull request #204 flag for error on truncated response
2 parents 9292a1a + 34ed006 commit 53e86eb

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed

tests/aio/test_tx.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,48 @@ async def check_transaction(s: ydb.aio.table.Session):
153153
assert rs[0].rows[0].cnt == 2
154154

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

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ def create_table(s):
138138

139139
pool.retry_operation_sync(create_table)
140140
return table_name
141+
142+
143+
@pytest.fixture()
144+
def table_path(database, table_name) -> str:
145+
return database + "/" + table_name

tests/table/test_tx.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,49 @@ def check_transaction(s: ydb.table.Session):
145145
assert rs[0].rows[0].cnt == 2
146146

147147
pool.retry_operation_sync(check_transaction)
148+
149+
150+
def test_truncated_response(driver_sync, table_name, table_path):
151+
column_types = ydb.BulkUpsertColumns().add_column("id", ydb.PrimitiveType.Int64)
152+
153+
rows = []
154+
155+
rows_count = 1100
156+
for i in range(rows_count):
157+
rows.append({"id": i})
158+
159+
driver_sync.table_client.bulk_upsert(table_path, rows, column_types)
160+
161+
table_client = (
162+
driver_sync.table_client
163+
) # default table client with driver's settings
164+
s = table_client.session()
165+
s.create()
166+
t = s.transaction()
167+
168+
result = t.execute("SELECT * FROM %s" % table_name)
169+
assert result[0].truncated
170+
assert len(result[0].rows) == 1000
171+
172+
173+
@pytest.mark.asyncio
174+
async def test_truncated_response_deny(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(False)
187+
)
188+
s = table_client.session()
189+
s.create()
190+
t = s.transaction()
191+
192+
with pytest.raises(ydb.TruncatedResponseError):
193+
t.execute("SELECT * FROM %s" % table_name)

ydb/convert.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,5 +496,13 @@ def __init__(self, result_sets_pb, table_client_settings=None):
496496
_ResultSet.from_message if not make_lazy else _ResultSet.lazy_from_message
497497
)
498498
for result_set in result_sets_pb:
499-
result_sets.append(initializer(result_set, table_client_settings))
499+
result_set = initializer(result_set, table_client_settings)
500+
if (
501+
result_set.truncated
502+
and not table_client_settings._allow_truncated_result
503+
):
504+
raise issues.TruncatedResponseError(
505+
"Response for the request was truncated by server"
506+
)
507+
result_sets.append(result_set)
500508
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
@@ -1004,6 +1004,7 @@ def __init__(self):
10041004
self._native_json_in_result_sets = False
10051005
self._native_interval_in_result_sets = False
10061006
self._native_timestamp_in_result_sets = False
1007+
self._allow_truncated_result = True
10071008

10081009
def with_native_timestamp_in_result_sets(self, enabled):
10091010
# type:(bool) -> ydb.TableClientSettings
@@ -1040,6 +1041,11 @@ def with_lazy_result_sets(self, enabled):
10401041
self._make_result_sets_lazy = enabled
10411042
return self
10421043

1044+
def with_allow_truncated_result(self, enabled):
1045+
# type:(bool) -> ydb.TableClientSettings
1046+
self._allow_truncated_result = enabled
1047+
return self
1048+
10431049

10441050
class ScanQueryResult(object):
10451051
def __init__(self, result, table_client_settings):

0 commit comments

Comments
 (0)