-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_parser.py
More file actions
1482 lines (1209 loc) · 58.8 KB
/
test_parser.py
File metadata and controls
1482 lines (1209 loc) · 58.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
from qql.ast_nodes import (
AndExpr,
BetweenExpr,
CompareExpr,
CreateCollectionStmt,
CreateIndexStmt,
DeleteStmt,
DropCollectionStmt,
InExpr,
InsertBulkStmt,
InsertStmt,
IsEmptyExpr,
IsNotEmptyExpr,
IsNotNullExpr,
IsNullExpr,
MatchAnyExpr,
MatchPhraseExpr,
MatchTextExpr,
NotExpr,
NotInExpr,
OrExpr,
QuantizationType,
QuantizationSearchWith,
RecommendStmt,
SelectStmt,
ScrollStmt,
SearchStmt,
ShowCollectionStmt,
ShowCollectionsStmt,
)
from qql.exceptions import QQLSyntaxError
from qql.lexer import Lexer
from qql.parser import Parser
def parse(query: str):
tokens = Lexer().tokenize(query)
return Parser(tokens).parse()
class TestInsert:
def test_basic_insert(self):
node = parse("INSERT INTO COLLECTION notes VALUES {'text': 'hello'}")
assert isinstance(node, InsertStmt)
assert node.collection == "notes"
assert node.values == {"text": "hello"}
assert node.model is None
def test_insert_with_metadata(self):
node = parse("INSERT INTO COLLECTION notes VALUES {'text': 'hi', 'author': 'alice'}")
assert node.values["author"] == "alice"
assert node.values["text"] == "hi"
def test_insert_with_model(self):
node = parse(
"INSERT INTO COLLECTION notes VALUES {'text': 'hi'} "
"USING MODEL 'sentence-transformers/all-MiniLM-L6-v2'"
)
assert node.model == "sentence-transformers/all-MiniLM-L6-v2"
def test_insert_case_insensitive(self):
node = parse("insert into collection notes values {'text': 'hello'}")
assert isinstance(node, InsertStmt)
def test_insert_nested_dict(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'x', 'meta': {'src': 'web'}}")
assert node.values["meta"] == {"src": "web"}
def test_insert_list_value(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'x', 'tags': ['a', 'b']}")
assert node.values["tags"] == ["a", "b"]
def test_insert_integer_value(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'x', 'count': 42}")
assert node.values["count"] == 42
def test_insert_float_value(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'x', 'score': 0.9}")
assert node.values["score"] == pytest.approx(0.9)
def test_insert_bool_value(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'x', 'active': true}")
assert node.values["active"] is True
def test_insert_null_value(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'x', 'ref': null}")
assert node.values["ref"] is None
def test_missing_text_is_not_parser_error(self):
# Schema validation is the executor's job, not the parser's
node = parse("INSERT INTO COLLECTION col VALUES {'author': 'bob'}")
assert isinstance(node, InsertStmt)
assert "text" not in node.values
class TestInsertBulk:
def test_basic_bulk_insert(self):
node = parse("INSERT BULK INTO COLLECTION col VALUES [{'text': 'hello'}]")
assert isinstance(node, InsertBulkStmt)
assert node.collection == "col"
assert len(node.values_list) == 1
assert node.values_list[0]["text"] == "hello"
def test_bulk_insert_two_items(self):
node = parse(
"INSERT BULK INTO COLLECTION col VALUES "
"[{'text': 'first'}, {'text': 'second'}]"
)
assert isinstance(node, InsertBulkStmt)
assert len(node.values_list) == 2
assert node.values_list[1]["text"] == "second"
def test_bulk_insert_preserves_metadata(self):
node = parse(
"INSERT BULK INTO COLLECTION col VALUES "
"[{'text': 'hello', 'year': 2021}, {'text': 'world', 'year': 2022}]"
)
assert node.values_list[0]["year"] == 2021
assert node.values_list[1]["year"] == 2022
def test_bulk_insert_using_model(self):
node = parse(
"INSERT BULK INTO COLLECTION col VALUES [{'text': 'a'}] "
"USING MODEL 'BAAI/bge-base-en-v1.5'"
)
assert node.model == "BAAI/bge-base-en-v1.5"
assert node.hybrid is False
def test_bulk_insert_using_hybrid(self):
node = parse(
"INSERT BULK INTO COLLECTION col VALUES [{'text': 'a'}] USING HYBRID"
)
assert node.hybrid is True
assert node.model is None
def test_bulk_insert_using_hybrid_dense_model(self):
node = parse(
"INSERT BULK INTO COLLECTION col VALUES [{'text': 'a'}] "
"USING HYBRID DENSE MODEL 'BAAI/bge-base-en-v1.5'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-base-en-v1.5"
def test_bulk_insert_collection_name(self):
node = parse("INSERT BULK INTO COLLECTION my_notes VALUES [{'text': 'x'}]")
assert node.collection == "my_notes"
def test_bulk_insert_case_insensitive(self):
node = parse("insert bulk into collection col values [{'text': 'hi'}]")
assert isinstance(node, InsertBulkStmt)
def test_bulk_insert_default_model_is_none(self):
node = parse("INSERT BULK INTO COLLECTION col VALUES [{'text': 'a'}]")
assert node.model is None
assert node.sparse_model is None
assert node.hybrid is False
def test_single_insert_still_works_after_bulk_addition(self):
"""Ensure single INSERT flow is not broken by the BULK branch."""
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'hello'}")
assert isinstance(node, InsertStmt)
assert node.values == {"text": "hello"}
class TestCreate:
def test_create_collection(self):
node = parse("CREATE COLLECTION my_col")
assert isinstance(node, CreateCollectionStmt)
assert node.collection == "my_col"
def test_create_index(self):
node = parse("CREATE INDEX ON COLLECTION articles FOR category TYPE keyword")
assert isinstance(node, CreateIndexStmt)
assert node.collection == "articles"
assert node.field_name == "category"
assert node.schema == "keyword"
class TestDrop:
def test_drop_collection(self):
node = parse("DROP COLLECTION my_col")
assert isinstance(node, DropCollectionStmt)
assert node.collection == "my_col"
class TestShow:
def test_show_collections(self):
node = parse("SHOW COLLECTIONS")
assert isinstance(node, ShowCollectionsStmt)
def test_show_collection(self):
node = parse("SHOW COLLECTION docs")
assert isinstance(node, ShowCollectionStmt)
assert node.collection == "docs"
def test_show_collection_case_insensitive(self):
node = parse("show collection MY_COL")
assert isinstance(node, ShowCollectionStmt)
assert node.collection == "MY_COL"
class TestScroll:
def test_scroll_basic(self):
node = parse("SCROLL FROM docs LIMIT 50")
assert isinstance(node, ScrollStmt)
assert node.collection == "docs"
assert node.limit == 50
assert node.query_filter is None
assert node.after is None
def test_scroll_with_where(self):
node = parse("SCROLL FROM docs WHERE year >= 2024 LIMIT 50")
assert isinstance(node, ScrollStmt)
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.field == "year"
assert node.after is None
def test_scroll_with_after(self):
node = parse("SCROLL FROM docs AFTER 'cursor-id' LIMIT 50")
assert isinstance(node, ScrollStmt)
assert node.after == "cursor-id"
def test_scroll_with_where_and_after(self):
node = parse("SCROLL FROM docs WHERE year >= 2024 AFTER 42 LIMIT 50")
assert isinstance(node, ScrollStmt)
assert node.after == 42
assert isinstance(node.query_filter, CompareExpr)
class TestSelect:
def test_select_by_string_id(self):
node = parse("SELECT * FROM notes WHERE id = 'abc-123'")
assert isinstance(node, SelectStmt)
assert node.collection == "notes"
assert node.point_id == "abc-123"
def test_select_by_integer_id(self):
node = parse("SELECT * FROM notes WHERE id = 42")
assert isinstance(node, SelectStmt)
assert node.point_id == 42
def test_select_requires_id_filter(self):
with pytest.raises(QQLSyntaxError):
parse("SELECT * FROM notes WHERE year = 2024")
class TestSearch:
def test_basic_search(self):
node = parse("SEARCH notes SIMILAR TO 'hello world' LIMIT 5")
assert isinstance(node, SearchStmt)
assert node.collection == "notes"
assert node.query_text == "hello world"
assert node.limit == 5
assert node.model is None
def test_search_with_model(self):
node = parse("SEARCH notes SIMILAR TO 'hi' LIMIT 3 USING MODEL 'my-model'")
assert node.model == "my-model"
class TestDelete:
def test_delete_by_string_id(self):
node = parse("DELETE FROM notes WHERE id = 'abc-123'")
assert isinstance(node, DeleteStmt)
assert node.collection == "notes"
assert node.point_id == "abc-123"
assert node.query_filter is None
def test_delete_by_integer_id(self):
node = parse("DELETE FROM notes WHERE id = 99")
assert isinstance(node, DeleteStmt)
assert node.point_id == 99
def test_delete_by_filter(self):
node = parse("DELETE FROM articles WHERE category = 'archived'")
assert isinstance(node, DeleteStmt)
assert node.point_id is None
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.field == "category"
assert node.query_filter.value == "archived"
class TestRecommend:
def test_recommend_with_positive_ids(self):
node = parse("RECOMMEND FROM notes POSITIVE IDS ('a', 'b') LIMIT 5")
assert isinstance(node, RecommendStmt)
assert node.collection == "notes"
assert node.positive_ids == ("a", "b")
assert node.negative_ids == ()
assert node.limit == 5
assert node.strategy is None
def test_recommend_with_negative_ids_and_strategy(self):
node = parse(
"RECOMMEND FROM notes POSITIVE IDS ('a', 2) "
"NEGATIVE IDS ('x') STRATEGY 'best_score' LIMIT 7"
)
assert node.positive_ids == ("a", 2)
assert node.negative_ids == ("x",)
assert node.strategy == "best_score"
assert node.limit == 7
def test_recommend_with_where_filter(self):
node = parse(
"RECOMMEND FROM notes POSITIVE IDS ('a') LIMIT 5 WHERE year > 2020"
)
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.field == "year"
def test_recommend_requires_non_empty_positive_ids(self):
with pytest.raises(QQLSyntaxError):
parse("RECOMMEND FROM notes POSITIVE IDS () LIMIT 5")
def test_recommend_with_offset(self):
node = parse("RECOMMEND FROM notes POSITIVE IDS ('a') LIMIT 10 OFFSET 5")
assert node.offset == 5
def test_recommend_with_score_threshold(self):
node = parse(
"RECOMMEND FROM notes POSITIVE IDS ('a') LIMIT 10 SCORE THRESHOLD 0.5"
)
assert node.score_threshold == pytest.approx(0.5)
def test_recommend_with_clause(self):
node = parse(
"RECOMMEND FROM notes POSITIVE IDS ('a') LIMIT 10 WITH { exact: true }"
)
assert node.with_clause is not None
assert node.with_clause.exact is True
def test_recommend_with_clause_hnsw_ef(self):
node = parse(
"RECOMMEND FROM notes POSITIVE IDS ('a') LIMIT 10 WITH { hnsw_ef: 128 }"
)
assert node.with_clause is not None
assert node.with_clause.hnsw_ef == 128
def test_recommend_with_indexed_only_and_quantization(self):
node = parse(
"RECOMMEND FROM notes POSITIVE IDS ('a') LIMIT 10 "
"WITH { indexed_only: true, quantization: { rescore: true } }"
)
assert node.with_clause is not None
assert node.with_clause.indexed_only is True
assert node.with_clause.quantization is not None
assert node.with_clause.quantization.rescore is True
def test_recommend_lookup_from(self):
node = parse(
"RECOMMEND FROM target_collection POSITIVE IDS ('a') "
"LOOKUP FROM source_collection LIMIT 5"
)
assert node.lookup_from == ("source_collection", None)
def test_recommend_lookup_from_with_vector(self):
node = parse(
"RECOMMEND FROM target_collection POSITIVE IDS ('a') "
"LOOKUP FROM source_collection VECTOR 'dense' LIMIT 5"
)
assert node.lookup_from == ("source_collection", "dense")
def test_recommend_using(self):
node = parse(
"RECOMMEND FROM docs POSITIVE IDS ('a') USING 'sparse' LIMIT 5"
)
assert node.using == "sparse"
def test_recommend_lookup_from_and_using(self):
node = parse(
"RECOMMEND FROM target_collection POSITIVE IDS ('a') "
"LOOKUP FROM source_collection VECTOR 'dense' USING 'sparse' LIMIT 5"
)
assert node.lookup_from == ("source_collection", "dense")
assert node.using == "sparse"
def test_recommend_full_clause_order(self):
node = parse(
"RECOMMEND FROM docs POSITIVE IDS ('a', 'b') "
"NEGATIVE IDS ('x') STRATEGY 'best_score' "
"LOOKUP FROM src VECTOR 'dense' USING 'sparse' "
"LIMIT 10 OFFSET 5 SCORE THRESHOLD 0.5 "
"WHERE year > 2020 WITH { exact: true, hnsw_ef: 128 }"
)
assert node.collection == "docs"
assert node.positive_ids == ("a", "b")
assert node.negative_ids == ("x",)
assert node.strategy == "best_score"
assert node.lookup_from == ("src", "dense")
assert node.using == "sparse"
assert node.limit == 10
assert node.offset == 5
assert node.score_threshold == pytest.approx(0.5)
assert isinstance(node.query_filter, CompareExpr)
assert node.with_clause is not None
assert node.with_clause.exact is True
assert node.with_clause.hnsw_ef == 128
class TestErrors:
def test_unknown_keyword(self):
with pytest.raises(QQLSyntaxError):
parse("UPSERT INTO foo VALUES {'text': 'x'}")
def test_missing_collection_name(self):
with pytest.raises(QQLSyntaxError):
parse("INSERT INTO COLLECTION VALUES {'text': 'x'}")
def test_empty_input(self):
with pytest.raises(QQLSyntaxError):
parse("")
class TestSearchWithWhere:
def test_no_where_clause(self):
node = parse("SEARCH docs SIMILAR TO 'ml' LIMIT 5")
assert node.query_filter is None
def test_equality_filter(self):
node = parse("SEARCH docs SIMILAR TO 'ml' LIMIT 5 WHERE category = 'paper'")
f = node.query_filter
assert isinstance(f, CompareExpr)
assert f.field == "category"
assert f.op == "="
assert f.value == "paper"
def test_not_equals_filter(self):
node = parse("SEARCH docs SIMILAR TO 'ml' LIMIT 5 WHERE status != 'draft'")
f = node.query_filter
assert isinstance(f, CompareExpr)
assert f.op == "!="
assert f.value == "draft"
def test_range_gt(self):
node = parse("SEARCH docs SIMILAR TO 'ml' LIMIT 5 WHERE score > 0.8")
f = node.query_filter
assert isinstance(f, CompareExpr)
assert f.op == ">"
assert f.value == pytest.approx(0.8)
def test_range_gte(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE year >= 2020")
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.op == ">="
assert node.query_filter.value == 2020
def test_range_lt(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE year < 2024")
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.op == "<"
def test_range_lte(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE year <= 2023")
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.op == "<="
def test_between(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE year BETWEEN 2018 AND 2023")
f = node.query_filter
assert isinstance(f, BetweenExpr)
assert f.field == "year"
assert f.low == 2018
assert f.high == 2023
def test_in_expr(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE status IN ('a', 'b')")
f = node.query_filter
assert isinstance(f, InExpr)
assert f.field == "status"
assert f.values == ("a", "b")
def test_boolean_equality_filter(self):
node = parse("SEARCH docs SIMILAR TO 'ml' LIMIT 5 WHERE active = true")
f = node.query_filter
assert isinstance(f, CompareExpr)
assert f.field == "active"
assert f.op == "="
assert f.value is True
def test_boolean_in_expr(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE active IN (true, false)")
f = node.query_filter
assert isinstance(f, InExpr)
assert f.values == (True, False)
def test_in_with_trailing_comma(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE status IN ('a', 'b',)")
assert isinstance(node.query_filter, InExpr)
assert len(node.query_filter.values) == 2
def test_not_in_expr(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE status NOT IN ('deleted', 'archived')")
f = node.query_filter
assert isinstance(f, NotInExpr)
assert f.values == ("deleted", "archived")
def test_is_null(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE reviewer IS NULL")
f = node.query_filter
assert isinstance(f, IsNullExpr)
assert f.field == "reviewer"
def test_is_not_null(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE reviewer IS NOT NULL")
assert isinstance(node.query_filter, IsNotNullExpr)
assert node.query_filter.field == "reviewer"
def test_is_empty(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE tags IS EMPTY")
assert isinstance(node.query_filter, IsEmptyExpr)
assert node.query_filter.field == "tags"
def test_is_not_empty(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE tags IS NOT EMPTY")
assert isinstance(node.query_filter, IsNotEmptyExpr)
def test_match_text(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE title MATCH 'deep learning'")
f = node.query_filter
assert isinstance(f, MatchTextExpr)
assert f.field == "title"
assert f.text == "deep learning"
def test_match_any(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE title MATCH ANY 'nlp ai'")
f = node.query_filter
assert isinstance(f, MatchAnyExpr)
assert f.text == "nlp ai"
def test_match_phrase(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE title MATCH PHRASE 'neural net'")
assert isinstance(node.query_filter, MatchPhraseExpr)
def test_and_expr_two_operands(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE a = '1' AND b = '2'")
f = node.query_filter
assert isinstance(f, AndExpr)
assert len(f.operands) == 2
assert all(isinstance(op, CompareExpr) for op in f.operands)
def test_and_expr_three_operands_flattened(self):
node = parse(
"SEARCH d SIMILAR TO 'x' LIMIT 5 WHERE a = '1' AND b = '2' AND c = '3'"
)
f = node.query_filter
assert isinstance(f, AndExpr)
assert len(f.operands) == 3 # flattened, not binary-nested
def test_or_expr(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE a = '1' OR b = '2'")
f = node.query_filter
assert isinstance(f, OrExpr)
assert len(f.operands) == 2
def test_not_expr(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE NOT status = 'draft'")
f = node.query_filter
assert isinstance(f, NotExpr)
assert isinstance(f.operand, CompareExpr)
def test_parenthesized_or_inside_and(self):
node = parse(
"SEARCH docs SIMILAR TO 'x' LIMIT 5 "
"WHERE (src = 'a' OR src = 'b') AND year > 2020"
)
f = node.query_filter
assert isinstance(f, AndExpr)
assert isinstance(f.operands[0], OrExpr)
assert isinstance(f.operands[1], CompareExpr)
def test_dotted_field_path(self):
node = parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE meta.source = 'web'")
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.field == "meta.source"
def test_using_model_then_where(self):
node = parse(
"SEARCH docs SIMILAR TO 'x' LIMIT 5 "
"USING MODEL 'my-model' WHERE category = 'paper'"
)
assert node.model == "my-model"
assert isinstance(node.query_filter, CompareExpr)
def test_between_and_does_not_confuse_logical_and(self):
# The AND inside BETWEEN must not be consumed by the logical AND loop
node = parse(
"SEARCH d SIMILAR TO 'x' LIMIT 5 WHERE year BETWEEN 2018 AND 2023 AND category = 'ai'"
)
f = node.query_filter
assert isinstance(f, AndExpr)
assert isinstance(f.operands[0], BetweenExpr)
assert isinstance(f.operands[1], CompareExpr)
assert len(f.operands) == 2
def test_not_negates_parenthesized_group(self):
node = parse(
"SEARCH d SIMILAR TO 'x' LIMIT 5 WHERE NOT (a = '1' OR b = '2')"
)
f = node.query_filter
assert isinstance(f, NotExpr)
assert isinstance(f.operand, OrExpr)
def test_missing_rparen_raises(self):
with pytest.raises(QQLSyntaxError):
parse("SEARCH docs SIMILAR TO 'x' LIMIT 5 WHERE (a = '1'")
# ── Hybrid vector tests ───────────────────────────────────────────────────────
class TestHybridCreate:
def test_create_hybrid_sets_flag(self):
node = parse("CREATE COLLECTION articles HYBRID")
assert isinstance(node, CreateCollectionStmt)
assert node.collection == "articles"
assert node.hybrid is True
def test_create_non_hybrid_default_false(self):
node = parse("CREATE COLLECTION articles")
assert node.hybrid is False
def test_create_hybrid_case_insensitive(self):
node = parse("create collection col hybrid")
assert node.hybrid is True
class TestCreateUsing:
def test_create_using_model(self):
node = parse("CREATE COLLECTION articles USING MODEL 'BAAI/bge-base-en-v1.5'")
assert isinstance(node, CreateCollectionStmt)
assert node.hybrid is False
assert node.model == "BAAI/bge-base-en-v1.5"
def test_create_using_hybrid(self):
node = parse("CREATE COLLECTION articles USING HYBRID")
assert isinstance(node, CreateCollectionStmt)
assert node.hybrid is True
assert node.model is None
def test_create_using_hybrid_dense_model(self):
node = parse("CREATE COLLECTION articles USING HYBRID DENSE MODEL 'BAAI/bge-base-en-v1.5'")
assert node.hybrid is True
assert node.model == "BAAI/bge-base-en-v1.5"
def test_create_bare_hybrid_backward_compat(self):
node = parse("CREATE COLLECTION articles HYBRID")
assert node.hybrid is True
assert node.model is None
def test_create_plain_backward_compat(self):
node = parse("CREATE COLLECTION articles")
assert node.hybrid is False
assert node.model is None
def test_create_using_model_sets_collection_name(self):
node = parse("CREATE COLLECTION my_col USING MODEL 'some/model'")
assert isinstance(node, CreateCollectionStmt)
assert node.collection == "my_col"
def test_create_using_hybrid_case_insensitive(self):
node = parse("create collection articles using hybrid")
assert node.hybrid is True
def test_create_using_model_case_insensitive(self):
node = parse("create collection articles using model 'some/model'")
assert node.model == "some/model"
class TestHybridInsert:
def test_insert_using_hybrid_sets_flag(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'hi'} USING HYBRID")
assert isinstance(node, InsertStmt)
assert node.hybrid is True
assert node.model is None
assert node.sparse_model is None
def test_insert_non_hybrid_default(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'hi'}")
assert node.hybrid is False
assert node.sparse_model is None
def test_insert_using_model_still_works(self):
node = parse("INSERT INTO COLLECTION col VALUES {'text': 'hi'} USING MODEL 'my-model'")
assert node.hybrid is False
assert node.model == "my-model"
assert node.sparse_model is None
def test_insert_hybrid_dense_model(self):
node = parse(
"INSERT INTO COLLECTION col VALUES {'text': 'hi'} "
"USING HYBRID DENSE MODEL 'BAAI/bge-small-en-v1.5'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-small-en-v1.5"
assert node.sparse_model is None
def test_insert_hybrid_sparse_model(self):
node = parse(
"INSERT INTO COLLECTION col VALUES {'text': 'hi'} "
"USING HYBRID SPARSE MODEL 'Qdrant/bm25'"
)
assert node.hybrid is True
assert node.model is None
assert node.sparse_model == "Qdrant/bm25"
def test_insert_hybrid_both_models(self):
node = parse(
"INSERT INTO COLLECTION col VALUES {'text': 'hi'} "
"USING HYBRID DENSE MODEL 'BAAI/bge-base-en-v1.5' SPARSE MODEL 'Qdrant/bm25'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-base-en-v1.5"
assert node.sparse_model == "Qdrant/bm25"
def test_insert_hybrid_both_models_reversed_order(self):
node = parse(
"INSERT INTO COLLECTION col VALUES {'text': 'hi'} "
"USING HYBRID SPARSE MODEL 'Qdrant/bm25' DENSE MODEL 'BAAI/bge-base-en-v1.5'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-base-en-v1.5"
assert node.sparse_model == "Qdrant/bm25"
class TestHybridSearch:
def test_search_using_hybrid_sets_flag(self):
node = parse("SEARCH articles SIMILAR TO 'ml' LIMIT 10 USING HYBRID")
assert isinstance(node, SearchStmt)
assert node.hybrid is True
assert node.model is None
assert node.sparse_model is None
def test_search_non_hybrid_default(self):
node = parse("SEARCH articles SIMILAR TO 'ml' LIMIT 10")
assert node.hybrid is False
assert node.sparse_model is None
def test_search_using_model_still_works(self):
node = parse("SEARCH articles SIMILAR TO 'ml' LIMIT 5 USING MODEL 'my-model'")
assert node.hybrid is False
assert node.model == "my-model"
assert node.sparse_model is None
def test_search_hybrid_dense_model(self):
node = parse(
"SEARCH articles SIMILAR TO 'ml' LIMIT 10 "
"USING HYBRID DENSE MODEL 'BAAI/bge-small-en-v1.5'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-small-en-v1.5"
assert node.sparse_model is None
def test_search_hybrid_sparse_model(self):
node = parse(
"SEARCH articles SIMILAR TO 'ml' LIMIT 10 "
"USING HYBRID SPARSE MODEL 'prithivida/Splade_PP_en_v1'"
)
assert node.hybrid is True
assert node.model is None
assert node.sparse_model == "prithivida/Splade_PP_en_v1"
def test_search_hybrid_both_models(self):
node = parse(
"SEARCH articles SIMILAR TO 'ml' LIMIT 10 "
"USING HYBRID DENSE MODEL 'BAAI/bge-base-en-v1.5' SPARSE MODEL 'Qdrant/bm25'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-base-en-v1.5"
assert node.sparse_model == "Qdrant/bm25"
def test_search_hybrid_both_models_reversed_order(self):
node = parse(
"SEARCH articles SIMILAR TO 'ml' LIMIT 10 "
"USING HYBRID SPARSE MODEL 'Qdrant/bm25' DENSE MODEL 'BAAI/bge-base-en-v1.5'"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-base-en-v1.5"
assert node.sparse_model == "Qdrant/bm25"
def test_search_hybrid_with_where(self):
node = parse(
"SEARCH articles SIMILAR TO 'ml' LIMIT 10 USING HYBRID WHERE year > 2020"
)
assert node.hybrid is True
assert isinstance(node.query_filter, CompareExpr)
assert node.query_filter.field == "year"
def test_search_hybrid_with_dbsf_fusion(self):
node = parse(
"SEARCH docs SIMILAR TO 'q' LIMIT 10 USING HYBRID FUSION 'dbsf'"
)
assert node.hybrid is True
assert node.fusion == "dbsf"
def test_search_hybrid_with_fusion_and_models(self):
node = parse(
"SEARCH docs SIMILAR TO 'q' LIMIT 10 "
"USING HYBRID FUSION 'rrf' SPARSE MODEL 'Qdrant/bm25' "
"DENSE MODEL 'BAAI/bge-base-en-v1.5'"
)
assert node.hybrid is True
assert node.fusion == "rrf"
assert node.sparse_model == "Qdrant/bm25"
assert node.model == "BAAI/bge-base-en-v1.5"
def test_search_hybrid_dense_model_and_where(self):
node = parse(
"SEARCH articles SIMILAR TO 'ml' LIMIT 10 "
"USING HYBRID DENSE MODEL 'BAAI/bge-small-en-v1.5' WHERE year > 2020"
)
assert node.hybrid is True
assert node.model == "BAAI/bge-small-en-v1.5"
assert isinstance(node.query_filter, CompareExpr)
def test_search_hybrid_rejects_unknown_fusion(self):
with pytest.raises(QQLSyntaxError, match="Unsupported hybrid fusion"):
parse("SEARCH docs SIMILAR TO 'q' LIMIT 10 USING HYBRID FUSION 'x'")
def test_search_hybrid_limit_preserved(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 7 USING HYBRID")
assert node.limit == 7
class TestRerankSearch:
def test_rerank_flag_set(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 RERANK")
assert node.rerank is True
assert node.rerank_model is None
def test_rerank_with_model(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 RERANK MODEL 'cross-encoder/ms-marco-MiniLM-L-6-v2'"
)
assert node.rerank is True
assert node.rerank_model == "cross-encoder/ms-marco-MiniLM-L-6-v2"
def test_rerank_default_false(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5")
assert node.rerank is False
assert node.rerank_model is None
def test_rerank_with_using_model(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 USING MODEL 'BAAI/bge-small-en-v1.5' RERANK")
assert node.model == "BAAI/bge-small-en-v1.5"
assert node.rerank is True
def test_rerank_with_hybrid(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 USING HYBRID RERANK")
assert node.hybrid is True
assert node.rerank is True
assert node.rerank_model is None
def test_rerank_with_where(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WHERE year > 2020 RERANK")
assert node.query_filter is not None
assert node.rerank is True
def test_rerank_with_hybrid_where_and_model(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 USING HYBRID WHERE year > 2020 "
"RERANK MODEL 'cross-encoder/ms-marco-MiniLM-L-6-v2'"
)
assert node.hybrid is True
assert node.query_filter is not None
assert node.rerank is True
assert node.rerank_model == "cross-encoder/ms-marco-MiniLM-L-6-v2"
def test_rerank_lowercase(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 rerank")
assert node.rerank is True
def test_rerank_model_custom(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 RERANK MODEL 'my-custom/reranker'")
assert node.rerank_model == "my-custom/reranker"
def test_existing_search_unaffected_by_rerank_addition(self):
"""Existing parse calls without RERANK still produce rerank=False."""
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 10 USING MODEL 'BAAI/bge-small-en-v1.5'")
assert node.rerank is False
assert node.rerank_model is None
class TestExactSearch:
def test_exact_keyword_sets_flag(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 EXACT")
assert node.with_clause is not None
def test_exact_with_where(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 EXACT WHERE year > 2020")
assert node.with_clause is not None
assert node.query_filter is not None
def test_exact_with_hybrid(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 USING HYBRID EXACT")
assert node.hybrid is True
assert node.with_clause is not None
class TestSearchWithClause:
def test_with_hnsw_ef(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { hnsw_ef: 256 }")
assert node.with_clause is not None
assert node.with_clause.hnsw_ef == 256
def test_with_exact_true(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { exact: true }")
assert node.with_clause is not None
assert node.with_clause.exact is True
def test_with_exact_false(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { exact: false }")
assert node.with_clause is not None
assert node.with_clause.exact is False
def test_with_acorn(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { acorn: true }")
assert node.with_clause is not None
assert node.with_clause.acorn is True
def test_with_indexed_only(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { indexed_only: true }")
assert node.with_clause is not None
assert node.with_clause.indexed_only is True
def test_with_quantization(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 "
"WITH { quantization: { ignore: true, rescore: false, oversampling: 2 } }"
)
assert node.with_clause is not None
assert node.with_clause.quantization is not None
assert node.with_clause.quantization.ignore is True
assert node.with_clause.quantization.rescore is False
assert node.with_clause.quantization.oversampling == pytest.approx(2.0)
def test_with_multiple_params(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { hnsw_ef: 256, acorn: true }"
)
assert node.with_clause.hnsw_ef == 256
assert node.with_clause.acorn is True
def test_with_mmr_params(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 "
"WITH { mmr_diversity: 0.5, mmr_candidates: 50 }"
)
assert node.with_clause is not None
assert node.with_clause.mmr_diversity == pytest.approx(0.5)
assert node.with_clause.mmr_candidates == 50
def test_with_after_where(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 WHERE year > 2020 WITH { hnsw_ef: 128 }"
)
assert node.with_clause is not None
assert node.with_clause.hnsw_ef == 128
assert node.query_filter is not None
def test_with_after_rerank(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 RERANK WITH { hnsw_ef: 256 }")
assert node.rerank is True
assert node.with_clause is not None
assert node.with_clause.hnsw_ef == 256
def test_with_full_search(self):
node = parse(
"SEARCH col SIMILAR TO 'q' LIMIT 5 USING HYBRID WHERE year > 2020 "
"RERANK WITH { hnsw_ef: 256, acorn: true }"
)
assert node.hybrid is True
assert node.query_filter is not None
assert node.rerank is True
assert node.with_clause is not None
assert node.with_clause.hnsw_ef == 256
assert node.with_clause.acorn is True
def test_with_unknown_keyword_raises(self):
with pytest.raises(QQLSyntaxError):
parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { diversity: 0.5 }")
def test_with_mmr_diversity_out_of_range_raises(self):
with pytest.raises(QQLSyntaxError, match="mmr_diversity must be between 0 and 1"):
parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { mmr_diversity: 1.5 }")
def test_with_mmr_candidates_non_positive_raises(self):
with pytest.raises(QQLSyntaxError, match="mmr_candidates must be a positive integer"):
parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { mmr_candidates: 0 }")
def test_with_quantization_unknown_key_raises(self):
with pytest.raises(QQLSyntaxError):
parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { quantization: { unknown: true } }")
def test_with_trailing_comma(self):
node = parse("SEARCH col SIMILAR TO 'q' LIMIT 5 WITH { hnsw_ef: 256, }")
assert node.with_clause.hnsw_ef == 256
def test_with_quantization_unknown_key_raises(self):
with pytest.raises(QQLSyntaxError):
parse(