risingwave_frontend/optimizer/rule/
project_top_n_transpose_rule.rs

1// Copyright 2026 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
15use super::prelude::{PlanRef, *};
16use crate::optimizer::plan_node::{LogicalProject, LogicalTopN, PlanTreeNodeUnary};
17use crate::utils::ColIndexMappingRewriteExt;
18
19/// Transpose `LogicalProject` and `LogicalTopN` when the project is a pure projection and
20/// preserves all columns required by TopN order and group keys.
21pub struct ProjectTopNTransposeRule {}
22
23impl Rule<Logical> for ProjectTopNTransposeRule {
24    fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
25        let project: &LogicalProject = plan.as_logical_project()?;
26        let top_n: LogicalTopN = project.input().as_logical_top_n()?.to_owned();
27        project.try_as_projection()?;
28
29        let mapping = project.i2o_col_mapping();
30        let new_order = mapping.rewrite_required_order(top_n.topn_order())?;
31        let new_group_key = mapping.rewrite_dist_key(top_n.group_key())?;
32
33        let limit_attr = top_n.limit_attr();
34        let new_project = project.clone_with_input(top_n.input());
35        let new_top_n = LogicalTopN::new(
36            new_project.into(),
37            limit_attr.limit(),
38            top_n.offset(),
39            limit_attr.with_ties(),
40            new_order,
41            new_group_key,
42        );
43        Some(new_top_n.into())
44    }
45}
46
47impl ProjectTopNTransposeRule {
48    pub fn create() -> BoxedRule {
49        Box::new(ProjectTopNTransposeRule {})
50    }
51}