nodedb-lite 0.1.0

Embedded edge client for NodeDB — offline vector search, graph traversal, and CRDT sync
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
// SPDX-License-Identifier: Apache-2.0

//! Convert `nodedb_sql::types_expr::SqlExpr` to `nodedb_query::expr::types::SqlExpr`
//! for WHERE predicates, sort keys, projections and assignments.
//!
//! The evaluator has no `IN`, `BETWEEN`, `LIKE` or array-construct variant.
//! Each is lowered the way Origin's planner lowers it, so an expression
//! evaluates the same on both.

use nodedb_query::expr::types::{BinaryOp as QBinaryOp, CastType, SqlExpr as QExpr};
use nodedb_sql::types_expr::{BinaryOp as SBinaryOp, SqlExpr as SExpr, UnaryOp};

use crate::error::LiteError;
use crate::query::filter_convert::sql_value_to_value;

/// Convert a SQL-side expression to a query-side expression.
///
/// Lowerings, as on Origin:
/// - `e IN (a, b)` is `e = a OR e = b`, and `NOT IN` is `e <> a AND e <> b`.
///   An empty list is `false`, or `true` for `NOT IN`.
/// - `e BETWEEN lo AND hi` is `e >= lo AND e <= hi`, and `NOT BETWEEN` is
///   `e < lo OR e > hi`.
/// - `e LIKE p` calls `like(e, p)`, or `ilike` when case-insensitive.
///   `NOT LIKE` negates the call.
/// - An array of literals is one array literal. An array with any other
///   element calls `make_array`, which builds the array per row.
///
/// A subquery or a wildcard has no evaluator form and is a `BadRequest`.
pub(crate) fn convert_sql_expr(expr: &SExpr) -> Result<QExpr, LiteError> {
    convert(expr, &[])
}

/// [`convert_sql_expr`] for an expression that reads a joined source row.
///
/// A column qualified by any name in `source_refs` converts to
/// `Column("<source_refs[0]>.<name>")`, the key a joined row gives that
/// source field, as Origin's qualified conversion does. Any other qualifier
/// names the target and is dropped.
pub(crate) fn convert_sql_expr_with_source(
    expr: &SExpr,
    source_refs: &[String],
) -> Result<QExpr, LiteError> {
    convert(expr, source_refs)
}

