graphfusion 0.2.0

GraphFusion graph database.
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
//! GQL plans resolve write targets; indexed snapshot overlays publish their changes.
use super::{
    execution::{batch, col, freeze, memory, Trace},
    expressions::Binder,
    graph::{self, Bindings, ElementBinding, ElementKind},
};
use crate::{
    catalog::ObjectId,
    gql as ast,
    graph::{EdgeTable, GraphData, NodeTable, Table, DESTINATION, ID, SOURCE},
    transaction::StatementTxn,
    Error, Result, SessionState,
};
use datafusion::{
    arrow::{
        array::{Array, UInt64Array},
        datatypes::{DataType, Schema},
        record_batch::RecordBatch,
    },
    common::{Column, TableReference},
    datasource::provider_as_source,
    execution::context::SessionContext,
    logical_expr::{lit, Expr, JoinType, LogicalPlan, LogicalPlanBuilder},
};
use std::{
    collections::{BTreeMap, BTreeSet},
    sync::Arc,
};

#[derive(Default)]
pub(super) struct Writes {
    pub graphs: BTreeMap<ObjectId, GraphData>,
    pub affected: usize,
}
impl Writes {
    pub fn data(&self, tx: &mut StatementTxn, id: ObjectId) -> Result<Arc<GraphData>> {
        match self.graphs.get(&id) {
            Some(data) => Ok(Arc::new(data.clone())),
            None => tx.graph_data(id),
        }
    }
}
pub(super) fn is_mutation(clause: &ast::QueryClause) -> bool {
    matches!(
        clause,
        ast::QueryClause::Insert(_)
            | ast::QueryClause::Set(_)
            | ast::QueryClause::Remove(_)
            | ast::QueryClause::Delete(_)
    )
}
async fn table(
    plan: LogicalPlanBuilder,
    labels: BTreeSet<String>,
    edge: bool,
    ctx: &SessionContext,
    trace: &mut Trace,
) -> Result<Option<Table>> {
    table_with_empty(plan, labels, edge, ctx, trace, false).await
}

async fn table_with_empty(
    plan: LogicalPlanBuilder,
    labels: BTreeSet<String>,
    edge: bool,
    ctx: &SessionContext,
    trace: &mut Trace,
    keep_empty: bool,
) -> Result<Option<Table>> {
    let (schema, batches) = trace.collect(ctx, &plan.build()?).await?;
    if !keep_empty && batches.iter().all(|b| b.num_rows() == 0) {
        return Ok(None);
    }
    let schema = Arc::new(Schema::new(
        schema
            .fields()
            .iter()
            .map(|f| {
                if [ID, SOURCE, DESTINATION].contains(&f.name().as_str()) {
                    Arc::new(f.as_ref().clone().with_nullable(false))
                } else {
                    f.clone()
                }
            })
            .collect::<Vec<_>>(),
    ));
    let batches = batches
        .into_iter()
        .map(|b| batch(schema.clone(), b.columns().to_vec(), b.num_rows()))
        .collect::<Result<_>>()?;
    Ok(Some(Table::try_new(
        labels.into_iter().collect(),
        schema,
        batches,
        if edge {
            &[ID, SOURCE, DESTINATION]
        } else {
            &[ID]
        },
    )?))
}
fn properties(
    map: Option<&ast::MapLiteral>,
    session: &SessionState,
    plan: &LogicalPlanBuilder,
    scope: &Bindings,
) -> Result<Vec<(String, Expr)>> {
    let mut result = Vec::new();
    if let Some(map) = map {
        for (name, value) in &map.entries {
            if name.value.starts_with("__gf_") {
                return Err(Error::InvalidDefinition(
                    "reserved graph property name".into(),
                ));
            }
            if result.iter().any(|(n, _)| n == &name.value) {
                return Err(Error::InvalidDefinition("duplicate property".into()));
            }
            let value = Binder::with_bindings(session, plan.schema(), scope).bind(value)?;
            result.push((name.value.clone(), value));
        }
    }
    Ok(result)
}

