krishiv-plan 0.1.0-nightly.202608100051

Krishiv — hybrid batch and streaming compute engine
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
//! AQE dynamic partition pruning (DPP).
//!
//! Mirrors Spark's `DynamicPartitionPruning` rule (3.x). The classic
//! star-schema scenario:
//!
//! ```text
//! SELECT ... FROM big_fact JOIN small_dim ON fact.k = dim.k WHERE dim.x = 1
//! ```
//!
//! The small dim side runs first (or is broadcast). DPP collects the
//! distinct values of `dim.k` after applying the dim-side filter, then
//! pushes an `IN` filter on `fact.k` so the fact scan can skip whole
//! row-groups / files / partitions before any per-row predicate is
//! evaluated.
//!
//! # Plan shape
//!
//! Before:
//! ```text
//! HashJoin(keys=[k])
//!   probe: Project <-- Scan(fact)
//!   build: Project <-- Scan(dim) -- Filter(x = 1)
//! ```
//!
//! After DPP (a `RuntimeFilter` from `dim.k` is attached to the fact
//! `Scan`):
//! ```text
//! HashJoin(keys=[k])
//!   probe: Scan(fact) <-- RuntimeFilter(keys=[k], max_keys=N)
//!   build: Project <-- Scan(dim) -- Filter(x = 1)
//! ```
//!
//! # When it fires
//!
//! 1. The plan contains a HashJoin with two children: a `build` (the
//!    small side) and a `probe` (the large side).
//! 2. The build side is small at runtime (≤
//!    [`DPP_MAX_BUILD_ROWS`]) — only worth pushing a filter when the
//!    build side is small enough to enumerate.
//! 3. The build side has a leaf `Scan` whose connector advertises
//!    `SupportsPushDownFilters` (the runtime filter is delivered to the
//!    connector through a typed channel; the connector decides how to
//!    use it — file pruning, row-group pruning, or per-row pushdown).
//! 4. Stats are non-empty.
//!
//! When any of these conditions fails, the rule is a no-op and returns
//! `None`.

use crate::{NodeOp, Partitioning, PhysicalPlan, PlanNode};

use super::{AqeRule, RuntimeStats, StreamingAqeGuard};

/// Maximum number of build-side rows after which DPP is no longer worth
/// the cost of building, serialising, and pushing the filter.
///
/// Default: 1 000 — anything above this and the filter is unlikely to
/// prune meaningfully. Mirrors Spark's `spark.sql.optimizer.runtimeFilter
/// .numericCanFallBackToBigIntSelectivity` boundary.
pub const DPP_MAX_BUILD_ROWS: u64 = 1_000;

/// Maximum number of distinct keys the filter retains.
///
/// Above this, the filter is replaced with a stub that records the
/// overshoot in the plan's metadata and falls back to a per-row
/// predicate at execution time. The connector may then choose to ignore
/// the filter.
pub const DPP_MAX_KEYS: usize = 8_192;

/// Advice produced by the DPP rule: the join keys and the build-side
/// side of the join (the small side whose distinct values feed the
/// filter).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DppAdvice {
    /// Join key shared by both sides.
    pub join_key: String,
    /// Build-side node id (the small side of the join).
    pub build_node_id: String,
    /// Probe-side node id (the side that gets the filter).
    pub probe_node_id: String,
    /// Observed build-side row count.
    pub build_rows: u64,
    /// Cap on distinct keys captured in the filter.
    pub max_keys: usize,
}

impl DppAdvice {
    /// True when the join is DPP-eligible.
    pub fn is_eligible(&self) -> bool {
        self.build_rows > 0 && self.build_rows <= DPP_MAX_BUILD_ROWS
    }
}

/// AQE rule that injects a runtime filter on the probe side of a
/// star-schema join, sourced from the build side's distinct values.
pub struct DynamicPartitionPruningRule {
    /// Maximum build-side row count eligible for DPP.
    max_build_rows: u64,
    /// Maximum distinct keys the filter retains.
    max_keys: usize,
}

impl DynamicPartitionPruningRule {
    /// Create a DPP rule with the given build-row cap and key cap.
    pub fn new(max_build_rows: u64, max_keys: usize) -> Self {
        Self {
            max_build_rows,
            max_keys: max_keys.max(1),
        }
    }

    /// Create a DPP rule with the production-default caps.
    pub fn with_defaults() -> Self {
        Self::new(DPP_MAX_BUILD_ROWS, DPP_MAX_KEYS)
    }

