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
//! Projection simplification: removes identity projections and fuses chains of
//! renaming projections.
//!
//! Pattern one: a projection whose outputs are, in order, every input field
//! under its own name and type. It is removed and its consumers read the input.
//! Pattern two: a projection over a projection whose outputs are all bare or
//! aliased columns; the outer column references are replaced by the inner
//! outputs' source columns and the inner projection disappears. Both keep every
//! row and every output field, so the facts at the consumer do not change. Only
//! unqualified names are matched; a qualified reference names a join input and
//! is left alone. The node list is rebuilt, so `QueryPlan::rewrite` renumbers
//! it and drops routes through removed nodes. The rule is safe to run more
//! than once; other rules introduce projections, and the pass table in
//! `optimizer.rs` decides where it runs.
//! Rejection reasons are report contracts; `SchemaMismatch` also covers a plain
//! rename, whose output field differs from the input field.

use std::collections::BTreeMap;

use arrow::datatypes::SchemaRef;

use super::rewrite;
use crate::{
    error::Result,
    expr::{ColumnRef, Expr},
    plan::{NodeId, PlanFacts, PlanNode, ProjectionNode, ProjectionOutput, QueryNode, QueryPlan, RewrittenNodes},
};

/// Stable rule name used in optimizer diagnostics.
pub const PROJECTION_SIMPLIFICATION_RULE_NAME: &str = "projection_simplification";

/// Configuration for removing redundant identity projections.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectionSimplificationOptions {
    /// Whether the pass is allowed to remove eligible projections.
    pub enabled: bool,
}

impl ProjectionSimplificationOptions {
    /// Returns options with the pass disabled.
    pub fn disabled() -> Self {
        Self { enabled: false }
    }
}

impl Default for ProjectionSimplificationOptions {
    fn default() -> Self {
        Self { enabled: true }
    }
}

/// Diagnostics for projection-simplification candidates.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProjectionSimplificationDiagnostics {
    /// Number of projection nodes considered.
    pub considered_count: usize,
    /// Number of projections replaced by their input or by a fused projection.
    pub applied_count: usize,
    /// Per-candidate evidence in traversal order.
    pub candidates: Vec<ProjectionSimplificationCandidateDiagnostics>,
}

impl ProjectionSimplificationDiagnostics {
    pub(crate) fn is_empty(&self) -> bool {
        self.considered_count == 0 && self.applied_count == 0 && self.candidates.is_empty()
    }

    pub(crate) fn merge(&mut self, other: Self) {
        self.considered_count += other.considered_count;
        self.applied_count += other.applied_count;
        self.candidates.extend(other.candidates);
    }
}

/// Per-candidate projection-simplification evidence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectionSimplificationCandidateDiagnostics {
    /// Stable rule name for this diagnostic row.
    pub(crate) rule_name: &'static str,
    /// Projection node considered before rewrite.
    pub(crate) candidate_node_id: NodeId,
    /// Node that replaces the projection when applied. It is the input's image for
    /// an identity projection and the fused projection for a chain.
    pub(crate) replacement_node_id: Option<NodeId>,
    /// True when this projection was replaced in the returned plan.
    pub(crate) applied: bool,
    /// Rejection reason when the projection was not replaced.
    pub rejected_reason: Option<ProjectionSimplificationRejectedReason>,
}

/// Reasons a projection-simplification candidate was rejected.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectionSimplificationRejectedReason {
    /// Pass was disabled.
    PassDisabled,
    /// Projection output schema differs from the input schema.
    SchemaMismatch,
    /// Projection contains a non-column expression or changes column mapping.
    NonIdentityExpression,
}

pub(crate) struct ProjectionSimplificationResult {
    pub(crate) plan: QueryPlan,
    pub(crate) change: super::NativeOptimizerChange,
    pub(crate) diagnostics: ProjectionSimplificationDiagnostics,
}

pub(crate) fn optimize_projection_simplification(
    plan: QueryPlan,
    options: &ProjectionSimplificationOptions,
) -> Result<ProjectionSimplificationResult> {
    if !options.enabled {
        return Ok(ProjectionSimplificationResult {
            plan,
            change: super::NativeOptimizerChange::Unchanged,
            diagnostics: ProjectionSimplificationDiagnostics::default(),
        });
    }

    let mut rewriter = ProjectionSimplificationRewriter::new(&plan);
    let new_root = rewriter.rewrite_subtree(plan.root_id());
    if !rewriter.changed {
        let diagnostics = rewriter.diagnostics.clone();
        return Ok(ProjectionSimplificationResult {
            plan,
            change: super::NativeOptimizerChange::Unchanged,
            diagnostics,
        });
    }

    Ok(ProjectionSimplificationResult {
        plan: plan.rewrite(RewrittenNodes::rebuilt(rewriter.nodes, new_root, rewriter.memo))?,
        change: super::NativeOptimizerChange::Changed,
        diagnostics: rewriter.diagnostics,
    })
}

