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
//! Admission boundary for compiled bounded pipelines.
//!
//! Capability-first bounded-pipeline admission evidence and rejections. This
//! is the single bounded admission contract; "capability" appears only in
//! preserved wire names. Runtime execution remains in
//! `exec::bounded`.

use std::{collections::BTreeMap, sync::Arc};

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

use super::shared::{
    CapabilityFirstCandidateStatus, CapabilityFirstPipelineAdmission, capability_first_bounded_pipeline_admission,
};
use crate::{
    backend::AnalysisContext,
    capability::{Reason, Rejection, RejectionOwner, UnsupportedPart},
    error::{QueryError, QueryErrorCode, QueryErrorKind, Result},
    expr::{AggregateFunction, AggregateSpec, Expr},
    mode::{CapabilityEnvelopeStatus, StatisticalAggregateExecutionMode},
    pipeline::CompiledPipeline,
    plan::{NodeId, PlanFingerprint, PlanNode, QueryPlan, ResultSink},
    policy::PlanningPolicy,
    prepared::{
        DataFusionExactHostAccumulator, aggregate_spec_has_floating_statistical_aggregate,
        datafusion_exact_host_accumulator, datafusion_exact_host_group_key_supported,
    },
    validation::validate_query_plan,
};

/// A plan that passed validation, capability analysis, compilation and the
/// compiled contracts, each checked exactly once.
///
/// Only [`admit`] and [`AdmittedPlan::with_pruned_sources`] construct it.
/// Execution and reporting read this artifact and do not decide again. It
/// owns the plan it admitted, so no other plan, sink or mode can reach the
/// pipeline it carries.
#[derive(Debug)]
pub struct AdmittedPlan {
    plan: QueryPlan,
    pipeline: CompiledPipeline,
    fingerprint: PlanFingerprint,
    statistical_aggregate_execution_mode: StatisticalAggregateExecutionMode,
    policy: PlanningPolicy,
}

impl AdmittedPlan {
    /// The admitted plan, in the sink form execution runs.
    pub fn plan(&self) -> &QueryPlan {
        &self.plan
    }

    /// Compiled pipeline admitted for bounded execution.
    pub fn pipeline(&self) -> &CompiledPipeline {
        &self.pipeline
    }

    /// Execution identity of the admitted plan.
    pub const fn fingerprint(&self) -> PlanFingerprint {
        self.fingerprint
    }

    /// Execution mode the admission decision was made under.
    pub const fn statistical_aggregate_execution_mode(&self) -> StatisticalAggregateExecutionMode {
        self.statistical_aggregate_execution_mode
    }

    /// Execution policy the pipeline was admitted under; execution reads its
    /// join and dynamic-filter thresholds from here.
    pub const fn policy(&self) -> PlanningPolicy {
        self.policy
    }

    /// Admits the plan footer pruning resolved from this one.
    ///
    /// Resolution may narrow Parquet row-group selections and append
    /// reader-predicate conjuncts; any other difference is an internal
    /// invariant violation. The resolved plan is admitted again under this
    /// artifact's mode and policy, and a rejection is the typed execution
    /// error, never a fallback.
    pub fn with_pruned_sources(&self, pruned: QueryPlan, ctx: AnalysisContext<'_>) -> Result<Arc<Self>> {
        if let Some(changed) = self.plan.first_change_beyond_source_refinement(&pruned) {
            return Err(QueryError::new(
                QueryErrorCode::InternalInvariantViolation,
                "query_engine.admission.pruned_sources",
                "footer pruning changed more than source row-group selections and reader predicates",
            )
            .with_fact("changed", changed)
            .with_fact("admitted_plan_fingerprint", self.fingerprint.to_string()));
        }
        match admit(&pruned, self.statistical_aggregate_execution_mode, self.policy, ctx)? {
            AdmissionDecision::Admitted(admitted) => Ok(admitted),
            AdmissionDecision::Rejected(rejection) => Err(rejection.execution_error(&pruned)),
        }
    }
}

/// The one admission decision for a native plan: it runs as a compiled
/// pipeline, or it is rejected with the first contract it misses.
#[derive(Debug)]
pub enum AdmissionDecision {
    /// The compiled pipeline the plan executes as.
    Admitted(Arc<AdmittedPlan>),
    /// No compiled contract covers the plan.
    Rejected(AdmissionRejection),
}

