fraiseql-core 2.3.2

Core execution engine for FraiseQL v2 - Compiled GraphQL over SQL
Documentation
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
#![allow(clippy::unwrap_used, clippy::panic)] // Reason: test code, panics acceptable
//! Test NULL handling in complex WHERE clause logic.
//!
//! This test verifies that:
//! 1. NULL values in WHERE clauses follow three-valued logic (TRUE, FALSE, UNKNOWN)
//! 2. Complex AND/OR logic with NULLs is handled correctly
//! 3. NULL comparisons don't accidentally match
//! 4. IS NULL and IS NOT NULL operators work correctly
//!
//! # Risk If Missing
//!
//! Without this test:
//! - NULL = `any_value` could incorrectly return TRUE (should be UNKNOWN)
//! - WHERE clauses could have silent bugs with NULL handling
//! - NULL filtering could exclude or include wrong rows

use fraiseql_core::db::where_clause::{WhereClause, WhereOperator};
use serde_json::json;

#[test]
fn test_where_null_equality_is_null() {
    // NULL = NULL should use IS NULL, not =
    let clause = WhereClause::Field {
        path:     vec!["field".to_string()],
        operator: WhereOperator::Eq,
        value:    json!(null),
    };

    match clause {
        WhereClause::Field {
            value, operator, ..
        } => {
            assert_eq!(value, json!(null));
            assert_eq!(operator, WhereOperator::Eq);
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_is_null_operator() {
    // IsNull operator for proper NULL checks
    let clause = WhereClause::Field {
        path:     vec!["optional_field".to_string()],
        operator: WhereOperator::IsNull,
        value:    json!(true), // Typically IsNull takes a boolean flag
    };

    match clause {
        WhereClause::Field {
            value, operator, ..
        } => {
            assert_eq!(value, json!(true));
            assert_eq!(operator, WhereOperator::IsNull);
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_is_not_null_operator() {
    // IsNull operator with false value means IS NOT NULL
    let clause = WhereClause::Field {
        path:     vec!["required_field".to_string()],
        operator: WhereOperator::IsNull,
        value:    json!(false), // false means IS NOT NULL
    };

    match clause {
        WhereClause::Field {
            value, operator, ..
        } => {
            assert_eq!(value, json!(false));
            assert_eq!(operator, WhereOperator::IsNull);
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_complex_and_with_null() {
    // AND logic: (TRUE AND UNKNOWN) = UNKNOWN
    let and_clause = WhereClause::And(vec![
        WhereClause::Field {
            path:     vec!["status".to_string()],
            operator: WhereOperator::Eq,
            value:    json!("active"),
        },
        WhereClause::Field {
            path:     vec!["deleted_at".to_string()],
            operator: WhereOperator::IsNull,
            value:    json!(true),
        },
    ]);

    match and_clause {
        WhereClause::And(clauses) => {
            assert_eq!(clauses.len(), 2);
        },
        _ => panic!("Should be And variant"),
    }
}

#[test]
fn test_where_complex_or_with_null() {
    // OR logic: (FALSE OR UNKNOWN) = UNKNOWN
    let or_clause = WhereClause::Or(vec![
        WhereClause::Field {
            path:     vec!["status".to_string()],
            operator: WhereOperator::Eq,
            value:    json!("inactive"),
        },
        WhereClause::Field {
            path:     vec!["archived_at".to_string()],
            operator: WhereOperator::IsNull,
            value:    json!(false), // IS NOT NULL
        },
    ]);

    match or_clause {
        WhereClause::Or(clauses) => {
            assert_eq!(clauses.len(), 2);
        },
        _ => panic!("Should be Or variant"),
    }
}

#[test]
fn test_where_not_with_null() {
    // NOT logic: NOT UNKNOWN = UNKNOWN
    let not_clause = WhereClause::Not(Box::new(WhereClause::Field {
        path:     vec!["value".to_string()],
        operator: WhereOperator::IsNull,
        value:    json!(true),
    }));

    match not_clause {
        WhereClause::Not(inner) => match *inner {
            WhereClause::Field { operator, .. } => {
                assert_eq!(operator, WhereOperator::IsNull);
            },
            _ => panic!("Inner should be Field variant"),
        },
        _ => panic!("Should be Not variant"),
    }
}

#[test]
fn test_where_null_with_different_operators() {
    // NULL with various operators should all use IS NULL
    let operators = vec![
        WhereOperator::Eq,
        WhereOperator::Neq,
        WhereOperator::Gt,
        WhereOperator::Gte,
        WhereOperator::Lt,
        WhereOperator::Lte,
        WhereOperator::Contains,
    ];

    for op in operators {
        let clause = WhereClause::Field {
            path:     vec!["field".to_string()],
            operator: op.clone(),
            value:    json!(null),
        };

        match clause {
            WhereClause::Field { value, .. } => {
                // All should preserve the null value
                assert_eq!(value, json!(null));
            },
            _ => panic!("Should be Field variant"),
        }
    }
}

#[test]
fn test_where_null_in_nested_paths() {
    // NULL comparisons in nested JSON paths
    let nested_clause = WhereClause::Field {
        path:     vec![
            "user".to_string(),
            "profile".to_string(),
            "middle_name".to_string(),
        ],
        operator: WhereOperator::IsNull,
        value:    json!(true),
    };

    match nested_clause {
        WhereClause::Field { path, value, .. } => {
            assert_eq!(path.len(), 3);
            assert_eq!(path[0], "user");
            assert_eq!(path[1], "profile");
            assert_eq!(path[2], "middle_name");
            assert_eq!(value, json!(true));
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_null_with_array_operators() {
    // NULL values in array operators
    let array_clause = WhereClause::Field {
        path:     vec!["tags".to_string()],
        operator: WhereOperator::In,
        value:    json!([1, 2, null, 4]),
    };

    match array_clause {
        WhereClause::Field { value, .. } => {
            let arr = value.as_array().unwrap();
            assert_eq!(arr.len(), 4);
            assert!(arr[2].is_null());
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_null_three_valued_logic_and() {
    // Test three-valued logic for AND operator
    let test_cases = vec![
        // (left_is_true, right_is_null, and_result_description)
        (true, true, "TRUE AND UNKNOWN = UNKNOWN"),
        (true, false, "TRUE AND FALSE = FALSE"),
        (false, true, "FALSE AND UNKNOWN = FALSE"), // Short-circuit
        (false, false, "FALSE AND FALSE = FALSE"),
    ];

    for (left_true, right_has_null, _description) in test_cases {
        let left_value = if left_true {
            json!("active")
        } else {
            json!("inactive")
        };

        let right_value = if right_has_null {
            json!(null)
        } else {
            json!(true)
        };

        let and_clause = WhereClause::And(vec![
            WhereClause::Field {
                path:     vec!["status".to_string()],
                operator: WhereOperator::Eq,
                value:    left_value,
            },
            WhereClause::Field {
                path:     vec!["deleted_at".to_string()],
                operator: WhereOperator::IsNull,
                value:    right_value,
            },
        ]);

        match and_clause {
            WhereClause::And(clauses) => {
                assert_eq!(clauses.len(), 2);
            },
            _ => panic!("Should be And variant"),
        }
    }
}

#[test]
fn test_where_null_three_valued_logic_or() {
    // Test three-valued logic for OR operator
    let test_cases = vec![
        // (left_is_true, right_is_null, or_result_description)
        (true, true, "TRUE OR UNKNOWN = TRUE"), // Short-circuit
        (true, false, "TRUE OR FALSE = TRUE"),  // Short-circuit
        (false, true, "FALSE OR UNKNOWN = UNKNOWN"),
        (false, false, "FALSE OR FALSE = FALSE"),
    ];

    for (left_true, right_has_null, _description) in test_cases {
        let left_value = if left_true {
            json!("active")
        } else {
            json!("inactive")
        };

        let right_value = if right_has_null {
            json!(null)
        } else {
            json!(true)
        };

        let or_clause = WhereClause::Or(vec![
            WhereClause::Field {
                path:     vec!["status".to_string()],
                operator: WhereOperator::Eq,
                value:    left_value,
            },
            WhereClause::Field {
                path:     vec!["deleted_at".to_string()],
                operator: WhereOperator::IsNull,
                value:    right_value,
            },
        ]);

        match or_clause {
            WhereClause::Or(clauses) => {
                assert_eq!(clauses.len(), 2);
            },
            _ => panic!("Should be Or variant"),
        }
    }
}

#[test]
fn test_where_null_not_in_operator() {
    // NOT IN with NULL values
    let nin_clause = WhereClause::Field {
        path:     vec!["status".to_string()],
        operator: WhereOperator::Nin,
        value:    json!(["deleted", "archived", null]),
    };

    match nin_clause {
        WhereClause::Field { value, .. } => {
            let arr = value.as_array().unwrap();
            assert_eq!(arr.len(), 3);
            assert!(arr[2].is_null());
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_null_comparison_null_handling() {
    // Verify NULL is distinct from false/0/empty string
    let clauses = vec![
        (json!(null), "null value"),
        (json!(false), "false value"),
        (json!(0), "zero value"),
        (json!(""), "empty string"),
    ];

    for (value, description) in clauses {
        let clause = WhereClause::Field {
            path:     vec!["field".to_string()],
            operator: WhereOperator::Eq,
            value:    value.clone(),
        };

        match clause {
            WhereClause::Field { value: v, .. } => {
                assert_eq!(v, value, "{} should be preserved exactly", description);
            },
            _ => panic!("Should be Field variant"),
        }
    }

    // Verify they're all different
    assert_ne!(json!(null), json!(false));
    assert_ne!(json!(null), json!(0));
    assert_ne!(json!(null), json!(""));
    assert_ne!(json!(false), json!(0));
}

#[test]
fn test_where_null_in_complex_nested_logic() {
    // Complex nested AND/OR with multiple NULLs
    let complex_clause = WhereClause::And(vec![
        WhereClause::Or(vec![
            WhereClause::Field {
                path:     vec!["status".to_string()],
                operator: WhereOperator::Eq,
                value:    json!("active"),
            },
            WhereClause::Field {
                path:     vec!["trial_expires".to_string()],
                operator: WhereOperator::IsNull,
                value:    json!(false),
            },
        ]),
        WhereClause::And(vec![
            WhereClause::Field {
                path:     vec!["deleted_at".to_string()],
                operator: WhereOperator::IsNull,
                value:    json!(true),
            },
            WhereClause::Field {
                path:     vec!["archived_at".to_string()],
                operator: WhereOperator::IsNull,
                value:    json!(true),
            },
        ]),
    ]);

    match complex_clause {
        WhereClause::And(outer_clauses) => {
            assert_eq!(outer_clauses.len(), 2);
            match &outer_clauses[0] {
                WhereClause::Or(inner_or) => assert_eq!(inner_or.len(), 2),
                _ => panic!("First should be Or"),
            }
            match &outer_clauses[1] {
                WhereClause::And(inner_and) => assert_eq!(inner_and.len(), 2),
                _ => panic!("Second should be And"),
            }
        },
        _ => panic!("Should be And variant"),
    }
}