algeon-datafusion 0.1.1

DataFusion physical-plan adapter for native GPU execution.
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
use std::fmt::Write;

use query_planning::mode::StatisticalAggregateExecutionMode;

#[cfg(feature = "server")]
use super::NativeDataFusionExecutionMetrics;
use super::{
    AlgeonNativeExecutionConfig, CandidateOutcomeKind, FinalPlanDisposition, NATIVE_DATAFUSION_EXECUTION_TSV_HEADER,
    NATIVE_DATAFUSION_PLANNING_TSV_HEADER, NATIVE_FALLBACK_PRESSURE_TSV_HEADER, NativeDataFusionExecutionRow,
    NativeDataFusionExecutionWorkloadItem, NativeDataFusionPlanningRow, NativeDataFusionPlanningWorkloadItem,
    NativeDataFusionRuntime, NativeDataFusionRuntimeSummary, NativeFallbackPressureRow,
    NativeFallbackPressureWorkloadItem, TsvRow, WorkloadDatasetMetadata, datafusion_execution_rows_for_item,
    datafusion_planning_rows_for_item, report_rows_for_item, write_metric_detail_completeness,
    write_workload_dataset_metadata,
};

/// In-memory fallback-pressure report.
///
/// Rows describe native candidate acceptance or rejection before execution.
/// This collector runs candidate-only lowering without cost selection. Its
/// rows cover only the producer subset (`Selected`, `NotSupported`). The shared
/// [`CandidateOutcomeKind`] projection still covers cost outcomes without a
/// narrowed enum.
/// TSV serialization writes optional workload metadata, then
/// [`NATIVE_FALLBACK_PRESSURE_TSV_HEADER`].
/// Optional planning fields serialize as `none`; missing dataset content
/// fingerprints serialize as `not_computed`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeFallbackPressureReport {
    /// Candidate rows in workload order. One query can produce multiple rows.
    pub rows: Vec<NativeFallbackPressureRow>,
    /// Optional fixture metadata written as `# workload_*` TSV header lines.
    pub workload_dataset: Option<WorkloadDatasetMetadata>,
}

impl NativeFallbackPressureReport {
    /// Collects diagnostic fallback-pressure rows using the canonical bounded
    /// native execution config.
    ///
    /// This convenience path uses the canonical bounded execution config.
    /// Call [`Self::collect_with_native_execution_config`] with an explicit
    /// config when its planning settings matter to the report.
    pub fn collect(items: impl IntoIterator<Item = NativeFallbackPressureWorkloadItem>) -> Self {
        let execution_config = AlgeonNativeExecutionConfig::default();
        Self::collect_with_native_execution_config(items, &execution_config)
    }

    /// Collects fallback-pressure rows using an explicit native execution config.
    ///
    /// Capability-contract evidence is emitted by the canonical planning
    /// report surfaces.
    pub fn collect_with_native_execution_config(
        items: impl IntoIterator<Item = NativeFallbackPressureWorkloadItem>,
        execution_config: &AlgeonNativeExecutionConfig,
    ) -> Self {
        let rows = items.into_iter().flat_map(|item| report_rows_for_item(item, execution_config)).collect();
        Self { rows, workload_dataset: None }
    }

    /// Attaches fixture metadata to comment lines before the TSV header.
    ///
    /// Free-form metadata text is sanitized for TSV comments by replacing tabs
    /// and line breaks with spaces.
    pub fn with_workload_dataset_metadata(mut self, metadata: WorkloadDatasetMetadata) -> Self {
        self.workload_dataset = Some(metadata);
        self
    }

    /// Number of accepted candidate rows; one query id can appear in several rows.
    pub fn selected_count(&self) -> usize {
        self.rows.iter().filter(|row| matches!(row.outcome, Some(CandidateOutcomeKind::Selected))).count()
    }

    /// Number of rejected candidate rows; one query id can appear in several rows.
    pub fn not_selected_count(&self) -> usize {
        self.rows
            .iter()
            .filter(|row| {
                matches!(
                    row.outcome,
                    Some(CandidateOutcomeKind::NotSupported | CandidateOutcomeKind::NotSelectedByCost)
                )
            })
            .count()
    }

    /// Serializes the report as TSV with metadata comments followed by rows.
    pub fn to_tsv(&self) -> String {
        let mut output = String::new();
        write_workload_dataset_metadata(&mut output, self.workload_dataset.as_ref());
        debug_assert_eq!(NativeFallbackPressureRow::COLUMNS.join("\t"), NATIVE_FALLBACK_PRESSURE_TSV_HEADER);
        output.push_str(NATIVE_FALLBACK_PRESSURE_TSV_HEADER);
        output.push('\n');
        for row in &self.rows {
            let _ = writeln!(output, "{}", row.to_tsv_line());
        }
        output
    }
}