fn validate_insert_labels(expression: Option<&ast::LabelExpression>) -> Result<()> {
    match expression {
        None | Some(ast::LabelExpression::Label(_)) => Ok(()),
        Some(ast::LabelExpression::And(left, right)) => {
            validate_insert_labels(Some(left))?;
            validate_insert_labels(Some(right))
        }
        Some(ast::LabelExpression::Parenthesized(inner)) => validate_insert_labels(Some(inner)),
        _ => Err(super::unsupported(
            "INSERT labels must be concrete names joined by &",
        )),
    }
}

#[allow(clippy::too_many_arguments)]
pub(super) async fn apply(
    mut plan: LogicalPlanBuilder,
    scope: &mut Bindings,
    session: &SessionState,
    id: ObjectId,
    clause: &ast::QueryClause,
    tx: &mut StatementTxn,
    ctx: &SessionContext,
    writes: &mut Writes,
    trace: &mut Trace,
) -> Result<LogicalPlanBuilder> {
    let affected_before = writes.affected;
    let mut data = (*writes.data(tx, id)?).clone();
    scope.sources.insert(id, Arc::new(data.clone()));
    plan = freeze(plan, ctx, trace, None).await?;
    match clause {
        ast::QueryClause::Insert(insert) => {
            for path in &insert.patterns {
                let (next, mut left) = insert_node(
                    plan,
                    scope,
                    session,
                    id,
                    &path.start,
                    &mut data,
                    tx,
                    ctx,
                    trace,
                    &mut writes.affected,
                )
                .await?;
                plan = next;
                for chain in &path.chains {
                    let (next, right) = insert_node(
                        plan,
                        scope,
                        session,
                        id,
                        &chain.node,
                        &mut data,
                        tx,
                        ctx,
                        trace,
                        &mut writes.affected,
                    )
                    .await?;
                    plan = next;
                    let edge = &chain.relationship;
                    validate_insert_labels(edge.label_expression.as_ref())?;
                    let (from, to, directed) = match edge.direction {
                        ast::Direction::Right => (left.column(ID), right.column(ID), true),
                        ast::Direction::Left => (right.column(ID), left.column(ID), true),
                        ast::Direction::Undirected => (left.column(ID), right.column(ID), false),
                        _ => return Err(super::unsupported("ambiguous INSERT edge direction")),
                    };
                    let values = properties(edge.properties.as_ref(), session, &plan, scope)?;
                    let (next, _) = insert_element(
                        plan,
                        scope,
                        id,
                        ElementKind::Edge,
                        edge.variable.as_ref(),
                        edge.labels.iter().map(|l| l.value.clone()).collect(),
                        values,
                        Some((from, to, directed)),
                        &mut data,
                        tx,
                        ctx,
                        trace,
                        &mut writes.affected,
                    )
                    .await?;
                    plan = next;
                    left = right;
                }
            }
        }
        ast::QueryClause::Set(set) => {
            for item in &set.items {
                let (name, mut edit, values) = match item {
                    ast::SetItem::Property { target, value } => {
                        let (name, property) = property_target(target)?;
                        let expr =
                            Binder::with_bindings(session, plan.schema(), scope).bind(value)?;
                        (name, Edit::default(), vec![(property, expr)])
                    }
                    ast::SetItem::AllProperties {
                        variable,
                        properties: map,
                    } => (
                        variable.value.clone(),
                        Edit {
                            replace: true,
                            ..Edit::default()
                        },
                        properties(Some(map), session, &plan, scope)?,
                    ),
                    ast::SetItem::Label { variable, label } => (
                        variable.value.clone(),
                        Edit {
                            add_label: Some(label.value.clone()),
                            ..Edit::default()
                        },
                        vec![],
                    ),
                };
                let binding = target(&plan, scope, &name, id, ctx, trace).await?;
                let patch = patch(&plan, &binding, values, &mut edit, ctx, trace).await?;
                if let Some((patch, ids)) = patch {
                    data = edit_graph(&data, binding.kind, &patch, &ids, &edit, ctx, trace).await?;
                    writes.affected += ids.len();
                    plan = refresh(plan, scope, id, &data)?;
                    plan = freeze(plan, ctx, trace, None).await?;
                }
            }
        }
        ast::QueryClause::Remove(remove) => {
            for item in &remove.items {
                let (name, edit) = match item {
                    ast::RemoveItem::Property(expr) => {
                        let (name, property) = property_target(expr)?;
                        (
                            name,
                            Edit {
                                remove: Some(property),
                                ..Edit::default()
                            },
                        )
                    }
                    ast::RemoveItem::Label { variable, label } => (
                        variable.value.clone(),
                        Edit {
                            remove_label: Some(label.value.clone()),
                            ..Edit::default()
                        },
                    ),
                };
                let binding = target(&plan, scope, &name, id, ctx, trace).await?;
                let mut edit = edit;
                if let Some((patch, ids)) =
                    patch(&plan, &binding, vec![], &mut edit, ctx, trace).await?
                {
                    data = edit_graph(&data, binding.kind, &patch, &ids, &edit, ctx, trace).await?;
                    writes.affected += ids.len();
                    plan = refresh(plan, scope, id, &data)?;
                    plan = freeze(plan, ctx, trace, None).await?;
                }
            }
        }
        ast::QueryClause::Delete(delete) => {
            let (next, affected, deleted_nodes, deleted_edges) =
                delete_graph(&plan, scope, id, &data, delete, ctx, trace).await?;
            data = next;
            writes.affected += affected;
            // Keep references to surviving elements. Detect deleted aliases as well as
            // explicitly named targets, so later expressions cannot read stale properties.
            let bindings: Vec<_> = scope
                .elements
                .iter()
                .filter(|(_, b)| b.graph == id)
                .map(|(name, binding)| (name.clone(), binding.clone()))
                .collect();
            let mut removed = BTreeSet::new();
            for (name, binding) in bindings {
                let deleted = if binding.kind == ElementKind::Node {
                    &deleted_nodes
                } else {
                    &deleted_edges
                };
                if deleted.is_empty() {
                    continue;
                }
                let missing = plan
                    .clone()
                    .filter(
                        binding
                            .column(ID)
                            .in_list(deleted.iter().map(|id| lit(*id)).collect(), false),
                    )?
                    .limit(0, Some(1))?
                    .build()?;
                let (_, rows) = trace.collect(ctx, &missing).await?;
                if rows.iter().any(|batch| batch.num_rows() != 0) {
                    removed.insert(name);
                }
            }
            scope.elements.retain(|name, _| !removed.contains(name));
            scope.scalars.retain(|name, _| !removed.contains(name));
            scope.references.retain(|name, _| !removed.contains(name));
            scope.domains.retain(|name, _| !removed.contains(name));
            scope.order.retain(|name| !removed.contains(name));
            if affected != 0 {
                plan = refresh(plan, scope, id, &data)?;
            }
        }
        _ => unreachable!("mutation dispatch"),
    }
    if !scope.references.is_empty() {
        // Keep allocated identity projections below reference lookup joins.
        plan = freeze(plan, ctx, trace, None).await?;
    }
    plan = super::references::refresh(plan, scope, id, &data)?;
    if !scope.references.is_empty() {
        plan = freeze(plan, ctx, trace, None).await?;
    }
    trace.sources.insert(id, Arc::new(data.clone()));
    if writes.affected != affected_before {
        writes.graphs.insert(id, data);
    }
    Ok(plan)
}

