hawdb-plan-cypher 0.6.0

An embedded Rust graph database for local data-plane 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
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
//! Preserve conventional read operators when a generic MATCH has the same semantics.

use super::*;

mod aggregate_projection;
mod order;

pub(super) fn normalize(plan: LogicalPlan) -> LogicalPlan {
    match plan {
        LogicalPlan::GraphMatch {
            program,
            input: None,
        } => {
            if independent_nodes(&program, None) && program.steps.len() > 1 {
                node_product(program, None)
            } else if let Some(plan) = initial_match(&program) {
                plan
            } else {
                LogicalPlan::GraphMatch {
                    program,
                    input: None,
                }
            }
        }
        LogicalPlan::GraphMatch {
            program,
            input: Some(input),
        } => {
            let input = normalize(*input);
            if let Some(plan) = chained_match(&program, &input) {
                plan
            } else if let Some(plan) = chained_optional_match(&program, input.clone()) {
                plan
            } else if let Some(plan) = column_node_lookup(&program, input.clone()) {
                plan
            } else if independent_nodes(&program, Some(&input)) {
                node_product(program, Some(input))
            } else {
                LogicalPlan::GraphMatch {
                    program,
                    input: Some(Box::new(input)),
                }
            }
        }
        LogicalPlan::Project { items, input } => project(items, normalize(*input)),
        LogicalPlan::Filter { predicate, input } => LogicalPlan::Filter {
            predicate,
            input: Box::new(normalize(*input)),
        },
        LogicalPlan::Sort { items, input } => aggregate_projection::sort(items, normalize(*input)),
        LogicalPlan::Aggregate {
            group_keys,
            items,
            input,
        } => {
            let input = normalize(*input);
            let input = if group_keys.is_empty() {
                lower_optional_count_match(&items, &input)
            } else {
                None
            }
            .unwrap_or(input);
            LogicalPlan::Aggregate {
                group_keys,
                items,
                input: Box::new(input),
            }
        }
        LogicalPlan::Distinct { input } => LogicalPlan::Distinct {
            input: Box::new(normalize(*input)),
        },
        LogicalPlan::Limit {
            offset,
            limit,
            input,
        } => LogicalPlan::Limit {
            offset,
            limit,
            input: Box::new(normalize(*input)),
        },
        LogicalPlan::NodeColumnLookup {
            variable,
            label,
            property,
            column,
            optional,
            input,
        } => LogicalPlan::NodeColumnLookup {
            variable,
            label,
            property,
            column,
            optional,
            input: Box::new(normalize(*input)),
        },
        plan => plan,
    }
}

/// A predicate-free optional one-hop MATCH over an existing source binding has
/// the same null-extending behavior as one optional Expand.
fn chained_optional_match(program: &GraphMatchProgram, input: LogicalPlan) -> Option<LogicalPlan> {
    if !program.optional
        || !program.imports.is_empty()
        || program.predicate.is_some()
        || !introduced_bindings_are_disjoint(program, &input)
    {
        return None;
    }
    let [GraphMatchStep::Node(source), GraphMatchStep::Expand {
        source: expand_source,
        relationship,
        rel_type,
        properties,
        direction,
        min_hops,
        max_hops,
        target,
    }] = program.steps.as_slice()
    else {
        return None;
    };
    // Expand cannot encode target-node properties. A filter above an optional
    // Expand would remove the required NULL row when all endpoints are rejected.
    if expand_source != &source.variable
        || !source.properties.is_empty()
        || !target.properties.is_empty()
    {
        return None;
    }
    introduces_expand_bindings(program, &target.variable, relationship.as_deref()).then(|| {
        LogicalPlan::Expand {
            source_variable: source.variable.clone(),
            source_label: source.label.clone(),
            rel_variable: relationship.clone(),
            rel_type: rel_type.clone(),
            rel_properties: properties.clone(),
            direction: *direction,
            target_variable: target.variable.clone(),
            target_label: target.label.clone(),
            min_hops: *min_hops,
            max_hops: *max_hops,
            optional: true,
            input: Box::new(input),
        }
    })
}

fn introduces_expand_bindings(
    program: &GraphMatchProgram,
    target: &str,
    relationship: Option<&str>,
) -> bool {
    let mut introduced = BTreeSet::from([target]);
    introduced.extend(relationship);
    introduced == program.introduced.iter().map(String::as_str).collect()
}

