-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest_query.py
355 lines (322 loc) · 11.5 KB
/
test_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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import base64
import orjson
import pytest
from tests.routers.v3.connector.postgres.conftest import base_url
manifest = {
"catalog": "wren",
"schema": "public",
"models": [
{
"name": "orders",
"tableReference": {
"schema": "public",
"table": "orders",
},
"columns": [
{"name": "o_orderkey", "type": "integer"},
{"name": "o_custkey", "type": "integer"},
{
"name": "o_orderstatus",
"type": "varchar",
},
{
"name": "o_totalprice",
"type": "text",
"isHidden": True,
},
{
"name": "o_totalprice_double",
"type": "double",
"expression": "cast(o_totalprice as double)",
},
{"name": "o_orderdate", "type": "date"},
{
"name": "order_cust_key",
"expression": "concat(o_orderkey, '_', o_custkey)",
"type": "varchar",
},
{
"name": "timestamp",
"expression": "cast('2024-01-01T23:59:59' as timestamp)",
"type": "timestamp",
},
{
"name": "timestamptz",
"expression": "cast('2024-01-01T23:59:59' as timestamp with time zone)",
"type": "timestamptz",
},
{
"name": "dst_utc_minus_5",
"expression": "cast('2024-01-15 23:00:00 America/New_York' as timestamp with time zone)",
"type": "timestamptz",
},
{
"name": "dst_utc_minus_4",
"expression": "cast('2024-07-15 23:00:00 America/New_York' as timestamp with time zone)",
"type": "timestamptz",
},
],
"primaryKey": ["o_orderkey"],
},
{
"name": "customer",
"tableReference": {
"schema": "public",
"table": "customer",
},
"columns": [
{"name": "c_custkey", "type": "integer"},
{"name": "c_name", "type": "varchar"},
{"name": "orders", "type": "orders", "relationship": "orders_customer"},
{
"name": "sum_totalprice",
"type": "double",
"isCalculated": True,
"expression": "sum(orders.o_totalprice_double)",
},
],
"primaryKey": ["c_custkey"],
},
],
"relationships": [
{
"name": "orders_customer",
"models": ["orders", "customer"],
"joinType": "many_to_one",
"condition": "orders.o_custkey = customer.c_custkey",
}
],
}
@pytest.fixture(scope="module")
def manifest_str():
return base64.b64encode(orjson.dumps(manifest)).decode("utf-8")
async def test_query(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 10
assert len(result["data"]) == 1
assert result["data"][0] == [
"2024-01-01 23:59:59.000000",
"2024-01-01 23:59:59.000000 UTC",
"2024-01-16 04:00:00.000000 UTC", # utc-5
"2024-07-16 03:00:00.000000 UTC", # utc-4
"172799.49",
"1_370",
370,
"1996-01-02 00:00:00.000000",
1,
"O",
]
assert result["dtypes"] == {
"o_orderkey": "int32",
"o_custkey": "int32",
"o_orderstatus": "object",
"o_totalprice_double": "float64",
"o_orderdate": "object",
"order_cust_key": "object",
"timestamp": "object",
"timestamptz": "object",
"dst_utc_minus_5": "object",
"dst_utc_minus_4": "object",
}
async def test_query_with_connection_url(client, manifest_str, connection_url):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": {"connectionUrl": connection_url},
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 10
assert len(result["data"]) == 1
assert result["data"][0][0] == "2024-01-01 23:59:59.000000"
assert result["dtypes"] is not None
async def test_query_with_limit(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1
response = await client.post(
url=f"{base_url}/query",
params={"limit": 1},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 10",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 1
async def test_query_with_invalid_manifest_str(client, connection_info):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": "xxx",
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 422
assert response.text == "Base64 decode error: Invalid padding"
async def test_query_without_manifest(client, connection_info):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "manifestStr"]
assert result["detail"][0]["msg"] == "Field required"
async def test_query_without_sql(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
json={"connectionInfo": connection_info, "manifestStr": manifest_str},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "sql"]
assert result["detail"][0]["msg"] == "Field required"
async def test_query_without_connection_info(client, manifest_str):
response = await client.post(
url=f"{base_url}/query",
json={
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 422
result = response.json()
assert result["detail"][0] is not None
assert result["detail"][0]["type"] == "missing"
assert result["detail"][0]["loc"] == ["body", "connectionInfo"]
assert result["detail"][0]["msg"] == "Field required"
async def test_query_with_dry_run(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
params={"dryRun": True},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM wren.public.orders LIMIT 1",
},
)
assert response.status_code == 204
async def test_query_with_dry_run_and_invalid_sql(
client, manifest_str, connection_info
):
response = await client.post(
url=f"{base_url}/query",
params={"dryRun": True},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM X",
},
)
assert response.status_code == 422
assert response.text is not None
async def test_query_to_many_calculation(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT sum_totalprice FROM wren.public.customer limit 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 1
assert len(result["data"]) == 1
assert result["dtypes"] == {"sum_totalprice": "float64"}
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT sum_totalprice FROM wren.public.customer where c_name = 'Customer#000000001' limit 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 1
assert len(result["data"]) == 1
assert result["dtypes"] == {"sum_totalprice": "float64"}
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT c_name, sum_totalprice FROM wren.public.customer limit 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 2
assert len(result["data"]) == 1
assert result["dtypes"] == {"c_name": "object", "sum_totalprice": "float64"}
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT c_custkey, sum_totalprice FROM wren.public.customer limit 1",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["columns"]) == 2
assert len(result["data"]) == 1
assert result["dtypes"] == {"c_custkey": "int32", "sum_totalprice": "float64"}
@pytest.mark.skip(reason="Datafusion does not implement filter yet")
async def test_query_with_keyword_filter(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT count(*) FILTER(WHERE o_orderkey != NULL) FROM wren.public.orders",
},
)
assert response.status_code == 200
assert response.text is not None
async def test_limit_pushdown(client, manifest_str, connection_info):
response = await client.post(
url=f"{base_url}/query",
params={"limit": 10},
json={
"connectionInfo": connection_info,
"manifestStr": manifest_str,
"sql": "SELECT * FROM orders LIMIT 100",
},
)
assert response.status_code == 200
result = response.json()
assert len(result["data"]) == 10