fn convert(expr: &SExpr, source_refs: &[String]) -> Result<QExpr, LiteError> {
    match expr {
        // `EXCLUDED.col` (`INSERT ... ON CONFLICT DO UPDATE`) resolves
        // against the incoming row via `eval_with_excluded`, mirroring
        // Origin's `sql_expr_to_bridge_expr`.
        SExpr::Column { table, name }
            if table
                .as_deref()
                .is_some_and(|t| t.eq_ignore_ascii_case("excluded")) =>
        {
            Ok(QExpr::ExcludedColumn(name.clone()))
        }
        SExpr::Column {
            table: Some(table),
            name,
        } if source_refs.iter().any(|r| r.eq_ignore_ascii_case(table)) => {
            let alias = source_refs.first().map_or(table.as_str(), String::as_str);
            Ok(QExpr::Column(format!("{alias}.{name}")))
        }
        SExpr::Column { name, .. } => Ok(QExpr::Column(name.clone())),

        SExpr::Literal(v) => {
            let val = sql_value_to_value(v)?;
            Ok(QExpr::Literal(val))
        }

        SExpr::BinaryOp { left, op, right } => {
            let ql = convert(left, source_refs)?;
            let qr = convert(right, source_refs)?;
            let qop = convert_binary_op(*op)?;
            Ok(QExpr::BinaryOp {
                left: Box::new(ql),
                op: qop,
                right: Box::new(qr),
            })
        }

        SExpr::UnaryOp { op, expr } => {
            let inner = convert(expr, source_refs)?;
            match op {
                UnaryOp::Neg => Ok(QExpr::Negate(Box::new(inner))),
                UnaryOp::Not => Ok(QExpr::BinaryOp {
                    left: Box::new(inner),
                    op: QBinaryOp::Eq,
                    right: Box::new(QExpr::Literal(nodedb_types::Value::Bool(false))),
                }),
            }
        }

        SExpr::Function { name, args, .. } => {
            let qargs: Result<Vec<QExpr>, LiteError> =
                args.iter().map(|a| convert(a, source_refs)).collect();
            Ok(QExpr::Function {
                name: name.clone(),
                args: qargs?,
            })
        }

        SExpr::Case {
            operand,
            when_then,
            else_expr,
        } => {
            let qoperand = operand
                .as_ref()
                .map(|e| convert(e, source_refs).map(Box::new))
                .transpose()?;
            let qwhen: Result<Vec<(QExpr, QExpr)>, LiteError> = when_then
                .iter()
                .map(|(cond, val)| Ok((convert(cond, source_refs)?, convert(val, source_refs)?)))
                .collect();
            let qelse = else_expr
                .as_ref()
                .map(|e| convert(e, source_refs).map(Box::new))
                .transpose()?;
            Ok(QExpr::Case {
                operand: qoperand,
                when_thens: qwhen?,
                else_expr: qelse,
            })
        }

        SExpr::Cast { expr, to_type } => {
            let inner = convert(expr, source_refs)?;
            let ct = convert_cast_type(to_type)?;
            Ok(QExpr::Cast {
                expr: Box::new(inner),
                to_type: ct,
            })
        }

        SExpr::IsNull { expr, negated } => {
            let inner = convert(expr, source_refs)?;
            Ok(QExpr::IsNull {
                expr: Box::new(inner),
                negated: *negated,
            })
        }

        SExpr::InList {
            expr,
            list,
            negated,
        } => {
            let target = convert(expr, source_refs)?;
            let (eq_op, combine_op) = if *negated {
                (QBinaryOp::NotEq, QBinaryOp::And)
            } else {
                (QBinaryOp::Eq, QBinaryOp::Or)
            };
            let mut lowered: Option<QExpr> = None;
            for item in list {
                let test = QExpr::BinaryOp {
                    left: Box::new(target.clone()),
                    op: eq_op,
                    right: Box::new(convert(item, source_refs)?),
                };
                lowered = Some(match lowered {
                    None => test,
                    Some(acc) => QExpr::BinaryOp {
                        left: Box::new(acc),
                        op: combine_op,
                        right: Box::new(test),
                    },
                });
            }
            Ok(lowered.unwrap_or(QExpr::Literal(nodedb_types::Value::Bool(*negated))))
        }

        SExpr::Between {
            expr,
            low,
            high,
            negated,
        } => {
            let e = convert(expr, source_refs)?;
            let lo = convert(low, source_refs)?;
            let hi = convert(high, source_refs)?;
            let (low_op, high_op, combine_op) = if *negated {
                (QBinaryOp::Lt, QBinaryOp::Gt, QBinaryOp::Or)
            } else {
                (QBinaryOp::GtEq, QBinaryOp::LtEq, QBinaryOp::And)
            };
            Ok(QExpr::BinaryOp {
                left: Box::new(QExpr::BinaryOp {
                    left: Box::new(e.clone()),
                    op: low_op,
                    right: Box::new(lo),
                }),
                op: combine_op,
                right: Box::new(QExpr::BinaryOp {
                    left: Box::new(e),
                    op: high_op,
                    right: Box::new(hi),
                }),
            })
        }

        SExpr::Like {
            expr,
            pattern,
            negated,
            case_insensitive,
        } => {
            let name = if *case_insensitive { "ilike" } else { "like" };
            let call = QExpr::Function {
                name: name.into(),
                args: vec![convert(expr, source_refs)?, convert(pattern, source_refs)?],
            };
            Ok(if *negated {
                QExpr::Negate(Box::new(call))
            } else {
                call
            })
        }

        SExpr::ArrayLiteral(elems) => {
            let lowered = elems
                .iter()
                .map(|e| convert(e, source_refs))
                .collect::<Result<Vec<QExpr>, LiteError>>()?;
            let literals: Option<Vec<nodedb_types::Value>> = lowered
                .iter()
                .map(|elem| {
                    if let QExpr::Literal(v) = elem {
                        Some(v.clone())
                    } else {
                        None
                    }
                })
                .collect();
            Ok(match literals {
                Some(values) => QExpr::Literal(nodedb_types::Value::Array(values)),
                None => QExpr::Function {
                    name: "make_array".into(),
                    args: lowered,
                },
            })
        }

        SExpr::Subquery(_) => Err(LiteError::BadRequest {
            detail: "a subquery has no expression form here".to_string(),
        }),

        SExpr::Wildcard => Err(LiteError::BadRequest {
            detail: "a wildcard has no expression form here".to_string(),
        }),
    }
}

fn convert_binary_op(op: SBinaryOp) -> Result<QBinaryOp, LiteError> {
    Ok(match op {
        SBinaryOp::Add => QBinaryOp::Add,
        SBinaryOp::Sub => QBinaryOp::Sub,
        SBinaryOp::Mul => QBinaryOp::Mul,
        SBinaryOp::Div => QBinaryOp::Div,
        SBinaryOp::Mod => QBinaryOp::Mod,
        SBinaryOp::Eq => QBinaryOp::Eq,
        SBinaryOp::Ne => QBinaryOp::NotEq,
        SBinaryOp::Gt => QBinaryOp::Gt,
        SBinaryOp::Ge => QBinaryOp::GtEq,
        SBinaryOp::Lt => QBinaryOp::Lt,
        SBinaryOp::Le => QBinaryOp::LtEq,
        SBinaryOp::And => QBinaryOp::And,
        SBinaryOp::Or => QBinaryOp::Or,
        SBinaryOp::Concat => QBinaryOp::Concat,
    })
}

