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
//! Empty array operator tests - matching Python test_empty_array_operators.py
//!
//! Tests how TQL operators behave with empty arrays to ensure
//! consistent and predictable behavior across all operators.
//!
//! NOTE: Rust has different semantics for array field comparisons than Python.
//! In Python, `tags = 'python'` checks if any element in tags equals 'python'.
//! In Rust, the behavior is to use explicit collection operators (any/all/none).
//! These tests document Rust's actual behavior.
use serde_json::json;
use tellaro_query_language::Tql;
// =============================================================================
// Basic Empty Array Tests with Collection Operators
// Note: In Rust, to check array elements, use ANY operator explicitly
// =============================================================================
#[test]
fn test_empty_array_any_equality() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Use ANY to check if any element equals 'python'
// Empty array does not contain 'python', so id=1 should not match
let results = tql
.query(&data, "any tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
#[test]
fn test_empty_array_none_inequality() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Use NONE to check if no element equals 'python'
// Empty array does not contain 'python' (so NONE is true), plus id=3
let results = tql
.query(&data, "none tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 2);
let ids: Vec<i64> = results.iter().filter_map(|r| r["id"].as_i64()).collect();
assert!(ids.contains(&1));
assert!(ids.contains(&3));
}
#[test]
fn test_empty_array_any_greater_than() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "scores": []}),
json!({"id": 2, "scores": [50, 75]}),
json!({"id": 3, "scores": [25]}),
];
// Using ANY to check if any element > 60
// Empty array has no elements > 60, only id=2 matches
let results = tql
.query(&data, "any scores gt 60")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
#[test]
fn test_empty_array_any_less_than() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "scores": []}),
json!({"id": 2, "scores": [50, 75]}),
json!({"id": 3, "scores": [25]}),
];
// Using ANY to check if any element < 60
// Empty array has no elements < 60, id=2 (50) and id=3 (25) match
let results = tql
.query(&data, "any scores lt 60")
.expect("Query should succeed");
assert_eq!(results.len(), 2);
let ids: Vec<i64> = results.iter().filter_map(|r| r["id"].as_i64()).collect();
assert!(ids.contains(&2));
assert!(ids.contains(&3));
}
#[test]
fn test_empty_array_any_contains() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Using ANY to check if any element contains 'py'
// Empty array does not contain 'py', only id=2 matches
let results = tql
.query(&data, "any tags contains 'py'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
#[test]
fn test_empty_array_any_startswith() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Using ANY to check if any element starts with 'py'
// Empty array has no elements starting with 'py', only id=2 matches
let results = tql
.query(&data, "any tags startswith 'py'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
#[test]
fn test_empty_array_any_endswith() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Using ANY to check if any element ends with 'on'
// Empty array has no elements ending with 'on', only id=2 matches (python)
let results = tql
.query(&data, "any tags endswith 'on'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
// =============================================================================
// Collection Operator Tests with Empty Arrays
// =============================================================================
#[test]
fn test_empty_array_collection_any_operator() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// ANY checks if value is in array - empty array does not contain 'python'
let results = tql
.query(&data, "any tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
#[test]
fn test_empty_array_collection_all_operator() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// ALL checks if all elements equal value
// Empty array: TQL explicitly returns False for empty arrays
let results = tql
.query(&data, "all tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 0);
}
#[test]
fn test_empty_array_collection_none_operator() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// NONE checks if value is NOT in array
// Empty array does not contain 'python', so NONE is true
let results = tql
.query(&data, "none tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 2);
let ids: Vec<i64> = results.iter().filter_map(|r| r["id"].as_i64()).collect();
assert!(ids.contains(&1));
assert!(ids.contains(&3));
}
// =============================================================================
// Existence Tests with Empty Arrays
// =============================================================================
#[test]
fn test_empty_array_exists() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Field exists even if it's an empty array, so all match
let results = tql
.query(&data, "tags exists")
.expect("Query should succeed");
assert_eq!(results.len(), 3);
}
#[test]
fn test_empty_array_not_exists() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3}), // Missing tags field
];
// Only id=3 has missing tags field
let results = tql
.query(&data, "tags not exists")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 3);
}
// =============================================================================
// Multiple Records with Empty Arrays
// =============================================================================
#[test]
fn test_multiple_empty_arrays() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": []}),
json!({"id": 3, "tags": ["python"]}),
];
// Using ANY to check element equality - only id=3 matches
let results = tql
.query(&data, "any tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 3);
}
// =============================================================================
// Compound Expressions with Empty Arrays
// =============================================================================
#[test]
fn test_empty_array_with_and_expression() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// id > 1: records 2, 3
// any tags eq 'python': record 2
// Combined: record 2
let results = tql
.query(&data, "id > 1 AND any tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
#[test]
fn test_empty_array_with_or_expression() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// any tags eq 'python': record 2
// id = 1: record 1
// Combined: records 1 and 2
let results = tql
.query(&data, "any tags eq 'python' OR id = 1")
.expect("Query should succeed");
assert_eq!(results.len(), 2);
let ids: Vec<i64> = results.iter().filter_map(|r| r["id"].as_i64()).collect();
assert!(ids.contains(&1));
assert!(ids.contains(&2));
}
// =============================================================================
// Empty Array with In Operator
// =============================================================================
#[test]
fn test_empty_array_field_with_in_operator() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// Test the reversed 'in' operator with ANY: check if 'python' is in tags array
// Empty array [] does not contain 'python', only id=2 matches
let results = tql
.query(&data, "any tags eq 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
// =============================================================================
// Empty Array with Negated Collection Operators
// =============================================================================
#[test]
fn test_empty_array_none_contains() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// NONE tags contain 'python' - true for empty array and javascript
let results = tql
.query(&data, "none tags contains 'python'")
.expect("Query should succeed");
assert_eq!(results.len(), 2);
let ids: Vec<i64> = results.iter().filter_map(|r| r["id"].as_i64()).collect();
assert!(ids.contains(&1));
assert!(ids.contains(&3));
}
#[test]
fn test_empty_array_none_startswith() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "tags": []}),
json!({"id": 2, "tags": ["python", "rust"]}),
json!({"id": 3, "tags": ["javascript"]}),
];
// NONE tags start with 'py' - true for empty array and javascript
let results = tql
.query(&data, "none tags startswith 'py'")
.expect("Query should succeed");
assert_eq!(results.len(), 2);
let ids: Vec<i64> = results.iter().filter_map(|r| r["id"].as_i64()).collect();
assert!(ids.contains(&1));
assert!(ids.contains(&3));
}
// =============================================================================
// Empty Nested Arrays
// =============================================================================
#[test]
fn test_empty_nested_array() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "data": {"items": []}}),
json!({"id": 2, "data": {"items": ["a", "b"]}}),
];
// Using ANY to check nested array elements
let results = tql
.query(&data, "any data.items eq 'a'")
.expect("Query should succeed");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
// =============================================================================
// Empty Array with Numeric Comparison
// =============================================================================
#[test]
fn test_empty_array_numeric_any_gte_and_lte() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "values": []}),
json!({"id": 2, "values": [5, 15, 25]}),
json!({"id": 3, "values": [50]}),
];
// Note: "any field between X and Y" syntax isn't supported in Rust
// Using combination of gte and lte with AND instead
// Check if any value is >= 10 AND any value is <= 20
// This tests that empty arrays don't match
let results = tql
.query(&data, "any values gte 10 AND any values lte 20")
.expect("Query should succeed");
// id=2 has values [5, 15, 25] - 15 and 25 are >= 10, and 5 and 15 are <= 20
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}
/// `any values in [1, 2]` is REFUSED, and `values in [1, 2]` is the spelling that
/// answers the question it was asking.
///
/// This test used to assert that the evaluator answered `[2]` here. It did — and
/// the translator refused the same query, with the list-operand `TypeError`,
/// because the refusal is on the OPERAND and does not inspect the inner
/// comparison operator. So the clause never worked end to end: the evaluator and
/// the OpenSearch path returned different record sets, which is exactly the split
/// the evaluator refusal closes.
///
/// The `any … in` construct has no translation at all, list operand or not:
/// `any values in [1]` answers `TranslationError("Unsupported collection
/// comparison operator: in")`. And the operator-first form is Rust-only — Python
/// raises `TQLSyntaxError: Expected operator after field 'any'`. There was
/// nothing portable here to preserve.
///
/// `values in [1, 2]` is the portable, translatable spelling, and it answers the
/// same question: OpenSearch matches a multi-valued field when ANY value
/// satisfies the clause, so plain membership over an array already means "any
/// element". Both engines return `[2]` and both emit a `bool.should` of `term`s.
#[test]
fn test_empty_array_numeric_any_in_list() {
let tql = Tql::new();
let data = vec![
json!({"id": 1, "values": []}),
json!({"id": 2, "values": [1, 2, 3]}),
json!({"id": 3, "values": [4, 5]}),
];
let err = tql
.query(&data, "any values in [1, 2]")
.expect_err("a LIST operand to `any` is refused, as the translator refuses it");
assert!(
format!("{err}").contains("list operand"),
"unexpected error: {err}"
);
// The spelling that expresses the intent, in both engines and in the DSL.
let results = tql
.query(&data, "values in [1, 2]")
.expect("plain membership over a multi-valued field still answers");
assert_eq!(results.len(), 1);
assert_eq!(results[0]["id"], 2);
}