#[allow(clippy::too_many_arguments)]
async fn insert_node(
    plan: LogicalPlanBuilder,
    scope: &mut Bindings,
    session: &SessionState,
    graph: ObjectId,
    node: &ast::NodePattern,
    data: &mut GraphData,
    tx: &mut StatementTxn,
    ctx: &SessionContext,
    trace: &mut Trace,
    affected: &mut usize,
) -> Result<(LogicalPlanBuilder, ElementBinding)> {
    validate_insert_labels(node.label_expression.as_ref())?;
    if let Some(name) = &node.variable {
        if scope.contains(&name.value) {
            let binding = target(&plan, scope, &name.value, graph, ctx, trace).await?;
            if binding.kind != ElementKind::Node
                || !node.labels.is_empty()
                || node.properties.is_some()
            {
                return Err(Error::InvalidQuery(
                    "bound INSERT node must be a bare node reference".into(),
                ));
            }
            return Ok((plan, binding));
        }
    }
    let values = properties(node.properties.as_ref(), session, &plan, scope)?;
    insert_element(
        plan,
        scope,
        graph,
        ElementKind::Node,
        node.variable.as_ref(),
        node.labels.iter().map(|l| l.value.clone()).collect(),
        values,
        None,
        data,
        tx,
        ctx,
        trace,
        affected,
    )
    .await
}