/// A single-node MATCH constrained by an existing scalar can use the bounded
/// column lookup operator instead of scanning every node and filtering it.
fn column_node_lookup(program: &GraphMatchProgram, input: LogicalPlan) -> Option<LogicalPlan> {
    if !program.imports.is_empty() || !introduced_bindings_are_disjoint(program, &input) {
        return None;
    }
    let [GraphMatchStep::Node(node)] = program.steps.as_slice() else {
        return None;
    };
    if program.introduced.as_slice() != [node.variable.as_str()] || !node.properties.is_empty() {
        return None;
    }
    let Some(Predicate::ExpressionEq { expression, value }) = &program.predicate else {
        return None;
    };
    let ProjectionExpression::Property { variable, property } = expression else {
        return None;
    };
    let ProjectionExpression::Column(column) = value else {
        return None;
    };
    (variable == &node.variable).then(|| LogicalPlan::NodeColumnLookup {
        variable: node.variable.clone(),
        label: node.label.clone(),
        property: property.clone(),
        column: column.clone(),
        optional: program.optional,
        input: Box::new(input),
    })
}

fn independent_nodes(program: &GraphMatchProgram, input: Option<&LogicalPlan>) -> bool {
    if program.optional || !program.imports.is_empty() || program.steps.is_empty() {
        return false;
    }
    let mut variables = BTreeSet::new();
    for step in &program.steps {
        let GraphMatchStep::Node(node) = step else {
            return false;
        };
        if !variables.insert(node.variable.as_str()) {
            return false;
        }
    }
    if variables != program.introduced.iter().map(String::as_str).collect() {
        return false;
    }
    // Projection may retain stale native bindings after dropping their logical scope.
    // Only combine with inputs whose actual native bindings are fully known here.
    input.is_none_or(|input| disjoint_node_input(input, &variables))
}

// Project retains native nodes and relationships after their logical variables
// leave scope. Native operators cannot clear logically introduced bindings;
// keep GraphMatch's explicit scope transition unless absence is proven here.
fn introduced_bindings_are_disjoint(program: &GraphMatchProgram, input: &LogicalPlan) -> bool {
    let variables = program.introduced.iter().map(String::as_str).collect();
    disjoint_native_bindings(input, &variables, 0)
}

fn disjoint_native_bindings(input: &LogicalPlan, variables: &BTreeSet<&str>, depth: usize) -> bool {
    if depth >= 256 {
        return false;
    }
    let next = depth + 1;
    match input {
        LogicalPlan::NodeScan { variable, .. } => !variables.contains(variable.as_str()),
        LogicalPlan::VectorSeed { .. } => true,
        // Aggregate constructs fresh value-only bindings, including its spill
        // paths. No native entity binding survives from the input.
        LogicalPlan::Aggregate { .. } => true,
        LogicalPlan::Filter { input, .. }
        | LogicalPlan::Project { input, .. }
        | LogicalPlan::Sort { input, .. }
        | LogicalPlan::Limit { input, .. }
        | LogicalPlan::Distinct { input }
        | LogicalPlan::OptionalDegree { input, .. } => {
            disjoint_native_bindings(input, variables, next)
        }
        LogicalPlan::NodeColumnLookup {
            variable, input, ..
        } => {
            !variables.contains(variable.as_str())
                && disjoint_native_bindings(input, variables, next)
        }
        LogicalPlan::Expand {
            source_variable,
            target_variable,
            rel_variable,
            input,
            ..
        } => {
            !variables.contains(source_variable.as_str())
                && !variables.contains(target_variable.as_str())
                && rel_variable
                    .as_ref()
                    .is_none_or(|name| !variables.contains(name.as_str()))
                && disjoint_native_bindings(input, variables, next)
        }
        LogicalPlan::NodeCartesianProduct { left, right } => {
            disjoint_native_bindings(left, variables, next)
                && disjoint_native_bindings(right, variables, next)
        }
        LogicalPlan::GraphMatch { program, input } => {
            program
                .imports
                .iter()
                .all(|import| !variables.contains(import.variable.as_str()))
                && program.steps.iter().all(|step| match step {
                    GraphMatchStep::Node(node) => !variables.contains(node.variable.as_str()),
                    GraphMatchStep::Expand {
                        source,
                        relationship,
                        target,
                        ..
                    } => {
                        !variables.contains(source.as_str())
                            && !variables.contains(target.variable.as_str())
                            && relationship
                                .as_ref()
                                .is_none_or(|name| !variables.contains(name.as_str()))
                    }
                })
                && input
                    .as_ref()
                    .is_none_or(|input| disjoint_native_bindings(input, variables, next))
        }
        _ => false,
    }
}

