Repository navigation
Expand file tree
/
Copy pathtest_streaming_query.py
More file actions
292 lines (246 loc) · 12.8 KB
/
Copy pathtest_streaming_query.py
File metadata and controls
292 lines (246 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!python3
import time
import shutil
import unittest
import threading
from chdb import session
import chdb
test_streaming_query_dir = ".tmp_test_streaming_query"
result_counter_streaming1 = 0
result_counter_streaming2 = 0
result_counter_normal1 = 0
result_counter_normal2 = 0
insert_counter = 0
stop_event = threading.Event()
class TestStreamingQuery(unittest.TestCase):
def setUp(self) -> None:
shutil.rmtree(test_streaming_query_dir, ignore_errors=True)
self.sess = session.Session(test_streaming_query_dir)
return super().setUp()
def tearDown(self) -> None:
self.sess.close()
shutil.rmtree(test_streaming_query_dir, ignore_errors=True)
return super().tearDown()
def test_streaming_query_with_session(self):
self.sess.query("CREATE DATABASE IF NOT EXISTS test")
self.sess.query("USE test")
self.sess.query("CREATE TABLE IF NOT EXISTS streaming_test (id Int64) ENGINE = MergeTree() ORDER BY id")
self.sess.query("INSERT INTO streaming_test SELECT number FROM numbers(1000000)")
total_rows = 0
with self.sess.send_query("SELECT * FROM streaming_test", "CSVWITHNAMES") as stream:
for chunk in stream:
rows = chunk.data().split('\n')
total_rows += len(rows) - 2
# print(f"Chunk received {len(rows) - 2} rows")
self.assertEqual(total_rows, 1000000)
self.sess.query("INSERT INTO streaming_test VALUES (1000001)")
result = self.sess.query("SELECT * FROM streaming_test", "CSVWITHNAMES")
self.assertEqual(result.rows_read(), 1000001)
stream = self.sess.send_query("SELECT * FROM streaming_test WHERE id > 999999", "CSVWITHNAMES")
chunks = list(stream)
self.assertEqual(len(chunks), 1)
self.assertIn("1000001", chunks[0].data())
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test;SELECT * FROM streaming_test", "CSVWITHNAMES")
with self.assertRaises(Exception):
ret = self.sess.send_query("INSERT INTO streaming_test SELECT number FROM numbers(1000000)", "CSVWITHNAMES")
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test;SELECT * FROM streaming_test", "CSVWITHNAMES")
def test_streaming_query_with_connection(self):
self.sess.close()
conn = chdb.connect(":memory:")
total_rows = 0
with conn.send_query("SELECT * FROM numbers(200000)") as stream:
for chunk in stream:
total_rows += chunk.rows_read()
self.assertEqual(total_rows, 200000)
conn.close()
self.sess = session.Session(test_streaming_query_dir)
def test_large_table(self):
# Test querying extremely large dataset (1 billion rows)
# with stable memory usage around 56MB
total_rows = 0
with self.sess.send_query("SELECT * FROM numbers(1073741824)", "CSVWITHNAMES") as stream:
for chunk in stream:
total_rows += chunk.rows_read()
self.assertEqual(total_rows, 1073741824)
def test_streaming_query_applies_date_time_output_format_iso(self):
# chdb-core #143: a query-level `SETTINGS date_time_output_format = 'iso'`
# must be applied to the CLIENT-SIDE output format of a STREAMING query,
# exactly as it already is for a materialized query. Before the fix the
# streaming path rolled the query settings back to defaults before the
# output format was created at fetch time, so a DateTime64 was emitted in
# the default form ("2025-12-19 13:52:04.496187") instead of the ISO form
# with a `T` separator and trailing `Z` ("2025-12-19T13:52:04.496187Z").
iso_query = (
"SELECT toDateTime64('2025-12-19 13:52:04.496187', 6, 'UTC') AS ts "
"SETTINGS date_time_output_format = 'iso'"
)
# CSVWITHNAMES: quoted header line, then the quoted value + trailing newline.
expected_iso = '"ts"\n"2025-12-19T13:52:04.496187Z"\n'
expected_default = '"ts"\n"2025-12-19 13:52:04.496187"\n'
# Control: the materialized (non-streaming) path already honors the SETTINGS.
materialized = self.sess.query(iso_query, "CSVWITHNAMES").data()
self.assertEqual(materialized, expected_iso)
# The bug: the streaming path must produce the SAME ISO-formatted output.
chunks = list(self.sess.send_query(iso_query, "CSVWITHNAMES"))
self.assertEqual(len(chunks), 1)
streaming = chunks[0].data()
# Specific checks first for clearer failure diagnostics, then exact match.
self.assertIn("2025-12-19T13:52:04.496187Z", streaming)
self.assertNotIn("2025-12-19 13:52:04.496187", streaming)
self.assertEqual(streaming, expected_iso)
# Parity: the streaming and materialized paths must agree exactly.
self.assertEqual(streaming, materialized)
# Guard against setting leakage: a subsequent streaming query with no
# SETTINGS clause must fall back to the default (non-ISO) form.
default_query = "SELECT toDateTime64('2025-12-19 13:52:04.496187', 6, 'UTC') AS ts"
default_chunks = list(self.sess.send_query(default_query, "CSVWITHNAMES"))
self.assertEqual(len(default_chunks), 1)
self.assertEqual(default_chunks[0].data(), expected_default)
def test_cancel_streaming_query(self):
self.sess.query("CREATE DATABASE IF NOT EXISTS test")
self.sess.query("USE test")
self.sess.query("CREATE TABLE IF NOT EXISTS cancel_streaming_test (id Int64) ENGINE = MergeTree() ORDER BY id")
self.sess.query("INSERT INTO cancel_streaming_test SELECT number FROM numbers(10000)")
stream_result = self.sess.send_query("SELECT * FROM cancel_streaming_test", "CSVWITHNAMES")
stream_result.cancel()
result = self.sess.query("SELECT * FROM cancel_streaming_test", "CSVWITHNAMES")
self.assertEqual(result.rows_read(), 10000)
stream_result = self.sess.send_query("SELECT * FROM cancel_streaming_test", "CSVWITHNAMES")
chunks = list(stream_result)
self.assertEqual(len(chunks), 1)
result = self.sess.query("SELECT * FROM cancel_streaming_test", "CSVWITHNAMES")
self.assertEqual(result.rows_read(), 10000)
stream_result = self.sess.send_query("SELECT * FROM cancel_streaming_test", "CSVWITHNAMES")
def streaming_worker1(self):
global result_counter_streaming1
# Create a separate session for this thread
worker_sess = session.Session(test_streaming_query_dir)
worker_sess.query("USE test")
try:
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = worker_sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 1:
ret = worker_sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
stream = worker_sess.send_query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
for chunk in stream:
result_counter_streaming1 += chunk.rows_read()
finally:
worker_sess.close()
def streaming_worker2(self):
global result_counter_streaming2
# Create a separate session for this thread
worker_sess = session.Session(test_streaming_query_dir)
worker_sess.query("USE test")
try:
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = worker_sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 2:
ret = worker_sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
stream = worker_sess.send_query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
for chunk in stream:
result_counter_streaming2 += chunk.rows_read()
finally:
worker_sess.close()
def normal_query_worker1(self):
global result_counter_normal1
# Create a separate session for this thread
worker_sess = session.Session(test_streaming_query_dir)
worker_sess.query("USE test")
try:
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = worker_sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 3:
ret = worker_sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
result = worker_sess.query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
result_counter_normal1 += result.rows_read()
finally:
worker_sess.close()
def normal_query_worker2(self):
global result_counter_normal2
# Create a separate session for this thread
worker_sess = session.Session(test_streaming_query_dir)
worker_sess.query("USE test")
try:
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = worker_sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 4:
ret = worker_sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
result = worker_sess.query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
result_counter_normal2 += result.rows_read()
finally:
worker_sess.close()
def normal_insert_worker(self):
global insert_counter
# Create a separate session for this thread
worker_sess = session.Session(test_streaming_query_dir)
worker_sess.query("USE test")
try:
query_count = 0
i = 500000
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = worker_sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 5:
ret = worker_sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
worker_sess.query(f"INSERT INTO streaming_test2 VALUES ({i})")
insert_counter += 1
i += 1
finally:
worker_sess.close()
def test_multi_thread_streaming_query(self):
self.sess.query("CREATE DATABASE IF NOT EXISTS test")
self.sess.query("USE test")
self.sess.query("CREATE TABLE IF NOT EXISTS streaming_test2 (id Int64) ENGINE = MergeTree() ORDER BY id")
self.sess.query("INSERT INTO streaming_test2 SELECT number FROM numbers(500000)")
threads = [
threading.Thread(target=self.streaming_worker1),
threading.Thread(target=self.streaming_worker2),
threading.Thread(target=self.normal_query_worker1),
threading.Thread(target=self.normal_query_worker2),
threading.Thread(target=self.normal_insert_worker)
]
for t in threads:
t.start()
time.sleep(10)
stop_event.set()
for t in threads:
t.join()
print(f"Total inserted rows: {insert_counter}")
print(f"Total normal selected rows1: {result_counter_normal1}")
print(f"Total normal selected rows2: {result_counter_normal2}")
print(f"Total streaming selected rows1: {result_counter_streaming1}")
print(f"Total streaming selected rows2: {result_counter_streaming2}")
self.assertGreater(result_counter_normal1, 500000)
self.assertGreater(result_counter_normal2, 500000)
self.assertGreater(result_counter_streaming1, 500000)
self.assertGreater(result_counter_streaming2, 500000)
final_count = self.sess.query("SELECT count() FROM streaming_test2", "CSV").data().strip()
self.assertEqual(int(final_count), 500000 + insert_counter)
if __name__ == "__main__":
shutil.rmtree(test_streaming_query_dir, ignore_errors=True)
unittest.main()