risingwave_frontend/optimizer/plan_visitor/
locality_provider_counter.rs

1// Copyright 2025 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::{DefaultBehavior, Merge, StreamPlanVisitor};
16use crate::optimizer::plan_node::{PlanTreeNodeUnary, StreamLocalityProvider, StreamPlanRef};
17use crate::optimizer::plan_visitor::PlanVisitor;
18
19#[derive(Debug, Clone, Default)]
20pub struct LocalityProviderCounter {}
21
22impl LocalityProviderCounter {
23    pub fn count(plan: StreamPlanRef) -> usize {
24        let mut locality_provider_counter = Self::default();
25        locality_provider_counter.visit(plan)
26    }
27}
28
29impl StreamPlanVisitor for LocalityProviderCounter {
30    type Result = usize;
31
32    type DefaultBehavior = impl DefaultBehavior<Self::Result>;
33
34    fn default_behavior() -> Self::DefaultBehavior {
35        Merge(|a: usize, b| a + b)
36    }
37
38    fn visit_stream_locality_provider(
39        &mut self,
40        locality_provider: &StreamLocalityProvider,
41    ) -> Self::Result {
42        1 + self.visit(locality_provider.input())
43    }
44}