fn convert_cast_type(to_type: &str) -> Result<CastType, LiteError> {
    match to_type.to_uppercase().as_str() {
        "INT" | "INT64" | "INTEGER" | "BIGINT" => Ok(CastType::Int),
        "FLOAT" | "FLOAT64" | "DOUBLE" | "REAL" | "NUMERIC" | "DECIMAL" => Ok(CastType::Float),
        "TEXT" | "STRING" | "VARCHAR" | "CHAR" => Ok(CastType::String),
        "BOOL" | "BOOLEAN" => Ok(CastType::Bool),
        other => Err(LiteError::BadRequest {
            detail: format!("CAST to type '{other}' is not supported"),
        }),
    }
}

#[cfg(test)]
mod tests {
    use nodedb_sql::types_expr::SqlValue;
    use nodedb_types::Value;

    use super::*;

    fn col(name: &str) -> SExpr {
        SExpr::Column {
            table: None,
            name: name.into(),
        }
    }

    fn lit(v: i64) -> SExpr {
        SExpr::Literal(SqlValue::Int(v))
    }

    fn eval(expr: &SExpr, n: i64) -> Value {
        let doc = Value::Object(std::collections::HashMap::from([
            ("n".to_string(), Value::Integer(n)),
            ("s".to_string(), Value::String("alpha".into())),
        ]));
        convert_sql_expr(expr)
            .expect("lower")
            .eval(&doc)
            .expect("eval")
    }

    #[test]
    fn in_lowers_to_an_or_of_equalities() {
        let expr = SExpr::InList {
            expr: Box::new(col("n")),
            list: vec![lit(1), lit(3)],
            negated: false,
        };
        assert_eq!(eval(&expr, 3), Value::Bool(true));
        assert_eq!(eval(&expr, 2), Value::Bool(false));
    }

    #[test]
    fn not_in_and_an_empty_list_follow_origin() {
        let not_in = SExpr::InList {
            expr: Box::new(col("n")),
            list: vec![lit(1)],
            negated: true,
        };
        assert_eq!(eval(&not_in, 2), Value::Bool(true));
        let empty = SExpr::InList {
            expr: Box::new(col("n")),
            list: Vec::new(),
            negated: false,
        };
        assert_eq!(eval(&empty, 1), Value::Bool(false));
    }

    #[test]
    fn like_and_not_like_lower_to_the_like_function() {
        let like = |negated, case_insensitive, pattern: &str| SExpr::Like {
            expr: Box::new(col("s")),
            pattern: Box::new(SExpr::Literal(SqlValue::String(pattern.into()))),
            negated,
            case_insensitive,
        };
        assert_eq!(eval(&like(false, false, "al%"), 0), Value::Bool(true));
        assert_eq!(eval(&like(true, false, "al%"), 0), Value::Bool(false));
        assert_eq!(eval(&like(false, true, "AL%"), 0), Value::Bool(true));
        assert_eq!(eval(&like(false, false, "AL%"), 0), Value::Bool(false));
    }

    #[test]
    fn an_array_with_a_column_element_is_built_per_row() {
        let expr = SExpr::ArrayLiteral(vec![col("n"), lit(1)]);
        assert_eq!(
            eval(&expr, 7),
            Value::Array(vec![Value::Integer(7), Value::Integer(1)])
        );
        let literal = SExpr::ArrayLiteral(vec![lit(1), lit(2)]);
        assert_eq!(
            convert_sql_expr(&literal).expect("lower"),
            QExpr::Literal(Value::Array(vec![Value::Integer(1), Value::Integer(2)]))
        );
    }

    #[test]
    fn source_qualified_column_reads_the_joined_source_key() {
        let qualified = |table: &str| SExpr::Column {
            table: Some(table.into()),
            name: "qty".into(),
        };
        let refs = vec!["s".to_string(), "stock".to_string()];
        assert_eq!(
            convert_sql_expr_with_source(&qualified("stock"), &refs).expect("lower"),
            QExpr::Column("s.qty".into())
        );
        assert_eq!(
            convert_sql_expr_with_source(&qualified("t"), &refs).expect("lower"),
            QExpr::Column("qty".into())
        );
        assert_eq!(
            convert_sql_expr(&qualified("s")).expect("lower"),
            QExpr::Column("qty".into())
        );
    }

    #[test]
    fn between_is_inclusive() {
        let expr = SExpr::Between {
            expr: Box::new(col("n")),
            low: Box::new(lit(2)),
            high: Box::new(lit(4)),
            negated: false,
        };
        assert_eq!(eval(&expr, 2), Value::Bool(true));
        assert_eq!(eval(&expr, 4), Value::Bool(true));
        assert_eq!(eval(&expr, 5), Value::Bool(false));
    }
}