algeon-query-planning 0.1.2

Backend-neutral native query planning, proof, and admission contracts.
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
//! Per-node capability analysis.
//!
//! Produces a [`NodeCapability`] for each [`QueryNode`]: operator kind,
//! executable decision, aggregate state contract, and the row bound the
//! operator alone establishes.
use std::collections::BTreeMap;

use arrow::datatypes::{DataType as ArrowDataType, Schema as ArrowSchema};

use super::{
    AggregateFunction, AggregateSpec, Expr, GroupingSetsSpec, JoinForm, JoinNode, NodeId, PlanNode, QueryNode,
    QueryPlan, SourceNode,
    aggregate::aggregate_state_contract,
    classify_join,
    expr::{
        expression_rejection_decision, validate_aggregate_expr_capability, validate_column_expr_capability,
        validate_general_expr_capability, validate_join_residual_expr_capability,
        validate_named_column_expr_capability, validate_post_join_residual_expr_capability,
    },
    model::{
        AggregateGrouping, CapabilityDecision, NodeCapability, NodeOperator, OrderingRequirement, ResidualRequirement,
        SourceFormatCapability,
    },
    rejection::{Reason, UnsupportedPart},
};
use crate::{
    backend::AnalysisContext,
    error::{QueryError, Result},
    ml_validation::validate_ml_algorithm_node,
    plan::{
        AggregateNode, EdgeNormalizeNode, EdgeWeightCombiner, GraphAlgorithmNode, GraphAlgorithmParams,
        GraphVertexPairInput, SortNode, graph_result_schema_for_vertex_type,
    },
    validation::{PlanColumns, algorithm_input, check_node},
    vector_validation::validate_vector_algorithm_node,
};

pub(super) fn analyze_node(
    plan: &QueryPlan,
    node: &QueryNode,
    columns: &PlanColumns,
    ctx: AnalysisContext<'_>,
) -> NodeCapability {
    let node_id = node.id;
    let kind = &node.kind;
    let operator = node_operator(kind);
    let executable = node_executable_decision(plan, node, columns, ctx);
    let aggregate_state = match kind {
        PlanNode::Aggregate(aggregate) => aggregate_state_contract(&aggregate.spec),
        _ => None,
    };
    let max_output_rows = node_max_output_rows(kind);
    NodeCapability { node_id, operator, executable, aggregate_state, max_output_rows }
}

/// Rows a node emits at most, when the operator alone bounds it: a scalar
/// aggregate (no groups and no grouping sets) emits one row; a limit with
/// fetch ≤ 1 emits at most one; a source with a ≤1 scan/max bound or a ≤1-row
/// literal emits at most one. Anything else is unbounded from the operator
/// alone; the compiler tracks tighter bounds per resource where it can prove
/// them.
fn node_max_output_rows(kind: &PlanNode) -> Option<u64> {
    match kind {
        PlanNode::Aggregate(aggregate)
            if aggregate.spec.group_exprs.is_empty() && aggregate.spec.grouping_sets.is_none() =>
        {
            Some(1)
        },
        PlanNode::Limit(limit) => limit.spec.fetch.filter(|fetch| *fetch <= 1).map(|fetch| fetch as u64),
        PlanNode::Source(source) => match source.as_ref() {
            SourceNode::LocalParquet(source) => source
                .scan_limit
                .filter(|limit| *limit <= 1)
                .map(|limit| limit as u64)
                .or_else(|| source.max_rows.filter(|rows| *rows <= 1).map(|rows| rows as u64)),
            SourceNode::LiteralBatch(source) => {
                let rows = source.row_count();
                (rows <= 1).then_some(rows as u64)
            },
            SourceNode::GpuResident(_) => None,
        },
        _ => None,
    }
}

