radixdb-storage 1.1.0

Storage contracts and physical persistence engine for RadixDB
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
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
// Copyright 2026 RadixDB Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Function expression for evaluating scalar functions in WHERE clauses
//!
//! This module provides `FunctionExpr` which wraps a scalar function call
//! and evaluates it as a boolean expression. This enables queries like:
//!
//! ```sql
//! SELECT * FROM users WHERE UPPER(name) = 'ALICE'
//! SELECT * FROM products WHERE LENGTH(description) > 100
//! ```

use std::any::Any;
use std::fmt::{self, Debug};

use rustc_hash::FxHashMap;
use std::sync::Arc;

use super::{find_column_index, resolve_alias, Expression};
use radixdb_core::{Operator, Result, Row, Schema, Value};

/// Lower-layer callable contract used by storage predicates.
///
/// Registry metadata, volatility policy and SQL name resolution remain above
/// storage; this port only evaluates already-bound scalar arguments.
pub trait ScalarEvaluator: Send + Sync {
    fn name(&self) -> &str;
    fn evaluate(&self, args: &[Value]) -> Result<Value>;
}

/// Expression that evaluates a scalar function and compares the result
///
/// This expression wraps a scalar function call with its arguments,
/// evaluates the function for each row, and compares the result to
/// a target value using a comparison operator.
///
/// # Example
///
/// For `UPPER(name) = 'ALICE'`:
/// - function: UPPER
/// - arguments: [ColumnArg("name")]
/// - operator: Eq
/// - compare_value: Text("ALICE")
pub struct FunctionExpr {
    /// The scalar function to call
    function: Arc<dyn ScalarEvaluator>,
    /// Arguments to pass to the function
    arguments: Vec<FunctionArg>,
    /// Comparison operator
    operator: Operator,
    /// Value to compare the function result against
    compare_value: Value,
    /// Pre-computed column indices for arguments
    arg_bindings: Vec<FunctionArgBinding>,
    /// Whether this expression has been prepared
    prepared: bool,
}

#[derive(Clone, Debug)]
enum FunctionArgBinding {
    Column(Option<usize>),
    Literal,
    Function(Vec<FunctionArgBinding>),
}

impl FunctionArgBinding {
    fn unbound(arg: &FunctionArg) -> Self {
        match arg {
            FunctionArg::Column(_) => Self::Column(None),
            FunctionArg::Literal(_) => Self::Literal,
            FunctionArg::Function { arguments, .. } => {
                Self::Function(arguments.iter().map(Self::unbound).collect())
            }
        }
    }

    fn bind(arg: &FunctionArg, schema: &Schema) -> Self {
        match arg {
            FunctionArg::Column(column) => Self::Column(find_column_index(schema, column)),
            FunctionArg::Literal(_) => Self::Literal,
            FunctionArg::Function { arguments, .. } => Self::Function(
                arguments
                    .iter()
                    .map(|arg| Self::bind(arg, schema))
                    .collect(),
            ),
        }
    }
}

// Thread-local buffer for function arguments (avoids allocation per row)
thread_local! {
    static ARG_BUFFER: std::cell::RefCell<Vec<Value>> = std::cell::RefCell::new(Vec::with_capacity(4));
}

/// Argument to a function in a FunctionExpr
#[derive(Clone)]
pub enum FunctionArg {
    /// A column reference
    Column(String),
    /// A literal value
    Literal(Value),
    /// A nested function call (for function composition)
    Function {
        function: Arc<dyn ScalarEvaluator>,
        arguments: Vec<FunctionArg>,
    },
}

impl Debug for FunctionArg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FunctionArg::Column(col) => f.debug_tuple("Column").field(col).finish(),
            FunctionArg::Literal(val) => f.debug_tuple("Literal").field(val).finish(),
            FunctionArg::Function {
                function,
                arguments,
            } => f
                .debug_struct("Function")
                .field("name", &function.name())
                .field("arguments", arguments)
                .finish(),
        }
    }
}

