nodedb 0.0.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
use datafusion::logical_expr::{FetchType, LogicalPlan};
use datafusion::prelude::*;

use crate::bridge::envelope::PhysicalPlan;
use crate::bridge::physical_plan::{DocumentOp, KvOp, QueryOp, TextOp, TimeseriesOp};
use crate::control::planner::physical::PhysicalTask;
use crate::control::security::credential::CredentialStore;
use crate::types::{TenantId, VShardId};

use super::extract::{expr_to_scan_filters, expr_to_usize, try_range_scan_from_predicate};
use super::search::try_extract_text_match_predicate;
use super::sql_expr_convert::try_convert_projection;

/// Converts DataFusion logical plans into NodeDB physical tasks.
///
/// This is the bridge between DataFusion's `Send` logical plans and
/// our `!Send` Data Plane execution. The converter walks the logical
/// plan tree and produces `PhysicalPlan` variants that the CoreLoop
/// can execute.
///
/// Lives on the Control Plane (Send + Sync).
#[derive(Default)]
pub struct PlanConverter {
    /// Optional catalog reference for collection-type-aware routing.
    credentials: Option<std::sync::Arc<CredentialStore>>,
}

impl PlanConverter {
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a converter with catalog access for timeseries detection.
    pub fn with_credentials(credentials: std::sync::Arc<CredentialStore>) -> Self {
        Self {
            credentials: Some(credentials),
        }
    }

    /// Check if a collection is a timeseries collection.
    fn is_timeseries(&self, tenant_id: TenantId, collection: &str) -> bool {
        if let Some(ref creds) = self.credentials
            && let Some(catalog) = creds.catalog()
            && let Ok(Some(coll)) = catalog.get_collection(tenant_id.as_u32(), collection)
        {
            return coll.collection_type.is_timeseries();
        }
        false
    }

    /// Check if a collection is a KV collection.
    pub(super) fn is_kv(&self, tenant_id: TenantId, collection: &str) -> bool {
        if let Some(ref creds) = self.credentials
            && let Some(catalog) = creds.catalog()
            && let Ok(Some(coll)) = catalog.get_collection(tenant_id.as_u32(), collection)
        {
            return coll.collection_type.is_kv();
        }
        false
    }

