risingwave_frontend/optimizer/
optimizer_context.rs

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
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use core::fmt::Formatter;
use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::Arc;

use risingwave_sqlparser::ast::{ExplainFormat, ExplainOptions, ExplainType};

use crate::binder::ShareId;
use crate::expr::{CorrelatedId, SessionTimezone};
use crate::handler::HandlerArgs;
use crate::optimizer::plan_node::PlanNodeId;
use crate::session::SessionImpl;
use crate::utils::{OverwriteOptions, WithOptions};
use crate::PlanRef;

const RESERVED_ID_NUM: u16 = 10000;

type PhantomUnsend = PhantomData<Rc<()>>;

pub struct OptimizerContext {
    session_ctx: Arc<SessionImpl>,
    /// Store plan node id
    next_plan_node_id: RefCell<i32>,
    /// The original SQL string, used for debugging.
    sql: Arc<str>,
    /// Normalized SQL string. See [`HandlerArgs::normalize_sql`].
    normalized_sql: String,
    /// Explain options
    explain_options: ExplainOptions,
    /// Store the trace of optimizer
    optimizer_trace: RefCell<Vec<String>>,
    /// Store the optimized logical plan of optimizer
    logical_explain: RefCell<Option<String>>,
    /// Store correlated id
    next_correlated_id: RefCell<u32>,
    /// Store options or properties from the `with` clause
    with_options: WithOptions,
    /// Store the Session Timezone and whether it was used.
    session_timezone: RefCell<SessionTimezone>,
    /// Store expr display id.
    next_expr_display_id: RefCell<usize>,
    /// Total number of optimization rules have been applied.
    total_rule_applied: RefCell<usize>,
    /// Store the configs can be overwritten in with clause
    /// if not specified, use the value from session variable.
    overwrite_options: OverwriteOptions,
    /// Store the mapping between `share_id` and the corresponding
    /// `PlanRef`, used by rcte's planning. (e.g., in `LogicalCteRef`)
    rcte_cache: RefCell<HashMap<ShareId, PlanRef>>,

    _phantom: PhantomUnsend,
}

pub type OptimizerContextRef = Rc<OptimizerContext>;

impl OptimizerContext {
    /// Create a new [`OptimizerContext`] from the given [`HandlerArgs`], with empty
    /// [`ExplainOptions`].
    pub fn from_handler_args(handler_args: HandlerArgs) -> Self {
        Self::new(handler_args, ExplainOptions::default())
    }

    /// Create a new [`OptimizerContext`] from the given [`HandlerArgs`] and [`ExplainOptions`].
    pub fn new(mut handler_args: HandlerArgs, explain_options: ExplainOptions) -> Self {
        let session_timezone = RefCell::new(SessionTimezone::new(
            handler_args.session.config().timezone().to_owned(),
        ));
        let overwrite_options = OverwriteOptions::new(&mut handler_args);
        Self {
            session_ctx: handler_args.session,
            next_plan_node_id: RefCell::new(RESERVED_ID_NUM.into()),
            sql: handler_args.sql,
            normalized_sql: handler_args.normalized_sql,
            explain_options,
            optimizer_trace: RefCell::new(vec![]),
            logical_explain: RefCell::new(None),
            next_correlated_id: RefCell::new(0),
            with_options: handler_args.with_options,
            session_timezone,
            next_expr_display_id: RefCell::new(RESERVED_ID_NUM.into()),
            total_rule_applied: RefCell::new(0),
            overwrite_options,
            rcte_cache: RefCell::new(HashMap::new()),
            _phantom: Default::default(),
        }
    }

    // TODO(TaoWu): Remove the async.
    #[cfg(test)]
    #[expect(clippy::unused_async)]
    pub async fn mock() -> OptimizerContextRef {
        Self {
            session_ctx: Arc::new(SessionImpl::mock()),
            next_plan_node_id: RefCell::new(0),
            sql: Arc::from(""),
            normalized_sql: "".to_owned(),
            explain_options: ExplainOptions::default(),
            optimizer_trace: RefCell::new(vec![]),
            logical_explain: RefCell::new(None),
            next_correlated_id: RefCell::new(0),
            with_options: Default::default(),
            session_timezone: RefCell::new(SessionTimezone::new("UTC".into())),
            next_expr_display_id: RefCell::new(0),
            total_rule_applied: RefCell::new(0),
            overwrite_options: OverwriteOptions::default(),
            rcte_cache: RefCell::new(HashMap::new()),
            _phantom: Default::default(),
        }
        .into()
    }