#[allow(clippy::too_many_arguments)]
async fn insert_element(
    mut plan: LogicalPlanBuilder,
    scope: &mut Bindings,
    graph_id: ObjectId,
    kind: ElementKind,
    variable: Option<&ast::Identifier>,
    labels: BTreeSet<String>,
    values: Vec<(String, Expr)>,
    endpoints: Option<(Expr, Expr, bool)>,
    data: &mut GraphData,
    tx: &mut StatementTxn,
    ctx: &SessionContext,
    trace: &mut Trace,
    affected: &mut usize,
) -> Result<(LogicalPlanBuilder, ElementBinding)> {
    if variable.is_some_and(|v| scope.contains(&v.value)) {
        return Err(Error::InvalidQuery(
            "INSERT edge variable already bound".into(),
        ));
    }
    // Count a frozen input with DataFusion. ID allocation is a storage-coordinator operation.
    let (_, batches) = trace.collect(ctx, &plan.clone().build()?).await?;
    let rows = batches.iter().map(RecordBatch::num_rows).sum();
    let ids = tx.allocate_element_ids(graph_id, rows)?;
    let identity = scope.fresh("insert_id");
    plan = freeze(plan, ctx, trace, Some((&identity, ids))).await?;
    let mut expressions = vec![Expr::Column(Column::new_unqualified(&identity)).alias(ID)];
    let mut directed = false;
    if let Some((from, to, is_directed)) = endpoints {
        directed = is_directed;
        expressions.extend([from.alias(SOURCE), to.alias(DESTINATION)]);
    }
    use datafusion::logical_expr::ExprSchemable;
    for (name, value) in values {
        if value.get_type(plan.schema())? != DataType::Null {
            expressions.push(value.alias(name));
        }
    }
    let mut next = data.clone();
    let inserted = table_with_empty(
        plan.clone().project(expressions)?,
        labels,
        kind == ElementKind::Edge,
        ctx,
        trace,
        true,
    )
    .await?;
    if let Some(table) = inserted.clone() {
        if kind == ElementKind::Node {
            if let Some(existing) = next.nodes.iter_mut().find(|node| {
                node.0.file_id.is_none()
                    && node.0.labels == table.labels
                    && node.0.schema.as_ref() == table.schema.as_ref()
            }) {
                existing.0.append_rows(table)?;
            } else {
                next.nodes.push(NodeTable(table));
            }
        } else if let Some(existing) = next.edges.iter_mut().find(|edge| {
            edge.table.file_id.is_none()
                && edge.directed == directed
                && edge.table.labels == table.labels
                && edge.table.schema.as_ref() == table.schema.as_ref()
        }) {
            existing.table.append_rows(table)?;
        } else {
            next.edges.push(EdgeTable { table, directed });
        }
    }
    // IDs are allocated by the coordinator and endpoints come from validated bindings.
    // Existing sealed fragments can remain lazy during append-only writes.
    crate::graph::property_types(next.nodes.iter().map(|t| &t.0))?;
    crate::graph::property_types(next.edges.iter().map(|t| &t.table))?;
    let (scan, binding) = match inserted.as_ref() {
        Some(table) => graph::scan_insert(scope, graph_id, &next, kind, table, directed)?,
        None => graph::scan(scope, graph_id, &next, kind, None)?,
    };
    // Empty input still needs typed bindings, but must not publish a new layout.
    if rows != 0 {
        *data = next;
        *affected += rows;
    }
    plan = plan.join_on(
        scan,
        JoinType::Inner,
        [Expr::Column(Column::new_unqualified(&identity)).eq(binding.column(ID))],
    )?;
    if let Some(name) = variable {
        graph::declare(scope, &name.value, &binding)?;
    }
    Ok((plan, binding))
}