/// In-memory DataFusion planning report after the native optimizer rule.
///
/// Rows describe post-optimizer native coverage and structured fallback
/// reasons. TSV serialization preserves header order.
/// Optional planning fields serialize as `none`; missing dataset content
/// fingerprints serialize as `not_computed`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeDataFusionPlanningReport {
    /// Per-query planning rows in workload order.
    pub rows: Vec<NativeDataFusionPlanningRow>,
    /// Optional fixture metadata written as `# workload_*` TSV header lines.
    pub workload_dataset: Option<WorkloadDatasetMetadata>,
}

impl NativeDataFusionPlanningReport {
    /// Projects each planned candidate or planning failure into rows in workload order.
    ///
    /// Collection reads retained plans and diagnostics; it does not plan or execute queries.
    pub fn collect(items: impl IntoIterator<Item = NativeDataFusionPlanningWorkloadItem>) -> Self {
        let rows = items.into_iter().flat_map(datafusion_planning_rows_for_item).collect();
        Self { rows, workload_dataset: None }
    }

    /// Attaches fixture metadata to comment lines before the TSV header.
    ///
    /// Free-form metadata text is sanitized for TSV comments by replacing tabs
    /// and line breaks with spaces.
    pub fn with_workload_dataset_metadata(mut self, metadata: WorkloadDatasetMetadata) -> Self {
        self.workload_dataset = Some(metadata);
        self
    }

    /// Number of planning rows where native planning replaced at least one fragment.
    pub fn selected_count(&self) -> usize {
        self.rows.iter().filter(|row| matches!(row.outcome, Some(CandidateOutcomeKind::Selected))).count()
    }

    /// Number of planning rows rejected before native execution.
    pub fn not_selected_count(&self) -> usize {
        self.rows
            .iter()
            .filter(|row| row.outcome.is_some_and(|outcome| outcome != CandidateOutcomeKind::Selected))
            .count()
    }

    /// Serializes the report as TSV with metadata comments followed by rows.
    pub fn to_tsv(&self) -> String {
        let mut output = String::new();
        write_workload_dataset_metadata(&mut output, self.workload_dataset.as_ref());
        debug_assert_eq!(NativeDataFusionPlanningRow::COLUMNS.join("\t"), NATIVE_DATAFUSION_PLANNING_TSV_HEADER);
        output.push_str(NATIVE_DATAFUSION_PLANNING_TSV_HEADER);
        output.push('\n');
        for row in &self.rows {
            let _ = writeln!(output, "{}", row.to_tsv_line());
        }
        output
    }
}

/// In-memory DataFusion execution report.
///
/// Rows combine native planning diagnostics, runtime outcome, output counters,
/// and projected native metrics. `not_run` marks output counters for rows
/// where native runtime did not start; optional metric observations use `none`.
/// Missing dataset content fingerprints serialize as `not_computed`.
#[derive(Debug, Clone)]
pub struct NativeDataFusionExecutionReport {
    /// Per-query execution rows in workload order.
    pub rows: Vec<NativeDataFusionExecutionRow>,
    /// Optional fixture metadata written as `# workload_*` TSV header lines.
    pub workload_dataset: Option<WorkloadDatasetMetadata>,
    /// Floating statistical aggregate contract selected for native execution.
    pub statistical_aggregate_execution_mode: Option<StatisticalAggregateExecutionMode>,
}

impl NativeDataFusionExecutionReport {
    /// Projects supplied plans, runtime outcomes, and failures into rows in workload order.
    ///
    /// Collection does not execute queries. Dataset and statistical-mode metadata
    /// are unset until attached by their builder methods.
    pub fn collect(items: impl IntoIterator<Item = NativeDataFusionExecutionWorkloadItem>) -> Self {
        let rows = items.into_iter().flat_map(datafusion_execution_rows_for_item).collect();
        Self { rows, workload_dataset: None, statistical_aggregate_execution_mode: None }
    }

    /// Attaches fixture metadata to comment lines before the TSV header.
    ///
    /// Free-form metadata text is sanitized for TSV comments by replacing tabs
    /// and line breaks with spaces.
    pub fn with_workload_dataset_metadata(mut self, metadata: WorkloadDatasetMetadata) -> Self {
        self.workload_dataset = Some(metadata);
        self
    }

    /// Attaches the native floating statistical aggregate contract to TSV metadata.
    pub fn with_statistical_aggregate_execution_mode(mut self, mode: StatisticalAggregateExecutionMode) -> Self {
        self.statistical_aggregate_execution_mode = Some(mode);
        self
    }

    /// Number of rows where native runtime was invoked.
    pub fn runtime_started_count(&self) -> usize {
        self.rows.iter().filter(|row| row.native_runtime_started).count()
    }