/// Rebuilds the plan top down. `memo` maps each visited source node to its
/// image, so a shared subtree is rewritten once and a removed projection maps
/// to its input's image; `memo` is also the origin map passed to
/// `RewrittenNodes::rebuilt`.
struct ProjectionSimplificationRewriter<'a> {
    original: &'a QueryPlan,
    nodes: Vec<QueryNode>,
    memo: BTreeMap<NodeId, NodeId>,
    diagnostics: ProjectionSimplificationDiagnostics,
    changed: bool,
}

impl<'a> ProjectionSimplificationRewriter<'a> {
    fn new(original: &'a QueryPlan) -> Self {
        Self {
            original,
            nodes: Vec::new(),
            memo: BTreeMap::new(),
            diagnostics: ProjectionSimplificationDiagnostics::default(),
            changed: false,
        }
    }

    fn rewrite_subtree(&mut self, old_id: NodeId) -> NodeId {
        if let Some(new_id) = self.memo.get(&old_id).copied() {
            return new_id;
        }

        let old_node = self.original.node(old_id).expect("optimizer input node must exist");
        if let PlanNode::Projection(projection) = &old_node.kind
            && let Some(replacement) = self.try_simplify_projection(old_id, projection)
        {
            self.memo.insert(old_id, replacement);
            return replacement;
        }

        let kind = self.rewrite_node_kind(&old_node.kind);
        let new_id = self.push_node(kind, old_node.facts.clone());
        self.memo.insert(old_id, new_id);
        new_id
    }

    fn try_simplify_projection(&mut self, old_id: NodeId, projection: &ProjectionNode) -> Option<NodeId> {
        let mut candidate = ProjectionSimplificationCandidateDiagnostics {
            rule_name: PROJECTION_SIMPLIFICATION_RULE_NAME,
            candidate_node_id: old_id,
            replacement_node_id: None,
            applied: false,
            rejected_reason: None,
        };
        self.diagnostics.considered_count += 1;

        let input_node = self.original.node(projection.input).expect("projection input must exist");
        if schemas_equal(input_node.output_schema(), projection.output_schema())
            && projection_is_identity(input_node.output_schema(), projection)
        {
            let replacement = self.rewrite_subtree(projection.input);
            candidate.replacement_node_id = Some(replacement);
            candidate.applied = true;
            self.diagnostics.applied_count += 1;
            self.diagnostics.candidates.push(candidate);
            self.changed = true;
            return Some(replacement);
        }

        if let Some(fused_outputs) = compose_direct_projection_chain(input_node, projection) {
            let PlanNode::Projection(input_projection) = &input_node.kind else {
                unreachable!("compose_direct_projection_chain requires a projection input");
            };
            let fused_input = self.rewrite_subtree(input_projection.input);
            let facts = self.original.node(old_id).expect("projection node must exist").facts.clone();
            let replacement =
                self.push_node(PlanNode::Projection(ProjectionNode::new(fused_input, fused_outputs)), facts);
            candidate.replacement_node_id = Some(replacement);
            candidate.applied = true;
            self.diagnostics.applied_count += 1;
            self.diagnostics.candidates.push(candidate);
            self.changed = true;
            return Some(replacement);
        }

        candidate.rejected_reason = Some(if schemas_equal(input_node.output_schema(), projection.output_schema()) {
            ProjectionSimplificationRejectedReason::NonIdentityExpression
        } else {
            ProjectionSimplificationRejectedReason::SchemaMismatch
        });
        self.diagnostics.candidates.push(candidate);
        None
    }

    fn rewrite_node_kind(&mut self, kind: &PlanNode) -> PlanNode {
        rewrite::clone_plan_node_with_inputs(kind, |input| self.rewrite_subtree(input))
    }

    fn push_node(&mut self, kind: PlanNode, facts: PlanFacts) -> NodeId {
        let id = NodeId(self.nodes.len());
        self.nodes.push(QueryNode { id, kind, facts });
        id
    }
}

