risingwave_expr/expr_context.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 std::future::Future;
16
17use risingwave_common::id::FragmentId;
18use risingwave_expr::{Result as ExprResult, define_context};
19use risingwave_pb::plan_common::ExprContext;
20
21// For all execution mode.
22define_context! {
23 pub TIME_ZONE: String,
24 pub FRAGMENT_ID: FragmentId,
25 pub VNODE_COUNT: usize,
26 pub STRICT_MODE: bool,
27}
28
29pub fn capture_expr_context() -> ExprResult<ExprContext> {
30 let time_zone = TIME_ZONE::try_with(ToOwned::to_owned)?;
31 let strict_mode = STRICT_MODE::try_with(|v| *v)?;
32 Ok(ExprContext {
33 time_zone,
34 strict_mode,
35 })
36}
37
38/// Get the vnode count from the context.
39///
40/// Always returns `Ok` in streaming mode and `Err` in batch mode.
41pub fn vnode_count() -> ExprResult<usize> {
42 VNODE_COUNT::try_with(|&x| x)
43}
44
45/// Get the strict mode from expr context
46///
47/// The return value depends on session variable. Default is true for batch query.
48///
49/// Conceptually, streaming always use non-strict mode. Our implementation doesn't read this value,
50/// although it's set to false as a placeholder.
51pub fn strict_mode() -> ExprResult<bool> {
52 STRICT_MODE::try_with(|&v| v)
53}
54
55pub async fn expr_context_scope<Fut>(expr_context: ExprContext, future: Fut) -> Fut::Output
56where
57 Fut: Future,
58{
59 TIME_ZONE::scope(
60 expr_context.time_zone.clone(),
61 STRICT_MODE::scope(expr_context.strict_mode, future),
62 )
63 .await
64}