impl AdmissionDecision {
    /// Admitted plan, when admitted.
    pub fn admission(&self) -> Option<&Arc<AdmittedPlan>> {
        match self {
            Self::Admitted(admission) => Some(admission),
            Self::Rejected(_) => None,
        }
    }

    /// Rejection evidence, when rejected.
    pub fn rejection(&self) -> Option<&Rejection> {
        match self {
            Self::Admitted(_) => None,
            Self::Rejected(rejection) => Some(rejection.rejection()),
        }
    }
}

/// Why a plan has no compiled execution.
#[derive(Debug, Clone)]
pub struct AdmissionRejection {
    rejection: Rejection,
    pipeline: Option<Box<CompiledPipeline>>,
    cause: RejectionCause,
}

/// Which contract refused the plan; it selects the error a caller reports.
#[derive(Debug, Clone)]
enum RejectionCause {
    /// Capability analysis, compilation or structural support refused it.
    CompiledContract,
    /// The requested statistical aggregate execution mode cannot run it.
    ExecutionMode,
    /// The validator found the plan well formed but outside what the backend
    /// supports. The error keeps the validator's code, operation, message,
    /// and facts.
    Validation(Arc<QueryError>),
}

impl AdmissionRejection {
    /// The first operator, edge, or pipeline contract the plan misses.
    pub fn rejection(&self) -> &Rejection {
        &self.rejection
    }

    /// Whether the requested statistical aggregate execution mode refuses the
    /// plan. A compiled-contract refusal and a validation refusal both return
    /// `false`.
    pub fn is_execution_mode_refusal(&self) -> bool {
        matches!(self.cause, RejectionCause::ExecutionMode)
    }

    /// The unsupported-construct error plan validation reported, when
    /// validation refused the plan.
    pub fn validation_error(&self) -> Option<&QueryError> {
        match &self.cause {
            RejectionCause::Validation(error) => Some(error),
            RejectionCause::CompiledContract | RejectionCause::ExecutionMode => None,
        }
    }

    /// The typed error a caller that must execute `plan` reports. A
    /// validation refusal reports the validator's own error.
    pub fn execution_error(&self, plan: &QueryPlan) -> QueryError {
        match &self.cause {
            RejectionCause::Validation(error) => error
                .facts()
                .iter()
                .fold(QueryError::new(error.code(), error.operation(), error.message()), |rebuilt, (key, value)| {
                    rebuilt.with_fact(key, value)
                }),
            RejectionCause::CompiledContract | RejectionCause::ExecutionMode => {
                compiled_bounded_pipeline_required_error(plan, self.pipeline.as_deref(), &self.rejection)
            },
        }
    }

    fn validation(error: QueryError) -> Self {
        let mut facts = error.facts().iter().map(|(key, value)| (key, value.to_string())).collect::<BTreeMap<_, _>>();
        facts.insert("validation_code", error.code().as_str().to_string());
        facts.insert("validation_operation", error.operation().to_string());
        Self {
            rejection: Rejection {
                owner: RejectionOwner::Plan,
                reason: Reason::PlanValidation(UnsupportedPart::from_code(error.code())),
                facts,
            },
            pipeline: None,
            cause: RejectionCause::Validation(Arc::new(error)),
        }
    }
}

/// Decides once whether `plan` runs as a compiled pipeline under the given
/// execution mode and policy.
///
/// The plan is validated first. A malformed plan has no correct fallback and
/// is the validator's error; a well-formed plan that uses a type, operator,
/// or expression the backend does not support is rejected with the
/// validator's code and operation in the rejection facts.
pub fn admit(
    plan: &QueryPlan,
    statistical_aggregate_execution_mode: StatisticalAggregateExecutionMode,
    policy: PlanningPolicy,
    ctx: AnalysisContext<'_>,
) -> Result<AdmissionDecision> {
    let ctx = ctx.with_policy(policy);
    if let Err(error) = validate_query_plan(plan, ctx) {
        if error.kind() != QueryErrorKind::Unsupported {
            return Err(error);
        }
        return Ok(AdmissionDecision::Rejected(AdmissionRejection::validation(error)));
    }
    let admission = capability_first_bounded_pipeline_admission(plan, ctx)?;
    // The mode check runs before the candidate status is read, so a mode
    // refusal is reported ahead of a compiled-contract rejection;
    // `is_execution_mode_refusal` lets a frontend that may fall back tell the
    // two apart.
    if let Some(rejection) = statistical_aggregate_execution_mode_rejection(plan, statistical_aggregate_execution_mode)
    {
        return Ok(AdmissionDecision::Rejected(AdmissionRejection {
            rejection,
            pipeline: None,
            cause: RejectionCause::ExecutionMode,
        }));
    }
    match admission.candidate_status() {
        CapabilityFirstCandidateStatus::Admitted => {
            let admitted =
                admitted_plan_from_capability_first(admission, plan, statistical_aggregate_execution_mode, policy)?;
            Ok(AdmissionDecision::Admitted(Arc::new(admitted)))
        },
        CapabilityFirstCandidateStatus::Rejected => {
            let rejection =
                admission.rejection().expect("rejected capability-first admission should carry a rejection").clone();
            Ok(AdmissionDecision::Rejected(AdmissionRejection {
                rejection,
                pipeline: admission.into_pipeline().map(Box::new),
                cause: RejectionCause::CompiledContract,
            }))
        },
    }
}