    /// Convert a DataFusion logical plan into one or more physical tasks.
    ///
    /// Simple queries (point gets, scans) produce a single task.
    /// Complex queries (joins, aggregations) may produce multiple tasks
    /// targeting different vShards.
    pub fn convert(
        &self,
        plan: &LogicalPlan,
        tenant_id: TenantId,
    ) -> crate::Result<Vec<PhysicalTask>> {
        match plan {
            LogicalPlan::Projection(proj) => {
                let mut tasks = self.convert(&proj.input, tenant_id)?;

                // Try to convert computed expressions (e.g., price * qty AS total).
                if let Some(computed) = try_convert_projection(&proj.expr) {
                    let computed_bytes = rmp_serde::to_vec_named(&computed).unwrap_or_default();
                    for task in &mut tasks {
                        if let PhysicalPlan::Document(DocumentOp::Scan {
                            computed_columns, ..
                        }) = &mut task.plan
                        {
                            *computed_columns = computed_bytes.clone();
                        }
                    }
                    return Ok(tasks);
                }

                // Simple column projection (no computed expressions).
                let columns: Vec<String> = proj
                    .expr
                    .iter()
                    .filter_map(|e| {
                        if let Expr::Column(col) = e {
                            Some(col.name.clone())
                        } else if let Expr::Alias(a) = e
                            && let Expr::Column(col) = &*a.expr
                        {
                            Some(col.name.clone())
                        } else {
                            None
                        }
                    })
                    .collect();

                if !columns.is_empty() {
                    for task in &mut tasks {
                        if let PhysicalPlan::Document(DocumentOp::Scan { projection, .. }) =
                            &mut task.plan
                        {
                            *projection = columns.clone();
                        }
                    }
                }

                Ok(tasks)
            }

            LogicalPlan::Filter(filter) => {
                // Check for text_match() UDF in filter → TextSearch plan.
                if let Some((collection, query_text)) =
                    try_extract_text_match_predicate(&filter.predicate, &filter.input)
                {
                    let vshard = VShardId::from_collection(&collection);
                    return Ok(vec![PhysicalTask {
                        tenant_id,
                        vshard_id: vshard,
                        plan: PhysicalPlan::Text(TextOp::Search {
                            collection,
                            query: query_text,
                            top_k: 1000,
                            fuzzy: true,
                            rls_filters: Vec::new(),
                        }),
                    }]);
                }

                // Check if the filter predicate can be converted to a point get
                // before recursing into the input.
                if let LogicalPlan::TableScan(scan) = filter.input.as_ref() {
                    let collection = scan.table_name.to_string().to_lowercase();
                    let vshard = VShardId::from_collection(&collection);

                    // KV routing: try PK point-get first, then fall back to scan.
                    // Note: is_kv() requires catalog access. If the catalog is
                    // unavailable, this returns false and the query falls through
                    // to document scan instead of KV scan.
                    if self.is_kv(tenant_id, &collection) {
                        // Try O(1) hash lookup: WHERE <any_column> = <literal>.
                        if let Some(key_bytes) =
                            extract_equality_key(&filter.predicate)
                        {
                            return Ok(vec![PhysicalTask {
                                tenant_id,
                                vshard_id: vshard,
                                plan: PhysicalPlan::Kv(KvOp::Get {
                                    collection,
                                    key: key_bytes,
                                    rls_filters: Vec::new(),
                                }),
                            }]);
                        }

                        // Fall back to KV scan with filters.
                        let filters = expr_to_scan_filters(&filter.predicate);
                        let filter_bytes =
                            rmp_serde::to_vec_named(&filters).map_err(|e| {
                                crate::Error::Serialization {
                                    format: "msgpack".into(),
                                    detail: format!("kv filter serialization: {e}"),
                                }
                            })?;
                        let limit = scan.fetch.unwrap_or(1000);
                        return Ok(vec![PhysicalTask {
                            tenant_id,
                            vshard_id: vshard,
                            plan: PhysicalPlan::Kv(KvOp::Scan {
                                collection,
                                cursor: Vec::new(),
                                count: limit,
                                filters: filter_bytes,
                                match_pattern: None,
                            }),
                        }]);
                    }

                    if let Some(task) = self.try_point_get(
                        &collection,
                        std::slice::from_ref(&filter.predicate),
                        tenant_id,
                        vshard,
                    )? {
                        return Ok(vec![task]);
                    }

                    // Try secondary index: equality or range on a non-id field → RangeScan.
                    if let Some(task) = try_range_scan_from_predicate(
                        &collection,
                        &filter.predicate,
                        tenant_id,
                        vshard,
                    ) {
                        return Ok(vec![task]);
                    }

                    // Not a point get or indexed scan — emit DocumentScan with filters.
                    let filters = expr_to_scan_filters(&filter.predicate);
                    let filter_bytes = rmp_serde::to_vec_named(&filters).map_err(|e| {
                        crate::Error::Serialization {
                            format: "msgpack".into(),
                            detail: format!("filter serialization: {e}"),
                        }
                    })?;
                    let limit = scan.fetch.unwrap_or(1000);

                    return Ok(vec![PhysicalTask {
                        tenant_id,
                        vshard_id: vshard,
                        plan: PhysicalPlan::Document(DocumentOp::Scan {
                            collection,
                            limit,
                            offset: 0,
                            sort_keys: Vec::new(),
                            filters: filter_bytes,
                            distinct: false,
                            projection: Vec::new(),
                            computed_columns: Vec::new(),
                            window_functions: Vec::new(),
                        }),
                    }]);
                }
                // Filter wrapping Aggregate = HAVING clause.
                if matches!(filter.input.as_ref(), LogicalPlan::Aggregate(_)) {
                    let mut tasks = self.convert(&filter.input, tenant_id)?;
                    let having_filters = expr_to_scan_filters(&filter.predicate);
                    let having_bytes = rmp_serde::to_vec_named(&having_filters).map_err(|e| {
                        crate::Error::Serialization {
                            format: "msgpack".into(),
                            detail: format!("having serialization: {e}"),
                        }
                    })?;
                    for task in &mut tasks {
                        if let PhysicalPlan::Query(QueryOp::Aggregate { having, .. }) =
                            &mut task.plan
                        {
                            *having = having_bytes.clone();
                        }
                    }
                    return Ok(tasks);
                }

                // Filter wrapping something else — recurse and apply filters.
                let mut tasks = self.convert(&filter.input, tenant_id)?;
                let filters = expr_to_scan_filters(&filter.predicate);
                let filter_bytes =
                    rmp_serde::to_vec_named(&filters).map_err(|e| crate::Error::Serialization {
                        format: "msgpack".into(),
                        detail: format!("filter serialization: {e}"),
                    })?;
                for task in &mut tasks {
                    if let PhysicalPlan::Document(DocumentOp::Scan { filters, .. }) =
                        &mut task.plan
                    {
                        *filters = filter_bytes.clone();
                    }
                }
                Ok(tasks)
            }

            LogicalPlan::TableScan(scan) => {
                let collection = scan.table_name.to_string().to_lowercase();
                let vshard = VShardId::from_collection(&collection);

                // Timeseries routing: if collection is timeseries, emit TimeseriesScan.
                if self.is_timeseries(tenant_id, &collection) {
                    let limit = scan.fetch.unwrap_or(10_000);
                    let (time_range, filter_bytes) =
                        super::converter_helpers::extract_timeseries_filters(&scan.filters)?;
                    return Ok(vec![PhysicalTask {
                        tenant_id,
                        vshard_id: vshard,
                        plan: PhysicalPlan::Timeseries(TimeseriesOp::Scan {
                            collection,
                            time_range,
                            projection: Vec::new(),
                            limit,
                            filters: filter_bytes,
                            bucket_interval_ms: 0,
                            rls_filters: Vec::new(),
                        }),
                    }]);
                }

                // KV routing: if collection is KV, emit KvScan.
                if self.is_kv(tenant_id, &collection) {
                    let limit = scan.fetch.unwrap_or(1000);
                    return Ok(vec![PhysicalTask {
                        tenant_id,
                        vshard_id: vshard,
                        plan: PhysicalPlan::Kv(KvOp::Scan {
                            collection,
                            cursor: Vec::new(),
                            count: limit,
                            filters: Vec::new(),
                            match_pattern: None,
                        }),
                    }]);
                }

                // Check for filter pushdown: equality on id → point get.
                if let Some(task) =
                    self.try_point_get(&collection, &scan.filters, tenant_id, vshard)?
                {
                    return Ok(vec![task]);
                }

                // Default: full document scan.
                let limit = scan.fetch.unwrap_or(1000);

                // Convert any TableScan filters to scan filters.
                let filter_bytes = if !scan.filters.is_empty() {
                    let mut all_filters = Vec::new();
                    for f in &scan.filters {
                        all_filters.extend(expr_to_scan_filters(f));
                    }
                    rmp_serde::to_vec_named(&all_filters).map_err(|e| {
                        crate::Error::Serialization {
                            format: "msgpack".into(),
                            detail: format!("filter serialization: {e}"),
                        }
                    })?
                } else {
                    Vec::new()
                };

                Ok(vec![PhysicalTask {
                    tenant_id,
                    vshard_id: vshard,
                    plan: PhysicalPlan::Document(DocumentOp::Scan {
                        collection,
                        limit,
                        offset: 0,
                        sort_keys: Vec::new(),
                        filters: filter_bytes,
                        distinct: false,
                        projection: Vec::new(),
                        computed_columns: Vec::new(),
                        window_functions: Vec::new(),
                    }),
                }])
            }

            LogicalPlan::Limit(limit_plan) => {
                let mut tasks = self.convert(&limit_plan.input, tenant_id)?;

                // Extract LIMIT (fetch) value.
                if let Ok(FetchType::Literal(Some(n))) = limit_plan.get_fetch_type() {
                    for task in &mut tasks {
                        match &mut task.plan {
                            PhysicalPlan::Document(DocumentOp::Scan { limit, .. }) => *limit = n,
                            PhysicalPlan::Document(DocumentOp::RangeScan { limit, .. }) => {
                                *limit = n
                            }
                            _ => {}
                        }
                    }
                }

                // Extract OFFSET (skip) value.
                if let Some(skip) = &limit_plan.skip
                    && let Ok(skip_n) = expr_to_usize(skip)
                {
                    for task in &mut tasks {
                        if let PhysicalPlan::Document(DocumentOp::Scan { offset, .. }) =
                            &mut task.plan
                        {
                            *offset = skip_n;
                        }
                    }
                }

                Ok(tasks)
            }

            LogicalPlan::SubqueryAlias(alias) => self.convert(&alias.input, tenant_id),

            LogicalPlan::Sort(sort) => self.convert_sort(sort, tenant_id),

            LogicalPlan::Aggregate(agg) => {
                super::converter_helpers::convert_aggregate(agg, tenant_id)
            }

            LogicalPlan::Dml(dml) => self.convert_dml(dml, tenant_id),

            LogicalPlan::Join(join) => super::join::convert_join(join, tenant_id),

            // DISTINCT: recurse into input, then mark DocumentScan for dedup.
            LogicalPlan::Distinct(distinct) => {
                let mut tasks = self.convert(distinct.input(), tenant_id)?;
                for task in &mut tasks {
                    if let PhysicalPlan::Document(DocumentOp::Scan { distinct, .. }) =
                        &mut task.plan
                    {
                        *distinct = true;
                    }
                }
                Ok(tasks)
            }

            // Window functions: convert inner plan + extract window specs.
            LogicalPlan::Window(window) => {
                let mut tasks = self.convert(&window.input, tenant_id)?;

                // Convert window expressions to WindowFuncSpec.
                let specs = super::sql_expr_convert::convert_window_exprs(&window.window_expr);
                if !specs.is_empty() {
                    let spec_bytes = rmp_serde::to_vec_named(&specs).unwrap_or_default();
                    for task in &mut tasks {
                        if let PhysicalPlan::Document(DocumentOp::Scan {
                            window_functions, ..
                        }) = &mut task.plan
                        {
                            *window_functions = spec_bytes.clone();
                        }
                    }
                }

                Ok(tasks)
            }

            // Subqueries: DataFusion's optimizer decorrelates many subqueries
            // into joins before they reach the converter. If a Subquery node
            // arrives here, it means the optimizer couldn't decorrelate it.
            // We handle it by converting the subquery's inner plan — this
            // works for uncorrelated subqueries (IN, EXISTS, scalar).
            // Correlated subqueries that reference outer columns will fail
            // at execution time if the Data Plane can't resolve them.
            LogicalPlan::Subquery(subquery) => self.convert(&subquery.subquery, tenant_id),

            // UNION / UNION ALL: convert each input plan and concatenate tasks.
            // The Data Plane executes each sub-plan independently; the Control
            // Plane concatenates the result sets (UNION ALL) or deduplicates
            // (UNION DISTINCT — handled by wrapping in Distinct if needed).
            LogicalPlan::Union(union) => {
                let mut all_tasks = Vec::new();
                for input in &union.inputs {
                    let tasks = self.convert(input, tenant_id)?;
                    all_tasks.extend(tasks);
                }
                Ok(all_tasks)
            }

            LogicalPlan::RecursiveQuery(_) => Err(crate::Error::PlanError {
                detail: "WITH RECURSIVE is not yet supported. Use iterative queries or graph traversal (GRAPH TRAVERSE) instead.".into(),
            }),

            _ => Err(crate::Error::PlanError {
                detail: format!("unsupported logical plan type: {}", plan.display()),
            }),
        }
    }
}