impl FunctionArg {
    fn with_aliases(&self, aliases: &FxHashMap<String, String>) -> Self {
        match self {
            Self::Column(column) => Self::Column(resolve_alias(column, aliases).to_string()),
            Self::Literal(value) => Self::Literal(value.clone()),
            Self::Function {
                function,
                arguments,
            } => Self::Function {
                function: Arc::clone(function),
                arguments: arguments
                    .iter()
                    .map(|argument| argument.with_aliases(aliases))
                    .collect(),
            },
        }
    }
}

impl FunctionExpr {
    /// Create a new function expression
    ///
    /// # Arguments
    /// * `function` - The scalar function to call
    /// * `arguments` - Arguments to pass to the function
    /// * `operator` - Comparison operator
    /// * `compare_value` - Value to compare the function result against
    pub fn new(
        function: Arc<dyn ScalarEvaluator>,
        arguments: Vec<FunctionArg>,
        operator: Operator,
        compare_value: Value,
    ) -> Self {
        let arg_bindings = arguments.iter().map(FunctionArgBinding::unbound).collect();
        Self {
            function,
            arguments,
            operator,
            compare_value,
            arg_bindings,
            prepared: false,
        }
    }

    /// Create a function expression for equality comparison
    pub fn eq(
        function: Arc<dyn ScalarEvaluator>,
        arguments: Vec<FunctionArg>,
        compare_value: Value,
    ) -> Self {
        Self::new(function, arguments, Operator::Eq, compare_value)
    }

    /// Create a function expression that evaluates to boolean (no comparison)
    ///
    /// This is for functions that return boolean directly, like custom predicates
    pub fn boolean(function: Arc<dyn ScalarEvaluator>, arguments: Vec<FunctionArg>) -> Self {
        Self::new(function, arguments, Operator::Eq, Value::Boolean(true))
    }

    /// Get the function name
    pub fn function_name(&self) -> &str {
        self.function.name()
    }

    /// Get the arguments
    pub fn get_arguments(&self) -> &[FunctionArg] {
        &self.arguments
    }

    /// Get the operator
    pub fn get_operator(&self) -> Operator {
        self.operator
    }

    /// Get the compare value
    pub fn get_compare_value(&self) -> &Value {
        &self.compare_value
    }

    /// Evaluate a function argument to get its value
    #[allow(clippy::only_used_in_recursion)]
    fn evaluate_arg(
        &self,
        arg: &FunctionArg,
        binding: &FunctionArgBinding,
        row: &Row,
    ) -> Result<Value> {
        match (arg, binding) {
            (FunctionArg::Column(col_name), FunctionArgBinding::Column(arg_index)) => {
                if let Some(idx) = *arg_index {
                    Ok(row.get(idx).cloned().unwrap_or_else(Value::null_unknown))
                } else {
                    // Fallback: try to find column by name (shouldn't happen if prepared)
                    Err(radixdb_core::Error::ColumnNotFound(col_name.to_string()))
                }
            }
            (FunctionArg::Literal(value), FunctionArgBinding::Literal) => Ok(value.clone()),
            (
                FunctionArg::Function {
                    function,
                    arguments,
                },
                FunctionArgBinding::Function(bindings),
            ) => {
                // Recursively evaluate nested function
                let args: Result<Vec<Value>> = arguments
                    .iter()
                    .zip(bindings)
                    .map(|(arg, binding)| self.evaluate_arg(arg, binding, row))
                    .collect();
                function.evaluate(&args?)
            }
            _ => Err(radixdb_core::Error::InvalidArgument(
                "function argument binding shape mismatch".to_string(),
            )),
        }
    }

    fn evaluate_function(&self, row: &Row) -> Result<Value> {
        if self.arguments.len() == 1 {
            if let FunctionArg::Column(_) = &self.arguments[0] {
                if let Some(FunctionArgBinding::Column(Some(idx))) = self.arg_bindings.first() {
                    let value = row.get(*idx).cloned().unwrap_or_else(Value::null_unknown);
                    return self.function.evaluate(std::slice::from_ref(&value));
                }
            }
        }

        ARG_BUFFER.with(|buf_cell| {
            let mut arg_values = buf_cell.borrow_mut();
            arg_values.clear();
            for (index, arg) in self.arguments.iter().enumerate() {
                let binding = self.arg_bindings.get(index).ok_or_else(|| {
                    radixdb_core::Error::InvalidArgument(
                        "missing function argument binding".to_string(),
                    )
                })?;
                arg_values.push(self.evaluate_arg(arg, binding, row)?);
            }
            self.function.evaluate(&arg_values)
        })
    }

