-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_sql_parser_general.py
More file actions
483 lines (391 loc) · 19.8 KB
/
test_sql_parser_general.py
File metadata and controls
483 lines (391 loc) · 19.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# -*- coding: utf-8 -*-
import pytest
from pymongosql.error import SqlSyntaxError
from pymongosql.sql.parser import SQLParser
class TestSQLParserGeneral:
"""Comprehensive test suite for SQL parser from simple to complex queries"""
def test_simple_select_all(self):
"""Test simple SELECT * without WHERE"""
sql = "SELECT * FROM users"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {} # No WHERE clause
assert isinstance(execution_plan.projection_stage, dict)
def test_simple_select_fields(self):
"""Test simple SELECT with specific fields, no WHERE"""
sql = "SELECT name, email FROM customers"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "customers"
assert execution_plan.filter_stage == {} # No WHERE clause
assert execution_plan.projection_stage == {"name": 1, "email": 1}
def test_select_single_field(self):
"""Test SELECT with single field"""
sql = "SELECT title FROM books"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "books"
assert execution_plan.filter_stage == {}
assert execution_plan.projection_stage == {"title": 1}
def test_select_with_simple_where_equals(self):
"""Test SELECT with simple WHERE equality condition"""
sql = "SELECT name FROM users WHERE status = 'active'"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"status": "active"}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_numeric_comparison(self):
"""Test SELECT with numeric comparison in WHERE"""
sql = "SELECT name, age FROM users WHERE age > 30"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"age": {"$gt": 30}}
assert execution_plan.projection_stage == {"name": 1, "age": 1}
def test_select_with_less_than(self):
"""Test SELECT with less than comparison"""
sql = "SELECT product_name FROM products WHERE price < 100"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "products"
assert execution_plan.filter_stage == {"price": {"$lt": 100}}
assert execution_plan.projection_stage == {"product_name": 1}
def test_select_with_greater_equal(self):
"""Test SELECT with greater than or equal"""
sql = "SELECT title FROM books WHERE year >= 2020"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "books"
assert execution_plan.filter_stage == {"year": {"$gte": 2020}}
assert execution_plan.projection_stage == {"title": 1}
def test_select_with_not_equals(self):
"""Test SELECT with not equals condition"""
sql = "SELECT name FROM users WHERE status != 'inactive'"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"status": {"$ne": "inactive"}}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_and_condition(self):
"""Test SELECT with AND condition"""
sql = "SELECT name FROM users WHERE age > 25 AND status = 'active'"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"$and": [{"age": {"$gt": 25}}, {"status": "active"}]}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_or_condition(self):
"""Test SELECT with OR condition"""
sql = "SELECT name FROM users WHERE age < 18 OR age > 65"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"$or": [{"age": {"$lt": 18}}, {"age": {"$gt": 65}}]}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_multiple_and_conditions(self):
"""Test SELECT with multiple AND conditions"""
sql = "SELECT * FROM products WHERE price > 50 AND category = 'electronics' AND stock > 0"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "products"
assert execution_plan.filter_stage == {
"$and": [
{"price": {"$gt": 50}},
{"category": "electronics"},
{"stock": {"$gt": 0}},
]
}
# SELECT * should include all fields or empty projection
assert execution_plan.projection_stage in [{}, None]
def test_select_with_mixed_and_or(self):
"""Test SELECT with mixed AND/OR conditions"""
sql = "SELECT name FROM users WHERE (age > 25 AND status = 'active') OR (age < 18 AND status = 'minor')"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {
"$or": [
{"$and": [{"age": {"$gt": 25}}, {"status": "active"}]},
{"$and": [{"age": {"$lt": 18}}, {"status": "minor"}]},
]
}
def test_select_with_in_condition(self):
"""Test SELECT with IN condition"""
sql = "SELECT name FROM users WHERE status IN ('active', 'pending', 'verified')"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"status": {"$in": ["active", "pending", "verified"]}}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_like_condition(self):
"""Test SELECT with LIKE condition"""
sql = "SELECT name FROM users WHERE name LIKE 'John%'"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"name": {"$regex": "^John.*"}}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_between_condition(self):
"""Test SELECT with BETWEEN condition"""
sql = "SELECT name FROM users WHERE age BETWEEN 25 AND 65"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"$and": [{"age": {"$gte": 25}}, {"age": {"$lte": 65}}]}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_null_condition(self):
"""Test SELECT with IS NULL condition"""
sql = "SELECT name FROM users WHERE email IS NULL"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"email": {"$eq": None}}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_not_null_condition(self):
"""Test SELECT with IS NOT NULL condition"""
sql = "SELECT name FROM users WHERE email IS NOT NULL"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"email": {"$ne": None}}
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_order_by(self):
"""Test SELECT with ORDER BY clause"""
sql = "SELECT name, age FROM users ORDER BY age ASC"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.sort_stage == [{"age": 1}] # 1 for ASC, -1 for DESC
assert execution_plan.projection_stage == {"name": 1, "age": 1}
def test_select_with_limit(self):
"""Test SELECT with LIMIT clause"""
sql = "SELECT name FROM users LIMIT 10"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.limit_stage == 10
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_offset(self):
"""Test SELECT with OFFSET clause"""
sql = "SELECT name FROM users OFFSET 5"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.skip_stage == 5
assert execution_plan.projection_stage == {"name": 1}
def test_select_with_limit_and_offset(self):
"""Test SELECT with both LIMIT and OFFSET clauses"""
sql = "SELECT name, email FROM users LIMIT 10 OFFSET 5"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.limit_stage == 10
assert execution_plan.skip_stage == 5
assert execution_plan.projection_stage == {"name": 1, "email": 1}
def test_complex_query_combination(self):
"""Test complex query with multiple clauses"""
sql = """
SELECT name, email, age
FROM users
WHERE age > 21 AND status = 'active'
ORDER BY name ASC
LIMIT 50
"""
parser = SQLParser(sql)
try:
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.filter_stage == {"$and": [{"age": {"$gt": 21}}, {"status": "active"}]}
assert execution_plan.projection_stage == {
"name": 1,
"email": 1,
"age": 1,
}
assert execution_plan.sort_stage == [{"name": 1}]
assert execution_plan.limit_stage == 50
except (SqlSyntaxError, AssertionError) as e:
pytest.skip(f"Complex query parsing not yet fully implemented: {e}")
def test_parser_error_handling(self):
"""Test parser error handling for invalid SQL"""
# Test empty SQL
with pytest.raises(ValueError):
SQLParser("")
# Test malformed SQL
with pytest.raises(SqlSyntaxError):
parser = SQLParser("INVALID SQL SYNTAX")
parser.get_execution_plan()
def test_different_collection_names(self):
"""Test parsing with different collection names"""
test_cases = [
("SELECT * FROM users", "users"),
("SELECT * FROM products", "products"),
("SELECT * FROM orders", "orders"),
("SELECT * FROM customer_data", "customer_data"),
("SELECT * FROM product_reviews", "product_reviews"),
]
for sql, expected_collection in test_cases:
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors for '{sql}': {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == expected_collection
def test_complex_mixed_operators(self):
"""Test SELECT with complex query combining multiple operators"""
sql = """
SELECT id, name, age, status FROM users WHERE age > 25 AND status = 'active' AND name != 'John'
OR department IN ('IT', 'HR') ORDER BY age DESC LIMIT 5
"""
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
# Verify collection and projection
assert execution_plan.collection == "users"
assert execution_plan.projection_stage == {"id": 1, "name": 1, "age": 1, "status": 1}
# Verify complex filter structure with mixed AND/OR conditions
expected_filter = {
"$or": [
{"$and": [{"age": {"$gt": 25}}, {"status": "active"}, {"name": {"$ne": "John"}}]},
{"department": {"$in": ["IT", "HR"]}},
]
}
assert execution_plan.filter_stage == expected_filter
# Verify ORDER BY and LIMIT
assert execution_plan.sort_stage == [{"age": -1}] # DESC = -1
assert execution_plan.limit_stage == 5
def test_select_with_simple_alias(self):
"""Test SELECT with a simple field alias"""
sql = "SELECT name AS user_name FROM users"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.projection_stage == {"name": 1}
assert execution_plan.column_aliases == {"name": "user_name"}
def test_select_with_multiple_aliases(self):
"""Test SELECT with multiple field aliases"""
sql = "SELECT name AS user_name, email AS user_email, age AS user_age FROM users"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.projection_stage == {"name": 1, "email": 1, "age": 1}
assert execution_plan.column_aliases == {
"name": "user_name",
"email": "user_email",
"age": "user_age",
}
def test_select_with_nested_field_alias(self):
"""Test SELECT with nested field alias like field.idx[0] as a"""
sql = "SELECT field.idx[0] AS a FROM users"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
# Nested field should be normalized to mongo dot notation
assert "field.idx.0" in execution_plan.projection_stage
assert execution_plan.projection_stage["field.idx.0"] == 1
assert execution_plan.column_aliases.get("field.idx.0") == "a"
def test_select_mixed_with_and_without_aliases(self):
"""Test SELECT with some fields having aliases and some not"""
sql = "SELECT name AS user_name, email, age AS user_age FROM users"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.projection_stage == {"name": 1, "email": 1, "age": 1}
# Only fields with aliases should be in column_aliases
assert execution_plan.column_aliases == {
"name": "user_name",
"age": "user_age",
}
def test_select_alias_without_as_keyword(self):
"""Test SELECT with implicit alias (without AS keyword)"""
sql = "SELECT name user_name, email user_email FROM users"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.projection_stage == {"name": 1, "email": 1}
assert execution_plan.column_aliases == {
"name": "user_name",
"email": "user_email",
}
def test_select_with_alias_and_where_clause(self):
"""Test SELECT with aliases and WHERE clause"""
sql = "SELECT name AS user_name, age AS user_age FROM users WHERE status = 'active'"
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == "users"
assert execution_plan.projection_stage == {"name": 1, "age": 1}
assert execution_plan.column_aliases == {
"name": "user_name",
"age": "user_age",
}
assert execution_plan.filter_stage == {"status": "active"}
@pytest.mark.parametrize(
"collection,sql,projection,filter_condition",
[
# Hyphen (-) tests
("user-accounts", "SELECT * FROM user-accounts", None, {}),
(
"user-accounts",
"SELECT name, email FROM user-accounts WHERE status = 'active'",
{"name": 1, "email": 1},
{"status": "active"},
),
# Period (.) tests
("user.accounts", 'SELECT * FROM "user.accounts"', None, {}),
(
"customer.orders",
'SELECT name FROM "customer.orders" WHERE total > 100',
{"name": 1},
{"total": {"$gt": 100}},
),
# Colon (:) tests
("user:accounts", 'SELECT * FROM "user:accounts"', None, {}),
(
"service:requests",
'SELECT id, name FROM "service:requests" WHERE resolved = false',
{"id": 1, "name": 1},
{"resolved": False},
),
# Multiple special characters test
("user-account.data:prod", 'SELECT * FROM "user-account.data:prod"', None, {}),
],
)
def test_collection_name_with_special_characters(self, collection, sql, projection, filter_condition):
"""Test SELECT with collection names containing special characters (-, ., :)"""
parser = SQLParser(sql)
assert not parser.has_errors, f"Parser errors: {parser.errors}"
execution_plan = parser.get_execution_plan()
assert execution_plan.collection == collection
# For SELECT *, projection should be a dict (possibly empty or with just keys)
if projection is None:
assert isinstance(execution_plan.projection_stage, dict)
else:
assert execution_plan.projection_stage == projection
assert execution_plan.filter_stage == filter_condition