pub(super) fn node_operator(kind: &PlanNode) -> NodeOperator {
    match kind {
        PlanNode::Source(source) => NodeOperator::Source { format: source_format_capability(source) },
        PlanNode::Filter(_) => NodeOperator::Filter,
        PlanNode::Projection(_) => NodeOperator::Projection,
        PlanNode::Aggregate(aggregate) => NodeOperator::Aggregate {
            grouping: if aggregate.spec.grouping_sets.is_some() {
                AggregateGrouping::GroupingSets
            } else if aggregate.spec.group_exprs.is_empty() {
                AggregateGrouping::Scalar
            } else {
                AggregateGrouping::Grouped
            },
            functions: aggregate.spec.aggr_exprs.iter().map(|expr| expr.function).collect(),
        },
        PlanNode::Window(window) => NodeOperator::Window { function_count: window.spec.functions.len() },
        PlanNode::Sort(sort) => NodeOperator::Sort {
            ordering: if sort.spec.expressions.is_empty() {
                OrderingRequirement::None
            } else {
                OrderingRequirement::Required
            },
        },
        PlanNode::Limit(_) => NodeOperator::Limit,
        PlanNode::Join(join) => NodeOperator::Join {
            join_type: join.spec.join_type.clone(),
            key_count: join.spec.keys.len(),
            residual: if join.spec.residual_predicate.is_some() {
                ResidualRequirement::Required
            } else {
                ResidualRequirement::None
            },
            build_side: join.spec.build_side,
        },
        PlanNode::Union(union) => NodeOperator::Union { input_count: union.inputs.len() },
        PlanNode::EdgeNormalize(node) => NodeOperator::EdgeNormalize {
            add_reverse: node.spec.add_reverse,
            dedup: node.spec.dedup,
            weight_combiner: node.spec.weight_combiner,
        },
        PlanNode::GraphAlgorithm(graph) => NodeOperator::GraphAlgorithm { algorithm: graph.spec.algorithm },
        PlanNode::VectorAlgorithm(vector) => NodeOperator::VectorAlgorithm { algorithm: vector.spec.algorithm },
        PlanNode::MlAlgorithm(ml) => NodeOperator::MlAlgorithm { algorithm: ml.spec.algorithm },
    }
}

pub(super) fn source_format_capability(source: &SourceNode) -> SourceFormatCapability {
    match source {
        SourceNode::LocalParquet(_) => SourceFormatCapability::LocalParquet,
        SourceNode::GpuResident(_) => SourceFormatCapability::GpuResident,
        SourceNode::LiteralBatch(_) => SourceFormatCapability::LiteralBatch,
    }
}

/// Whether a native executor runs this node's operator form.
///
/// Source, projection, window and limit nodes have no capability rule beyond
/// the validator's, so their decision replays `check_node` and reports its
/// failure under `PlanValidation`; a standalone analysis (reports, `EXPLAIN
/// GPU`) thereby agrees with admission, which validates first. A join runs its
/// form and residual checks before `check_node` so the operator-form reason
/// wins over a type rule.
fn node_executable_decision(
    plan: &QueryPlan,
    node: &QueryNode,
    columns: &PlanColumns,
    ctx: AnalysisContext<'_>,
) -> CapabilityDecision {
    match &node.kind {
        PlanNode::Source(_) | PlanNode::Projection(_) | PlanNode::Window(_) | PlanNode::Limit(_) => {
            checked_node_decision(node.id, check_node(plan, node, columns, ctx))
        },
        PlanNode::Filter(filter) => expression_node_decision(
            node.id,
            Reason::UnsupportedFilterExpressionCapability,
            std::iter::once(&filter.predicate),
        ),
        PlanNode::Aggregate(aggregate) => aggregate_node_executable_decision(plan, node.id, aggregate),
        PlanNode::Sort(sort) => sort_node_executable_decision(node.id, sort),
        PlanNode::Join(join) => match join_node_executable_decision(node.id, join) {
            CapabilityDecision::Satisfied => checked_node_decision(node.id, check_node(plan, node, columns, ctx)),
            rejected => rejected,
        },
        PlanNode::Union(_) => CapabilityDecision::Satisfied,
        PlanNode::EdgeNormalize(edge_normalize) => {
            edge_normalize_node_executable_decision(plan, node.id, edge_normalize)
        },
        PlanNode::GraphAlgorithm(graph) => graph_algorithm_node_executable_decision(plan, node.id, graph),
        PlanNode::VectorAlgorithm(vector) => algorithm_node_decision(
            node.id,
            vector.spec.algorithm.as_str(),
            validate_vector_algorithm_node(vector, plan, columns.available_by_node(), node.id),
            Reason::UnsupportedVectorAlgorithmNodeCapability,
        ),
        PlanNode::MlAlgorithm(ml) => algorithm_node_decision(
            node.id,
            ml.spec.algorithm.as_str(),
            validate_ml_algorithm_node(ml, plan, columns.available_by_node(), node.id),
            Reason::UnsupportedMlAlgorithmNodeCapability,
        ),
    }
}