fn disjoint_node_input(input: &LogicalPlan, variables: &BTreeSet<&str>) -> bool {
    match input {
        LogicalPlan::NodeScan { variable, .. } => !variables.contains(variable.as_str()),
        LogicalPlan::Filter { input, .. } => disjoint_node_input(input, variables),
        LogicalPlan::NodeCartesianProduct { left, right } => {
            disjoint_node_input(left, variables) && disjoint_node_input(right, variables)
        }
        _ => false,
    }
}

fn node_product(program: GraphMatchProgram, mut input: Option<LogicalPlan>) -> LogicalPlan {
    for step in program.steps {
        let GraphMatchStep::Node(node) = step else {
            unreachable!()
        };
        let predicate = combine_predicates(node_predicates(&node));
        let mut right = LogicalPlan::NodeScan {
            variable: node.variable,
            label: node.label,
        };
        if let Some(predicate) = predicate {
            right = LogicalPlan::Filter {
                predicate,
                input: Box::new(right),
            };
        }
        input = Some(match input {
            Some(left) => LogicalPlan::NodeCartesianProduct {
                left: Box::new(left),
                right: Box::new(right),
            },
            None => right,
        });
    }
    let mut input = input.expect("at least one independent node pattern");
    if let Some(predicate) = program.predicate {
        input = LogicalPlan::Filter {
            predicate,
            input: Box::new(input),
        };
    }
    input
}

/// Lowers a later one-hop MATCH whose source is already bound by an earlier
/// clause. The generic representation includes the source node again so the
/// binder can validate scope; the conventional plan represents that clause as
/// one more `Expand` over the prior input.
fn chained_match(program: &GraphMatchProgram, input: &LogicalPlan) -> Option<LogicalPlan> {
    if program.optional
        || !program.imports.is_empty()
        || !introduced_bindings_are_disjoint(program, input)
    {
        return None;
    }
    let [GraphMatchStep::Node(source), GraphMatchStep::Expand {
        source: expand_source,
        relationship,
        rel_type,
        properties,
        direction,
        min_hops,
        max_hops,
        target,
    }] = program.steps.as_slice()
    else {
        return None;
    };
    if expand_source != &source.variable {
        return None;
    }

    if !introduces_expand_bindings(program, &target.variable, relationship.as_deref()) {
        return None;
    }

    let (input, prior_predicate) = match input.clone() {
        LogicalPlan::Filter { predicate, input } if reorderable_filter(&predicate) => {
            (*input, Some(predicate))
        }
        input => (input, None),
    };
    let mut output = LogicalPlan::Expand {
        source_variable: source.variable.clone(),
        source_label: source.label.clone(),
        rel_variable: relationship.clone(),
        rel_type: rel_type.clone(),
        rel_properties: properties.clone(),
        direction: *direction,
        target_variable: target.variable.clone(),
        target_label: target.label.clone(),
        min_hops: *min_hops,
        max_hops: *max_hops,
        optional: false,
        input: Box::new(input),
    };
    let mut predicates = node_predicates(source);
    predicates.extend(node_predicates(target));
    let predicate =
        combine_optional_predicates(combine_predicates(predicates), program.predicate.clone());
    let predicate = match (prior_predicate, predicate) {
        (Some(prior), Some(predicate)) => combine_predicates(vec![prior, predicate]),
        (Some(predicate), None) | (None, Some(predicate)) => Some(predicate),
        (None, None) => None,
    };
    if let Some(predicate) = predicate
        && let Some(predicate) =
            pushdown_relationship_property_eq_predicates(&mut output, predicate)
    {
        output = LogicalPlan::Filter {
            predicate,
            input: Box::new(output),
        };
    }
    Some(output)
}

fn reorderable_filter(predicate: &Predicate) -> bool {
    match predicate {
        Predicate::And(children) | Predicate::Or(children) => {
            children.iter().all(reorderable_filter)
        }
        Predicate::Not(child) => reorderable_filter(child),
        Predicate::ConstantBool(_)
        | Predicate::IdEq { .. }
        | Predicate::IdNotEq { .. }
        | Predicate::IdCompare { .. }
        | Predicate::IdIn { .. }
        | Predicate::PropertyEq { .. }
        | Predicate::PropertyNotEq { .. }
        | Predicate::PropertyCompare { .. }
        | Predicate::PropertyListContains { .. }
        | Predicate::PropertyListContainsLower { .. }
        | Predicate::PropertyContains { .. }
        | Predicate::PropertyStartsWith { .. }
        | Predicate::PropertyEndsWith { .. }
        | Predicate::PropertyRegexMatch { .. }
        | Predicate::PropertyIsNull { .. }
        | Predicate::PropertyIsNotNull { .. }
        | Predicate::PropertyIn { .. } => true,
        Predicate::RelationshipExists { .. }
        | Predicate::BoundRelationshipExists { .. }
        | Predicate::ExpressionEq { .. }
        | Predicate::ExpressionNotEq { .. }
        | Predicate::ExpressionCompare { .. }
        | Predicate::ExpressionContains { .. } => false,
    }
}