fn projection_is_identity(input_schema: &SchemaRef, projection: &ProjectionNode) -> bool {
    projection.outputs.len() == input_schema.fields().len()
        && projection.outputs.iter().zip(input_schema.fields().iter()).all(|(output, input_field)| {
            output.field == input_field.as_ref().clone()
                && direct_column_name(&output.expr).is_some_and(|name| name == input_field.name())
        })
}

/// Outputs of `projection` restated over its input projection's input, when the
/// inner projection only renames columns. Any computed inner output makes the
/// chain opaque, since the outer reference would then name a value the fused
/// node could not read.
fn compose_direct_projection_chain(
    input_node: &QueryNode,
    projection: &ProjectionNode,
) -> Option<Vec<ProjectionOutput>> {
    let PlanNode::Projection(input_projection) = &input_node.kind else {
        return None;
    };
    let mut direct_outputs = BTreeMap::<String, Expr>::new();
    for output in &input_projection.outputs {
        direct_outputs.insert(output.field.name().clone(), direct_projection_source_expr(&output.expr)?);
    }

    projection
        .outputs
        .iter()
        .map(|output| {
            let expr = compose_direct_projection_expr(&output.expr, &direct_outputs)?;
            Some(ProjectionOutput::new(expr, output.field.clone()))
        })
        .collect()
}

fn compose_direct_projection_expr(expr: &Expr, direct_outputs: &BTreeMap<String, Expr>) -> Option<Expr> {
    match expr {
        Expr::Column(column) => direct_outputs.get(unqualified_column_name(column)?).cloned(),
        Expr::Alias { expr, alias } => Some(compose_direct_projection_expr(expr, direct_outputs)?.alias(alias.clone())),
        _ => None,
    }
}

fn direct_projection_source_expr(expr: &Expr) -> Option<Expr> {
    match expr {
        Expr::Column(column) => Some(Expr::Column(column.clone())),
        Expr::Alias { expr, .. } => direct_projection_source_expr(expr),
        _ => None,
    }
}

fn direct_column_name(expr: &Expr) -> Option<&str> {
    match expr {
        Expr::Column(column) => unqualified_column_name(column),
        Expr::Alias { expr, alias } => {
            let column = direct_column_name(expr)?;
            (column == alias).then_some(column)
        },
        _ => None,
    }
}

fn unqualified_column_name(column: &ColumnRef) -> Option<&str> {
    column.qualifier().is_none().then_some(column.name())
}

