-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest_streaming_query.py
223 lines (182 loc) · 9.27 KB
/
test_streaming_query.py
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
#!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_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
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 1:
ret = self.sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
stream = self.sess.send_query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
for chunk in stream:
result_counter_streaming1 += chunk.data().count('\n') - 2
def streaming_worker2(self):
global result_counter_streaming2
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 2:
ret = self.sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
stream = self.sess.send_query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
for chunk in stream:
result_counter_streaming2 += chunk.data().count('\n') - 2
def normal_query_worker1(self):
global result_counter_normal1
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 3:
ret = self.sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
result = self.sess.query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
result_counter_normal1 += result.data().count('\n') - 2
def normal_query_worker2(self):
global result_counter_normal2
query_count = 0
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 4:
ret = self.sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
result = self.sess.query("SELECT * FROM streaming_test2", "CSVWITHNAMES")
result_counter_normal2 += result.data().count('\n') - 2
def normal_insert_worker(self):
global insert_counter
query_count = 0
i = 500000
while not stop_event.is_set():
query_count += 1
if query_count % 10 == 0:
with self.assertRaises(Exception):
ret = self.sess.send_query("SELECT * FROM streaming_test2;SELECT * FROM streaming_test2", "CSVWITHNAMES")
if query_count % 10 == 5:
ret = self.sess.send_query("SELECT * FROM streaming_test2;", "CSVWITHNAMES")
ret.cancel()
self.sess.query(f"INSERT INTO streaming_test2 VALUES ({i})")
insert_counter += 1
i += 1
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()