/// A global `COUNT` ignores unmatched rows from an OPTIONAL MATCH. It can
/// therefore use the existing Expand path when the generic program is exactly
/// one optional hop and returns no other optional bindings.
fn lower_optional_count_match(items: &[Aggregation], input: &LogicalPlan) -> Option<LogicalPlan> {
    let [Aggregation {
        function: AggregateFunction::Count,
        target: AggregateTarget::Variable(counted),
        ..
    }] = items
    else {
        return None;
    };
    if let LogicalPlan::Expand {
        source_variable,
        source_label,
        rel_variable,
        rel_type,
        rel_properties,
        direction,
        target_variable,
        target_label,
        min_hops: 1,
        max_hops: 1,
        optional: true,
        input,
    } = input
    {
        let counts_relationship = rel_variable.as_deref() == Some(counted);
        let counts_target = target_variable == counted;
        if (counts_relationship || counts_target)
            && disjoint_native_bindings(input, &BTreeSet::from([counted.as_str()]), 0)
        {
            return Some(LogicalPlan::Expand {
                source_variable: source_variable.clone(),
                source_label: source_label.clone(),
                rel_variable: rel_variable.clone(),
                rel_type: rel_type.clone(),
                rel_properties: rel_properties.clone(),
                direction: *direction,
                target_variable: target_variable.clone(),
                target_label: target_label.clone(),
                min_hops: 1,
                max_hops: 1,
                optional: false,
                input: input.clone(),
            });
        }
    }
    let LogicalPlan::GraphMatch {
        program,
        input: Some(input),
    } = input
    else {
        return None;
    };
    if !program.optional
        || !program.imports.is_empty()
        || program.predicate.is_some()
        || !introduced_bindings_are_disjoint(program, input)
    {
        return None;
    }
    let [GraphMatchStep::Node(first), GraphMatchStep::Expand {
        source,
        relationship,
        rel_type,
        properties,
        direction,
        min_hops: 1,
        max_hops: 1,
        target,
    }] = program.steps.as_slice()
    else {
        return None;
    };
    let counts_relationship = relationship.as_deref() == Some(counted);
    let counts_target = target.variable == *counted;
    let first_is_introduced = program
        .introduced
        .iter()
        .any(|name| name == &first.variable);
    let target_is_introduced = program
        .introduced
        .iter()
        .any(|name| name == &target.variable);
    if !first.properties.is_empty() || !target.properties.is_empty() {
        return None;
    }

    if source == &first.variable
        && !first_is_introduced
        && target_is_introduced
        && (counts_relationship || counts_target)
        && introduces_expand_bindings(program, &target.variable, relationship.as_deref())
    {
        return Some(LogicalPlan::Expand {
            source_variable: first.variable.clone(),
            source_label: first.label.clone(),
            rel_variable: relationship.clone(),
            rel_type: rel_type.clone(),
            rel_properties: properties.clone(),
            direction: *direction,
            target_variable: target.variable.clone(),
            target_label: target.label.clone(),
            min_hops: 1,
            max_hops: 1,
            optional: false,
            input: input.clone(),
        });
    }
    if source == &first.variable
        && first_is_introduced
        && !target_is_introduced
        && counts_relationship
        && introduces_expand_bindings(program, &first.variable, relationship.as_deref())
    {
        return Some(LogicalPlan::Expand {
            source_variable: target.variable.clone(),
            source_label: target.label.clone(),
            rel_variable: relationship.clone(),
            rel_type: rel_type.clone(),
            rel_properties: properties.clone(),
            direction: reverse_direction(*direction),
            target_variable: first.variable.clone(),
            target_label: first.label.clone(),
            min_hops: 1,
            max_hops: 1,
            optional: false,
            input: input.clone(),
        });
    }
    None
}

fn reverse_direction(direction: RelationshipDirection) -> RelationshipDirection {
    match direction {
        RelationshipDirection::Outgoing => RelationshipDirection::Incoming,
        RelationshipDirection::Incoming => RelationshipDirection::Outgoing,
        RelationshipDirection::Undirected => RelationshipDirection::Undirected,
    }
}