fn schemas_equal(left: &SchemaRef, right: &SchemaRef) -> bool {
    left.fields().len() == right.fields().len()
        && left.fields().iter().zip(right.fields().iter()).all(|(left, right)| left == right)
}

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

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

    use super::*;
    use crate::{
        expr::{BinaryOp, LiteralValue},
        plan::{ProjectionOutput, QueryPlanBuilder},
        source::{GpuResidentSource, SourceNode},
    };

    #[test]
    fn removes_chained_identity_projections() {
        let mut builder = QueryPlanBuilder::new();
        let source = builder.add_source(source("input", schema(vec![field("k"), field("v")])));
        let first = builder.add_projection(source, identity_outputs(&["k", "v"])).unwrap();
        let second = builder.add_projection(first, identity_alias_outputs(&["k", "v"])).unwrap();
        let plan = builder.finish_gpu_resident(second).unwrap();

        let result = optimize_projection_simplification(plan, &ProjectionSimplificationOptions::default()).unwrap();

        assert!(result.change.changed());
        assert_eq!(result.diagnostics.considered_count, 2);
        assert_eq!(result.diagnostics.applied_count, 2);
        assert_eq!(result.plan.node_count(), 1);
        assert!(matches!(result.plan.root_node().kind, PlanNode::Source(_)));
    }

    #[test]
    fn keeps_projection_that_changes_output_name() {
        let mut builder = QueryPlanBuilder::new();
        let source = builder.add_source(source("input", schema(vec![field("k")])));
        let projection = builder
            .add_projection(source, vec![ProjectionOutput::new(Expr::column("k").alias("renamed"), field("renamed"))])
            .unwrap();
        let plan = builder.finish_gpu_resident(projection).unwrap();

        let result = optimize_projection_simplification(plan, &ProjectionSimplificationOptions::default()).unwrap();

        assert!(!result.change.changed());
        assert_eq!(result.diagnostics.considered_count, 1);
        assert_eq!(result.diagnostics.applied_count, 0);
        assert_eq!(
            result.diagnostics.candidates[0].rejected_reason,
            Some(ProjectionSimplificationRejectedReason::SchemaMismatch)
        );
    }

    #[test]
    fn keeps_projection_with_computed_expression() {
        let mut builder = QueryPlanBuilder::new();
        let source = builder.add_source(source("input", schema(vec![field("k")])));
        let projection = builder
            .add_projection(
                source,
                vec![ProjectionOutput::new(
                    Expr::binary(Expr::column("k"), BinaryOp::Add, Expr::literal(LiteralValue::Int32(1))).alias("k"),
                    field("k"),
                )],
            )
            .unwrap();
        let plan = builder.finish_gpu_resident(projection).unwrap();

        let result = optimize_projection_simplification(plan, &ProjectionSimplificationOptions::default()).unwrap();

        assert!(!result.change.changed());
        assert_eq!(result.diagnostics.considered_count, 1);
        assert_eq!(result.diagnostics.applied_count, 0);
        assert_eq!(
            result.diagnostics.candidates[0].rejected_reason,
            Some(ProjectionSimplificationRejectedReason::NonIdentityExpression)
        );
    }

    #[test]
    fn fuses_direct_projection_chain() {
        let mut builder = QueryPlanBuilder::new();
        let source = builder.add_source(source("input", schema(vec![field("k"), field("v"), field("payload")])));
        let first = builder.add_projection(source, identity_alias_outputs(&["k", "v", "payload"])).unwrap();
        let second = builder
            .add_projection(first, vec![ProjectionOutput::new(Expr::column("v").alias("renamed"), field("renamed"))])
            .unwrap();
        let plan = builder.finish_gpu_resident(second).unwrap();

        let result = optimize_projection_simplification(plan, &ProjectionSimplificationOptions::default()).unwrap();

        assert!(result.change.changed());
        assert_eq!(result.diagnostics.considered_count, 1);
        assert_eq!(result.diagnostics.applied_count, 1);
        assert_eq!(result.plan.node_count(), 2);
        let PlanNode::Projection(projection) = &result.plan.root_node().kind else {
            panic!("root should remain a projection");
        };
        assert!(matches!(result.plan.node(projection.input).map(|node| &node.kind), Some(PlanNode::Source(_))));
        assert_eq!(projection.outputs.len(), 1);
        assert_eq!(projection.outputs[0].field.name(), "renamed");
        assert_eq!(projection.outputs[0].expr.to_string(), "v AS renamed");
    }

    #[test]
    fn keeps_projection_chain_when_inner_projection_computes_expression() {
        let mut builder = QueryPlanBuilder::new();
        let source = builder.add_source(source("input", schema(vec![field("k")])));
        let first = builder
            .add_projection(
                source,
                vec![ProjectionOutput::new(
                    Expr::binary(Expr::column("k"), BinaryOp::Add, Expr::literal(LiteralValue::Int32(1)))
                        .alias("computed"),
                    field("computed"),
                )],
            )
            .unwrap();
        let second = builder
            .add_projection(first, vec![ProjectionOutput::new(Expr::column("computed"), field("renamed"))])
            .unwrap();
        let plan = builder.finish_gpu_resident(second).unwrap();

        let result = optimize_projection_simplification(plan, &ProjectionSimplificationOptions::default()).unwrap();

        assert!(!result.change.changed());
        assert_eq!(result.diagnostics.considered_count, 2);
        assert_eq!(result.diagnostics.applied_count, 0);
        assert!(
            result
                .diagnostics
                .candidates
                .iter()
                .any(|candidate| candidate.rejected_reason
                    == Some(ProjectionSimplificationRejectedReason::SchemaMismatch))
        );
        assert!(result.diagnostics.candidates.iter().all(|candidate| !candidate.applied));
    }

    fn source(name: &str, schema: Arc<Schema>) -> SourceNode {
        SourceNode::GpuResident(GpuResidentSource::new(name, schema))
    }

    fn schema(fields: Vec<Field>) -> Arc<Schema> {
        Arc::new(Schema::new(fields))
    }

    fn field(name: &str) -> Field {
        Field::new(name, DataType::Int32, false)
    }

    fn identity_outputs(names: &[&str]) -> Vec<ProjectionOutput> {
        names.iter().map(|name| ProjectionOutput::new(Expr::column(*name), field(name))).collect()
    }

    fn identity_alias_outputs(names: &[&str]) -> Vec<ProjectionOutput> {
        names
            .iter()
            .map(|name| ProjectionOutput::new(Expr::column(*name).alias((*name).to_string()), field(name)))
            .collect()
    }
}