/// The decision for a node [`check_node`] checks: a failed rule rejects the
/// node with the reason admission gives the same validation failure.
fn checked_node_decision(node_id: NodeId, result: Result<()>) -> CapabilityDecision {
    match result {
        Ok(()) => CapabilityDecision::Satisfied,
        Err(error) => CapabilityDecision::Rejected {
            reason: Reason::PlanValidation(UnsupportedPart::from_code(error.code())),
            facts: validation_error_facts(node_id, &error),
        },
    }
}

fn algorithm_node_decision(
    node_id: NodeId,
    algorithm: &str,
    result: Result<()>,
    reason: fn(UnsupportedPart) -> Reason,
) -> CapabilityDecision {
    match result {
        Ok(()) => CapabilityDecision::Satisfied,
        Err(error) => {
            let mut facts = validation_error_facts(node_id, &error);
            facts.entry("algorithm").or_insert_with(|| algorithm.to_string());
            CapabilityDecision::Rejected { reason: reason(UnsupportedPart::from_code(error.code())), facts }
        },
    }
}

fn validation_error_facts(node_id: NodeId, error: &QueryError) -> BTreeMap<&'static str, String> {
    let mut facts = error.facts().iter().map(|(key, value)| (key, value.to_string())).collect::<BTreeMap<_, _>>();
    facts.entry("node_id").or_insert_with(|| node_id.to_string());
    facts.insert("validation_code", error.code().to_string());
    facts.insert("validation_operation", error.operation().to_string());
    facts
}

fn expression_node_decision<'a>(
    node_id: NodeId,
    reason: Reason,
    expressions: impl IntoIterator<Item = &'a Expr>,
) -> CapabilityDecision {
    for expression in expressions {
        if let Err(rejection) = validate_general_expr_capability(expression) {
            return expression_rejection_decision(node_id, reason, expression, rejection);
        }
    }
    CapabilityDecision::Satisfied
}

fn aggregate_node_executable_decision(
    plan: &QueryPlan,
    node_id: NodeId,
    aggregate: &AggregateNode,
) -> CapabilityDecision {
    if let Some(grouping_sets) = &aggregate.spec.grouping_sets
        && let Err(reason) = validate_grouping_sets_capability(&aggregate.spec, grouping_sets)
    {
        return CapabilityDecision::rejected(
            Reason::UnsupportedAggregateGroupingSetsCapability,
            [
                ("node_id", node_id.to_string()),
                ("reason", reason.to_string()),
                ("group_expr_count", aggregate.spec.group_exprs.len().to_string()),
                ("grouping_set_count", grouping_sets.groups.len().to_string()),
            ],
        );
    }
    // Median runs single-stage with no mergeable state and the floating
    // statistical aggregates run through the statistical contract; no
    // executor evaluates both families in one aggregate node.
    if aggregate.spec.aggr_exprs.iter().any(|expr| expr.function == AggregateFunction::Median)
        && aggregate.spec.aggr_exprs.iter().any(|expr| expr.function.is_floating_statistical())
    {
        return CapabilityDecision::rejected(
            Reason::UnsupportedAggregateExpressionCapability,
            [
                ("node_id", node_id.to_string()),
                ("reason", "median_floating_statistical_mixed_unsupported".to_string()),
                ("expression_family", "aggregate".to_string()),
                ("expression", "aggregate_spec".to_string()),
            ],
        );
    }
    let Some(input) = plan.node(aggregate.input) else {
        return CapabilityDecision::rejected(
            Reason::UnsupportedAggregateNodeCapability,
            [("node_id", node_id.to_string()), ("reason", "missing_input_node".to_string())],
        );
    };
    for expr in &aggregate.spec.group_exprs {
        if let Err(rejection) = validate_named_column_expr_capability(expr) {
            return expression_rejection_decision(
                node_id,
                Reason::UnsupportedAggregateGroupExpressionCapability,
                expr,
                rejection,
            );
        }
    }
    for aggregate_expr in &aggregate.spec.aggr_exprs {
        if let Err(rejection) =
            validate_aggregate_expr_capability(aggregate_expr, aggregate.spec.group_exprs.is_empty())
        {
            return expression_rejection_decision(
                node_id,
                Reason::UnsupportedAggregateExpressionCapability,
                &aggregate_expr.expr,
                rejection,
            );
        }
        if aggregate_expr.function == AggregateFunction::Median {
            let Some(data_type) = aggregate_expr_input_data_type(&aggregate_expr.expr, input.output_schema().as_ref())
            else {
                return CapabilityDecision::rejected(
                    Reason::UnsupportedAggregateExpressionCapability,
                    [
                        ("node_id", node_id.to_string()),
                        ("reason", "median_requires_column_input".to_string()),
                        ("expression_family", "aggregate".to_string()),
                        ("expression", aggregate_expr.expr.to_string()),
                    ],
                );
            };
            if !is_cudf_median_input_type(data_type) {
                return CapabilityDecision::rejected(
                    Reason::UnsupportedAggregateExpressionCapability,
                    [
                        ("node_id", node_id.to_string()),
                        ("reason", "unsupported_median_input_type".to_string()),
                        ("expression_family", "aggregate".to_string()),
                        ("expression", aggregate_expr.expr.to_string()),
                        ("data_type", format!("{data_type:?}")),
                    ],
                );
            }
        }
    }
    CapabilityDecision::Satisfied
}