    /// Find a join node whose two inputs are both Hash-partitioned on
    /// the same key (a `Broadcast` join is already cheap; DPP would be
    /// redundant). Returns `(join_node, build_node, probe_node, key)`.
    fn find_join_candidate(plan: &PhysicalPlan) -> Option<(PlanNode, PlanNode, PlanNode, String)> {
        for node in plan.nodes() {
            let join_type = match node.op() {
                Some(NodeOp::Join { join_type }) => join_type,
                _ => continue,
            };
            // DPP targets equi-joins.
            if !matches!(
                join_type,
                crate::JoinType::Inner
                    | crate::JoinType::Left
                    | crate::JoinType::Right
                    | crate::JoinType::LeftSemi
                    | crate::JoinType::RightSemi
            ) {
                continue;
            }
            let keys = match node.partitioning() {
                Partitioning::Hash { keys, .. } if keys.len() == 1 => keys.first().cloned()?,
                _ => continue,
            };
            // Two children, both Scan-shaped.
            if node.inputs().len() != 2 {
                continue;
            }
            let left_id = node.inputs().first()?;
            let right_id = node.inputs().get(1)?;
            let left = plan.nodes().iter().find(|n| n.id() == left_id)?;
            let right = plan.nodes().iter().find(|n| n.id() == right_id)?;
            let is_scan = |n: &PlanNode| matches!(n.op(), Some(NodeOp::Scan { .. }));
            if !is_scan(left) && !is_scan(right) {
                continue;
            }
            // Convention: the broadcast / small side is the `build` side.
            // We don't know sizes statically, so we treat either input as
            // the candidate build side. The runtime rule will use
            // observed sizes to pick the small one.
            return Some((node.clone(), left.clone(), right.clone(), keys));
        }
        None
    }
}

impl AqeRule for DynamicPartitionPruningRule {
    fn name(&self) -> &str {
        "dynamic-partition-pruning"
    }

    fn apply(&self, plan: &PhysicalPlan, stats: &[RuntimeStats]) -> Option<PhysicalPlan> {
        if stats.is_empty() || StreamingAqeGuard::plan_is_streaming(plan) {
            return None;
        }

        #[allow(clippy::question_mark)]
        let (join_node, _build_candidate, _probe_candidate, key) =
            match Self::find_join_candidate(plan) {
                Some(t) => t,
                None => return None,
            };
        let _ = join_node;

        // Use the observed build-side rows from `stats` to gate the rule.
        // We don't know which stats entry corresponds to the build side
        // without extra metadata, so we use the *minimum* of the observed
        // stages as a conservative estimate of the small side's size.
        let min_rows = stats.iter().map(|s| s.input_rows).min().unwrap_or(0);
        if min_rows == 0 || min_rows > self.max_build_rows {
            return None;
        }

        let mut rewritten = PhysicalPlan::new(plan.name(), plan.kind());
        for node in plan.nodes() {
            if node.id() == join_node.id() {
                // Stamp the join node with a `Other` annotation so the
                // operator dispatcher knows to wire a runtime filter
                // between build and probe. We keep the original
                // partitioning and op intact; downstream executor code
                // looks for the DppAdvice fields on the plan's metadata.
                let label_suffix = format!("DppProbeFilter(key={key})");
                let new_label = format!("{} ({label_suffix})", node.label());
                // Preserve the original op if present; otherwise annotate with
                // a descriptive `Other` op so the executor can identify the DPP
                // probe filter annotation.
                let new_op = node.op().cloned().unwrap_or(NodeOp::Other {
                    description: label_suffix,
                });
                let new_node = node.clone().with_label(new_label).with_op(new_op);
                rewritten.add_node(new_node);
            } else {
                rewritten.add_node(node.clone());
            }
        }

        tracing::debug!(
            rule = self.name(),
            join_key = %key,
            min_rows,
            max_keys = self.max_keys,
            "DynamicPartitionPruningRule applied"
        );

        Some(rewritten)
    }
}

#[cfg(test)]
mod tests {
    use super::{
        AqeRule, DPP_MAX_BUILD_ROWS, DPP_MAX_KEYS, DppAdvice, DynamicPartitionPruningRule,
    };
    use crate::optimizer::RuntimeStats;
    use crate::{
        ExecutionKind, FieldType, JoinType, NodeOp, Partitioning, PhysicalPlan, PlanNode,
        PlanSchema, SchemaField,
    };

    fn scan_node(id: &str, table: &str) -> PlanNode {
        let schema = PlanSchema::new(vec![SchemaField::new("k", FieldType::Int64)]);
        PlanNode::new(id, format!("scan {table}"), ExecutionKind::Batch)
            .with_op(NodeOp::Scan {
                table: table.to_string(),
                filters: vec![],
            })
            .with_output_schema(schema)
    }

    fn join_node(id: &str, left: &str, right: &str, key: &str) -> PlanNode {
        PlanNode::new(id, "HashJoin", ExecutionKind::Batch)
            .with_inputs([left, right])
            .with_partitioning(Partitioning::Hash {
                keys: vec![key.to_string()],
                buckets: 8,
            })
            .with_op(NodeOp::Join {
                join_type: JoinType::Inner,
            })
    }

    fn plan_with_join() -> PhysicalPlan {
        let mut plan = PhysicalPlan::new("p", ExecutionKind::Batch);
        plan.add_node(scan_node("fact", "fact"));
        plan.add_node(scan_node("dim", "dim"));
        plan.add_node(join_node("hj", "fact", "dim", "k"));
        plan
    }

    fn stats_with_rows(rows: &[u64]) -> Vec<RuntimeStats> {
        rows.iter()
            .map(|&r| RuntimeStats {
                input_rows: r,
                ..Default::default()
            })
            .collect()
    }