    /// Compare using SQL NULL and canonical scalar semantics.
    fn compare(&self, result: &Value, target: &Value) -> Result<Option<bool>> {
        if result.is_null() || target.is_null() {
            return Ok(None);
        }
        let ordering = result.compare(target)?;
        Ok(Some(match self.operator {
            Operator::Eq => ordering == std::cmp::Ordering::Equal,
            Operator::Ne => ordering != std::cmp::Ordering::Equal,
            Operator::Lt => ordering == std::cmp::Ordering::Less,
            Operator::Lte => ordering != std::cmp::Ordering::Greater,
            Operator::Gt => ordering == std::cmp::Ordering::Greater,
            Operator::Gte => ordering != std::cmp::Ordering::Less,
            _ => false,
        }))
    }
}

impl Debug for FunctionExpr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FunctionExpr")
            .field("function", &self.function.name())
            .field("arguments", &self.arguments)
            .field("operator", &self.operator)
            .field("compare_value", &self.compare_value)
            .field("prepared", &self.prepared)
            .finish()
    }
}

impl Expression for FunctionExpr {
    fn evaluate(&self, row: &Row) -> Result<bool> {
        let result = self.evaluate_function(row)?;
        Ok(self.compare(&result, &self.compare_value)?.unwrap_or(false))
    }

    fn evaluate_fast(&self, row: &Row) -> bool {
        self.evaluate(row).unwrap_or(false)
    }

    fn with_aliases(&self, aliases: &FxHashMap<String, String>) -> Box<dyn Expression> {
        let new_arguments: Vec<FunctionArg> = self
            .arguments
            .iter()
            .map(|arg| arg.with_aliases(aliases))
            .collect();
        let arg_bindings = new_arguments
            .iter()
            .map(FunctionArgBinding::unbound)
            .collect();

        Box::new(FunctionExpr {
            function: Arc::clone(&self.function),
            arguments: new_arguments,
            operator: self.operator,
            compare_value: self.compare_value.clone(),
            arg_bindings,
            prepared: false,
        })
    }

    fn prepare_for_schema(&mut self, schema: &Schema) {
        self.arg_bindings = self
            .arguments
            .iter()
            .map(|arg| FunctionArgBinding::bind(arg, schema))
            .collect();
        self.prepared = true;
    }

    fn is_prepared(&self) -> bool {
        self.prepared
    }

    fn can_use_index(&self) -> bool {
        // Function expressions generally can't use indexes
        // unless we have function-based indexes (future optimization)
        false
    }

    fn clone_box(&self) -> Box<dyn Expression> {
        Box::new(FunctionExpr {
            function: Arc::clone(&self.function),
            arguments: self.arguments.clone(),
            operator: self.operator,
            compare_value: self.compare_value.clone(),
            arg_bindings: self.arg_bindings.clone(),
            prepared: self.prepared,
        })
    }

    fn is_unknown_due_to_null(&self, row: &Row) -> bool {
        self.compare_value.is_null()
            || self
                .evaluate_function(row)
                .ok()
                .is_some_and(|value| value.is_null())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Expression that wraps a closure for dynamic evaluation
///
/// This is useful for testing or when you need a custom predicate
/// that doesn't fit the standard expression types.
pub struct EvalExpr {
    /// The evaluation function
    eval_fn: Arc<dyn Fn(&Row) -> bool + Send + Sync>,
}

impl EvalExpr {
    /// Create a new eval expression from a closure
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(&Row) -> bool + Send + Sync + 'static,
    {
        Self {
            eval_fn: Arc::new(f),
        }
    }
}

impl Debug for EvalExpr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EvalExpr").finish()
    }
}

impl Expression for EvalExpr {
    fn evaluate(&self, row: &Row) -> Result<bool> {
        Ok((self.eval_fn)(row))
    }

    fn evaluate_fast(&self, row: &Row) -> bool {
        (self.eval_fn)(row)
    }

    fn with_aliases(&self, _aliases: &FxHashMap<String, String>) -> Box<dyn Expression> {
        self.clone_box()
    }