async fn target(
    plan: &LogicalPlanBuilder,
    scope: &Bindings,
    name: &str,
    graph: ObjectId,
    ctx: &SessionContext,
    trace: &mut Trace,
) -> Result<ElementBinding> {
    let binding = if let Some(binding) = scope.elements.get(name) {
        binding.clone()
    } else if let Some(reference) = scope.references.get(name) {
        let [binding] = reference.parts.as_slice() else {
            return Err(super::unsupported("mutation reference requires one known graph and element kind; bind it with MATCH first"));
        };
        binding.clone()
    } else {
        return Err(Error::InvalidQuery(format!(
            "unbound element variable {name}"
        )));
    };
    if binding.graph != graph {
        return Err(Error::InvalidQuery(
            "mutation target belongs to another working graph".into(),
        ));
    }
    if let Some(reference) = scope.references.get(name) {
        let value = Expr::Column(scope.scalars.get(name).expect("scalar reference").clone());
        // Evaluate before null-target elimination: a genuine null is a no-op,
        // while a non-null identity deleted earlier in the statement is an error.
        trace
            .collect(
                ctx,
                &plan.clone().project([reference.guard(value)])?.build()?,
            )
            .await?;
    }
    Ok(binding)
}
fn property_target(expr: &ast::Expr) -> Result<(String, String)> {
    if let ast::Expr::Property { base, key } = expr {
        if let ast::Expr::Identifier(name) = base.as_ref() {
            if key.value.starts_with("__gf_") {
                return Err(Error::InvalidDefinition(
                    "reserved graph property name".into(),
                ));
            }
            return Ok((name.value.clone(), key.value.clone()));
        }
    }
    Err(Error::InvalidQuery(
        "property mutation requires an element variable".into(),
    ))
}

#[derive(Default)]
struct Edit {
    values: BTreeMap<String, Option<String>>,
    replace: bool,
    remove: Option<String>,
    add_label: Option<String>,
    remove_label: Option<String>,
}
async fn patch(
    plan: &LogicalPlanBuilder,
    binding: &ElementBinding,
    values: Vec<(String, Expr)>,
    edit: &mut Edit,
    ctx: &SessionContext,
    trace: &mut Trace,
) -> Result<Option<(LogicalPlan, BTreeSet<u64>)>> {
    use datafusion::logical_expr::ExprSchemable;
    let mut expressions = vec![binding.column(ID).alias(ID)];
    for (i, (name, value)) in values.into_iter().enumerate() {
        if value.get_type(plan.schema())? == DataType::Null {
            edit.values.insert(name, None);
        } else {
            let column = format!("__gf_value_{i}");
            expressions.push(value.alias(&column));
            edit.values.insert(name, Some(column));
        }
    }
    let patch = plan
        .clone()
        .filter(binding.column(ID).is_not_null())?
        .project(expressions)?
        .distinct()?
        .build()?;
    let (schema, batches) = trace.collect(ctx, &patch).await?;
    let mut ids = BTreeSet::new();
    for batch in &batches {
        let column = batch
            .column(0)
            .as_any()
            .downcast_ref::<UInt64Array>()
            .ok_or_else(|| Error::Corrupt("invalid patch identity type".into()))?;
        if column.null_count() != 0 {
            return Err(Error::InvalidQuery("null mutation target".into()));
        }
        for id in column.values() {
            if !ids.insert(*id) {
                return Err(super::unsupported(
                    "conflicting values for one element from multiple input rows",
                ));
            }
        }
    }
    if ids.is_empty() {
        return Ok(None);
    }
    Ok(Some((
        memory(schema, batches)?.alias("__gf_patch")?.build()?,
        ids,
    )))
}