fn aggregate_expr_input_data_type<'a>(expr: &'a Expr, input_schema: &'a ArrowSchema) -> Option<&'a ArrowDataType> {
    match expr {
        Expr::Column(column) => input_schema.field_with_name(column.name()).ok().map(|field| field.data_type()),
        Expr::Cast { target_type, .. } | Expr::TryCast { target_type, .. } => Some(target_type),
        Expr::Alias { expr, .. } => aggregate_expr_input_data_type(expr, input_schema),
        _ => None,
    }
}

/// Input types cuDF's groupby median accepts. The aggregation is a quantile
/// over a numeric column; decimal, boolean, string, and temporal inputs fail at
/// execution, so the type is rejected here instead.
pub fn is_cudf_median_input_type(data_type: &ArrowDataType) -> bool {
    matches!(
        data_type,
        ArrowDataType::Int8
            | ArrowDataType::Int16
            | ArrowDataType::Int32
            | ArrowDataType::Int64
            | ArrowDataType::UInt8
            | ArrowDataType::UInt16
            | ArrowDataType::UInt32
            | ArrowDataType::UInt64
            | ArrowDataType::Float32
            | ArrowDataType::Float64
    )
}

fn validate_grouping_sets_capability(
    spec: &AggregateSpec,
    grouping_sets: &GroupingSetsSpec,
) -> std::result::Result<(), &'static str> {
    if spec.group_exprs.is_empty() {
        return Err("empty_grouping_sets_keys");
    }
    // The grouping id is a u64 mask with one bit per key.
    if spec.group_exprs.len() > 64 {
        return Err("too_many_grouping_sets_keys");
    }
    if grouping_sets.groups.is_empty() {
        return Err("empty_grouping_sets");
    }
    if grouping_sets.grouping_id_alias.is_empty() {
        return Err("empty_grouping_id_alias");
    }
    if grouping_sets.groups.iter().any(|group| group.len() != spec.group_exprs.len()) {
        return Err("grouping_set_mask_length_mismatch");
    }
    if spec.aggr_exprs.iter().any(|expr| expr.function.is_floating_statistical()) {
        return Err("grouping_sets_floating_statistical_aggregate_not_supported");
    }
    Ok(())
}

fn sort_node_executable_decision(node_id: NodeId, sort: &SortNode) -> CapabilityDecision {
    if sort.spec.expressions.is_empty() {
        return CapabilityDecision::rejected(
            Reason::UnsupportedSortNodeCapability,
            [("node_id", node_id.to_string()), ("reason", "empty_sort_expressions".to_string())],
        );
    }
    for sort_expr in &sort.spec.expressions {
        if let Err(rejection) = validate_column_expr_capability(&sort_expr.expr) {
            return expression_rejection_decision(
                node_id,
                Reason::UnsupportedSortExpressionCapability,
                &sort_expr.expr,
                rejection,
            );
        }
    }
    CapabilityDecision::Satisfied
}