    /// Number of rows rejected before native runtime started.
    pub fn rejected_before_execution_count(&self) -> usize {
        self.rows.iter().filter(|row| row.rejected_before_execution).count()
    }

    /// Serializes the report as TSV with metadata comments followed by rows.
    pub fn to_tsv(&self) -> String {
        let mut output = String::new();
        if let Some(mode) = self.statistical_aggregate_execution_mode {
            let _ = writeln!(output, "# statistical_aggregate_execution_mode={}", mode.as_str());
            let _ =
                writeln!(output, "# statistical_aggregate_tolerance_contract={}", mode.tolerance_contract().as_str());
        }
        write_workload_dataset_metadata(&mut output, self.workload_dataset.as_ref());
        write_metric_detail_completeness(&mut output, &self.rows);
        debug_assert_eq!(NativeDataFusionExecutionRow::COLUMNS.join("\t"), NATIVE_DATAFUSION_EXECUTION_TSV_HEADER);
        output.push_str(NATIVE_DATAFUSION_EXECUTION_TSV_HEADER);
        output.push('\n');
        for row in &self.rows {
            let _ = writeln!(output, "{}", row.to_tsv_line());
        }
        output
    }
}

/// Compact execution-report template retained by the server after planning.
///
/// Planning-only fields are flattened into exact-size strings so the original
/// physical plan and diagnostics can be dropped before execution. Runtime
/// fields are filled from the runtime outcome supplied when the artifact is
/// rendered.
#[derive(Debug)]
pub struct NativeDataFusionExecutionTsvTemplate {
    rows: Box<[NativeDataFusionExecutionTsvTemplateRow]>,
    retained_capacity_bytes: u64,
}

#[derive(Debug)]
struct NativeDataFusionExecutionTsvTemplateRow {
    prefix: Box<[Box<str>]>,
    rejected_before_execution: bool,
}

impl NativeDataFusionExecutionTsvTemplate {
    const QUERY_ID_FIELD: usize = 0;
    const RUNTIME_FIELD_START: usize = 15;

    /// Consumes a report and retains only the flattened planning fields needed for later TSV rendering.
    ///
    /// Dataset and statistical-mode metadata are not retained by this compact template.
    pub fn from_report(report: NativeDataFusionExecutionReport) -> Self {
        let rows = report
            .rows
            .into_iter()
            .map(|row| {
                let rejected_before_execution = row.rejected_before_execution;
                let fields = row.fields();
                debug_assert_eq!(fields.len(), NativeDataFusionExecutionRow::COLUMNS.len());
                let prefix = fields
                    .into_iter()
                    .take(Self::RUNTIME_FIELD_START)
                    .map(String::into_boxed_str)
                    .collect::<Vec<_>>()
                    .into_boxed_slice();
                NativeDataFusionExecutionTsvTemplateRow { prefix, rejected_before_execution }
            })
            .collect::<Vec<_>>()
            .into_boxed_slice();
        let retained_capacity_bytes = rows.iter().fold(0u64, |total, row| {
            let strings = row.prefix.iter().fold(0u64, |bytes, field| bytes.saturating_add(field.len() as u64));
            total.saturating_add(strings).saturating_add((row.prefix.len() * std::mem::size_of::<Box<str>>()) as u64)
        });
        Self { rows, retained_capacity_bytes }
    }

    /// Returns logical retained bytes for the flattened strings and their pointer arrays.
    ///
    /// This diagnostic count does not measure allocator overhead or process resident memory.
    pub const fn retained_capacity_bytes(&self) -> u64 {
        self.retained_capacity_bytes
    }

    /// Renders the execution TSV header and rows, filling runtime fields from the supplied outcome.
    pub fn to_tsv(&self, runtime: NativeDataFusionRuntime) -> String {
        self.render(NativeDataFusionRuntimeSummary::from_runtime(runtime))
    }

    /// Renders `runtime` with native metrics already projected from a sealed
    /// attempt. They replace any metrics carried by `runtime`, except that a
    /// runtime that never started keeps its empty metrics.
    #[cfg(feature = "server")]
    pub(crate) fn to_tsv_with_projected_metrics(
        &self,
        runtime: NativeDataFusionRuntime,
        metrics: NativeDataFusionExecutionMetrics,
    ) -> String {
        let mut runtime_summary = NativeDataFusionRuntimeSummary::from_runtime(runtime);
        if runtime_summary.started {
            runtime_summary.metrics = metrics;
        }
        self.render(runtime_summary)
    }

