Skip to main content

risingwave_frontend/optimizer/rule/
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//! Define all [`Rule`]
16
17use std::convert::Infallible;
18use std::ops::FromResidual;
19
20use thiserror_ext::AsReport;
21
22use super::PlanRef;
23use crate::error::RwError;
24
25/// Result when applying a [`Rule`] to a `PlanNode`
26pub enum ApplyResult<T> {
27    /// Successfully applied the rule and returned a new plan.
28    Ok(T),
29    /// The current rule is not applicable to the input.
30    /// The optimizer may try another rule.
31    NotApplicable,
32    /// An unrecoverable error occurred while applying the rule.
33    /// The optimizer should stop applying other rules and report the error to the user.
34    Err(RwError),
35}
36
37impl<T> ApplyResult<T> {
38    /// Unwrap the result, panicking if it's not `Ok`.
39    pub fn unwrap(self) -> T {
40        match self {
41            ApplyResult::Ok(plan) => plan,
42            ApplyResult::NotApplicable => panic!("unwrap ApplyResult::NotApplicable"),
43            ApplyResult::Err(e) => panic!("unwrap ApplyResult::Err, error: {:?}", e.as_report()),
44        }
45    }
46}
47
48/// Allow calling `?` on an `Option` in a function returning `ApplyResult`.
49impl<T> FromResidual<Option<Infallible>> for ApplyResult<T> {
50    fn from_residual(residual: Option<Infallible>) -> Self {
51        match residual {
52            Some(i) => match i {},
53            None => Self::NotApplicable,
54        }
55    }
56}
57
58/// Allow calling `?` on a `Result` in a function returning `ApplyResult`.
59impl<T, E> FromResidual<Result<Infallible, E>> for ApplyResult<T>
60where
61    E: Into<RwError>,
62{
63    fn from_residual(residual: Result<Infallible, E>) -> Self {
64        match residual {
65            Ok(i) => match i {},
66            Err(e) => Self::Err(e.into()),
67        }
68    }
69}
70
71/// An one-to-one transform for the `PlanNode`.
72///
73/// It's a convenient trait to implement [`FallibleRule`], thus made available only within this module.
74trait InfallibleRule<C: ConventionMarker>: Send + Sync + Description {
75    /// Apply the rule to the plan node.
76    ///
77    /// - Returns `Some` if the apply is successful.
78    /// - Returns `None` if it's not applicable. The optimizer may try other rules.
79    fn apply(&self, plan: PlanRef<C>) -> Option<PlanRef<C>>;
80}
81
82use InfallibleRule as Rule;
83
84/// An one-to-one transform for the `PlanNode` that may return an
85/// unrecoverable error that stops further optimization.
86///
87/// An [`InfallibleRule`] is always a [`FallibleRule`].
88pub trait FallibleRule<C: ConventionMarker>: Send + Sync + Description {
89    /// Apply the rule to the plan node, which may return an unrecoverable error.
90    ///
91    /// - Returns `ApplyResult::Ok` if the apply is successful.
92    /// - Returns `ApplyResult::NotApplicable` if it's not applicable. The optimizer may try other rules.
93    /// - Returns `ApplyResult::Err` if an unrecoverable error occurred. The optimizer should stop applying
94    ///   other rules and report the error to the user.
95    fn apply(&self, plan: PlanRef<C>) -> ApplyResult<PlanRef<C>>;
96}
97
98impl<C: ConventionMarker, R> FallibleRule<C> for R
99where
100    R: InfallibleRule<C>,
101{
102    fn apply(&self, plan: PlanRef<C>) -> ApplyResult<PlanRef<C>> {
103        match InfallibleRule::apply(self, plan) {
104            Some(plan) => ApplyResult::Ok(plan),
105            None => ApplyResult::NotApplicable,
106        }
107    }
108}
109
110pub trait Description {
111    fn description(&self) -> &str;
112}
113
114pub(super) type BoxedRule<C> = Box<dyn FallibleRule<C>>;
115
116mod correlated_expr_rewriter;
117mod logical_filter_expression_simplify_rule;
118pub use logical_filter_expression_simplify_rule::*;
119mod mv_selection_rule;
120pub use mv_selection_rule::*;
121mod over_window_merge_rule;
122pub use over_window_merge_rule::*;
123mod project_join_merge_rule;
124pub use project_join_merge_rule::*;
125mod project_eliminate_rule;
126pub use project_eliminate_rule::*;
127mod project_merge_rule;
128pub use project_merge_rule::*;
129mod project_top_n_transpose_rule;
130pub use project_top_n_transpose_rule::*;
131mod top_n_project_transpose_rule;
132pub use top_n_project_transpose_rule::*;
133mod pull_up_correlated_predicate_rule;
134pub use pull_up_correlated_predicate_rule::*;
135mod pull_up_correlated_project_value_rule;
136pub use pull_up_correlated_project_value_rule::*;
137mod index_delta_join_rule;
138pub use index_delta_join_rule::*;
139mod left_deep_tree_join_ordering_rule;
140pub use left_deep_tree_join_ordering_rule::*;
141mod apply_agg_transpose_rule;
142pub use apply_agg_transpose_rule::*;
143mod apply_filter_transpose_rule;
144pub use apply_filter_transpose_rule::*;
145mod apply_project_transpose_rule;
146pub use apply_project_transpose_rule::*;
147mod apply_eliminate_rule;
148pub use apply_eliminate_rule::*;
149mod translate_apply_rule;
150pub use translate_apply_rule::*;
151mod merge_multijoin_rule;
152pub use merge_multijoin_rule::*;
153mod max_one_row_eliminate_rule;
154pub use max_one_row_eliminate_rule::*;
155mod apply_join_transpose_rule;
156pub use apply_join_transpose_rule::*;
157mod apply_to_join_rule;
158pub use apply_to_join_rule::*;
159mod distinct_agg_rule;
160pub use distinct_agg_rule::*;
161mod index_selection_rule;
162pub use index_selection_rule::*;
163mod streaming_index_selection_rule;
164pub use streaming_index_selection_rule::StreamingIndexSelectionRule;
165mod push_calculation_of_join_rule;
166pub use push_calculation_of_join_rule::*;
167mod join_commute_rule;
168mod over_window_to_agg_and_join_rule;
169pub use over_window_to_agg_and_join_rule::*;
170mod over_window_split_rule;
171pub use over_window_split_rule::*;
172mod over_window_to_topn_rule;
173pub use join_commute_rule::*;
174pub use over_window_to_topn_rule::*;
175mod union_to_distinct_rule;
176pub use union_to_distinct_rule::*;
177mod agg_project_merge_rule;
178pub use agg_project_merge_rule::*;
179mod union_merge_rule;
180pub use union_merge_rule::*;
181mod dag_to_tree_rule;
182pub use dag_to_tree_rule::*;
183mod apply_share_eliminate_rule;
184pub use apply_share_eliminate_rule::*;
185mod top_n_on_index_rule;
186pub use top_n_on_index_rule::*;
187mod stream;
188pub use stream::bushy_tree_join_ordering_rule::*;
189pub use stream::filter_with_now_to_join_rule::*;
190pub use stream::generate_series_with_now_rule::*;
191pub use stream::separate_consecutive_join_rule::*;
192pub use stream::split_now_and_rule::*;
193pub use stream::split_now_or_rule::*;
194pub use stream::stream_project_merge_rule::*;
195pub use stream::stream_project_split_rule::*;
196mod trivial_project_to_values_rule;
197pub use trivial_project_to_values_rule::*;
198mod union_input_values_merge_rule;
199pub use union_input_values_merge_rule::*;
200mod rewrite_like_expr_rule;
201pub use rewrite_like_expr_rule::*;
202mod min_max_on_index_rule;
203pub use min_max_on_index_rule::*;
204mod always_false_filter_rule;
205pub use always_false_filter_rule::*;
206mod join_project_transpose_rule;
207pub use join_project_transpose_rule::*;
208mod limit_push_down_rule;
209pub use limit_push_down_rule::*;
210mod pull_up_hop_rule;
211pub use pull_up_hop_rule::*;
212mod apply_offset_rewriter;
213use apply_offset_rewriter::ApplyOffsetRewriter;
214mod intersect_to_semi_join_rule;
215pub use intersect_to_semi_join_rule::*;
216mod except_to_anti_join_rule;
217pub use except_to_anti_join_rule::*;
218mod intersect_merge_rule;
219pub use intersect_merge_rule::*;
220mod except_merge_rule;
221pub use except_merge_rule::*;
222mod apply_union_transpose_rule;
223pub use apply_union_transpose_rule::*;
224mod apply_dedup_transpose_rule;
225pub use apply_dedup_transpose_rule::*;
226mod project_join_separate_rule;
227pub use project_join_separate_rule::*;
228mod grouping_sets_to_expand_rule;
229pub use grouping_sets_to_expand_rule::*;
230mod apply_project_set_transpose_rule;
231pub use apply_project_set_transpose_rule::*;
232mod apply_table_function_to_project_set_rule;
233pub use apply_table_function_to_project_set_rule::*;
234mod cross_join_eliminate_rule;
235pub use cross_join_eliminate_rule::*;
236mod table_function_to_project_set_rule;
237
238pub use table_function_to_project_set_rule::*;
239mod apply_topn_transpose_rule;
240pub use apply_topn_transpose_rule::*;
241mod apply_limit_transpose_rule;
242pub use apply_limit_transpose_rule::*;
243mod batch;
244pub use batch::batch_project_merge_rule::*;
245mod common_sub_expr_extract_rule;
246pub use common_sub_expr_extract_rule::*;
247mod apply_over_window_transpose_rule;
248pub use apply_over_window_transpose_rule::*;
249mod apply_expand_transpose_rule;
250pub use apply_expand_transpose_rule::*;
251mod expand_to_project_rule;
252pub use expand_to_project_rule::*;
253mod agg_group_by_simplify_rule;
254pub use agg_group_by_simplify_rule::*;
255mod apply_hop_window_transpose_rule;
256pub use apply_hop_window_transpose_rule::*;
257mod agg_call_merge_rule;
258pub use agg_call_merge_rule::*;
259mod unify_first_last_value_rule;
260pub use unify_first_last_value_rule::*;
261mod empty_agg_remove_rule;
262pub use empty_agg_remove_rule::*;
263mod add_logstore_rule;
264mod correlated_topn_to_vector_search;
265mod iceberg_count_star_rule;
266mod iceberg_engine_storage_selection_rule;
267mod iceberg_intermediate_scan_rule;
268mod pull_up_correlated_predicate_agg_rule;
269mod source_to_kafka_scan_rule;
270mod table_function_to_file_scan_rule;
271mod table_function_to_internal_backfill_progress;
272mod table_function_to_internal_get_channel_delta_stats;
273mod table_function_to_internal_source_backfill_progress;
274mod table_function_to_mssql_query_rule;
275mod table_function_to_mysql_query_rule;
276mod table_function_to_postgres_query_rule;
277mod top_n_to_vector_search_rule;
278mod values_extract_project_rule;
279pub use add_logstore_rule::*;
280pub use batch::batch_push_limit_to_scan_rule::*;
281pub use correlated_topn_to_vector_search::*;
282pub use iceberg_count_star_rule::IcebergCountStarRule;
283pub use iceberg_engine_storage_selection_rule::*;
284pub use iceberg_intermediate_scan_rule::*;
285pub use pull_up_correlated_predicate_agg_rule::*;
286pub use source_to_kafka_scan_rule::*;
287pub use table_function_to_file_scan_rule::*;
288pub use table_function_to_internal_backfill_progress::*;
289pub use table_function_to_internal_get_channel_delta_stats::*;
290pub use table_function_to_internal_source_backfill_progress::*;
291pub use table_function_to_mssql_query_rule::*;
292pub use table_function_to_mysql_query_rule::*;
293pub use table_function_to_postgres_query_rule::*;
294pub use top_n_to_vector_search_rule::*;
295pub use values_extract_project_rule::*;
296
297use crate::optimizer::plan_node::ConventionMarker;
298
299#[macro_export]
300macro_rules! for_all_rules {
301    ($macro:ident) => {
302        $macro! {
303              { ApplyAggTransposeRule }
304            , { ApplyFilterTransposeRule }
305            , { ApplyProjectTransposeRule }
306            , { ApplyProjectSetTransposeRule }
307            , { ApplyTableFunctionToProjectSetRule }
308            , { ApplyEliminateRule }
309            , { ApplyJoinTransposeRule }
310            , { ApplyShareEliminateRule }
311            , { ApplyToJoinRule }
312            , { MaxOneRowEliminateRule }
313            , { DistinctAggRule }
314            , { IndexDeltaJoinRule }
315            , { MergeMultiJoinRule }
316            , { ProjectEliminateRule }
317            , { ProjectJoinMergeRule }
318            , { ProjectMergeRule }
319            , { PullUpCorrelatedPredicateRule }
320            , { PullUpCorrelatedProjectValueRule }
321            , { LeftDeepTreeJoinOrderingRule }
322            , { TranslateApplyRule }
323            , { PushCalculationOfJoinRule }
324            , { IndexSelectionRule }
325            , { OverWindowToTopNRule }
326            , { OverWindowToAggAndJoinRule }
327            , { OverWindowSplitRule }
328            , { OverWindowMergeRule }
329            , { JoinCommuteRule }
330            , { UnionToDistinctRule }
331            , { AggProjectMergeRule }
332            , { UnionMergeRule }
333            , { DagToTreeRule }
334            , { SplitNowAndRule }
335            , { SplitNowOrRule }
336            , { FilterWithNowToJoinRule }
337            , { GenerateSeriesWithNowRule }
338            , { ProjectTopNTransposeRule }
339            , { TopNProjectTransposeRule }
340            , { TopNOnIndexRule }
341            , { TrivialProjectToValuesRule }
342            , { UnionInputValuesMergeRule }
343            , { RewriteLikeExprRule }
344            , { MinMaxOnIndexRule }
345            , { AlwaysFalseFilterRule }
346            , { BushyTreeJoinOrderingRule }
347            , { StreamProjectMergeRule }
348            , { StreamProjectSplitRule }
349            , { SeparateConsecutiveJoinRule }
350            , { LogicalFilterExpressionSimplifyRule }
351            , { JoinProjectTransposeRule }
352            , { LimitPushDownRule }
353            , { PullUpHopRule }
354            , { IntersectToSemiJoinRule }
355            , { ExceptToAntiJoinRule }
356            , { IntersectMergeRule }
357            , { ExceptMergeRule }
358            , { ApplyUnionTransposeRule }
359            , { ApplyDedupTransposeRule }
360            , { ProjectJoinSeparateRule }
361            , { GroupingSetsToExpandRule }
362            , { CrossJoinEliminateRule }
363            , { ApplyTopNTransposeRule }
364            , { TableFunctionToProjectSetRule }
365            , { TableFunctionToFileScanRule }
366            , { TableFunctionToPostgresQueryRule }
367            , { TableFunctionToMySqlQueryRule }
368            , { TableFunctionToMssqlQueryRule }
369            , { TableFunctionToInternalBackfillProgressRule }
370            , { TableFunctionToInternalGetChannelDeltaStatsRule }
371            , { TableFunctionToInternalSourceBackfillProgressRule }
372            , { ApplyLimitTransposeRule }
373            , { CommonSubExprExtractRule }
374            , { BatchProjectMergeRule }
375            , { ApplyOverWindowTransposeRule }
376            , { ApplyExpandTransposeRule }
377            , { ExpandToProjectRule }
378            , { AggGroupBySimplifyRule }
379            , { ApplyHopWindowTransposeRule }
380            , { AggCallMergeRule }
381            , { UnifyFirstLastValueRule }
382            , { ValuesExtractProjectRule }
383            , { BatchPushLimitToScanRule }
384            , { PullUpCorrelatedPredicateAggRule }
385            , { SourceToKafkaScanRule }
386            , { IcebergEngineStorageSelectionRule }
387            , { IcebergCountStarRule}
388            , { IcebergIntermediateScanRule }
389            , { AddLogstoreRule }
390            , { EmptyAggRemoveRule }
391            , { TopNToVectorSearchRule }
392            , { CorrelatedTopNToVectorSearchRule }
393            , { MvSelectionRule }
394        }
395    };
396}
397
398macro_rules! impl_description {
399    ($( { $name:ident }),*) => {
400        paste::paste!{
401            $(impl Description for [<$name>] {
402                fn description(&self) -> &str {
403                    stringify!([<$name>])
404                }
405            })*
406        }
407    }
408}
409
410for_all_rules! {impl_description}
411
412mod prelude {
413    pub(super) use crate::optimizer::plan_node::{Logical, LogicalPlanRef as PlanRef};
414    pub(super) use crate::optimizer::rule::Rule;
415
416    pub(super) type BoxedRule = crate::optimizer::rule::BoxedRule<Logical>;
417}