async fn edit_graph(
    data: &GraphData,
    kind: ElementKind,
    patch: &LogicalPlan,
    ids: &BTreeSet<u64>,
    edit: &Edit,
    ctx: &SessionContext,
    trace: &mut Trace,
) -> Result<GraphData> {
    let mut nodes = if kind == ElementKind::Node {
        vec![]
    } else {
        data.nodes.clone()
    };
    let mut edges = if kind == ElementKind::Edge {
        vec![]
    } else {
        data.edges.clone()
    };
    let tables: Vec<_> = if kind == ElementKind::Node {
        data.nodes.iter().map(|t| (&t.0, false)).collect()
    } else {
        data.edges.iter().map(|t| (&t.table, t.directed)).collect()
    };
    for (old, directed) in tables {
        let Some(provider) = old.select_ids(ids)? else {
            if kind == ElementKind::Node {
                nodes.push(NodeTable(old.clone()));
            } else {
                edges.push(EdgeTable {
                    table: old.clone(),
                    directed,
                });
            }
            continue;
        };
        let selected = LogicalPlanBuilder::scan("__gf_old", provider_as_source(provider), None)?;
        let on = col("__gf_old", ID).eq(col("__gf_patch", ID));
        let changed = selected.join_on(patch.clone(), JoinType::Inner, [on])?;
        let mut projections = Vec::new();
        for field in old.schema.fields() {
            let name = field.name();
            if name.starts_with("__gf_")
                || (!edit.replace
                    && !edit.values.contains_key(name)
                    && edit.remove.as_ref() != Some(name))
            {
                projections.push(col("__gf_old", name).alias(name));
            }
        }
        for (name, value) in &edit.values {
            if let Some(column) = value {
                projections.push(col("__gf_patch", column).alias(name));
            }
        }
        let mut labels = old.labels.clone();
        if let Some(label) = &edit.add_label {
            labels.insert(label.clone());
        }
        if let Some(label) = &edit.remove_label {
            labels.remove(label);
        }
        let changed = table(
            changed.project(projections)?,
            labels,
            kind == ElementKind::Edge,
            ctx,
            trace,
        )
        .await?;
        let mut kept = old.clone();
        kept.mask_rows(ids)?;
        let kept = (kept.row_count != 0).then_some(kept);
        for table in [kept, changed].into_iter().flatten() {
            if kind == ElementKind::Node {
                nodes.push(NodeTable(table));
            } else {
                edges.push(EdgeTable { table, directed });
            }
        }
    }
    GraphData::from_mutation(nodes, edges)
}

fn refresh(
    mut plan: LogicalPlanBuilder,
    scope: &mut Bindings,
    graph_id: ObjectId,
    data: &GraphData,
) -> Result<LogicalPlanBuilder> {
    let old: Vec<_> = scope
        .elements
        .iter()
        .filter(|(_, b)| b.graph == graph_id)
        .map(|(name, b)| (name.clone(), b.clone()))
        .collect();
    for (name, before) in old {
        let retained: Vec<_> = plan
            .schema()
            .columns()
            .into_iter()
            .filter(|c| c.relation.as_ref() != Some(&TableReference::bare(before.alias.as_str())))
            .map(Expr::Column)
            .collect();
        let (scan, after) = graph::scan(scope, graph_id, data, before.kind, None)?;
        let appended: Vec<_> = scan
            .schema()
            .columns()
            .into_iter()
            .map(Expr::Column)
            .collect();
        plan = plan
            .join_on(
                scan,
                JoinType::Left,
                [before.column(ID).eq(after.column(ID))],
            )?
            .project(retained.into_iter().chain(appended))?;
        scope.elements.insert(name, after);
    }
    super::references::refresh(plan, scope, graph_id, data)
}