/// Whether a join executor form runs the join and evaluates its residual.
/// The IR and type rules every node meets come from [`check_node`] after it.
fn join_node_executable_decision(node_id: NodeId, join: &JoinNode) -> CapabilityDecision {
    // A residual that only a post-join filter evaluates rejects a non-inner
    // join after the residual expression is checked; every other structural
    // miss rejects first.
    let form = match classify_join(node_id, join) {
        Err(rejection) if rejection.reason != Reason::PostJoinResidualFilterRequiresOneShotEquiJoin => {
            return CapabilityDecision::Rejected { reason: rejection.reason, facts: rejection.facts };
        },
        form => form,
    };
    if let Some(predicate) = &join.spec.residual_predicate {
        let residual_capability = validate_join_residual_expr_capability(predicate).or_else(|rejection| {
            if matches!(form, Ok(JoinForm::KeyedEquiPostFilter)) {
                validate_post_join_residual_expr_capability(predicate)
            } else {
                Err(rejection)
            }
        });
        if let Err(rejection) = residual_capability {
            return expression_rejection_decision(
                node_id,
                Reason::UnsupportedJoinResidualExpressionCapability,
                predicate,
                rejection,
            );
        }
    }
    match form {
        Ok(_) => CapabilityDecision::Satisfied,
        Err(rejection) => CapabilityDecision::Rejected { reason: rejection.reason, facts: rejection.facts },
    }
}

/// Checks the edge-list binding against the cuGraph instantiations the engine
/// links: vertex columns are `Int32` or `Int64` (`vertex_t`), weights are
/// `Float32` or `Float64` (`weight_t`), and an edge id column has the vertex
/// type because `edge_t` equals `vertex_t` in those instantiations. The node's
/// declared output schema must equal the one derived for the vertex type, so a
/// frontend cannot declare a result shape the algorithm does not produce.
fn graph_algorithm_node_executable_decision(
    plan: &QueryPlan,
    node_id: NodeId,
    graph: &GraphAlgorithmNode,
) -> CapabilityDecision {
    if let Some(required) = graph.spec.algorithm.definition().required_graph_directedness()
        && graph.spec.graph_options.directed != required
    {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "unsupported_graph_directedness".to_string()),
                ("algorithm", graph.spec.algorithm.as_str().to_string()),
                ("directed", graph.spec.graph_options.directed.to_string()),
                ("required_directed", required.to_string()),
            ],
        );
    }
    let Some(input) = plan.node(graph.edge_input()) else {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [("node_id", node_id.to_string()), ("reason", "missing_input_node".to_string())],
        );
    };
    for (role, column) in [
        ("source", graph.spec.edge_columns.source.as_str()),
        ("destination", graph.spec.edge_columns.destination.as_str()),
    ] {
        let Some(field) = input.output_schema().field_with_name(column).ok() else {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "missing_edge_column".to_string()),
                    ("edge_column_role", role.to_string()),
                    ("column", column.to_string()),
                ],
            );
        };
        if !matches!(field.data_type(), ArrowDataType::Int32 | ArrowDataType::Int64) {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::DataType),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "unsupported_vertex_type".to_string()),
                    ("edge_column_role", role.to_string()),
                    ("column", column.to_string()),
                    ("data_type", format!("{:?}", field.data_type())),
                ],
            );
        }
        let vertex_id_type_support = graph.spec.algorithm.vertex_id_type_support();
        if !vertex_id_type_support.accepts_arrow_data_type(field.data_type()) {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::DataType),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "unsupported_vertex_dtype".to_string()),
                    ("algorithm", graph.spec.algorithm.as_str().to_string()),
                    ("edge_column_role", role.to_string()),
                    ("column", column.to_string()),
                    ("data_type", format!("{:?}", field.data_type())),
                    ("vertex_id_type_support", vertex_id_type_support.as_str().to_string()),
                    ("supported_vertex_dtypes", vertex_id_type_support.supported_dtypes_label().to_string()),
                ],
            );
        }
    }

    if let Some(weight_col) = graph.spec.edge_columns.weight.as_deref() {
        let Some(field) = input.output_schema().field_with_name(weight_col).ok() else {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "missing_edge_column".to_string()),
                    ("edge_column_role", "weight".to_string()),
                    ("column", weight_col.to_string()),
                ],
            );
        };
        if !matches!(field.data_type(), ArrowDataType::Float32 | ArrowDataType::Float64) {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::DataType),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "unsupported_weight_type".to_string()),
                    ("edge_column_role", "weight".to_string()),
                    ("column", weight_col.to_string()),
                    ("data_type", format!("{:?}", field.data_type())),
                ],
            );
        }
    }

    if let Some(edge_id_col) = graph.spec.edge_columns.edge_id.as_deref() {
        let Some(field) = input.output_schema().field_with_name(edge_id_col).ok() else {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "missing_edge_column".to_string()),
                    ("edge_column_role", "edge_id".to_string()),
                    ("column", edge_id_col.to_string()),
                ],
            );
        };
        let source_type = input
            .output_schema()
            .field_with_name(&graph.spec.edge_columns.source)
            .expect("source column was validated above")
            .data_type();
        if field.data_type() != source_type {
            return CapabilityDecision::rejected(
                Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "edge_id_dtype_mismatch".to_string()),
                    ("edge_column_role", "edge_id".to_string()),
                    ("column", edge_id_col.to_string()),
                    ("edge_id_type", format!("{:?}", field.data_type())),
                    ("vertex_type", format!("{source_type:?}")),
                ],
            );
        }
    }

    if let GraphAlgorithmParams::Bfs(params) = &graph.spec.params
        && params.predicates.include_edge_ids.is_some()
        && graph.spec.edge_columns.edge_id.is_none()
    {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "missing_edge_id_binding".to_string()),
                ("predicate_role", "include_edge_ids".to_string()),
                ("missing_binding", "edge_id_col".to_string()),
            ],
        );
    }

    if let GraphAlgorithmParams::Similarity(params) = &graph.spec.params {
        let vertex_type = input
            .output_schema()
            .field_with_name(&graph.spec.edge_columns.source)
            .expect("source column was validated above")
            .data_type();
        let pair_input_decision =
            graph_similarity_pair_input_decision(plan, node_id, &params.vertex_pairs, vertex_type);
        if !matches!(pair_input_decision, CapabilityDecision::Satisfied) {
            return pair_input_decision;
        }
    }

    let vertex_type = input
        .output_schema()
        .field_with_name(&graph.spec.edge_columns.source)
        .expect("source column was validated above")
        .data_type();
    if graph.output_schema().as_ref()
        != graph_result_schema_for_vertex_type(graph.spec.algorithm, &graph.spec.params, vertex_type).as_ref()
    {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "unexpected_output_schema".to_string()),
                ("algorithm", graph.spec.algorithm.as_str().to_string()),
                ("vertex_type", format!("{vertex_type:?}")),
                ("schema_field_count", graph.output_schema().fields().len().to_string()),
            ],
        );
    }

    CapabilityDecision::Satisfied
}

