-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_filtering_api.py
198 lines (182 loc) · 8.17 KB
/
test_filtering_api.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
from functools import reduce
import pytest
from adapta.storage.models.filter_expression import (
FilterField,
FilterExpression,
ArrowFilterExpression,
AstraFilterExpression,
compile_expression,
)
from adapta.schema_management.schema_entity import PythonSchemaEntity
from dataclasses import dataclass, field
from typing import List, Any, Dict, Union
from pyarrow.dataset import field as pyarrow_field
import pyarrow.compute as pc
@dataclass
class TestEntity:
col_a: str = field(metadata={"is_primary_key": True, "is_partition_key": True})
col_b: str = field(metadata={"is_primary_key": True, "is_partition_key": False})
col_c: int
col_d: List[int]
TEST_ENTITY_SCHEMA: TestEntity = PythonSchemaEntity(TestEntity)
@pytest.mark.parametrize(
"filter_expr, pyarrow_expected_expr, astra_expected_expr",
[
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test", (pyarrow_field("col_a") == "test"), [{"col_a": "test"}]),
(
FilterField(TEST_ENTITY_SCHEMA.col_a) >= "test",
(pyarrow_field("col_a") >= "test"),
[{"col_a__gte": "test"}],
),
(FilterField(TEST_ENTITY_SCHEMA.col_a) > "test", (pyarrow_field("col_a") > "test"), [{"col_a__gt": "test"}]),
(FilterField(TEST_ENTITY_SCHEMA.col_a) < "test", (pyarrow_field("col_a") < "test"), [{"col_a__lt": "test"}]),
(
FilterField(TEST_ENTITY_SCHEMA.col_a) <= "test",
(pyarrow_field("col_a") <= "test"),
[{"col_a__lte": "test"}],
),
(
FilterField(TEST_ENTITY_SCHEMA.col_a).isin(["val1", "val2"]),
(pyarrow_field("col_a").isin(["val1", "val2"])),
[{"col_a__in": ["val1", "val2"]}],
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") & (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other"),
(pyarrow_field("col_a") == "test") & (pyarrow_field("col_b") == "other"),
[{"col_a": "test", "col_b": "other"}],
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") & (FilterField(TEST_ENTITY_SCHEMA.col_c).isin([1, 2, 3])),
((pyarrow_field("col_a") == "test") & (pyarrow_field("col_c").isin([1, 2, 3]))),
[{"col_a": "test", "col_c__in": [1, 2, 3]}],
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") | (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other"),
(pyarrow_field("col_a") == "test") | (pyarrow_field("col_b") == "other"),
[{"col_a": "test"}, {"col_b": "other"}],
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test")
| (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other")
| (FilterField(TEST_ENTITY_SCHEMA.col_c) == 1),
((pyarrow_field("col_a") == "test") | (pyarrow_field("col_b") == "other") | (pyarrow_field("col_c") == 1)),
[{"col_a": "test"}, {"col_b": "other"}, {"col_c": 1}],
),
(
((FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") | (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other"))
& (FilterField(TEST_ENTITY_SCHEMA.col_c) == 1),
((pyarrow_field("col_a") == "test") | (pyarrow_field("col_b") == "other")) & (pyarrow_field("col_c") == 1),
[{"col_a": "test", "col_c": 1}, {"col_b": "other", "col_c": 1}],
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") & (FilterField(TEST_ENTITY_SCHEMA.col_c) == 1)
| (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other"),
((pyarrow_field("col_a") == "test") & (pyarrow_field("col_c") == 1) | (pyarrow_field("col_b") == "other")),
[{"col_a": "test", "col_c": 1}, {"col_b": "other"}],
),
(
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") & (FilterField(TEST_ENTITY_SCHEMA.col_c) == 1)
| (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other")
& ((FilterField(TEST_ENTITY_SCHEMA.col_c) == 2) | (FilterField(TEST_ENTITY_SCHEMA.col_d).isin([1, 2])))
),
(
(pyarrow_field("col_a") == "test") & (pyarrow_field("col_c") == 1)
| (pyarrow_field("col_b") == "other")
& ((pyarrow_field("col_c") == 2) | (pyarrow_field("col_d").isin([1, 2])))
),
([{"col_a": "test", "col_c": 1}, {"col_b": "other", "col_c": 2}, {"col_b": "other", "col_d__in": [1, 2]}]),
),
],
)
def test_generic_filtering(
filter_expr: Union[FilterField, FilterExpression],
pyarrow_expected_expr: pc.Expression,
astra_expected_expr: Dict[str, Any],
):
assert compile_expression(filter_expr, ArrowFilterExpression).equals(pyarrow_expected_expr)
assert compile_expression(filter_expr, AstraFilterExpression) == astra_expected_expr
@pytest.mark.parametrize(
"filter_expr, expected_output",
[
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test", "col_a == test"),
(FilterField(TEST_ENTITY_SCHEMA.col_a) > "test", "col_a > test"),
(FilterField(TEST_ENTITY_SCHEMA.col_a) >= "test", "col_a >= test"),
(FilterField(TEST_ENTITY_SCHEMA.col_a) < "test", "col_a < test"),
(FilterField(TEST_ENTITY_SCHEMA.col_a) <= "test", "col_a <= test"),
(
(
(FilterField(TEST_ENTITY_SCHEMA.col_a) == "test") & (FilterField(TEST_ENTITY_SCHEMA.col_c) == 1)
| (FilterField(TEST_ENTITY_SCHEMA.col_b) == "other")
& ((FilterField(TEST_ENTITY_SCHEMA.col_c) == 2) | (FilterField(TEST_ENTITY_SCHEMA.col_d).isin([1, 2])))
),
"((col_a == test) AND (col_c == 1)) OR ((col_b == other) AND ((col_c == 2) OR (col_d IN [1, 2])))",
),
],
)
def test_print_filter_expression(filter_expr: FilterExpression, expected_output: str):
assert str(filter_expr) == expected_output
@pytest.mark.parametrize(
"filter_expr, pyarrow_expected_expr, astra_expected_expr",
[
(
(FilterField(TEST_ENTITY_SCHEMA.col_d).isin([str(i) for i in range(1, 28)])),
((pyarrow_field("col_d").isin([str(i) for i in range(1, 28)]))),
(
[
{"col_d__in": [str(i) for i in range(1, 15)]},
{"col_d__in": [str(i) for i in range(15, 28)]},
]
),
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_d).isin([str(i) for i in range(1, 26)])),
(pyarrow_field("col_d").isin([str(i) for i in range(1, 26)])),
(
[
{"col_d__in": [str(i) for i in range(1, 26)]},
]
),
),
(
(FilterField(TEST_ENTITY_SCHEMA.col_d).isin([str(i) for i in range(1, 101)])),
((pyarrow_field("col_d").isin([str(i) for i in range(1, 101)]))),
(
[
{"col_d__in": [str(i) for i in range(1, 26)]},
{"col_d__in": [str(i) for i in range(26, 51)]},
{"col_d__in": [str(i) for i in range(51, 76)]},
{"col_d__in": [str(i) for i in range(76, 101)]},
]
),
),
],
)
def test_long_is_in_list(
filter_expr: Union[FilterField, FilterExpression],
pyarrow_expected_expr: pc.Expression,
astra_expected_expr: Dict[str, Any],
):
assert compile_expression(filter_expr, ArrowFilterExpression).equals(pyarrow_expected_expr)
assert compile_expression(filter_expr, AstraFilterExpression) == astra_expected_expr
@pytest.mark.parametrize(
"filter_expr",
[
reduce(
lambda x, y: x | y,
[
(FilterField(TEST_ENTITY_SCHEMA.col_d).isin([col_d]))
& (FilterField(TEST_ENTITY_SCHEMA.col_a) == col_a)
& (FilterField(TEST_ENTITY_SCHEMA.col_b) == col_b)
for col_a in [str(i) for i in range(1, 20)]
for col_b in [str(i) for i in range(1, 50)]
for col_d in [str(i) for i in range(1, 50)]
],
)
],
)
def test_large_filter(filter_expr: Union[FilterField, FilterExpression]):
try:
compile_expression(filter_expr, AstraFilterExpression)
except RecursionError as re:
assert False, f"Raised RecursionError for large filters"