/// Extract key bytes from `col = literal` for KV point-get optimization.
/// Returns None for complex predicates — those fall through to scan.
fn extract_equality_key(expr: &Expr) -> Option<Vec<u8>> {
    use datafusion::logical_expr::Operator;

    if let Expr::BinaryExpr(binary) = expr
        && binary.op == Operator::Eq
    {
        let lit_value = match (&*binary.left, &*binary.right) {
            (Expr::Column(_), Expr::Literal(lit, _)) => Some(lit),
            (Expr::Literal(lit, _), Expr::Column(_)) => Some(lit),
            _ => None,
        }?;

        // Convert the scalar value to key bytes.
        let key = match lit_value {
            datafusion::common::ScalarValue::Utf8(Some(s))
            | datafusion::common::ScalarValue::LargeUtf8(Some(s)) => s.as_bytes().to_vec(),
            datafusion::common::ScalarValue::Int64(Some(i)) => i.to_be_bytes().to_vec(),
            datafusion::common::ScalarValue::Int32(Some(i)) => (*i as i64).to_be_bytes().to_vec(),
            datafusion::common::ScalarValue::Binary(Some(b))
            | datafusion::common::ScalarValue::LargeBinary(Some(b)) => b.clone(),
            _ => {
                // Other types: serialize as string representation.
                let s = lit_value.to_string().trim_matches('\'').to_string();
                s.into_bytes()
            }
        };

        return Some(key);
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use datafusion::execution::context::SessionContext;

    async fn make_context_with_table() -> SessionContext {
        let ctx = SessionContext::new();
        ctx.sql("CREATE TABLE users (id VARCHAR, name VARCHAR, email VARCHAR) AS VALUES ('u1', 'alice', 'a@b.com'), ('u2', 'bob', 'b@c.com')")
            .await
            .unwrap();
        ctx
    }

    #[tokio::test]
    async fn converts_point_get() {
        let ctx = make_context_with_table().await;
        let df = ctx
            .sql("SELECT * FROM users WHERE id = 'u1'")
            .await
            .unwrap();
        let plan = df.into_optimized_plan().unwrap();

        let converter = PlanConverter::new();
        let tasks = converter.convert(&plan, TenantId::new(1)).unwrap();
        assert_eq!(tasks.len(), 1);

        match &tasks[0].plan {
            PhysicalPlan::Document(DocumentOp::PointGet { document_id, .. }) => {
                assert_eq!(document_id, "u1");
            }
            other => panic!("expected PointGet, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn converts_table_scan() {
        let ctx = make_context_with_table().await;
        let df = ctx.sql("SELECT name FROM users").await.unwrap();
        let plan = df.into_optimized_plan().unwrap();

        let converter = PlanConverter::new();
        let tasks = converter.convert(&plan, TenantId::new(1)).unwrap();
        assert_eq!(tasks.len(), 1);
        assert!(matches!(
            &tasks[0].plan,
            PhysicalPlan::Document(DocumentOp::Scan { .. })
        ));
    }

    #[tokio::test]
    async fn limit_propagates() {
        let ctx = make_context_with_table().await;
        let df = ctx.sql("SELECT * FROM users LIMIT 5").await.unwrap();
        let plan = df.into_optimized_plan().unwrap();

        let converter = PlanConverter::new();
        let tasks = converter.convert(&plan, TenantId::new(1)).unwrap();
        assert_eq!(tasks.len(), 1);
        match &tasks[0].plan {
            PhysicalPlan::Document(DocumentOp::Scan { limit, .. }) => assert_eq!(*limit, 5),
            other => panic!("expected DocumentScan, got {other:?}"),
        }
    }
}