/// Rejects a plan whose floating statistical aggregates the selected execution
/// mode cannot run.
///
/// Only [`StatisticalAggregateExecutionMode::DataFusionExactHostWelford`]
/// narrows support: its host kernel reproduces DataFusion's accumulators for a
/// fixed set of group-key and aggregate types. A frontend that may fall back
/// keeps the other types on DataFusion, and rejecting at admission lets that
/// fallback happen before execution starts.
fn statistical_aggregate_execution_mode_rejection(
    plan: &QueryPlan,
    statistical_aggregate_execution_mode: StatisticalAggregateExecutionMode,
) -> Option<Rejection> {
    if !statistical_aggregate_execution_mode.is_datafusion_exact() {
        return None;
    }
    plan.nodes().iter().find_map(|node| {
        let PlanNode::Aggregate(aggregate) = &node.kind else {
            return None;
        };
        if !aggregate_spec_has_floating_statistical_aggregate(&aggregate.spec) {
            return None;
        }
        let input = plan.node(aggregate.input)?;
        datafusion_exact_host_aggregate_rejection(node.id, &aggregate.spec, input.output_schema(), node.output_schema())
    })
}

fn datafusion_exact_host_aggregate_rejection(
    node_id: NodeId,
    spec: &AggregateSpec,
    input_schema: &Schema,
    output_schema: &Schema,
) -> Option<Rejection> {
    let mut output_types = output_schema.fields().iter().map(|field| field.data_type());
    for expr in &spec.group_exprs {
        let input_type = column_input_type(expr, input_schema);
        let output_type = output_types.next();
        if let (Some(input), Some(output)) = (input_type, output_type)
            && datafusion_exact_host_group_key_supported(input, output)
        {
            continue;
        }
        return Some(datafusion_exact_host_rejection(
            node_id,
            Reason::DatafusionExactStatisticalGroupKeyUnsupported,
            [
                ("group_key", expr.to_string()),
                ("input_data_type", data_type_label(input_type)),
                ("output_data_type", data_type_label(output_type)),
            ],
        ));
    }
    for expr in &spec.aggr_exprs {
        let input_type = column_input_type(&expr.expr, input_schema);
        let output_type = output_types.next();
        match output_type.and_then(|output| datafusion_exact_host_accumulator(expr, input_type, output)) {
            // DataFusion's ungrouped SUM and AVG add each input batch with
            // Arrow's lane-wise sum kernel, so their floating-point result
            // depends on batch boundaries; its grouped accumulators, like the
            // host kernel, add one row at a time.
            Some(DataFusionExactHostAccumulator::FloatSum | DataFusionExactHostAccumulator::Avg)
                if spec.group_exprs.is_empty() =>
            {
                return Some(datafusion_exact_host_rejection(
                    node_id,
                    Reason::DatafusionExactUngroupedFloatSumUnsupported,
                    [
                        ("aggregate_function", expr.function.as_str().to_string()),
                        ("aggregate_alias", expr.alias.clone()),
                    ],
                ));
            },
            Some(_) => continue,
            None => {},
        }
        let part = if expr.distinct || matches!(expr.function, AggregateFunction::NUnique | AggregateFunction::Median) {
            UnsupportedPart::Operator
        } else {
            UnsupportedPart::DataType
        };
        return Some(datafusion_exact_host_rejection(
            node_id,
            Reason::DatafusionExactStatisticalAggregateUnsupported(part),
            [
                ("aggregate_function", expr.function.as_str().to_string()),
                ("aggregate_alias", expr.alias.clone()),
                ("distinct", expr.distinct.to_string()),
                ("input_data_type", data_type_label(input_type)),
                ("output_data_type", data_type_label(output_type)),
            ],
        ));
    }
    None
}