fn graph_similarity_pair_input_decision(
    plan: &QueryPlan,
    node_id: NodeId,
    pair_input: &GraphVertexPairInput,
    vertex_type: &ArrowDataType,
) -> CapabilityDecision {
    let Some(input) = plan.node(algorithm_input(plan, node_id, pair_input.input)) else {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "missing_vertex_pair_input_node".to_string()),
                ("pair_input", pair_input.input.to_string()),
            ],
        );
    };
    let Some(first) = input.output_schema().field_with_name(&pair_input.first_column).ok() else {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "missing_vertex_pair_column".to_string()),
                ("pair_column_role", "first".to_string()),
                ("column", pair_input.first_column.clone()),
            ],
        );
    };
    let Some(second) = input.output_schema().field_with_name(&pair_input.second_column).ok() else {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "missing_vertex_pair_column".to_string()),
                ("pair_column_role", "second".to_string()),
                ("column", pair_input.second_column.clone()),
            ],
        );
    };
    if first.data_type() != second.data_type() {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "vertex_pair_dtype_mismatch".to_string()),
                ("first_column", pair_input.first_column.clone()),
                ("first_type", format!("{:?}", first.data_type())),
                ("second_column", pair_input.second_column.clone()),
                ("second_type", format!("{:?}", second.data_type())),
            ],
        );
    }
    if !matches!(first.data_type(), ArrowDataType::Int32 | ArrowDataType::Int64) {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::DataType),
            [
                ("node_id", node_id.to_string()),
                ("reason", "unsupported_vertex_pair_type".to_string()),
                ("pair_column_role", "first".to_string()),
                ("column", pair_input.first_column.clone()),
                ("data_type", format!("{:?}", first.data_type())),
            ],
        );
    }
    if first.data_type() != vertex_type {
        return CapabilityDecision::rejected(
            Reason::UnsupportedGraphAlgorithmNodeCapability(UnsupportedPart::Operator),
            [
                ("node_id", node_id.to_string()),
                ("reason", "vertex_pair_graph_vertex_dtype_mismatch".to_string()),
                ("first_column", pair_input.first_column.clone()),
                ("pair_type", format!("{:?}", first.data_type())),
                ("vertex_type", format!("{vertex_type:?}")),
            ],
        );
    }
    CapabilityDecision::Satisfied
}