    fn render(&self, runtime_summary: NativeDataFusionRuntimeSummary) -> String {
        let mut output = String::new();
        output.push_str(NATIVE_DATAFUSION_EXECUTION_TSV_HEADER);
        output.push('\n');
        for row in &self.rows {
            for (index, field) in row.prefix.iter().enumerate() {
                if index > 0 {
                    output.push('\t');
                }
                output.push_str(field);
            }
            let summary = if row.rejected_before_execution {
                NativeDataFusionRuntimeSummary::from_runtime(NativeDataFusionRuntime::NotStarted)
            } else {
                runtime_summary.clone()
            };
            let runtime_fields = runtime_fields(row.prefix[Self::QUERY_ID_FIELD].as_ref(), summary);
            for field in runtime_fields {
                output.push('\t');
                output.push_str(&field);
            }
            output.push('\n');
        }
        output
    }
}

fn runtime_fields(query_id: &str, summary: NativeDataFusionRuntimeSummary) -> Vec<String> {
    let row = NativeDataFusionExecutionRow {
        query_id: query_id.to_string(),
        physical_root: String::new(),
        physical_shape: String::new(),
        native_candidate_shape: None,
        native_candidate_shape_family: None,
        planning_outcome: Some(CandidateOutcomeKind::Selected),
        reason_code: None,
        unsupported_category: None,
        capability_decision_rows: Vec::new(),
        capability_compiled_graphs: Vec::new(),
        source_diagnostics: Vec::new(),
        post_native_plan_root: String::new(),
        final_disposition: Some(FinalPlanDisposition::DataFusion),
        native_plan_fragment_count: Some(0),
        host_datafusion_boundary_count: Some(0),
        rejected_before_execution: false,
        native_runtime_started: summary.started,
        runtime_outcome: summary.outcome,
        runtime_failure: summary.failure,
        output_batches: summary.output_batches,
        output_rows: summary.output_rows,
        output_bytes: summary.output_bytes,
        metrics: summary.metrics,
    };
    row.fields().into_iter().skip(NativeDataFusionExecutionTsvTemplate::RUNTIME_FIELD_START).collect()
}

#[cfg(test)]
mod tests {
    use datafusion::physical_plan::metrics::MetricsSet;

    use super::*;
    use crate::native_report::{NativeDataFusionExecutionMetrics, NativeDataFusionRuntimeOutcome};

    #[test]
    fn server_execution_template_preserves_runtime_column_alignment() {
        let report = NativeDataFusionExecutionReport {
            rows: vec![NativeDataFusionExecutionRow {
                query_id: "server-template".to_string(),
                physical_root: "Root".to_string(),
                physical_shape: "Root(Source)".to_string(),
                native_candidate_shape: None,
                native_candidate_shape_family: None,
                planning_outcome: Some(CandidateOutcomeKind::Selected),
                reason_code: None,
                unsupported_category: None,
                capability_decision_rows: Vec::new(),
                capability_compiled_graphs: Vec::new(),
                source_diagnostics: Vec::new(),
                post_native_plan_root: "AlgeonNativeExec".to_string(),
                final_disposition: Some(FinalPlanDisposition::Native),
                native_plan_fragment_count: Some(1),
                host_datafusion_boundary_count: Some(0),
                rejected_before_execution: false,
                native_runtime_started: false,
                runtime_outcome: NativeDataFusionRuntimeOutcome::NotStarted,
                runtime_failure: None,
                output_batches: None,
                output_rows: None,
                output_bytes: None,
                metrics: NativeDataFusionExecutionMetrics::default(),
            }],
            workload_dataset: None,
            statistical_aggregate_execution_mode: None,
        };

        let template = NativeDataFusionExecutionTsvTemplate::from_report(report);
        let tsv = template.to_tsv(NativeDataFusionRuntime::Succeeded {
            output_batches: 1,
            output_rows: 2,
            output_bytes: 3,
            metrics: MetricsSet::new(),
        });
        let mut lines = tsv.lines();
        let header = lines.next().expect("TSV header").split('\t').collect::<Vec<_>>();
        let row = lines.next().expect("TSV row").split('\t').collect::<Vec<_>>();

        assert_eq!(header.len(), row.len(), "server template row must preserve the execution TSV width");
        assert_eq!(
            NativeDataFusionExecutionTsvTemplate::QUERY_ID_FIELD,
            header.iter().position(|column| *column == "query_id").expect("query id column")
        );
        assert_eq!(
            NativeDataFusionExecutionTsvTemplate::RUNTIME_FIELD_START,
            header.iter().position(|column| *column == "admission_sequence").expect("runtime segment start column")
        );
        let native_runtime_started =
            header.iter().position(|column| *column == "native_runtime_started").expect("native runtime start column");
        assert_eq!(native_runtime_started, NativeDataFusionExecutionTsvTemplate::RUNTIME_FIELD_START + 4);
        assert_eq!(row[native_runtime_started], "true");
    }
}