fn datafusion_exact_host_rejection(
    node_id: NodeId,
    reason: Reason,
    facts: impl IntoIterator<Item = (&'static str, String)>,
) -> Rejection {
    let mut facts = BTreeMap::from_iter(facts);
    facts.insert(
        "statistical_aggregate_execution_mode",
        StatisticalAggregateExecutionMode::DataFusionExactHostWelford.as_str().to_string(),
    );
    Rejection { owner: RejectionOwner::Node(node_id), reason, facts }
}

fn column_input_type<'a>(expr: &Expr, schema: &'a Schema) -> Option<&'a ArrowDataType> {
    match expr {
        Expr::Column(column) => schema.field_with_name(column.name()).ok().map(|field| field.data_type()),
        Expr::Alias { expr, .. } => column_input_type(expr, schema),
        _ => None,
    }
}

fn data_type_label(data_type: Option<&ArrowDataType>) -> String {
    data_type.map_or_else(|| "none".to_string(), |data_type| format!("{data_type:?}"))
}

fn admitted_plan_from_capability_first(
    admission: CapabilityFirstPipelineAdmission,
    plan: &QueryPlan,
    statistical_aggregate_execution_mode: StatisticalAggregateExecutionMode,
    policy: PlanningPolicy,
) -> Result<AdmittedPlan> {
    let pipeline = admission.into_pipeline().ok_or_else(|| {
        QueryError::new(
            QueryErrorCode::InvalidPlan,
            "query_engine.bounded.capability_admission",
            "admitted capability candidate must include a compiled pipeline",
        )
        .with_fact("execution_mode", CapabilityEnvelopeStatus::Admitted.as_str())
        .with_fact("candidate_status", "admitted")
    })?;
    Ok(AdmittedPlan {
        fingerprint: plan.execution_fingerprint(),
        plan: plan.clone(),
        pipeline,
        statistical_aggregate_execution_mode,
        policy,
    })
}