/// Checks the same vertex, weight, and edge id column types as the graph
/// algorithm node, since the normalized edge list feeds cuGraph construction.
/// A weight combiner that reads weights requires a weight column.
fn edge_normalize_node_executable_decision(
    plan: &QueryPlan,
    node_id: NodeId,
    edge_normalize: &EdgeNormalizeNode,
) -> CapabilityDecision {
    let Some(input) = plan.node(edge_normalize.input) else {
        return CapabilityDecision::rejected(
            Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::Operator),
            [("node_id", node_id.to_string()), ("reason", "missing_input_node".to_string())],
        );
    };
    for (role, column) in [
        ("source", edge_normalize.spec.edge_columns.source.as_str()),
        ("destination", edge_normalize.spec.edge_columns.destination.as_str()),
    ] {
        let Some(field) = input.output_schema().field_with_name(column).ok() else {
            return CapabilityDecision::rejected(
                Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "missing_edge_column".to_string()),
                    ("edge_column_role", role.to_string()),
                    ("column", column.to_string()),
                ],
            );
        };
        if !matches!(field.data_type(), ArrowDataType::Int32 | ArrowDataType::Int64) {
            return CapabilityDecision::rejected(
                Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::DataType),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "unsupported_vertex_type".to_string()),
                    ("edge_column_role", role.to_string()),
                    ("column", column.to_string()),
                    ("data_type", format!("{:?}", field.data_type())),
                ],
            );
        }
    }

    if matches!(
        edge_normalize.spec.weight_combiner,
        EdgeWeightCombiner::MinWeight | EdgeWeightCombiner::KeepLastInputOrder
    ) && edge_normalize.spec.edge_columns.weight.is_none()
    {
        return CapabilityDecision::rejected(
            Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::Operator),
            [("node_id", node_id.to_string()), ("reason", "weight_combiner_without_weight_column".to_string())],
        );
    }

    if let Some(weight_col) = edge_normalize.spec.edge_columns.weight.as_deref() {
        let Some(field) = input.output_schema().field_with_name(weight_col).ok() else {
            return CapabilityDecision::rejected(
                Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "missing_edge_column".to_string()),
                    ("edge_column_role", "weight".to_string()),
                    ("column", weight_col.to_string()),
                ],
            );
        };
        if !matches!(field.data_type(), ArrowDataType::Float32 | ArrowDataType::Float64) {
            return CapabilityDecision::rejected(
                Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::DataType),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "unsupported_weight_type".to_string()),
                    ("edge_column_role", "weight".to_string()),
                    ("column", weight_col.to_string()),
                    ("data_type", format!("{:?}", field.data_type())),
                ],
            );
        }
    }

    if let Some(edge_id_col) = edge_normalize.spec.edge_columns.edge_id.as_deref() {
        let Some(field) = input.output_schema().field_with_name(edge_id_col).ok() else {
            return CapabilityDecision::rejected(
                Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::Operator),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "missing_edge_column".to_string()),
                    ("edge_column_role", "edge_id".to_string()),
                    ("column", edge_id_col.to_string()),
                ],
            );
        };
        if !matches!(field.data_type(), ArrowDataType::Int32 | ArrowDataType::Int64) {
            return CapabilityDecision::rejected(
                Reason::UnsupportedEdgeNormalizeNodeCapability(UnsupportedPart::DataType),
                [
                    ("node_id", node_id.to_string()),
                    ("reason", "unsupported_edge_id_type".to_string()),
                    ("edge_column_role", "edge_id".to_string()),
                    ("column", edge_id_col.to_string()),
                    ("data_type", format!("{:?}", field.data_type())),
                ],
            );
        }
    }

    CapabilityDecision::Satisfied
}