    pub fn next_plan_node_id(&self) -> PlanNodeId {
        *self.next_plan_node_id.borrow_mut() += 1;
        PlanNodeId(*self.next_plan_node_id.borrow())
    }

    pub fn get_plan_node_id(&self) -> i32 {
        *self.next_plan_node_id.borrow()
    }

    pub fn set_plan_node_id(&self, next_plan_node_id: i32) {
        *self.next_plan_node_id.borrow_mut() = next_plan_node_id;
    }

    pub fn next_expr_display_id(&self) -> usize {
        *self.next_expr_display_id.borrow_mut() += 1;
        *self.next_expr_display_id.borrow()
    }

    pub fn get_expr_display_id(&self) -> usize {
        *self.next_expr_display_id.borrow()
    }

    pub fn set_expr_display_id(&self, expr_display_id: usize) {
        *self.next_expr_display_id.borrow_mut() = expr_display_id;
    }

    pub fn next_correlated_id(&self) -> CorrelatedId {
        *self.next_correlated_id.borrow_mut() += 1;
        *self.next_correlated_id.borrow()
    }

    pub fn add_rule_applied(&self, num: usize) {
        *self.total_rule_applied.borrow_mut() += num;
    }

    pub fn total_rule_applied(&self) -> usize {
        *self.total_rule_applied.borrow()
    }

    pub fn is_explain_verbose(&self) -> bool {
        self.explain_options.verbose
    }

    pub fn is_explain_trace(&self) -> bool {
        self.explain_options.trace
    }

    pub fn explain_type(&self) -> ExplainType {
        self.explain_options.explain_type.clone()
    }

    pub fn explain_format(&self) -> ExplainFormat {
        self.explain_options.explain_format.clone()
    }

    pub fn is_explain_logical(&self) -> bool {
        self.explain_type() == ExplainType::Logical
    }

    pub fn trace(&self, str: impl Into<String>) {
        // If explain type is logical, do not store the trace for any optimizations beyond logical.
        if self.is_explain_logical() && self.logical_explain.borrow().is_some() {
            return;
        }
        let mut optimizer_trace = self.optimizer_trace.borrow_mut();
        let string = str.into();
        tracing::trace!(target: "explain_trace", "{}", string);
        optimizer_trace.push(string);
        optimizer_trace.push("\n".to_string());
    }

    pub fn warn_to_user(&self, str: impl Into<String>) {
        self.session_ctx().notice_to_user(str);
    }

    pub fn store_logical(&self, str: impl Into<String>) {
        *self.logical_explain.borrow_mut() = Some(str.into())
    }

    pub fn take_logical(&self) -> Option<String> {
        self.logical_explain.borrow_mut().take()
    }

    pub fn take_trace(&self) -> Vec<String> {
        self.optimizer_trace.borrow_mut().drain(..).collect()
    }

    pub fn with_options(&self) -> &WithOptions {
        &self.with_options
    }

    pub fn overwrite_options(&self) -> &OverwriteOptions {
        &self.overwrite_options
    }

    pub fn session_ctx(&self) -> &Arc<SessionImpl> {
        &self.session_ctx
    }

    /// Return the original SQL.
    pub fn sql(&self) -> &str {
        &self.sql
    }

    /// Return the normalized SQL.
    pub fn normalized_sql(&self) -> &str {
        &self.normalized_sql
    }

    pub fn session_timezone(&self) -> RefMut<'_, SessionTimezone> {
        self.session_timezone.borrow_mut()
    }

    pub fn get_session_timezone(&self) -> String {
        self.session_timezone.borrow().timezone()
    }

    pub fn get_rcte_cache_plan(&self, id: &ShareId) -> Option<PlanRef> {
        self.rcte_cache.borrow().get(id).cloned()
    }

    pub fn insert_rcte_cache_plan(&self, id: ShareId, plan: PlanRef) {
        self.rcte_cache.borrow_mut().insert(id, plan);
    }
}

impl std::fmt::Debug for OptimizerContext {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "QueryContext {{ next_plan_node_id = {}, sql = {}, explain_options = {}, next_correlated_id = {}, with_options = {:?} }}",
            self.next_plan_node_id.borrow(),
            self.sql,
            self.explain_options,
            self.next_correlated_id.borrow(),
            &self.with_options
        )
    }
}