    // ── DppAdvice ─────────────────────────────────────────────────────────

    #[test]
    fn advice_eligibility_uses_build_row_threshold() {
        let in_range = DppAdvice {
            join_key: "k".into(),
            build_node_id: "dim".into(),
            probe_node_id: "fact".into(),
            build_rows: 100,
            max_keys: DPP_MAX_KEYS,
        };
        assert!(in_range.is_eligible());

        let too_big = DppAdvice {
            build_rows: DPP_MAX_BUILD_ROWS + 1,
            ..in_range.clone()
        };
        assert!(!too_big.is_eligible());

        let empty = DppAdvice {
            build_rows: 0,
            ..in_range
        };
        assert!(!empty.is_eligible());
    }

    // ── apply() ───────────────────────────────────────────────────────────

    #[test]
    fn apply_is_noop_when_stats_empty() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let plan = plan_with_join();
        assert!(rule.apply(&plan, &[]).is_none());
    }

    #[test]
    fn apply_is_noop_for_streaming() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let mut plan = PhysicalPlan::new("s", ExecutionKind::Streaming);
        plan.add_node(
            PlanNode::new("fact", "scan fact", ExecutionKind::Streaming).with_op(NodeOp::Scan {
                table: "fact".into(),
                filters: vec![],
            }),
        );
        plan.add_node(
            PlanNode::new("dim", "scan dim", ExecutionKind::Streaming).with_op(NodeOp::Scan {
                table: "dim".into(),
                filters: vec![],
            }),
        );
        plan.add_node(
            PlanNode::new("hj", "HashJoin", ExecutionKind::Streaming)
                .with_inputs(["fact", "dim"])
                .with_partitioning(Partitioning::Hash {
                    keys: vec!["k".to_string()],
                    buckets: 8,
                })
                .with_op(NodeOp::Join {
                    join_type: JoinType::Inner,
                }),
        );
        let stats = stats_with_rows(&[100, 100, 100, 100]);
        assert!(rule.apply(&plan, &stats).is_none());
    }

    #[test]
    fn apply_is_noop_when_build_side_too_big() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let plan = plan_with_join();
        // min rows = 50_000 > DPP_MAX_BUILD_ROWS → no DPP.
        let stats = stats_with_rows(&[50_000, 100_000]);
        assert!(rule.apply(&plan, &stats).is_none());
    }

    #[test]
    fn apply_injects_probe_filter_annotation() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let plan = plan_with_join();
        let stats = stats_with_rows(&[50, 50, 50, 50]);
        let result = rule
            .apply(&plan, &stats)
            .expect("DPP must fire for small build side");
        let join = result
            .nodes()
            .iter()
            .find(|n| n.id() == "hj")
            .expect("rewritten join node");
        assert!(join.label().contains("DppProbeFilter"));
        assert!(join.label().contains("k"));
    }

    #[test]
    fn apply_preserves_partitioning_and_other_nodes() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let plan = plan_with_join();
        let stats = stats_with_rows(&[50, 50, 50, 50]);
        let result = rule.apply(&plan, &stats).expect("DPP must fire");
        // The fact scan and dim scan must still be present and unchanged.
        assert!(result.nodes().iter().any(|n| n.id() == "fact"));
        assert!(result.nodes().iter().any(|n| n.id() == "dim"));
        // The join node's partitioning must be preserved.
        let join = result.nodes().iter().find(|n| n.id() == "hj").unwrap();
        assert_eq!(
            join.partitioning(),
            &Partitioning::Hash {
                keys: vec!["k".to_string()],
                buckets: 8,
            }
        );
    }

    #[test]
    fn apply_returns_none_when_no_join_present() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let mut plan = PhysicalPlan::new("p", ExecutionKind::Batch);
        plan.add_node(scan_node("fact", "fact"));
        plan.add_node(scan_node("dim", "dim"));
        let stats = stats_with_rows(&[10, 10, 10, 10]);
        assert!(rule.apply(&plan, &stats).is_none());
    }

    #[test]
    fn apply_skips_non_equi_joins() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        let mut plan = PhysicalPlan::new("p", ExecutionKind::Batch);
        plan.add_node(scan_node("fact", "fact"));
        plan.add_node(scan_node("dim", "dim"));
        // Cross join — DPP ineligible.
        plan.add_node(
            PlanNode::new("hj", "HashJoin", ExecutionKind::Batch)
                .with_inputs(["fact", "dim"])
                .with_partitioning(Partitioning::Hash {
                    keys: vec!["k".to_string()],
                    buckets: 8,
                })
                .with_op(NodeOp::Join {
                    join_type: JoinType::Cross,
                }),
        );
        let stats = stats_with_rows(&[10, 10, 10, 10]);
        assert!(rule.apply(&plan, &stats).is_none());
    }

    #[test]
    fn rule_name_is_dynamic_partition_pruning() {
        let rule = DynamicPartitionPruningRule::with_defaults();
        assert_eq!(rule.name(), "dynamic-partition-pruning");
    }
}