risingwave_frontend/optimizer/plan_node/
mod.rs

1// Copyright 2022 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines all kinds of node in the plan tree, each node represent a relational expression.
16//!
17//! We use a immutable style tree structure, every Node are immutable and cannot be modified after
18//! it has been created. If you want to modify the node, such as rewriting the expression in a
19//! `ProjectNode` or changing a node's input node, you need to create a new node. We use Rc as the
20//! node's reference, and a node just storage its inputs' reference, so change a node just need
21//! create one new node but not the entire sub-tree.
22//!
23//! So when you want to add a new node, make sure:
24//! - each field in the node struct are private
25//! - recommend to implement the construction of Node in a unified `new()` function, if have multi
26//!   methods to construct, make they have a consistent behavior
27//! - all field should be valued in construction, so the properties' derivation should be finished
28//!   in the `new()` function.
29
30use std::collections::HashMap;
31use std::fmt::Debug;
32use std::hash::Hash;
33use std::marker::PhantomData;
34use std::ops::Deref;
35use std::rc::Rc;
36
37use downcast_rs::{Downcast, impl_downcast};
38use dyn_clone::DynClone;
39use itertools::Itertools;
40use paste::paste;
41use petgraph::dot::{Config, Dot};
42use petgraph::graph::Graph;
43use pretty_xmlish::{Pretty, PrettyConfig};
44use risingwave_common::catalog::Schema;
45use risingwave_common::util::recursive::{self, Recurse};
46use risingwave_pb::batch_plan::PlanNode as PbBatchPlan;
47use risingwave_pb::stream_plan::StreamNode as PbStreamPlan;
48use serde::Serialize;
49
50use self::batch::BatchPlanNodeMetadata;
51use self::generic::{GenericPlanRef, PhysicalPlanRef};
52use self::stream::StreamPlanNodeMetadata;
53use self::utils::Distill;
54use super::property::{
55    Distribution, FunctionalDependencySet, MonotonicityMap, Order, WatermarkColumns,
56};
57use crate::error::{ErrorCode, Result};
58use crate::optimizer::property::StreamKind;
59use crate::optimizer::{ExpressionSimplifyRewriter, PlanVisitor};
60use crate::session::current::notice_to_user;
61use crate::utils::{PrettySerde, build_graph_from_pretty};
62
63/// A marker trait for different conventions, used for enforcing type safety.
64///
65/// Implementors are [`Logical`], [`Batch`], and [`Stream`].
66pub trait ConventionMarker: 'static + Sized + Clone + Debug + Eq + PartialEq + Hash {
67    /// The extra fields in the [`PlanBase`] of this convention.
68    type Extra: 'static + Eq + Hash + Clone + Debug;
69    type ShareNode: ShareNode<Self>;
70    type PlanRefDyn: PlanNodeCommon<Self> + Eq + Hash + ?Sized;
71    type PlanNodeType;
72
73    fn as_share(plan: &Self::PlanRefDyn) -> Option<&Self::ShareNode>;
74}
75
76pub trait ShareNode<C: ConventionMarker>:
77    AnyPlanNodeMeta<C> + PlanTreeNodeUnary<C> + 'static
78{
79    fn new_share(share: generic::Share<PlanRef<C>>) -> PlanRef<C>;
80    fn replace_input(&self, plan: PlanRef<C>);
81}
82
83pub struct NoShareNode<C: ConventionMarker>(!, PhantomData<C>);
84
85impl<C: ConventionMarker> ShareNode<C> for NoShareNode<C> {
86    fn new_share(_plan: generic::Share<PlanRef<C>>) -> PlanRef<C> {
87        unreachable!()
88    }
89
90    fn replace_input(&self, _plan: PlanRef<C>) {
91        unreachable!()
92    }
93}
94
95impl<C: ConventionMarker> PlanTreeNodeUnary<C> for NoShareNode<C> {
96    fn input(&self) -> PlanRef<C> {
97        unreachable!()
98    }
99
100    fn clone_with_input(&self, _input: PlanRef<C>) -> Self {
101        unreachable!()
102    }
103}
104
105impl<C: ConventionMarker> AnyPlanNodeMeta<C> for NoShareNode<C> {
106    fn node_type(&self) -> C::PlanNodeType {
107        unreachable!()
108    }
109
110    fn plan_base(&self) -> &PlanBase<C> {
111        unreachable!()
112    }
113}
114
115/// The marker for logical convention.
116#[derive(Clone, Debug, Eq, PartialEq, Hash)]
117pub struct Logical;
118impl ConventionMarker for Logical {
119    type Extra = plan_base::NoExtra;
120    type PlanNodeType = LogicalPlanNodeType;
121    type PlanRefDyn = dyn LogicalPlanNode;
122    type ShareNode = LogicalShare;
123
124    fn as_share(plan: &Self::PlanRefDyn) -> Option<&Self::ShareNode> {
125        plan.as_logical_share()
126    }
127}
128
129/// The marker for batch convention.
130#[derive(Clone, Debug, Eq, PartialEq, Hash)]
131pub struct Batch;
132impl ConventionMarker for Batch {
133    type Extra = plan_base::BatchExtra;
134    type PlanNodeType = BatchPlanNodeType;
135    type PlanRefDyn = dyn BatchPlanNode;
136    type ShareNode = NoShareNode<Batch>;
137
138    fn as_share(_plan: &Self::PlanRefDyn) -> Option<&Self::ShareNode> {
139        None
140    }
141}
142
143/// The marker for stream convention.
144#[derive(Clone, Debug, Eq, PartialEq, Hash)]
145pub struct Stream;
146impl ConventionMarker for Stream {
147    type Extra = plan_base::StreamExtra;
148    type PlanNodeType = StreamPlanNodeType;
149    type PlanRefDyn = dyn StreamPlanNode;
150    type ShareNode = StreamShare;
151
152    fn as_share(plan: &Self::PlanRefDyn) -> Option<&Self::ShareNode> {
153        plan.as_stream_share()
154    }
155}
156
157/// The trait for accessing the meta data and [`PlanBase`] for plan nodes.
158pub trait PlanNodeMeta {
159    type Convention: ConventionMarker;
160    const NODE_TYPE: <Self::Convention as ConventionMarker>::PlanNodeType;
161    /// Get the reference to the [`PlanBase`] with corresponding convention.
162    fn plan_base(&self) -> &PlanBase<Self::Convention>;
163}
164
165// Intentionally made private.
166mod plan_node_meta {
167    use super::*;
168
169    /// The object-safe version of [`PlanNodeMeta`], used as a super trait of `PlanNode`.
170    ///
171    /// Check [`PlanNodeMeta`] for more details.
172    pub trait AnyPlanNodeMeta<C: ConventionMarker> {
173        fn node_type(&self) -> C::PlanNodeType;
174        fn plan_base(&self) -> &PlanBase<C>;
175    }
176
177    /// Implement [`AnyPlanNodeMeta`] for all [`PlanNodeMeta`].
178    impl<P> AnyPlanNodeMeta<P::Convention> for P
179    where
180        P: PlanNodeMeta,
181    {
182        fn node_type(&self) -> <P::Convention as ConventionMarker>::PlanNodeType {
183            P::NODE_TYPE
184        }
185
186        fn plan_base(&self) -> &PlanBase<P::Convention> {
187            <Self as PlanNodeMeta>::plan_base(self)
188        }
189    }
190}
191use plan_node_meta::AnyPlanNodeMeta;
192
193pub trait PlanNodeCommon<C: ConventionMarker> = PlanTreeNode<C>
194    + DynClone
195    + DynEq
196    + DynHash
197    + Distill
198    + Debug
199    + Downcast
200    + ExprRewritable<C>
201    + ExprVisitable
202    + AnyPlanNodeMeta<C>;
203
204/// The common trait over all plan nodes. Used by optimizer framework which will treat all node as
205/// `dyn PlanNode`
206///
207/// We split the trait into lots of sub-trait so that we can easily use macro to impl them.
208pub trait StreamPlanNode: PlanNodeCommon<Stream> + TryToStreamPb {}
209pub trait BatchPlanNode:
210    PlanNodeCommon<Batch> + ToDistributedBatch + ToLocalBatch + TryToBatchPb
211{
212}
213pub trait LogicalPlanNode:
214    PlanNodeCommon<Logical> + ColPrunable + PredicatePushdown + ToBatch + ToStream
215{
216}
217
218macro_rules! impl_trait {
219    ($($convention:ident),+) => {
220        paste! {
221            $(
222                impl Hash for dyn [<$convention  PlanNode>] {
223                    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
224                        self.dyn_hash(state);
225                    }
226                }
227
228                impl PartialEq for dyn [<$convention  PlanNode>] {
229                    fn eq(&self, other: &Self) -> bool {
230                        self.dyn_eq(other.as_dyn_eq())
231                    }
232                }
233
234                impl Eq for dyn [<$convention  PlanNode>] {}
235            )+
236        }
237    };
238}
239
240impl_trait!(Batch, Stream, Logical);
241impl_downcast!(BatchPlanNode);
242impl_downcast!(LogicalPlanNode);
243impl_downcast!(StreamPlanNode);
244
245// Using a new type wrapper allows direct function implementation on `PlanRef`,
246// and we currently need a manual implementation of `PartialEq` for `PlanRef`.
247#[expect(clippy::derived_hash_with_manual_eq)]
248#[derive(Debug, Eq, Hash)]
249pub struct PlanRef<C: ConventionMarker>(Rc<C::PlanRefDyn>);
250
251impl<C: ConventionMarker> Clone for PlanRef<C> {
252    fn clone(&self) -> Self {
253        Self(self.0.clone())
254    }
255}
256
257pub type LogicalPlanRef = PlanRef<Logical>;
258pub type StreamPlanRef = PlanRef<Stream>;
259pub type BatchPlanRef = PlanRef<Batch>;
260
261// Cannot use the derived implementation for now.
262// See https://github.com/rust-lang/rust/issues/31740
263#[expect(clippy::op_ref)]
264impl<C: ConventionMarker> PartialEq for PlanRef<C> {
265    fn eq(&self, other: &Self) -> bool {
266        &self.0 == &other.0
267    }
268}
269
270impl<C: ConventionMarker> Deref for PlanRef<C> {
271    type Target = C::PlanRefDyn;
272
273    fn deref(&self) -> &Self::Target {
274        self.0.deref()
275    }
276}
277
278impl<T: LogicalPlanNode> From<T> for PlanRef<Logical> {
279    fn from(value: T) -> Self {
280        PlanRef(Rc::new(value) as _)
281    }
282}
283
284impl<T: StreamPlanNode> From<T> for PlanRef<Stream> {
285    fn from(value: T) -> Self {
286        PlanRef(Rc::new(value) as _)
287    }
288}
289
290impl<T: BatchPlanNode> From<T> for PlanRef<Batch> {
291    fn from(value: T) -> Self {
292        PlanRef(Rc::new(value) as _)
293    }
294}
295
296impl<C: ConventionMarker> Layer for PlanRef<C> {
297    type Sub = Self;
298
299    fn map<F>(self, f: F) -> Self
300    where
301        F: FnMut(Self::Sub) -> Self::Sub,
302    {
303        self.clone_root_with_inputs(&self.inputs().into_iter().map(f).collect_vec())
304    }
305
306    fn descent<F>(&self, f: F)
307    where
308        F: FnMut(&Self::Sub),
309    {
310        self.inputs().iter().for_each(f);
311    }
312}
313
314#[derive(Clone, Debug, Copy, Serialize, Hash, Eq, PartialEq, PartialOrd, Ord)]
315pub struct PlanNodeId(pub i32);
316
317impl PlanNodeId {
318    pub fn to_stream_node_operator_id(self) -> StreamNodeLocalOperatorId {
319        StreamNodeLocalOperatorId::new(self.0 as _)
320    }
321}
322
323/// A more sophisticated `Endo` taking into account of the DAG structure of `PlanRef`.
324/// In addition to `Endo`, one have to specify the `cached` function
325/// to persist transformed `LogicalShare` and their results,
326/// and the `dag_apply` function will take care to only transform every `LogicalShare` nodes once.
327///
328/// Note: Due to the way super trait is designed in rust,
329/// one need to have separate implementation blocks of `Endo<PlanRef>` and `EndoPlan`.
330/// And conventionally the real transformation `apply` is under `Endo<PlanRef>`,
331/// although one can refer to `dag_apply` in the implementation of `apply`.
332pub trait EndoPlan: Endo<LogicalPlanRef> {
333    // Return the cached result of `plan` if present,
334    // otherwise store and return the value provided by `f`.
335    // Notice that to allow mutable access of `self` in `f`,
336    // we let `f` to take `&mut Self` as its first argument.
337    fn cached<F>(&mut self, plan: LogicalPlanRef, f: F) -> LogicalPlanRef
338    where
339        F: FnMut(&mut Self) -> LogicalPlanRef;
340
341    fn dag_apply(&mut self, plan: LogicalPlanRef) -> LogicalPlanRef {
342        match plan.as_logical_share() {
343            Some(_) => self.cached(plan.clone(), |this| this.tree_apply(plan.clone())),
344            None => self.tree_apply(plan),
345        }
346    }
347}
348
349/// A more sophisticated `Visit` taking into account of the DAG structure of `PlanRef`.
350/// In addition to `Visit`, one have to specify `visited`
351/// to store and report visited `LogicalShare` nodes,
352/// and the `dag_visit` function will take care to only visit every `LogicalShare` nodes once.
353/// See also `EndoPlan`.
354pub trait VisitPlan: Visit<LogicalPlanRef> {
355    // Skip visiting `plan` if visited, otherwise run the traversal provided by `f`.
356    // Notice that to allow mutable access of `self` in `f`,
357    // we let `f` to take `&mut Self` as its first argument.
358    fn visited<F>(&mut self, plan: &LogicalPlanRef, f: F)
359    where
360        F: FnMut(&mut Self);
361
362    fn dag_visit(&mut self, plan: &LogicalPlanRef) {
363        match plan.as_logical_share() {
364            Some(_) => self.visited(plan, |this| this.tree_visit(plan)),
365            None => self.tree_visit(plan),
366        }
367    }
368}
369
370impl<C: ConventionMarker> PlanRef<C> {
371    pub fn rewrite_exprs_recursive(&self, r: &mut impl ExprRewriter) -> PlanRef<C> {
372        let new = self.rewrite_exprs(r);
373        let inputs: Vec<PlanRef<C>> = new
374            .inputs()
375            .iter()
376            .map(|plan_ref| plan_ref.rewrite_exprs_recursive(r))
377            .collect();
378        new.clone_root_with_inputs(&inputs[..])
379    }
380}
381
382pub(crate) trait VisitExprsRecursive {
383    fn visit_exprs_recursive(&self, r: &mut impl ExprVisitor);
384}
385
386impl<C: ConventionMarker> VisitExprsRecursive for PlanRef<C> {
387    fn visit_exprs_recursive(&self, r: &mut impl ExprVisitor) {
388        self.visit_exprs(r);
389        self.inputs()
390            .iter()
391            .for_each(|plan_ref| plan_ref.visit_exprs_recursive(r));
392    }
393}
394
395impl<C: ConventionMarker> PlanRef<C> {
396    pub fn expect_stream_key(&self) -> &[usize] {
397        self.stream_key().unwrap_or_else(|| {
398            panic!(
399                "a stream key is expected but not exist, plan:\n{}",
400                self.explain_to_string()
401            )
402        })
403    }
404}
405
406impl LogicalPlanRef {
407    fn prune_col_inner(
408        &self,
409        required_cols: &[usize],
410        ctx: &mut ColumnPruningContext,
411    ) -> LogicalPlanRef {
412        if let Some(logical_share) = self.as_logical_share() {
413            // Check the share cache first. If cache exists, it means this is the second round of
414            // column pruning.
415            if let Some((new_share, merge_required_cols)) = ctx.get_share_cache(self.id()) {
416                // Piggyback share remove if its has only one parent.
417                if ctx.get_parent_num(logical_share) == 1 {
418                    let input: LogicalPlanRef = logical_share.input();
419                    return input.prune_col(required_cols, ctx);
420                }
421
422                // If it is the first visit, recursively call `prune_col` for its input and
423                // replace it.
424                if ctx.visit_share_at_first_round(self.id()) {
425                    let new_logical_share: &LogicalShare = new_share
426                        .as_logical_share()
427                        .expect("must be share operator");
428                    let new_share_input = new_logical_share.input().prune_col(
429                        &(0..new_logical_share.base.schema().len()).collect_vec(),
430                        ctx,
431                    );
432                    new_logical_share.replace_input(new_share_input);
433                }
434
435                // Calculate the new required columns based on the new share.
436                let new_required_cols: Vec<usize> = required_cols
437                    .iter()
438                    .map(|col| merge_required_cols.iter().position(|x| x == col).unwrap())
439                    .collect_vec();
440                let mapping = ColIndexMapping::with_remaining_columns(
441                    &new_required_cols,
442                    new_share.schema().len(),
443                );
444                return LogicalProject::with_mapping(new_share, mapping).into();
445            }
446
447            // `LogicalShare` can't clone, so we implement column pruning for `LogicalShare`
448            // here.
449            // Basically, we need to wait for all parents of `LogicalShare` to prune columns before
450            // we merge the required columns and prune.
451            let parent_has_pushed = ctx.add_required_cols(self.id(), required_cols.into());
452            if parent_has_pushed == ctx.get_parent_num(logical_share) {
453                let merge_require_cols = ctx
454                    .take_required_cols(self.id())
455                    .expect("must have required columns")
456                    .into_iter()
457                    .flat_map(|x| x.into_iter())
458                    .sorted()
459                    .dedup()
460                    .collect_vec();
461                let input: LogicalPlanRef = logical_share.input();
462                let input = input.prune_col(&merge_require_cols, ctx);
463
464                // Cache the new share operator for the second round.
465                let new_logical_share = LogicalShare::create(input.clone());
466                ctx.add_share_cache(self.id(), new_logical_share, merge_require_cols.clone());
467
468                let exprs = logical_share
469                    .base
470                    .schema()
471                    .fields
472                    .iter()
473                    .enumerate()
474                    .map(|(i, field)| {
475                        if let Some(pos) = merge_require_cols.iter().position(|x| *x == i) {
476                            ExprImpl::InputRef(Box::new(InputRef::new(
477                                pos,
478                                field.data_type.clone(),
479                            )))
480                        } else {
481                            ExprImpl::Literal(Box::new(Literal::new(None, field.data_type.clone())))
482                        }
483                    })
484                    .collect_vec();
485                let project = LogicalProject::create(input, exprs);
486                logical_share.replace_input(project);
487            }
488            let mapping =
489                ColIndexMapping::with_remaining_columns(required_cols, self.schema().len());
490            LogicalProject::with_mapping(self.clone(), mapping).into()
491        } else {
492            // Dispatch to dyn PlanNode instead of PlanRef.
493            let dyn_t = self.deref();
494            dyn_t.prune_col(required_cols, ctx)
495        }
496    }
497
498    fn predicate_pushdown_inner(
499        &self,
500        predicate: Condition,
501        ctx: &mut PredicatePushdownContext,
502    ) -> LogicalPlanRef {
503        if let Some(logical_share) = self.as_logical_share() {
504            // Piggyback share remove if its has only one parent.
505            if ctx.get_parent_num(logical_share) == 1 {
506                let input: LogicalPlanRef = logical_share.input();
507                return input.predicate_pushdown(predicate, ctx);
508            }
509
510            // `LogicalShare` can't clone, so we implement predicate pushdown for `LogicalShare`
511            // here.
512            // Basically, we need to wait for all parents of `LogicalShare` to push down the
513            // predicate before we merge the predicates and pushdown.
514            let parent_has_pushed = ctx.add_predicate(self.id(), predicate.clone());
515            if parent_has_pushed == ctx.get_parent_num(logical_share) {
516                let merge_predicate = ctx
517                    .take_predicate(self.id())
518                    .expect("must have predicate")
519                    .into_iter()
520                    .map(|mut c| Condition {
521                        conjunctions: c
522                            .conjunctions
523                            .extract_if(.., |e| {
524                                // If predicates contain now, impure or correlated input ref, don't push through share operator.
525                                // The predicate with now() function is regarded as a temporal filter predicate, which will be transformed to a temporal filter operator and can not do the OR operation with other predicates.
526                                let mut finder = ExprCorrelatedIdFinder::default();
527                                finder.visit_expr(e);
528                                e.count_nows() == 0
529                                    && e.is_pure()
530                                    && !finder.has_correlated_input_ref()
531                            })
532                            .collect(),
533                    })
534                    .reduce(|a, b| a.or(b))
535                    .unwrap();
536
537                // rewrite the *entire* predicate for `LogicalShare`
538                // before pushing down to whatever plan node(s)
539                // ps: the reason here contains a "special" optimization
540                // rather than directly apply explicit rule in stream or
541                // batch plan optimization, is because predicate push down
542                // will *instantly* push down all predicates, and rule(s)
543                // can not be applied in the middle.
544                // thus we need some on-the-fly (in the middle) rewrite
545                // technique to help with this kind of optimization.
546                let mut expr_rewriter = ExpressionSimplifyRewriter {};
547                let mut new_predicate = Condition::true_cond();
548
549                for c in merge_predicate.conjunctions {
550                    let c = Condition::with_expr(expr_rewriter.rewrite_cond(c));
551                    // rebuild the conjunctions
552                    new_predicate = new_predicate.and(c);
553                }
554
555                let input: LogicalPlanRef = logical_share.input();
556                let input = input.predicate_pushdown(new_predicate, ctx);
557                logical_share.replace_input(input);
558            }
559            LogicalFilter::create(self.clone(), predicate)
560        } else {
561            // Dispatch to dyn PlanNode instead of PlanRef.
562            let dyn_t = self.deref();
563            dyn_t.predicate_pushdown(predicate, ctx)
564        }
565    }
566
567    pub fn forbid_snapshot_backfill(&self) -> Option<String> {
568        struct ForbidSnapshotBackfill {
569            warning_msg: Option<String>,
570        }
571        impl LogicalPlanVisitor for ForbidSnapshotBackfill {
572            type Result = ();
573
574            type DefaultBehavior = impl DefaultBehavior<Self::Result>;
575
576            fn default_behavior() -> Self::DefaultBehavior {
577                DefaultValue
578            }
579
580            fn visit_logical_join(&mut self, plan: &LogicalJoin) -> Self::Result {
581                self.visit(plan.left());
582                self.visit(plan.right());
583                if self.warning_msg.is_none() && plan.should_be_temporal_join() {
584                    self.warning_msg =
585                        Some("snapshot backfill disabled due to temporal join".to_owned());
586                }
587            }
588
589            fn visit_logical_source(&mut self, plan: &LogicalSource) -> Self::Result {
590                if self.warning_msg.is_none() && plan.is_shared_source() {
591                    self.warning_msg = Some(format!(
592                        "snapshot backfill disabled due to using shared source {:?}",
593                        plan.core.catalog.as_ref().map(|c| &c.name)
594                    ));
595                }
596            }
597        }
598        let mut forbid_snapshot = ForbidSnapshotBackfill { warning_msg: None };
599        forbid_snapshot.visit(self.clone());
600        forbid_snapshot.warning_msg
601    }
602}
603
604impl ColPrunable for LogicalPlanRef {
605    fn prune_col(&self, required_cols: &[usize], ctx: &mut ColumnPruningContext) -> LogicalPlanRef {
606        let res = self.prune_col_inner(required_cols, ctx);
607        #[cfg(debug_assertions)]
608        super::heuristic_optimizer::HeuristicOptimizer::check_equivalent_plan(
609            "column pruning",
610            &LogicalProject::with_out_col_idx(self.clone(), required_cols.iter().cloned()).into(),
611            &res,
612        );
613        res
614    }
615}
616
617impl PredicatePushdown for LogicalPlanRef {
618    fn predicate_pushdown(
619        &self,
620        predicate: Condition,
621        ctx: &mut PredicatePushdownContext,
622    ) -> LogicalPlanRef {
623        #[cfg(debug_assertions)]
624        let predicate_clone = predicate.clone();
625
626        let res = self.predicate_pushdown_inner(predicate, ctx);
627
628        #[cfg(debug_assertions)]
629        super::heuristic_optimizer::HeuristicOptimizer::check_equivalent_plan(
630            "predicate push down",
631            &LogicalFilter::new(self.clone(), predicate_clone).into(),
632            &res,
633        );
634
635        res
636    }
637}
638
639impl<C: ConventionMarker> PlanRef<C> {
640    pub fn clone_root_with_inputs(&self, inputs: &[PlanRef<C>]) -> PlanRef<C> {
641        if let Some(share) = self.as_share_node() {
642            assert_eq!(inputs.len(), 1);
643            // We can't clone `LogicalShare`, but only can replace input instead.
644            share.replace_input(inputs[0].clone());
645            self.clone()
646        } else {
647            // Dispatch to dyn PlanNode instead of PlanRef.
648            let dyn_t = self.deref();
649            dyn_t.clone_with_inputs(inputs)
650        }
651    }
652}
653
654/// Implement again for the `dyn` newtype wrapper.
655impl<C: ConventionMarker> PlanRef<C> {
656    pub fn node_type(&self) -> C::PlanNodeType {
657        self.0.node_type()
658    }
659
660    pub fn plan_base(&self) -> &PlanBase<C> {
661        self.0.plan_base()
662    }
663}
664
665/// Allow access to all fields defined in [`GenericPlanRef`] for the type-erased plan node.
666// TODO: may also implement on `dyn PlanNode` directly.
667impl<C: ConventionMarker> GenericPlanRef for PlanRef<C> {
668    fn id(&self) -> PlanNodeId {
669        self.plan_base().id()
670    }
671
672    fn schema(&self) -> &Schema {
673        self.plan_base().schema()
674    }
675
676    fn stream_key(&self) -> Option<&[usize]> {
677        self.plan_base().stream_key()
678    }
679
680    fn ctx(&self) -> OptimizerContextRef {
681        self.plan_base().ctx()
682    }
683
684    fn functional_dependency(&self) -> &FunctionalDependencySet {
685        self.plan_base().functional_dependency()
686    }
687}
688
689/// Allow access to all fields defined in [`PhysicalPlanRef`] for the type-erased plan node.
690// TODO: may also implement on `dyn PlanNode` directly.
691impl PhysicalPlanRef for BatchPlanRef {
692    fn distribution(&self) -> &Distribution {
693        self.plan_base().distribution()
694    }
695}
696
697impl PhysicalPlanRef for StreamPlanRef {
698    fn distribution(&self) -> &Distribution {
699        self.plan_base().distribution()
700    }
701}
702
703/// Allow access to all fields defined in [`StreamPlanNodeMetadata`] for the type-erased plan node.
704// TODO: may also implement on `dyn PlanNode` directly.
705impl StreamPlanNodeMetadata for StreamPlanRef {
706    fn stream_kind(&self) -> StreamKind {
707        self.plan_base().stream_kind()
708    }
709
710    fn emit_on_window_close(&self) -> bool {
711        self.plan_base().emit_on_window_close()
712    }
713
714    fn watermark_columns(&self) -> &WatermarkColumns {
715        self.plan_base().watermark_columns()
716    }
717
718    fn columns_monotonicity(&self) -> &MonotonicityMap {
719        self.plan_base().columns_monotonicity()
720    }
721}
722
723/// Allow access to all fields defined in [`BatchPlanNodeMetadata`] for the type-erased plan node.
724// TODO: may also implement on `dyn PlanNode` directly.
725impl BatchPlanNodeMetadata for BatchPlanRef {
726    fn order(&self) -> &Order {
727        self.plan_base().order()
728    }
729
730    fn orders(&self) -> Vec<Order> {
731        self.plan_base().orders()
732    }
733}
734
735/// In order to let expression display id started from 1 for explaining, hidden column names and
736/// other places. We will reset expression display id to 0 and clone the whole plan to reset the
737/// schema.
738pub fn reorganize_elements_id<C: ConventionMarker>(plan: PlanRef<C>) -> PlanRef<C> {
739    let backup = plan.ctx().backup_elem_ids();
740    plan.ctx().reset_elem_ids();
741    let plan = PlanCloner::clone_whole_plan(plan);
742    plan.ctx().restore_elem_ids(backup);
743    plan
744}
745
746pub trait Explain {
747    /// Write explain the whole plan tree.
748    fn explain<'a>(&self) -> Pretty<'a>;
749
750    /// Write explain the whole plan tree with node id.
751    fn explain_with_id<'a>(&self) -> Pretty<'a>;
752
753    /// Explain the plan node and return a string.
754    fn explain_to_string(&self) -> String;
755
756    /// Explain the plan node and return a json string.
757    fn explain_to_json(&self) -> String;
758
759    /// Explain the plan node and return a xml string.
760    fn explain_to_xml(&self) -> String;
761
762    /// Explain the plan node and return a yaml string.
763    fn explain_to_yaml(&self) -> String;
764
765    /// Explain the plan node and return a dot format string.
766    fn explain_to_dot(&self) -> String;
767}
768
769impl<C: ConventionMarker> Explain for PlanRef<C> {
770    /// Write explain the whole plan tree.
771    fn explain<'a>(&self) -> Pretty<'a> {
772        let mut node = self.distill();
773        let inputs = self.inputs();
774        for input in inputs.iter().peekable() {
775            node.children.push(input.explain());
776        }
777        Pretty::Record(node)
778    }
779
780    /// Write explain the whole plan tree with node id.
781    fn explain_with_id<'a>(&self) -> Pretty<'a> {
782        let node_id = self.id();
783        let mut node = self.distill();
784        // NOTE(kwannoel): Can lead to poor performance if plan is very large,
785        // but we want to show the id first.
786        node.fields
787            .insert(0, ("id".into(), Pretty::display(&node_id.0)));
788        let inputs = self.inputs();
789        for input in inputs.iter().peekable() {
790            node.children.push(input.explain_with_id());
791        }
792        Pretty::Record(node)
793    }
794
795    /// Explain the plan node and return a string.
796    fn explain_to_string(&self) -> String {
797        let plan = reorganize_elements_id(self.clone());
798
799        let mut output = String::with_capacity(2048);
800        let mut config = pretty_config();
801        config.unicode(&mut output, &plan.explain());
802        output
803    }
804
805    /// Explain the plan node and return a json string.
806    fn explain_to_json(&self) -> String {
807        let plan = reorganize_elements_id(self.clone());
808        let explain_ir = plan.explain();
809        serde_json::to_string_pretty(&PrettySerde(explain_ir, true))
810            .expect("failed to serialize plan to json")
811    }
812
813    /// Explain the plan node and return a xml string.
814    fn explain_to_xml(&self) -> String {
815        let plan = reorganize_elements_id(self.clone());
816        let explain_ir = plan.explain();
817        quick_xml::se::to_string(&PrettySerde(explain_ir, true))
818            .expect("failed to serialize plan to xml")
819    }
820
821    /// Explain the plan node and return a yaml string.
822    fn explain_to_yaml(&self) -> String {
823        let plan = reorganize_elements_id(self.clone());
824        let explain_ir = plan.explain();
825        serde_yaml::to_string(&PrettySerde(explain_ir, true))
826            .expect("failed to serialize plan to yaml")
827    }
828
829    /// Explain the plan node and return a dot format string.
830    fn explain_to_dot(&self) -> String {
831        let plan = reorganize_elements_id(self.clone());
832        let explain_ir = plan.explain_with_id();
833        let mut graph = Graph::<String, String>::new();
834        let mut nodes = HashMap::new();
835        build_graph_from_pretty(&explain_ir, &mut graph, &mut nodes, None);
836        let dot = Dot::with_config(&graph, &[Config::EdgeNoLabel]);
837        dot.to_string()
838    }
839}
840
841impl<C: ConventionMarker> PlanRef<C> {
842    pub fn as_share_node(&self) -> Option<&C::ShareNode> {
843        C::as_share(self)
844    }
845}
846
847pub(crate) fn pretty_config() -> PrettyConfig {
848    PrettyConfig {
849        indent: 3,
850        need_boundaries: false,
851        width: 2048,
852        reduced_spaces: true,
853    }
854}
855
856macro_rules! impl_generic_plan_ref_method {
857    ($($convention:ident),+) => {
858        paste! {
859            $(
860                /// Directly implement methods for `PlanNode` to access the fields defined in [`GenericPlanRef`].
861                impl dyn [<$convention PlanNode>] {
862                    pub fn id(&self) -> PlanNodeId {
863                        self.plan_base().id()
864                    }
865
866                    pub fn ctx(&self) -> OptimizerContextRef {
867                        self.plan_base().ctx().clone()
868                    }
869
870                    pub fn schema(&self) -> &Schema {
871                        self.plan_base().schema()
872                    }
873
874                    pub fn stream_key(&self) -> Option<&[usize]> {
875                        self.plan_base().stream_key()
876                    }
877
878                    pub fn functional_dependency(&self) -> &FunctionalDependencySet {
879                        self.plan_base().functional_dependency()
880                    }
881
882                    pub fn explain_myself_to_string(&self) -> String {
883                        self.distill_to_string()
884                    }
885                }
886            )+
887        }
888    };
889}
890
891impl_generic_plan_ref_method!(Batch, Stream, Logical);
892
893/// Recursion depth threshold for plan node visitor to send notice to user.
894pub const PLAN_DEPTH_THRESHOLD: usize = 30;
895/// Notice message for plan node visitor to send to user when the depth threshold is reached.
896pub const PLAN_TOO_DEEP_NOTICE: &str = "The plan is too deep. \
897Consider simplifying or splitting the query if you encounter any issues.";
898
899impl dyn StreamPlanNode {
900    /// Serialize the plan node and its children to a stream plan proto.
901    ///
902    /// Note that some operators has their own implementation of `to_stream_prost`. We have a
903    /// hook inside to do some ad-hoc things.
904    pub fn to_stream_prost(
905        &self,
906        state: &mut BuildFragmentGraphState,
907    ) -> SchedulerResult<PbStreamPlan> {
908        recursive::tracker!().recurse(|t| {
909            if t.depth_reaches(PLAN_DEPTH_THRESHOLD) {
910                notice_to_user(PLAN_TOO_DEEP_NOTICE);
911            }
912
913            use stream::prelude::*;
914
915            if let Some(stream_table_scan) = self.as_stream_table_scan() {
916                return stream_table_scan.adhoc_to_stream_prost(state);
917            }
918            if let Some(stream_cdc_table_scan) = self.as_stream_cdc_table_scan() {
919                return stream_cdc_table_scan.adhoc_to_stream_prost(state);
920            }
921            if let Some(stream_source_scan) = self.as_stream_source_scan() {
922                return stream_source_scan.adhoc_to_stream_prost(state);
923            }
924            if let Some(stream_share) = self.as_stream_share() {
925                return stream_share.adhoc_to_stream_prost(state);
926            }
927
928            let node = Some(self.try_to_stream_prost_body(state)?);
929            let input = self
930                .inputs()
931                .into_iter()
932                .map(|plan| plan.to_stream_prost(state))
933                .try_collect()?;
934            // TODO: support pk_indices and operator_id
935            Ok(PbStreamPlan {
936                input,
937                identity: self.explain_myself_to_string(),
938                node_body: node,
939                operator_id: self.id().to_stream_node_operator_id(),
940                stream_key: self
941                    .stream_key()
942                    .unwrap_or_default()
943                    .iter()
944                    .map(|x| *x as u32)
945                    .collect(),
946                fields: self.schema().to_prost(),
947                stream_kind: self.plan_base().stream_kind().to_protobuf() as i32,
948            })
949        })
950    }
951}
952
953impl dyn BatchPlanNode {
954    /// Serialize the plan node and its children to a batch plan proto.
955    pub fn to_batch_prost(&self) -> SchedulerResult<PbBatchPlan> {
956        self.to_batch_prost_identity(true)
957    }
958
959    /// Serialize the plan node and its children to a batch plan proto without the identity field
960    /// (for testing).
961    pub fn to_batch_prost_identity(&self, identity: bool) -> SchedulerResult<PbBatchPlan> {
962        recursive::tracker!().recurse(|t| {
963            if t.depth_reaches(PLAN_DEPTH_THRESHOLD) {
964                notice_to_user(PLAN_TOO_DEEP_NOTICE);
965            }
966
967            let node_body = Some(self.try_to_batch_prost_body()?);
968            let children = self
969                .inputs()
970                .into_iter()
971                .map(|plan| plan.to_batch_prost_identity(identity))
972                .try_collect()?;
973            Ok(PbBatchPlan {
974                children,
975                identity: if identity {
976                    self.explain_myself_to_string()
977                } else {
978                    "".into()
979                },
980                node_body,
981            })
982        })
983    }
984}
985
986mod plan_base;
987pub use plan_base::*;
988#[macro_use]
989mod plan_tree_node;
990pub use plan_tree_node::*;
991mod col_pruning;
992pub use col_pruning::*;
993mod expr_rewritable;
994pub use expr_rewritable::*;
995mod expr_visitable;
996
997mod convert;
998pub use convert::*;
999mod eq_join_predicate;
1000pub use eq_join_predicate::*;
1001mod to_prost;
1002pub use to_prost::*;
1003mod predicate_pushdown;
1004pub use predicate_pushdown::*;
1005mod merge_eq_nodes;
1006pub use merge_eq_nodes::*;
1007
1008pub mod batch;
1009pub mod generic;
1010pub mod stream;
1011
1012pub use generic::{PlanAggCall, PlanAggCallDisplay};
1013
1014mod batch_delete;
1015mod batch_exchange;
1016mod batch_expand;
1017mod batch_filter;
1018mod batch_get_channel_delta_stats;
1019mod batch_group_topn;
1020mod batch_hash_agg;
1021mod batch_hash_join;
1022mod batch_hop_window;
1023mod batch_insert;
1024mod batch_limit;
1025mod batch_log_seq_scan;
1026mod batch_lookup_join;
1027mod batch_max_one_row;
1028mod batch_nested_loop_join;
1029mod batch_over_window;
1030mod batch_project;
1031mod batch_project_set;
1032mod batch_seq_scan;
1033mod batch_simple_agg;
1034mod batch_sort;
1035mod batch_sort_agg;
1036mod batch_source;
1037mod batch_sys_seq_scan;
1038mod batch_table_function;
1039mod batch_topn;
1040mod batch_union;
1041mod batch_update;
1042mod batch_values;
1043mod logical_agg;
1044mod logical_apply;
1045mod logical_cdc_scan;
1046mod logical_changelog;
1047mod logical_dedup;
1048mod logical_delete;
1049mod logical_except;
1050mod logical_expand;
1051mod logical_filter;
1052mod logical_gap_fill;
1053mod logical_get_channel_delta_stats;
1054mod logical_hop_window;
1055mod logical_insert;
1056mod logical_intersect;
1057mod logical_join;
1058mod logical_kafka_scan;
1059mod logical_limit;
1060mod logical_locality_provider;
1061mod logical_max_one_row;
1062mod logical_multi_join;
1063mod logical_now;
1064mod logical_over_window;
1065mod logical_project;
1066mod logical_project_set;
1067mod logical_scan;
1068mod logical_share;
1069mod logical_source;
1070mod logical_sys_scan;
1071mod logical_table_function;
1072mod logical_topn;
1073mod logical_union;
1074mod logical_update;
1075mod logical_values;
1076mod stream_asof_join;
1077mod stream_changelog;
1078mod stream_dedup;
1079mod stream_delta_join;
1080mod stream_dml;
1081mod stream_dynamic_filter;
1082mod stream_eowc_gap_fill;
1083mod stream_eowc_over_window;
1084mod stream_exchange;
1085mod stream_expand;
1086mod stream_filter;
1087mod stream_fs_fetch;
1088mod stream_gap_fill;
1089mod stream_global_approx_percentile;
1090mod stream_group_topn;
1091mod stream_hash_agg;
1092mod stream_hash_join;
1093mod stream_hop_window;
1094mod stream_iceberg_with_pk_index_dv_merger;
1095mod stream_iceberg_with_pk_index_writer;
1096mod stream_join_common;
1097mod stream_local_approx_percentile;
1098mod stream_locality_provider;
1099mod stream_materialize;
1100mod stream_materialized_exprs;
1101mod stream_now;
1102mod stream_over_window;
1103mod stream_project;
1104mod stream_project_set;
1105mod stream_row_id_gen;
1106mod stream_row_merge;
1107mod stream_simple_agg;
1108mod stream_sink;
1109mod stream_sort;
1110mod stream_source;
1111mod stream_source_scan;
1112mod stream_stateless_simple_agg;
1113mod stream_sync_log_store;
1114mod stream_table_scan;
1115mod stream_topn;
1116mod stream_union;
1117mod stream_values;
1118mod stream_watermark_filter;
1119
1120mod batch_file_scan;
1121mod batch_iceberg_scan;
1122mod batch_kafka_scan;
1123mod batch_postgres_query;
1124
1125mod batch_mysql_query;
1126mod derive;
1127mod logical_file_scan;
1128mod logical_iceberg_intermediate_scan;
1129mod logical_iceberg_scan;
1130mod logical_postgres_query;
1131
1132mod batch_vector_search;
1133mod logical_mysql_query;
1134mod logical_vector_search;
1135mod logical_vector_search_lookup_join;
1136mod stream_cdc_table_scan;
1137mod stream_share;
1138mod stream_temporal_join;
1139mod stream_upstream_sink_union;
1140mod stream_vector_index_lookup_join;
1141mod stream_vector_index_write;
1142pub mod utils;
1143
1144pub use batch_delete::BatchDelete;
1145pub use batch_exchange::BatchExchange;
1146pub use batch_expand::BatchExpand;
1147pub use batch_file_scan::BatchFileScan;
1148pub use batch_filter::BatchFilter;
1149pub use batch_get_channel_delta_stats::BatchGetChannelDeltaStats;
1150pub use batch_group_topn::BatchGroupTopN;
1151pub use batch_hash_agg::BatchHashAgg;
1152pub use batch_hash_join::BatchHashJoin;
1153pub use batch_hop_window::BatchHopWindow;
1154pub use batch_iceberg_scan::BatchIcebergScan;
1155pub use batch_insert::BatchInsert;
1156pub use batch_kafka_scan::BatchKafkaScan;
1157pub use batch_limit::BatchLimit;
1158pub use batch_log_seq_scan::BatchLogSeqScan;
1159pub use batch_lookup_join::BatchLookupJoin;
1160pub use batch_max_one_row::BatchMaxOneRow;
1161pub use batch_mysql_query::BatchMySqlQuery;
1162pub use batch_nested_loop_join::BatchNestedLoopJoin;
1163pub use batch_over_window::BatchOverWindow;
1164pub use batch_postgres_query::BatchPostgresQuery;
1165pub use batch_project::BatchProject;
1166pub use batch_project_set::BatchProjectSet;
1167pub use batch_seq_scan::BatchSeqScan;
1168pub use batch_simple_agg::BatchSimpleAgg;
1169pub use batch_sort::BatchSort;
1170pub use batch_sort_agg::BatchSortAgg;
1171pub use batch_source::BatchSource;
1172pub use batch_sys_seq_scan::BatchSysSeqScan;
1173pub use batch_table_function::BatchTableFunction;
1174pub use batch_topn::BatchTopN;
1175pub use batch_union::BatchUnion;
1176pub use batch_update::BatchUpdate;
1177pub use batch_values::BatchValues;
1178pub use batch_vector_search::BatchVectorSearch;
1179pub use logical_agg::LogicalAgg;
1180pub use logical_apply::LogicalApply;
1181pub use logical_cdc_scan::LogicalCdcScan;
1182pub use logical_changelog::LogicalChangeLog;
1183pub use logical_dedup::LogicalDedup;
1184pub use logical_delete::LogicalDelete;
1185pub use logical_except::LogicalExcept;
1186pub use logical_expand::LogicalExpand;
1187pub use logical_file_scan::LogicalFileScan;
1188pub use logical_filter::LogicalFilter;
1189pub use logical_gap_fill::LogicalGapFill;
1190pub use logical_get_channel_delta_stats::LogicalGetChannelDeltaStats;
1191pub use logical_hop_window::LogicalHopWindow;
1192pub use logical_iceberg_intermediate_scan::{HummockRewriteInfo, LogicalIcebergIntermediateScan};
1193pub use logical_iceberg_scan::LogicalIcebergScan;
1194pub use logical_insert::LogicalInsert;
1195pub use logical_intersect::LogicalIntersect;
1196pub use logical_join::LogicalJoin;
1197pub use logical_kafka_scan::LogicalKafkaScan;
1198pub use logical_limit::LogicalLimit;
1199pub use logical_locality_provider::LogicalLocalityProvider;
1200pub use logical_max_one_row::LogicalMaxOneRow;
1201pub use logical_multi_join::{LogicalMultiJoin, LogicalMultiJoinBuilder};
1202pub use logical_mysql_query::LogicalMySqlQuery;
1203pub use logical_now::LogicalNow;
1204pub use logical_over_window::LogicalOverWindow;
1205pub use logical_postgres_query::LogicalPostgresQuery;
1206pub use logical_project::LogicalProject;
1207pub use logical_project_set::LogicalProjectSet;
1208pub use logical_scan::LogicalScan;
1209pub use logical_share::LogicalShare;
1210pub use logical_source::LogicalSource;
1211pub use logical_sys_scan::LogicalSysScan;
1212pub use logical_table_function::LogicalTableFunction;
1213pub use logical_topn::LogicalTopN;
1214pub use logical_union::LogicalUnion;
1215pub use logical_update::LogicalUpdate;
1216pub use logical_values::LogicalValues;
1217pub use logical_vector_search::LogicalVectorSearch;
1218pub use logical_vector_search_lookup_join::LogicalVectorSearchLookupJoin;
1219use risingwave_pb::id::StreamNodeLocalOperatorId;
1220pub use stream_asof_join::StreamAsOfJoin;
1221pub use stream_cdc_table_scan::StreamCdcTableScan;
1222pub use stream_changelog::StreamChangeLog;
1223pub use stream_dedup::StreamDedup;
1224pub use stream_delta_join::StreamDeltaJoin;
1225pub use stream_dml::StreamDml;
1226pub use stream_dynamic_filter::StreamDynamicFilter;
1227pub use stream_eowc_gap_fill::StreamEowcGapFill;
1228pub use stream_eowc_over_window::StreamEowcOverWindow;
1229pub use stream_exchange::StreamExchange;
1230pub use stream_expand::StreamExpand;
1231pub use stream_filter::StreamFilter;
1232pub use stream_fs_fetch::StreamFsFetch;
1233pub use stream_gap_fill::StreamGapFill;
1234pub use stream_global_approx_percentile::StreamGlobalApproxPercentile;
1235pub use stream_group_topn::StreamGroupTopN;
1236pub use stream_hash_agg::StreamHashAgg;
1237pub use stream_hash_join::StreamHashJoin;
1238pub use stream_hop_window::StreamHopWindow;
1239pub use stream_iceberg_with_pk_index_dv_merger::StreamIcebergWithPkIndexDvMerger;
1240pub use stream_iceberg_with_pk_index_writer::StreamIcebergWithPkIndexWriter;
1241use stream_join_common::StreamJoinCommon;
1242pub use stream_local_approx_percentile::StreamLocalApproxPercentile;
1243pub use stream_locality_provider::StreamLocalityProvider;
1244pub use stream_materialize::StreamMaterialize;
1245pub use stream_materialized_exprs::StreamMaterializedExprs;
1246pub use stream_now::StreamNow;
1247pub use stream_over_window::StreamOverWindow;
1248pub use stream_project::StreamProject;
1249pub use stream_project_set::StreamProjectSet;
1250pub use stream_row_id_gen::StreamRowIdGen;
1251pub use stream_row_merge::StreamRowMerge;
1252pub use stream_share::StreamShare;
1253pub use stream_simple_agg::StreamSimpleAgg;
1254pub use stream_sink::{IcebergPartitionInfo, PartitionComputeInfo, StreamSink};
1255pub use stream_sort::StreamEowcSort;
1256pub use stream_source::StreamSource;
1257pub use stream_source_scan::StreamSourceScan;
1258pub use stream_stateless_simple_agg::StreamStatelessSimpleAgg;
1259pub use stream_sync_log_store::StreamSyncLogStore;
1260pub(crate) use stream_sync_log_store::ensure_sync_log_store_fragment_root;
1261pub use stream_table_scan::StreamTableScan;
1262pub use stream_temporal_join::StreamTemporalJoin;
1263pub use stream_topn::StreamTopN;
1264pub use stream_union::StreamUnion;
1265pub use stream_upstream_sink_union::StreamUpstreamSinkUnion;
1266pub use stream_values::StreamValues;
1267pub use stream_vector_index_lookup_join::StreamVectorIndexLookupJoin;
1268pub use stream_vector_index_write::StreamVectorIndexWrite;
1269pub use stream_watermark_filter::StreamWatermarkFilter;
1270
1271use crate::expr::{ExprImpl, ExprRewriter, ExprVisitor, InputRef, Literal};
1272use crate::optimizer::optimizer_context::OptimizerContextRef;
1273use crate::optimizer::plan_node::expr_visitable::ExprVisitable;
1274use crate::optimizer::plan_rewriter::PlanCloner;
1275use crate::optimizer::plan_visitor::{
1276    DefaultBehavior, DefaultValue, ExprCorrelatedIdFinder, LogicalPlanVisitor,
1277};
1278use crate::scheduler::SchedulerResult;
1279use crate::stream_fragmenter::BuildFragmentGraphState;
1280use crate::utils::{ColIndexMapping, Condition, DynEq, DynHash, Endo, Layer, Visit};
1281
1282/// `for_all_plan_nodes` includes all plan nodes. If you added a new plan node
1283/// inside the project, be sure to add here and in its conventions like `for_logical_plan_nodes`
1284///
1285/// Every tuple has two elements, where `{ convention, name }`
1286/// You can use it as follows
1287/// ```rust
1288/// macro_rules! use_plan {
1289///     ($({ $convention:ident, $name:ident }),*) => {};
1290/// }
1291/// risingwave_frontend::for_all_plan_nodes! { use_plan }
1292/// ```
1293/// See the following implementations for example.
1294#[macro_export]
1295macro_rules! for_all_plan_nodes {
1296    ($macro:path $(,$rest:tt)*) => {
1297        $macro! {
1298              { Logical, Agg }
1299            , { Logical, Apply }
1300            , { Logical, Filter }
1301            , { Logical, Project }
1302            , { Logical, Scan }
1303            , { Logical, CdcScan }
1304            , { Logical, SysScan }
1305            , { Logical, Source }
1306            , { Logical, Insert }
1307            , { Logical, Delete }
1308            , { Logical, Update }
1309            , { Logical, Join }
1310            , { Logical, Values }
1311            , { Logical, Limit }
1312            , { Logical, TopN }
1313            , { Logical, HopWindow }
1314            , { Logical, TableFunction }
1315            , { Logical, MultiJoin }
1316            , { Logical, Expand }
1317            , { Logical, ProjectSet }
1318            , { Logical, Union }
1319            , { Logical, OverWindow }
1320            , { Logical, Share }
1321            , { Logical, Now }
1322            , { Logical, Dedup }
1323            , { Logical, Intersect }
1324            , { Logical, Except }
1325            , { Logical, MaxOneRow }
1326            , { Logical, KafkaScan }
1327            , { Logical, IcebergScan }
1328            , { Logical, IcebergIntermediateScan }
1329            , { Logical, ChangeLog }
1330            , { Logical, FileScan }
1331            , { Logical, PostgresQuery }
1332            , { Logical, MySqlQuery }
1333            , { Logical, GapFill }
1334            , { Logical, VectorSearch }
1335            , { Logical, GetChannelDeltaStats }
1336            , { Logical, LocalityProvider }
1337            , { Logical, VectorSearchLookupJoin }
1338            , { Batch, SimpleAgg }
1339            , { Batch, HashAgg }
1340            , { Batch, SortAgg }
1341            , { Batch, Project }
1342            , { Batch, Filter }
1343            , { Batch, Insert }
1344            , { Batch, Delete }
1345            , { Batch, Update }
1346            , { Batch, SeqScan }
1347            , { Batch, SysSeqScan }
1348            , { Batch, LogSeqScan }
1349            , { Batch, HashJoin }
1350            , { Batch, NestedLoopJoin }
1351            , { Batch, Values }
1352            , { Batch, Sort }
1353            , { Batch, Exchange }
1354            , { Batch, Limit }
1355            , { Batch, TopN }
1356            , { Batch, HopWindow }
1357            , { Batch, TableFunction }
1358            , { Batch, Expand }
1359            , { Batch, LookupJoin }
1360            , { Batch, ProjectSet }
1361            , { Batch, Union }
1362            , { Batch, GroupTopN }
1363            , { Batch, Source }
1364            , { Batch, OverWindow }
1365            , { Batch, MaxOneRow }
1366            , { Batch, KafkaScan }
1367            , { Batch, IcebergScan }
1368            , { Batch, FileScan }
1369            , { Batch, PostgresQuery }
1370            , { Batch, MySqlQuery }
1371            , { Batch, GetChannelDeltaStats }
1372            , { Batch, VectorSearch }
1373            , { Stream, Project }
1374            , { Stream, Filter }
1375            , { Stream, TableScan }
1376            , { Stream, CdcTableScan }
1377            , { Stream, Sink }
1378            , { Stream, Source }
1379            , { Stream, SourceScan }
1380            , { Stream, HashJoin }
1381            , { Stream, Exchange }
1382            , { Stream, HashAgg }
1383            , { Stream, SimpleAgg }
1384            , { Stream, StatelessSimpleAgg }
1385            , { Stream, Materialize }
1386            , { Stream, TopN }
1387            , { Stream, HopWindow }
1388            , { Stream, DeltaJoin }
1389            , { Stream, Expand }
1390            , { Stream, DynamicFilter }
1391            , { Stream, ProjectSet }
1392            , { Stream, GroupTopN }
1393            , { Stream, Union }
1394            , { Stream, RowIdGen }
1395            , { Stream, Dml }
1396            , { Stream, Now }
1397            , { Stream, Share }
1398            , { Stream, WatermarkFilter }
1399            , { Stream, TemporalJoin }
1400            , { Stream, Values }
1401            , { Stream, Dedup }
1402            , { Stream, EowcOverWindow }
1403            , { Stream, EowcSort }
1404            , { Stream, OverWindow }
1405            , { Stream, FsFetch }
1406            , { Stream, ChangeLog }
1407            , { Stream, GlobalApproxPercentile }
1408            , { Stream, LocalApproxPercentile }
1409            , { Stream, RowMerge }
1410            , { Stream, AsOfJoin }
1411            , { Stream, SyncLogStore }
1412            , { Stream, MaterializedExprs }
1413            , { Stream, VectorIndexWrite }
1414            , { Stream, VectorIndexLookupJoin }
1415            , { Stream, UpstreamSinkUnion }
1416            , { Stream, LocalityProvider }
1417            , { Stream, EowcGapFill }
1418            , { Stream, GapFill }
1419            , { Stream, IcebergWithPkIndexWriter }
1420            , { Stream, IcebergWithPkIndexDvMerger }
1421            $(,$rest)*
1422        }
1423    };
1424}
1425
1426#[macro_export]
1427macro_rules! for_each_convention_all_plan_nodes {
1428    ($macro:path $(,$rest:tt)*) => {
1429        $crate::for_all_plan_nodes! {
1430            $crate::for_each_convention_all_plan_nodes
1431            , $macro
1432            $(,$rest)*
1433        }
1434    };
1435    (
1436        $( { Logical, $logical_name:ident } ),*
1437        , $( { Batch, $batch_name:ident } ),*
1438        , $( { Stream, $stream_name:ident } ),*
1439        , $macro:path $(,$rest:tt)*
1440    ) => {
1441        $macro! {
1442            {
1443                Logical, { $( $logical_name ),* },
1444                Batch, { $( $batch_name ),* },
1445                Stream, { $( $stream_name ),* }
1446            }
1447            $(,$rest)*
1448        }
1449    }
1450}
1451
1452/// impl `PlanNodeType` fn for each node.
1453macro_rules! impl_plan_node_meta {
1454    ({
1455        $( $convention:ident, { $( $name:ident ),* }),*
1456    }) => {
1457        paste!{
1458            $(
1459                /// each enum value represent a `PlanNode` struct type, help us to dispatch and downcast
1460                #[derive(Copy, Clone, PartialEq, Debug, Hash, Eq, Serialize)]
1461                pub enum [<$convention PlanNodeType>] {
1462                    $( [<$convention $name>] ),*
1463                }
1464            )*
1465            $(
1466                $(impl PlanNodeMeta for [<$convention $name>] {
1467                    type Convention = $convention;
1468                    const NODE_TYPE: [<$convention PlanNodeType>] = [<$convention PlanNodeType>]::[<$convention $name>];
1469
1470                    fn plan_base(&self) -> &PlanBase<$convention> {
1471                        &self.base
1472                    }
1473                }
1474
1475                impl Deref for [<$convention $name>] {
1476                    type Target = PlanBase<$convention>;
1477
1478                    fn deref(&self) -> &Self::Target {
1479                        &self.base
1480                    }
1481                })*
1482            )*
1483        }
1484    }
1485}
1486
1487for_each_convention_all_plan_nodes! { impl_plan_node_meta }
1488
1489macro_rules! impl_plan_node {
1490    ($({ $convention:ident, $name:ident }),*) => {
1491        paste!{
1492            $(impl [<$convention PlanNode>] for [<$convention $name>] { })*
1493        }
1494    }
1495}
1496
1497for_all_plan_nodes! { impl_plan_node }
1498
1499/// impl plan node downcast fn for each node.
1500macro_rules! impl_down_cast_fn {
1501    ({
1502        $( $convention:ident, { $( $name:ident ),* }),*
1503    }) => {
1504        paste!{
1505            $(
1506                impl dyn [<$convention PlanNode>] {
1507                    $( pub fn [< as_ $convention:snake _ $name:snake>](&self) -> Option<&[<$convention $name>]> {
1508                        self.downcast_ref::<[<$convention $name>]>()
1509                    } )*
1510                }
1511            )*
1512        }
1513    }
1514}
1515
1516for_each_convention_all_plan_nodes! { impl_down_cast_fn }