/// Collect identities once, skipping null targets before touching any fragment.
async fn delete_ids(
    plan: Option<LogicalPlanBuilder>,
    ctx: &SessionContext,
    trace: &mut Trace,
) -> Result<BTreeSet<u64>> {
    let Some(plan) = plan else {
        return Ok(BTreeSet::new());
    };
    let (_, batches) = trace.collect(ctx, &plan.distinct()?.build()?).await?;
    Ok(batches
        .iter()
        .flat_map(|batch| {
            let ids = batch
                .column(0)
                .as_any()
                .downcast_ref::<UInt64Array>()
                .expect("element IDs");
            ids.iter().flatten()
        })
        .collect())
}

#[allow(clippy::too_many_arguments)]
async fn delete_graph(
    plan: &LogicalPlanBuilder,
    scope: &Bindings,
    graph_id: ObjectId,
    data: &GraphData,
    delete: &ast::DeleteStatement,
    ctx: &SessionContext,
    trace: &mut Trace,
) -> Result<(GraphData, usize, BTreeSet<u64>, BTreeSet<u64>)> {
    let mut node_ids: Option<LogicalPlanBuilder> = None;
    let mut edge_ids: Option<LogicalPlanBuilder> = None;
    for item in &delete.items {
        let ast::Expr::Identifier(name) = item else {
            return Err(super::unsupported("DELETE requires element variables"));
        };
        let binding = target(plan, scope, &name.value, graph_id, ctx, trace).await?;
        let ids = plan.clone().project([binding.column(ID).alias(ID)])?;
        let target = if binding.kind == ElementKind::Node {
            &mut node_ids
        } else {
            &mut edge_ids
        };
        *target = Some(match target.take() {
            Some(previous) => previous.union(ids.build()?)?,
            None => ids,
        });
    }
    let nodes = delete_ids(node_ids, ctx, trace).await?;
    let edges = delete_ids(edge_ids, ctx, trace).await?;
    if nodes.is_empty() && edges.is_empty() {
        return Ok((data.clone(), 0, nodes, edges));
    }
    let mut new_nodes = Vec::new();
    let mut new_edges = Vec::new();
    let mut deleted_nodes = BTreeSet::new();
    let mut deleted_edges = BTreeSet::new();
    for node in &data.nodes {
        let mut table = node.0.clone();
        if !nodes.is_empty() {
            deleted_nodes.extend(table.mask_rows(&nodes)?);
        }
        if table.row_count != 0 {
            new_nodes.push(NodeTable(table));
        }
    }
    for edge in &data.edges {
        let mut removed = edges.clone();
        if !deleted_nodes.is_empty() {
            let incident = edge.table.incident_ids(&deleted_nodes)?;
            if delete.detach {
                removed.extend(incident);
            } else if !incident.is_subset(&edges) {
                return Err(Error::InvalidQuery(
                    "DELETE cannot remove a node that still has edges; use DETACH DELETE".into(),
                ));
            }
        }
        let mut table = edge.table.clone();
        if !removed.is_empty() {
            deleted_edges.extend(table.mask_rows(&removed)?);
        }
        if table.row_count != 0 {
            new_edges.push(EdgeTable {
                table,
                directed: edge.directed,
            });
        }
    }
    let next = GraphData::from_mutation(new_nodes, new_edges)?;
    let affected = deleted_nodes.len() + deleted_edges.len();
    Ok((next, affected, deleted_nodes, deleted_edges))
}