fn compiled_bounded_pipeline_required_error(
    plan: &QueryPlan,
    pipeline: Option<&CompiledPipeline>,
    rejection: &Rejection,
) -> QueryError {
    let sink_kind = match plan.sink() {
        ResultSink::GpuResident { .. } | ResultSink::DeferredHostArrow { .. } => "gpu_resident",
        ResultSink::HostArrow { .. } => "host_arrow",
    };
    let mut error = QueryError::new(
        QueryErrorCode::UnsupportedPipelineContract,
        "query_engine.bounded.compiled_pipeline_required",
        "bounded execution requires an admitted compiled pipeline",
    )
    .with_fact("compiled_pipeline_required", "true")
    .with_fact("compiled_pipeline_rejection_reason", rejection.reason)
    .with_fact("result_sink_kind", sink_kind);

    if let Some(pipeline) = pipeline {
        error = error
            .with_fact("compiled_task_count", pipeline.tasks().len())
            .with_fact("compiled_edge_count", pipeline.edges().len())
            .with_fact("compiled_barrier_count", pipeline.barriers().len())
            .with_fact("compiled_resource_count", pipeline.resources().len())
            .with_fact("compiled_sink_kind", pipeline.sink().kind().as_str());
    }

    // Rejection facts may repeat pipeline-derived keys. The pipeline values
    // above are authoritative; only absent keys are added here.
    for (key, value) in &rejection.facts {
        error = error.with_fact_if_absent(key, value.as_str());
    }

    error
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use arrow::datatypes::{DataType, Field, Schema};

    use super::*;
    use crate::{
        expr::AggregateExpr,
        plan::QueryPlanBuilder,
        source::{LocalParquetSource, SourceNode},
    };

    fn statistical_aggregate_plan(extra: AggregateExpr, extra_output: Field) -> QueryPlan {
        let input_schema = Arc::new(Schema::new(vec![
            Field::new("key", DataType::Utf8, false),
            Field::new("quantity", DataType::Int32, true),
            Field::new("amount", DataType::Int64, true),
            Field::new("sold_date", DataType::Date32, true),
        ]));
        let output_schema = Arc::new(Schema::new(vec![
            Field::new("key", DataType::Utf8, false),
            Field::new("quantity_count", DataType::Int64, false),
            Field::new("quantity_avg", DataType::Float64, true),
            Field::new("quantity_stddev", DataType::Float64, true),
            extra_output,
        ]));
        let mut builder = QueryPlanBuilder::new();
        let source = builder.add_source(SourceNode::LocalParquet(LocalParquetSource::new(
            "/tmp/sales.parquet",
            Arc::clone(&input_schema),
            input_schema.fields().iter().map(|field| field.name().clone()).collect(),
        )));
        let aggregate = builder
            .add_aggregate(
                source,
                AggregateSpec::new(
                    vec![Expr::column("key")],
                    vec![
                        AggregateExpr::new(AggregateFunction::Count, Expr::column("quantity"), "quantity_count"),
                        AggregateExpr::new(AggregateFunction::Avg, Expr::column("quantity"), "quantity_avg"),
                        AggregateExpr::new(AggregateFunction::StddevSamp, Expr::column("quantity"), "quantity_stddev"),
                        extra,
                    ],
                ),
                output_schema,
            )
            .unwrap();
        builder.finish_with_host_sink(aggregate).unwrap()
    }

    #[test]
    fn exact_statistical_mode_rejects_ungrouped_float_sums_that_datafusion_adds_by_batch() {
        let exact = StatisticalAggregateExecutionMode::DataFusionExactHostWelford;
        let input_schema = Arc::new(Schema::new(vec![
            Field::new("quantity", DataType::Int32, true),
            Field::new("amount", DataType::Int64, true),
        ]));
        let ungrouped = |extra: AggregateExpr, extra_output: Field| {
            let mut builder = QueryPlanBuilder::new();
            let source = builder.add_source(SourceNode::LocalParquet(LocalParquetSource::new(
                "/tmp/sales.parquet",
                Arc::clone(&input_schema),
                vec!["quantity".to_string(), "amount".to_string()],
            )));
            let aggregate = builder
                .add_aggregate(
                    source,
                    AggregateSpec::new(
                        Vec::new(),
                        vec![
                            AggregateExpr::new(
                                AggregateFunction::StddevSamp,
                                Expr::column("quantity"),
                                "quantity_stddev",
                            ),
                            extra,
                        ],
                    ),
                    Arc::new(Schema::new(vec![Field::new("quantity_stddev", DataType::Float64, true), extra_output])),
                )
                .unwrap();
            builder.finish_with_host_sink(aggregate).unwrap()
        };

        let avg = ungrouped(
            AggregateExpr::new(AggregateFunction::Avg, Expr::column("quantity"), "quantity_avg"),
            Field::new("quantity_avg", DataType::Float64, true),
        );
        let rejection = statistical_aggregate_execution_mode_rejection(&avg, exact)
            .expect("an ungrouped AVG sums by batch in DataFusion");
        assert_eq!(rejection.reason, Reason::DatafusionExactUngroupedFloatSumUnsupported);
        assert_eq!(rejection.facts.get("aggregate_alias").map(String::as_str), Some("quantity_avg"));

        let integer_sum = ungrouped(
            AggregateExpr::new(AggregateFunction::Sum, Expr::column("amount"), "amount_sum"),
            Field::new("amount_sum", DataType::Int64, true),
        );
        assert!(statistical_aggregate_execution_mode_rejection(&integer_sum, exact).is_none());
    }

    #[test]
    fn exact_statistical_mode_rejects_only_aggregates_outside_the_host_kernel() {
        let exact = StatisticalAggregateExecutionMode::DataFusionExactHostWelford;
        let integer_sum = statistical_aggregate_plan(
            AggregateExpr::new(AggregateFunction::Sum, Expr::column("amount"), "amount_sum"),
            Field::new("amount_sum", DataType::Int64, true),
        );
        assert!(statistical_aggregate_execution_mode_rejection(&integer_sum, exact).is_none());

        let date_min = statistical_aggregate_plan(
            AggregateExpr::new(AggregateFunction::Min, Expr::column("sold_date"), "first_sold_date"),
            Field::new("first_sold_date", DataType::Date32, true),
        );
        let rejection = statistical_aggregate_execution_mode_rejection(&date_min, exact)
            .expect("the exact host kernel has no Date32 MIN accumulator");
        assert_eq!(rejection.reason, Reason::DatafusionExactStatisticalAggregateUnsupported(UnsupportedPart::DataType));
        assert_eq!(rejection.facts.get("aggregate_alias").map(String::as_str), Some("first_sold_date"));
        assert!(
            statistical_aggregate_execution_mode_rejection(
                &date_min,
                StatisticalAggregateExecutionMode::GpuNativeTolerant
            )
            .is_none()
        );
    }
}