    fn prepare_for_schema(&mut self, _schema: &Schema) {
        // Nothing to prepare for closures
    }

    fn is_prepared(&self) -> bool {
        true // Always "prepared"
    }

    fn clone_box(&self) -> Box<dyn Expression> {
        Box::new(Self {
            eval_fn: Arc::clone(&self.eval_fn),
        })
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use radixdb_core::{DataType, SchemaBuilder};

    fn test_schema() -> Schema {
        SchemaBuilder::new("test")
            .add_primary_key("id", DataType::Integer)
            .add("name", DataType::Text)
            .add("age", DataType::Integer)
            .build()
    }

    // Helper function for testing - LENGTH function
    struct TestLengthFn;

    impl ScalarEvaluator for TestLengthFn {
        fn name(&self) -> &str {
            "LENGTH"
        }

        fn evaluate(&self, args: &[Value]) -> Result<Value> {
            match args.first() {
                Some(Value::Text(s)) => Ok(Value::Integer(s.len() as i64)),
                Some(Value::Null(_)) => Ok(Value::null_unknown()),
                _ => Ok(Value::Integer(0)),
            }
        }
    }

    struct TestIdentityFn;

    impl ScalarEvaluator for TestIdentityFn {
        fn name(&self) -> &str {
            "TEST_IDENTITY"
        }

        fn evaluate(&self, args: &[Value]) -> Result<Value> {
            Ok(args.first().cloned().unwrap_or_else(Value::null_unknown))
        }
    }

    struct TestUpperFn;

    impl ScalarEvaluator for TestUpperFn {
        fn name(&self) -> &str {
            "UPPER"
        }

        fn evaluate(&self, args: &[Value]) -> Result<Value> {
            match args.first() {
                Some(Value::Text(value)) => Ok(Value::text(value.to_uppercase())),
                Some(Value::Null(_)) => Ok(Value::null_unknown()),
                _ => Err(radixdb_core::Error::Type(
                    "UPPER test evaluator expects text".to_string(),
                )),
            }
        }
    }

    #[test]
    fn test_function_expr_upper() {
        let schema = test_schema();
        let upper_fn = Arc::new(TestUpperFn);

        let mut expr = FunctionExpr::eq(
            upper_fn,
            vec![FunctionArg::Column("name".to_string())],
            Value::text("ALICE"),
        );
        expr.prepare_for_schema(&schema);

        // Row with name = "alice" should match UPPER(name) = 'ALICE'
        let row1 = Row::from_values(vec![
            Value::Integer(1),
            Value::text("alice"),
            Value::Integer(30),
        ]);
        assert!(expr.evaluate(&row1).unwrap());

        // Row with name = "Alice" should also match
        let row2 = Row::from_values(vec![
            Value::Integer(2),
            Value::text("Alice"),
            Value::Integer(25),
        ]);
        assert!(expr.evaluate(&row2).unwrap());

        // Row with name = "bob" should not match
        let row3 = Row::from_values(vec![
            Value::Integer(3),
            Value::text("bob"),
            Value::Integer(35),
        ]);
        assert!(!expr.evaluate(&row3).unwrap());
    }

    #[test]
    fn test_function_expr_with_literal() {
        let schema = test_schema();
        let upper_fn = Arc::new(TestUpperFn);

        let mut expr = FunctionExpr::eq(
            upper_fn,
            vec![FunctionArg::Literal(Value::text("hello"))],
            Value::text("HELLO"),
        );
        expr.prepare_for_schema(&schema);

        // Should always match since it's comparing literals
        let row = Row::from_values(vec![
            Value::Integer(1),
            Value::text("anything"),
            Value::Integer(30),
        ]);
        assert!(expr.evaluate(&row).unwrap());
    }

    #[test]
    fn test_function_expr_operators() {
        let schema = test_schema();

        let length_fn = Arc::new(TestLengthFn);

        // Test LENGTH(name) > 3
        let mut expr = FunctionExpr::new(
            length_fn,
            vec![FunctionArg::Column("name".to_string())],
            Operator::Gt,
            Value::Integer(3),
        );
        expr.prepare_for_schema(&schema);

        // "alice" has length 5 > 3
        let row1 = Row::from_values(vec![
            Value::Integer(1),
            Value::text("alice"),
            Value::Integer(30),
        ]);
        assert!(expr.evaluate(&row1).unwrap());

        // "bob" has length 3, not > 3
        let row2 = Row::from_values(vec![
            Value::Integer(2),
            Value::text("bob"),
            Value::Integer(25),
        ]);
        assert!(!expr.evaluate(&row2).unwrap());
    }

    #[test]
    fn test_function_expr_mixed_numeric_identity_at_f64_boundary() {
        let schema = test_schema();
        let identity_fn = Arc::new(TestIdentityFn);
        let boundary = 1_i64 << 53;
        let row = Row::new();

        let mut exact_eq = FunctionExpr::eq(
            identity_fn.clone(),
            vec![FunctionArg::Literal(Value::Integer(boundary))],
            Value::Float(boundary as f64),
        );
        exact_eq.prepare_for_schema(&schema);
        assert!(exact_eq.evaluate(&row).unwrap());

        let mut rounded_eq = FunctionExpr::new(
            identity_fn.clone(),
            vec![FunctionArg::Literal(Value::Integer(boundary + 1))],
            Operator::Eq,
            Value::Float(boundary as f64),
        );
        rounded_eq.prepare_for_schema(&schema);
        assert!(!rounded_eq.evaluate(&row).unwrap());

        let mut rounded_gt = FunctionExpr::new(
            identity_fn,
            vec![FunctionArg::Literal(Value::Integer(boundary + 1))],
            Operator::Gt,
            Value::Float(boundary as f64),
        );
        rounded_gt.prepare_for_schema(&schema);
        assert!(rounded_gt.evaluate(&row).unwrap());
    }

    #[test]
    fn test_eval_expr() {
        let expr = EvalExpr::new(|row| {
            // Return true if first column is Integer > 5
            match row.get(0) {
                Some(Value::Integer(n)) => *n > 5,
                _ => false,
            }
        });

        let row1 = Row::from_values(vec![Value::Integer(10)]);
        assert!(expr.evaluate(&row1).unwrap());

        let row2 = Row::from_values(vec![Value::Integer(3)]);
        assert!(!expr.evaluate(&row2).unwrap());
    }

    #[test]
    fn test_function_expr_clone() {
        let upper_fn = Arc::new(TestUpperFn);

        let expr = FunctionExpr::eq(
            upper_fn,
            vec![FunctionArg::Column("name".to_string())],
            Value::text("ALICE"),
        );

        let cloned = expr.clone_box();
        assert!(format!("{:?}", cloned).contains("FunctionExpr"));
    }

    #[test]
    fn function_comparison_preserves_sql_null_unknown() {
        let schema = test_schema();
        let mut expr = FunctionExpr::eq(
            Arc::new(TestIdentityFn),
            vec![FunctionArg::Column("name".to_string())],
            Value::text("ALICE"),
        );
        expr.prepare_for_schema(&schema);
        let row = Row::from_values(vec![
            Value::integer(1),
            Value::null(DataType::Text),
            Value::integer(30),
        ]);
        assert!(!expr.evaluate(&row).unwrap());
        assert!(!expr.evaluate_fast(&row));
        assert!(expr.is_unknown_due_to_null(&row));

        let mut null_target = FunctionExpr::eq(
            Arc::new(TestIdentityFn),
            vec![FunctionArg::Literal(Value::integer(1))],
            Value::null(DataType::Integer),
        );
        null_target.prepare_for_schema(&schema);
        assert!(!null_target.evaluate(&Row::new()).unwrap());
        assert!(null_target.is_unknown_due_to_null(&Row::new()));
    }

    #[test]
    fn eval_expr_lifecycle_is_total() {
        let expr: Box<dyn Expression> = Box::new(EvalExpr::new(|row| !row.is_empty()));
        let cloned = expr.clone();
        let aliased = cloned.with_aliases(&FxHashMap::default());
        assert!(aliased.as_any().is::<EvalExpr>());
        assert!(aliased
            .evaluate(&Row::from_values(vec![Value::integer(1)]))
            .unwrap());
    }
}