fn initial_match(program: &GraphMatchProgram) -> Option<LogicalPlan> {
    if program.optional || !program.imports.is_empty() {
        return None;
    }
    let [GraphMatchStep::Node(first), tail @ ..] = program.steps.as_slice() else {
        return None;
    };
    // Multiple expansions in one clause normally have relationship-isomorphism
    // constraints that conventional Expand operators cannot express. Fixed,
    // distinct relationship types cannot alias the same relationship, so that
    // subset can retain the conventional chain shape safely.
    if tail.len() > 1 && !has_distinct_fixed_relationship_types(tail) {
        return None;
    }
    let mut introduced = BTreeSet::from([first.variable.as_str()]);
    let mut predicates = node_predicates(first);
    let mut input = LogicalPlan::NodeScan {
        variable: first.variable.clone(),
        label: first.label.clone(),
    };
    let mut source_variable = first.variable.clone();
    let mut source_label = first.label.clone();
    for step in tail {
        let GraphMatchStep::Expand {
            source,
            relationship,
            rel_type,
            properties,
            direction,
            min_hops,
            max_hops,
            target,
        } = step
        else {
            return None;
        };
        if source != &source_variable || !introduced.insert(target.variable.as_str()) {
            return None;
        }
        if let Some(relationship) = relationship
            && !introduced.insert(relationship.as_str())
        {
            return None;
        }
        predicates.extend(node_predicates(target));
        input = LogicalPlan::Expand {
            source_variable: source_variable.clone(),
            source_label,
            rel_variable: relationship.clone(),
            rel_type: rel_type.clone(),
            rel_properties: properties.clone(),
            direction: *direction,
            target_variable: target.variable.clone(),
            target_label: target.label.clone(),
            min_hops: *min_hops,
            max_hops: *max_hops,
            optional: false,
            input: Box::new(input),
        };
        source_variable = target.variable.clone();
        source_label = target.label.clone();
    }
    if introduced != program.introduced.iter().map(String::as_str).collect() {
        return None;
    }
    let predicate =
        combine_optional_predicates(combine_predicates(predicates), program.predicate.clone());
    if let Some(predicate) = predicate
        && let Some(predicate) = pushdown_relationship_property_eq_predicates(&mut input, predicate)
    {
        input = LogicalPlan::Filter {
            predicate,
            input: Box::new(input),
        };
    }
    Some(input)
}

fn has_distinct_fixed_relationship_types(steps: &[GraphMatchStep]) -> bool {
    let mut relationship_types = BTreeSet::new();
    steps.iter().all(|step| {
        let GraphMatchStep::Expand { rel_type, .. } = step else {
            return false;
        };
        !rel_type.is_empty() && relationship_types.insert(rel_type)
    })
}

fn node_predicates(node: &GraphMatchNode) -> Vec<Predicate> {
    node.properties
        .iter()
        .map(|(property, value)| Predicate::PropertyEq {
            variable: node.variable.clone(),
            property: property.clone(),
            value: value.clone(),
        })
        .collect()
}

fn project(projections: Vec<Projection>, input: LogicalPlan) -> LogicalPlan {
    let (projections, input) = match aggregate_projection::project(projections, input) {
        aggregate_projection::ProjectRewrite::Applied(plan) => return plan,
        aggregate_projection::ProjectRewrite::Unchanged { items, input } => (items, input),
    };
    if let Some(keys) = order::inline_keys(&projections, &input) {
        return order::remove_hidden_keys(projections.len(), input, keys);
    }
    if let LogicalPlan::Aggregate {
        group_keys, items, ..
    } = &input
    {
        let input_names = group_keys
            .iter()
            .map(|item| &item.name)
            .chain(items.iter().map(|item| &item.name));
        let mut output_names = BTreeSet::new();
        let identity = group_keys.iter().all(|item| item.name.starts_with('\0'))
            && items.iter().all(|item| item.name.starts_with('\0'))
            && projections.len() == group_keys.len() + items.len()
            && projections.iter().zip(input_names).all(|(projection, name)| {
                matches!(&projection.expression, ProjectionExpression::Column(column) if column == name)
                    && output_names.insert(projection.name.as_str())
            });
        if identity {
            let LogicalPlan::Aggregate {
                mut group_keys,
                mut items,
                input,
            } = input
            else {
                unreachable!()
            };
            for (name, projection) in group_keys
                .iter_mut()
                .map(|item| &mut item.name)
                .chain(items.iter_mut().map(|item| &mut item.name))
                .zip(projections)
            {
                *name = projection.name;
            }
            return LogicalPlan::Aggregate {
                group_keys,
                items,
                input,
            };
        }
    }
    LogicalPlan::Project {
        items: projections,
        input: Box::new(input),
    }
}