Skip to main content

risingwave_frontend/expr/
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
15use enum_as_inner::EnumAsInner;
16use fixedbitset::FixedBitSet;
17use futures::FutureExt;
18use paste::paste;
19use risingwave_common::array::ListValue;
20use risingwave_common::types::{
21    DataType, Datum, JsonbVal, MapType, Scalar, ScalarImpl, ToOwnedDatum,
22};
23use risingwave_expr::aggregate::PbAggKind;
24use risingwave_expr::expr::build_from_prost;
25use risingwave_pb::expr::expr_node::RexNode;
26use risingwave_pb::expr::{ExprNode, ProjectSetSelectItem};
27use user_defined_function::UserDefinedFunctionDisplay;
28
29use crate::error::{ErrorCode, Result as RwResult};
30use crate::session::current;
31
32mod agg_call;
33mod asynchronous;
34mod correlated_input_ref;
35mod function_call;
36mod function_call_with_lambda;
37mod input_ref;
38mod literal;
39mod now;
40mod parameter;
41mod pure;
42mod subquery;
43mod table_function;
44mod user_defined_function;
45mod window_function;
46
47mod order_by_expr;
48pub use order_by_expr::{OrderBy, OrderByExpr};
49
50mod expr_mutator;
51mod expr_rewriter;
52mod expr_visitor;
53pub mod function_impl;
54mod secret_ref;
55mod session_timezone;
56mod type_inference;
57mod utils;
58
59pub use agg_call::AggCall;
60pub use correlated_input_ref::{CorrelatedId, CorrelatedInputRef, InputRefScopeRewriter, ScopeId};
61pub use expr_mutator::ExprMutator;
62pub use expr_rewriter::{ExprRewriter, default_rewrite_expr};
63pub use expr_visitor::{ExprVisitor, default_visit_expr};
64pub use function_call::{FunctionCall, FunctionCallDisplay, is_row_function};
65pub use function_call_with_lambda::FunctionCallWithLambda;
66pub use input_ref::{InputRef, InputRefDisplay, input_ref_to_column_indices};
67pub use literal::Literal;
68pub use now::{InlineNowProcTime, Now, NowProcTimeFinder};
69pub use parameter::Parameter;
70pub use pure::*;
71pub use risingwave_pb::expr::expr_node::Type as ExprType;
72pub use secret_ref::SecretRef;
73pub use session_timezone::{SessionTimezone, TimestamptzExprFinder};
74pub use subquery::{Subquery, SubqueryKind};
75pub(crate) use table_function::{MSSQL_QUERY_ARG_COUNT, expr_impl_to_string_fn};
76pub use table_function::{TableFunction, TableFunctionType};
77pub use type_inference::*;
78pub use user_defined_function::UserDefinedFunction;
79pub use utils::*;
80pub use window_function::WindowFunction;
81
82pub(crate) const EXPR_DEPTH_THRESHOLD: usize = 30;
83pub(crate) const EXPR_TOO_DEEP_NOTICE: &str = "Some expression is too complicated. \
84Consider simplifying or splitting the query if you encounter any issues.";
85
86pub(crate) fn reject_impure(expr: impl Into<ExprImpl>, context: &str) -> RwResult<()> {
87    if let Some(impure_expr_desc) = impure_expr_desc(&expr.into()) {
88        let msg = format!(
89            "using an impure expression ({impure_expr_desc}) in {context} \
90             on a retract stream may lead to inconsistent results"
91        );
92        if current::config()
93            .is_some_and(|c| c.read().streaming_unsafe_allow_unmaterialized_impure_expr())
94        {
95            current::notice_to_user(msg);
96        } else {
97            return Err(ErrorCode::NotSupported(
98                msg,
99                "rewrite the query to extract the impure expression into the select list, \
100                 or set `streaming_unsafe_allow_unmaterialized_impure_expr` to allow \
101                 the behavior at the risk of inconsistent results or panics during execution"
102                    .into(),
103            )
104            .into());
105        }
106    }
107    Ok(())
108}
109
110/// the trait of bound expressions
111pub trait Expr: Into<ExprImpl> {
112    /// Get the return type of the expr
113    fn return_type(&self) -> DataType;
114
115    /// Try to serialize the expression, returning an error if it's impossible.
116    fn try_to_expr_proto(&self) -> Result<ExprNode, String>;
117
118    /// Serialize the expression. Panic if it's impossible.
119    fn to_expr_proto(&self) -> ExprNode {
120        self.try_to_expr_proto()
121            .expect("failed to serialize expression to protobuf")
122    }
123
124    /// Serialize the expression. Returns an error if this will result in an impure expression on a
125    /// retract stream, which may lead to inconsistent results.
126    fn to_expr_proto_checked_pure(
127        &self,
128        retract: bool,
129        context: &str,
130    ) -> crate::error::Result<ExprNode>
131    where
132        Self: Clone,
133    {
134        if retract {
135            reject_impure(self.clone(), context)?;
136        }
137        self.try_to_expr_proto()
138            .map_err(|e| ErrorCode::InternalError(e).into())
139    }
140}
141
142macro_rules! impl_expr_impl {
143    ($($t:ident,)*) => {
144        #[derive(Clone, Eq, PartialEq, Hash, EnumAsInner)]
145        pub enum ExprImpl {
146            $($t(Box<$t>),)*
147        }
148
149        impl ExprImpl {
150            pub fn variant_name(&self) -> &'static str {
151                match self {
152                    $(ExprImpl::$t(_) => stringify!($t),)*
153                }
154            }
155        }
156
157        $(
158        impl From<$t> for ExprImpl {
159            fn from(o: $t) -> ExprImpl {
160                ExprImpl::$t(Box::new(o))
161            }
162        })*
163
164        impl Expr for ExprImpl {
165            fn return_type(&self) -> DataType {
166                match self {
167                    $(ExprImpl::$t(expr) => expr.return_type(),)*
168                }
169            }
170
171            fn try_to_expr_proto(&self) -> Result<ExprNode, String> {
172                match self {
173                    $(ExprImpl::$t(expr) => expr.try_to_expr_proto(),)*
174                }
175            }
176        }
177    };
178}
179
180impl_expr_impl!(
181    // BoundColumnRef, might be used in binder.
182    CorrelatedInputRef,
183    InputRef,
184    Literal,
185    FunctionCall,
186    FunctionCallWithLambda,
187    AggCall,
188    Subquery,
189    TableFunction,
190    WindowFunction,
191    UserDefinedFunction,
192    Parameter,
193    Now,
194    SecretRef,
195);
196
197impl ExprImpl {
198    /// A literal int value.
199    #[inline(always)]
200    pub fn literal_int(v: i32) -> Self {
201        Literal::new(Some(v.to_scalar_value()), DataType::Int32).into()
202    }
203
204    /// A literal bigint value
205    #[inline(always)]
206    pub fn literal_bigint(v: i64) -> Self {
207        Literal::new(Some(v.to_scalar_value()), DataType::Int64).into()
208    }
209
210    /// A literal float64 value.
211    #[inline(always)]
212    pub fn literal_f64(v: f64) -> Self {
213        Literal::new(Some(v.into()), DataType::Float64).into()
214    }
215
216    /// A literal boolean value.
217    #[inline(always)]
218    pub fn literal_bool(v: bool) -> Self {
219        Literal::new(Some(v.to_scalar_value()), DataType::Boolean).into()
220    }
221
222    /// A literal varchar value.
223    #[inline(always)]
224    pub fn literal_varchar(v: String) -> Self {
225        Literal::new(Some(v.into()), DataType::Varchar).into()
226    }
227
228    /// A literal null value.
229    #[inline(always)]
230    pub fn literal_null(element_type: DataType) -> Self {
231        Literal::new(None, element_type).into()
232    }
233
234    /// A literal jsonb value.
235    #[inline(always)]
236    pub fn literal_jsonb(v: JsonbVal) -> Self {
237        Literal::new(Some(v.into()), DataType::Jsonb).into()
238    }
239
240    /// A literal list value.
241    #[inline(always)]
242    pub fn literal_list(v: ListValue, element_type: DataType) -> Self {
243        Literal::new(Some(v.to_scalar_value()), DataType::list(element_type)).into()
244    }
245
246    /// Takes the expression, leaving a literal null of the same type in its place.
247    pub fn take(&mut self) -> Self {
248        std::mem::replace(self, Self::literal_null(self.return_type()))
249    }
250
251    /// A `count(*)` aggregate function.
252    #[inline(always)]
253    pub fn count_star() -> Self {
254        AggCall::new(
255            PbAggKind::Count.into(),
256            vec![],
257            false,
258            OrderBy::any(),
259            Condition::true_cond(),
260            vec![],
261        )
262        .unwrap()
263        .into()
264    }
265
266    /// Create a new expression by merging the given expressions by `And`.
267    ///
268    /// If `exprs` is empty, return a literal `true`.
269    pub fn and(exprs: impl IntoIterator<Item = ExprImpl>) -> Self {
270        merge_expr_by_logical(exprs, ExprType::And, ExprImpl::literal_bool(true))
271    }
272
273    /// Create a new expression by merging the given expressions by `Or`.
274    ///
275    /// If `exprs` is empty, return a literal `false`.
276    pub fn or(exprs: impl IntoIterator<Item = ExprImpl>) -> Self {
277        merge_expr_by_logical(exprs, ExprType::Or, ExprImpl::literal_bool(false))
278    }
279
280    /// Collect all `InputRef`s' indexes in the expression.
281    ///
282    /// # Panics
283    /// Panics if `input_ref >= input_col_num`.
284    pub fn collect_input_refs(&self, input_col_num: usize) -> FixedBitSet {
285        collect_input_refs(input_col_num, [self])
286    }
287
288    /// Check if the expression has no side effects and output is deterministic
289    pub fn is_pure(&self) -> bool {
290        is_pure(self)
291    }
292
293    pub fn is_impure(&self) -> bool {
294        is_impure(self)
295    }
296
297    pub fn needs_materialization(&self) -> bool {
298        needs_materialization(self)
299    }
300
301    /// Count `Now`s in the expression.
302    pub fn count_nows(&self) -> usize {
303        let mut visitor = CountNow::default();
304        visitor.visit_expr(self);
305        visitor.count()
306    }
307
308    /// Check whether self is literal NULL.
309    pub fn is_null(&self) -> bool {
310        matches!(self, ExprImpl::Literal(literal) if literal.get_data().is_none())
311    }
312
313    /// Check whether self is a literal NULL or literal string.
314    pub fn is_untyped(&self) -> bool {
315        matches!(self, ExprImpl::Literal(literal) if literal.is_untyped())
316            || matches!(self, ExprImpl::Parameter(parameter) if !parameter.has_infer())
317    }
318
319    /// Shorthand to create cast expr to `target` type in implicit context.
320    pub fn cast_implicit(mut self, target: &DataType) -> Result<ExprImpl, CastError> {
321        FunctionCall::cast_mut(&mut self, target, CastContext::Implicit)?;
322        Ok(self)
323    }
324
325    /// Shorthand to create cast expr to `target` type in assign context.
326    pub fn cast_assign(mut self, target: &DataType) -> Result<ExprImpl, CastError> {
327        FunctionCall::cast_mut(&mut self, target, CastContext::Assign)?;
328        Ok(self)
329    }
330
331    /// Shorthand to create cast expr to `target` type in explicit context.
332    pub fn cast_explicit(mut self, target: &DataType) -> Result<ExprImpl, CastError> {
333        FunctionCall::cast_mut(&mut self, target, CastContext::Explicit)?;
334        Ok(self)
335    }
336
337    /// Shorthand to inplace cast expr to `target` type in implicit context.
338    pub fn cast_implicit_mut(&mut self, target: &DataType) -> Result<(), CastError> {
339        FunctionCall::cast_mut(self, target, CastContext::Implicit)
340    }
341
342    /// Shorthand to inplace cast expr to `target` type in explicit context.
343    pub fn cast_explicit_mut(&mut self, target: &DataType) -> Result<(), CastError> {
344        FunctionCall::cast_mut(self, target, CastContext::Explicit)
345    }
346
347    /// Casting to Regclass type means getting the oid of expr.
348    /// See <https://www.postgresql.org/docs/current/datatype-oid.html>
349    pub fn cast_to_regclass(self) -> Result<ExprImpl, CastError> {
350        match self.return_type() {
351            DataType::Varchar => Ok(ExprImpl::FunctionCall(Box::new(
352                FunctionCall::new_unchecked(ExprType::CastRegclass, vec![self], DataType::Int32),
353            ))),
354            DataType::Int32 => Ok(self),
355            dt if dt.is_int() => Ok(self.cast_explicit(&DataType::Int32)?),
356            _ => bail_cast_error!("unsupported input type"),
357        }
358    }
359
360    /// Shorthand to inplace cast expr to `regclass` type.
361    pub fn cast_to_regclass_mut(&mut self) -> Result<(), CastError> {
362        let owned = std::mem::replace(self, ExprImpl::literal_bool(false));
363        *self = owned.cast_to_regclass()?;
364        Ok(())
365    }
366
367    /// Ensure the return type of this expression is an array of some type.
368    pub fn ensure_array_type(&self) -> Result<(), ErrorCode> {
369        if self.is_untyped() {
370            return Err(ErrorCode::BindError(
371                "could not determine polymorphic type because input has type unknown".into(),
372            ));
373        }
374        match self.return_type() {
375            DataType::List(_) => Ok(()),
376            t => Err(ErrorCode::BindError(format!("expects array but got {t}"))),
377        }
378    }
379
380    /// Ensure the return type of this expression is a map of some type.
381    pub fn try_into_map_type(&self) -> Result<MapType, ErrorCode> {
382        if self.is_untyped() {
383            return Err(ErrorCode::BindError(
384                "could not determine polymorphic type because input has type unknown".into(),
385            ));
386        }
387        match self.return_type() {
388            DataType::Map(m) => Ok(m),
389            t => Err(ErrorCode::BindError(format!("expects map but got {t}"))),
390        }
391    }
392
393    /// Shorthand to enforce implicit cast to boolean
394    pub fn enforce_bool_clause(self, clause: &str) -> RwResult<ExprImpl> {
395        if self.is_untyped() {
396            let inner = self.cast_implicit(&DataType::Boolean)?;
397            return Ok(inner);
398        }
399        let return_type = self.return_type();
400        if return_type != DataType::Boolean {
401            bail!(
402                "argument of {} must be boolean, not type {:?}",
403                clause,
404                return_type
405            )
406        }
407        Ok(self)
408    }
409
410    /// Create "cast" expr to string (`varchar`) type. This is different from a real cast, as
411    /// boolean is converted to a single char rather than full word.
412    ///
413    /// Choose between `cast_output` and `cast_{assign,explicit}(Varchar)` based on `PostgreSQL`'s
414    /// behavior on bools. For example, `concat(':', true)` is `:t` but `':' || true` is `:true`.
415    /// All other types have the same behavior when formatting to output and casting to string.
416    ///
417    /// References in `PostgreSQL`:
418    /// * [cast](https://github.com/postgres/postgres/blob/a3ff08e0b08dbfeb777ccfa8f13ebaa95d064c04/src/include/catalog/pg_cast.dat#L437-L444)
419    /// * [impl](https://github.com/postgres/postgres/blob/27b77ecf9f4d5be211900eda54d8155ada50d696/src/backend/utils/adt/bool.c#L204-L209)
420    pub fn cast_output(self) -> RwResult<ExprImpl> {
421        if self.return_type() == DataType::Boolean {
422            return Ok(FunctionCall::new(ExprType::BoolOut, vec![self])?.into());
423        }
424        // Use normal cast for other types. Both `assign` and `explicit` can pass the castability
425        // check and there is no difference.
426        self.cast_assign(&DataType::Varchar)
427            .map_err(|err| err.into())
428    }
429
430    /// Evaluate the expression on the given input.
431    ///
432    /// TODO: This is a naive implementation. We should avoid proto ser/de.
433    /// Tracking issue: <https://github.com/risingwavelabs/risingwave/issues/3479>
434    pub async fn eval_row(&self, input: &OwnedRow) -> RwResult<Datum> {
435        let backend_expr = build_from_prost(&self.to_expr_proto())?;
436        Ok(backend_expr.eval_row(input).await?)
437    }
438
439    /// Try to evaluate an expression if it's a constant expression by `ExprImpl::is_const`.
440    ///
441    /// Returns...
442    /// - `None` if it's not a constant expression,
443    /// - `Some(Ok(_))` if constant evaluation succeeds,
444    /// - `Some(Err(_))` if there's an error while evaluating a constant expression.
445    pub fn try_fold_const(&self) -> Option<RwResult<Datum>> {
446        if self.is_const() {
447            self.eval_row(&OwnedRow::empty())
448                .now_or_never()
449                .expect("constant expression should not be async")
450                .into()
451        } else {
452            None
453        }
454    }
455
456    /// Similar to `ExprImpl::try_fold_const`, but panics if the expression is not constant.
457    pub fn fold_const(&self) -> RwResult<Datum> {
458        self.try_fold_const().expect("expression is not constant")
459    }
460}
461
462/// Implement helper functions which recursively checks whether an variant is included in the
463/// expression. e.g., `has_subquery(&self) -> bool`
464///
465/// It will not traverse inside subqueries.
466macro_rules! impl_has_variant {
467    ( $($variant:ty),* ) => {
468        paste! {
469            impl ExprImpl {
470                $(
471                    pub fn [<has_ $variant:snake>](&self) -> bool {
472                        struct Has { has: bool }
473
474                        impl ExprVisitor for Has {
475                            fn [<visit_ $variant:snake>](&mut self, _: &$variant) {
476                                self.has = true;
477                            }
478                        }
479
480                        let mut visitor = Has { has: false };
481                        visitor.visit_expr(self);
482                        visitor.has
483                    }
484                )*
485            }
486        }
487    };
488}
489
490impl_has_variant! {InputRef, Literal, FunctionCall, FunctionCallWithLambda, AggCall, Subquery, TableFunction, WindowFunction, UserDefinedFunction, Now}
491
492/// Inequality condition between two input columns with clearer semantics.
493/// Represents: `left_col <op> right_col` where op is one of `<`, `<=`, `>`, `>=`.
494#[derive(Debug, Clone, PartialEq, Eq, Hash)]
495pub struct InequalityInputPair {
496    /// Index of the left side column (from left input).
497    pub left_idx: usize,
498    /// Index of the right side column (from right input, NOT offset by `left_cols_num`).
499    pub right_idx: usize,
500    /// Comparison operator: `left_col <op> right_col`.
501    pub op: ExprType,
502}
503
504impl InequalityInputPair {
505    pub fn new(left_idx: usize, right_idx: usize, op: ExprType) -> Self {
506        debug_assert!(matches!(
507            op,
508            ExprType::LessThan
509                | ExprType::LessThanOrEqual
510                | ExprType::GreaterThan
511                | ExprType::GreaterThanOrEqual
512        ));
513        Self {
514            left_idx,
515            right_idx,
516            op,
517        }
518    }
519
520    /// Returns true if the left side has larger values based on the operator.
521    /// For `>` and `>=`, left side is larger.
522    /// State cleanup applies to the side with larger values.
523    pub fn left_side_is_larger(&self) -> bool {
524        matches!(
525            self.op,
526            ExprType::GreaterThan | ExprType::GreaterThanOrEqual
527        )
528    }
529}
530
531impl ExprImpl {
532    /// This function is not meant to be called. In most cases you would want
533    /// [`ExprImpl::has_correlated_input_ref_by_scope`].
534    ///
535    /// When an expr only contains [`CorrelatedInputRef`]s owned by scopes inside it, the whole
536    /// expr is still considered to be uncorrelated, and can be checked with
537    /// [`ExprImpl::has_subquery`] as well.
538    /// See examples on [`crate::binder::BoundQuery::is_correlated_by_scope`] for details.
539    ///
540    /// This is a placeholder to trigger a compiler error when a trivial implementation checking for
541    /// enum variant is generated by accident. It cannot be called either because you cannot pass
542    /// `Infallible` to it.
543    pub fn has_correlated_input_ref(&self, _: std::convert::Infallible) -> bool {
544        unreachable!()
545    }
546
547    /// Used to check whether the expression has [`CorrelatedInputRef`] owned by `scope`.
548    ///
549    /// This is the core logic that supports [`crate::binder::BoundQuery::is_correlated_by_scope`].
550    // We need to traverse inside subqueries.
551    pub fn has_correlated_input_ref_by_scope(&self, scope: ScopeId) -> bool {
552        struct Has {
553            scope: ScopeId,
554            has: bool,
555        }
556
557        impl ExprVisitor for Has {
558            fn visit_correlated_input_ref(&mut self, correlated_input_ref: &CorrelatedInputRef) {
559                if correlated_input_ref.scope() == Some(self.scope) {
560                    self.has = true;
561                }
562            }
563
564            fn visit_subquery(&mut self, subquery: &Subquery) {
565                self.has |= subquery.is_correlated_by_scope(self.scope);
566            }
567        }
568
569        let mut visitor = Has { scope, has: false };
570        visitor.visit_expr(self);
571        visitor.has
572    }
573
574    pub fn has_correlated_input_ref_by_correlated_id(&self, correlated_id: CorrelatedId) -> bool {
575        struct Has {
576            correlated_id: CorrelatedId,
577            has: bool,
578        }
579
580        impl ExprVisitor for Has {
581            fn visit_correlated_input_ref(&mut self, correlated_input_ref: &CorrelatedInputRef) {
582                if correlated_input_ref.correlated_id() == self.correlated_id {
583                    self.has = true;
584                }
585            }
586
587            fn visit_subquery(&mut self, subquery: &Subquery) {
588                self.has |= subquery.is_correlated_by_correlated_id(self.correlated_id);
589            }
590        }
591
592        let mut visitor = Has {
593            correlated_id,
594            has: false,
595        };
596        visitor.visit_expr(self);
597        visitor.has
598    }
599
600    /// Collect `CorrelatedInputRef`s in `ExprImpl` owned by `scope`, return their indices, and
601    /// assign absolute `correlated_id` for them.
602    pub fn collect_correlated_indices_by_scope_and_assign_id(
603        &mut self,
604        scope: ScopeId,
605        correlated_id: CorrelatedId,
606    ) -> Vec<usize> {
607        struct Collector {
608            scope: ScopeId,
609            correlated_indices: Vec<usize>,
610            correlated_id: CorrelatedId,
611        }
612
613        impl ExprMutator for Collector {
614            fn visit_correlated_input_ref(
615                &mut self,
616                correlated_input_ref: &mut CorrelatedInputRef,
617            ) {
618                if correlated_input_ref.scope() == Some(self.scope) {
619                    self.correlated_indices.push(correlated_input_ref.index());
620                    correlated_input_ref.set_correlated_id(self.correlated_id);
621                }
622            }
623
624            fn visit_subquery(&mut self, subquery: &mut Subquery) {
625                self.correlated_indices.extend(
626                    subquery.collect_correlated_indices_by_scope_and_assign_id(
627                        self.scope,
628                        self.correlated_id,
629                    ),
630                );
631            }
632        }
633
634        let mut collector = Collector {
635            scope,
636            correlated_indices: vec![],
637            correlated_id,
638        };
639        collector.visit_expr(self);
640        collector.correlated_indices.sort();
641        collector.correlated_indices.dedup();
642        collector.correlated_indices
643    }
644
645    pub fn only_literal_and_func(&self) -> bool {
646        {
647            struct HasOthers {
648                has_others: bool,
649            }
650
651            impl ExprVisitor for HasOthers {
652                fn visit_expr(&mut self, expr: &ExprImpl) {
653                    match expr {
654                        ExprImpl::CorrelatedInputRef(_)
655                        | ExprImpl::InputRef(_)
656                        | ExprImpl::AggCall(_)
657                        | ExprImpl::Subquery(_)
658                        | ExprImpl::TableFunction(_)
659                        | ExprImpl::WindowFunction(_)
660                        | ExprImpl::UserDefinedFunction(_)
661                        | ExprImpl::Parameter(_)
662                        | ExprImpl::Now(_)
663                        | ExprImpl::SecretRef(_) => self.has_others = true,
664                        ExprImpl::Literal(_inner) => {}
665                        ExprImpl::FunctionCall(inner) => {
666                            if !self.is_short_circuit(inner) {
667                                // only if the current `func_call` is *not* a short-circuit
668                                // expression, e.g., true or (...) | false and (...),
669                                // shall we proceed to visit it.
670                                self.visit_function_call(inner)
671                            }
672                        }
673                        ExprImpl::FunctionCallWithLambda(inner) => {
674                            self.visit_function_call_with_lambda(inner)
675                        }
676                    }
677                }
678            }
679
680            impl HasOthers {
681                fn is_short_circuit(&self, func_call: &FunctionCall) -> bool {
682                    /// evaluate the first parameter of `Or` or `And` function call
683                    fn eval_first(e: &ExprImpl, expect: bool) -> bool {
684                        if let ExprImpl::Literal(l) = e {
685                            *l.get_data() == Some(ScalarImpl::Bool(expect))
686                        } else {
687                            false
688                        }
689                    }
690
691                    match func_call.func_type {
692                        ExprType::Or => eval_first(&func_call.inputs()[0], true),
693                        ExprType::And => eval_first(&func_call.inputs()[0], false),
694                        _ => false,
695                    }
696                }
697            }
698
699            let mut visitor = HasOthers { has_others: false };
700            visitor.visit_expr(self);
701            !visitor.has_others
702        }
703    }
704
705    /// Checks whether this is a constant expr that can be evaluated over a dummy chunk.
706    ///
707    /// The expression tree should only consist of literals and **pure** function calls.
708    pub fn is_const(&self) -> bool {
709        self.only_literal_and_func() && self.is_pure()
710    }
711
712    /// Returns the `InputRefs` of an Equality predicate if it matches
713    /// ordered by the canonical ordering (lower, higher), else returns None
714    pub fn as_eq_cond(&self) -> Option<(InputRef, InputRef)> {
715        if let ExprImpl::FunctionCall(function_call) = self
716            && function_call.func_type() == ExprType::Equal
717            && let (_, ExprImpl::InputRef(x), ExprImpl::InputRef(y)) =
718                function_call.clone().decompose_as_binary()
719        {
720            if x.index() < y.index() {
721                Some((*x, *y))
722            } else {
723                Some((*y, *x))
724            }
725        } else {
726            None
727        }
728    }
729
730    pub fn as_is_not_distinct_from_cond(&self) -> Option<(InputRef, InputRef)> {
731        if let ExprImpl::FunctionCall(function_call) = self
732            && function_call.func_type() == ExprType::IsNotDistinctFrom
733            && let (_, ExprImpl::InputRef(x), ExprImpl::InputRef(y)) =
734                function_call.clone().decompose_as_binary()
735        {
736            if x.index() < y.index() {
737                Some((*x, *y))
738            } else {
739                Some((*y, *x))
740            }
741        } else {
742            None
743        }
744    }
745
746    pub fn reverse_comparison(comparison: ExprType) -> ExprType {
747        match comparison {
748            ExprType::LessThan => ExprType::GreaterThan,
749            ExprType::LessThanOrEqual => ExprType::GreaterThanOrEqual,
750            ExprType::GreaterThan => ExprType::LessThan,
751            ExprType::GreaterThanOrEqual => ExprType::LessThanOrEqual,
752            ExprType::Equal | ExprType::IsNotDistinctFrom => comparison,
753            _ => unreachable!(),
754        }
755    }
756
757    pub fn as_comparison_cond(&self) -> Option<(InputRef, ExprType, InputRef)> {
758        if let ExprImpl::FunctionCall(function_call) = self {
759            match function_call.func_type() {
760                ty @ (ExprType::LessThan
761                | ExprType::LessThanOrEqual
762                | ExprType::GreaterThan
763                | ExprType::GreaterThanOrEqual) => {
764                    let (_, op1, op2) = function_call.clone().decompose_as_binary();
765                    if let (ExprImpl::InputRef(x), ExprImpl::InputRef(y)) = (op1, op2) {
766                        if x.index < y.index {
767                            Some((*x, ty, *y))
768                        } else {
769                            Some((*y, Self::reverse_comparison(ty), *x))
770                        }
771                    } else {
772                        None
773                    }
774                }
775                _ => None,
776            }
777        } else {
778            None
779        }
780    }
781
782    /// Accepts expressions of the form `input_expr cmp now_expr` or `now_expr cmp input_expr`,
783    /// where `input_expr` contains an `InputRef` and contains no `now()`, and `now_expr`
784    /// contains a `now()` but no `InputRef`.
785    ///
786    /// Canonicalizes to the first ordering and returns `(input_expr, cmp, now_expr)`
787    pub fn as_now_comparison_cond(&self) -> Option<(ExprImpl, ExprType, ExprImpl)> {
788        if let ExprImpl::FunctionCall(function_call) = self {
789            match function_call.func_type() {
790                ty @ (ExprType::Equal
791                | ExprType::LessThan
792                | ExprType::LessThanOrEqual
793                | ExprType::GreaterThan
794                | ExprType::GreaterThanOrEqual) => {
795                    let (_, op1, op2) = function_call.clone().decompose_as_binary();
796                    if !op1.has_now()
797                        && op1.has_input_ref()
798                        && op2.has_now()
799                        && !op2.has_input_ref()
800                    {
801                        Some((op1, ty, op2))
802                    } else if op1.has_now()
803                        && !op1.has_input_ref()
804                        && !op2.has_now()
805                        && op2.has_input_ref()
806                    {
807                        Some((op2, Self::reverse_comparison(ty), op1))
808                    } else {
809                        None
810                    }
811                }
812                _ => None,
813            }
814        } else {
815            None
816        }
817    }
818
819    /// Returns the `InputRef` and offset of a predicate if it matches
820    /// the form `InputRef [+- const_expr]`, else returns None.
821    ///
822    /// Deprecated: Only used by `as_input_comparison_cond`.
823    #[expect(dead_code)]
824    fn as_input_offset(&self) -> Option<(usize, Option<(ExprType, ExprImpl)>)> {
825        match self {
826            ExprImpl::InputRef(input_ref) => Some((input_ref.index(), None)),
827            ExprImpl::FunctionCall(function_call) => {
828                let expr_type = function_call.func_type();
829                match expr_type {
830                    ExprType::Add | ExprType::Subtract => {
831                        let (_, lhs, rhs) = function_call.clone().decompose_as_binary();
832                        if let ExprImpl::InputRef(input_ref) = &lhs
833                            && rhs.is_const()
834                        {
835                            // Currently we will return `None` for non-literal because the result of the expression might be '1 day'. However, there will definitely exist false positives such as '1 second + 1 second'.
836                            // We will treat the expression as an input offset when rhs is `null`.
837                            if rhs.return_type() == DataType::Interval
838                                && rhs.as_literal().is_none_or(|literal| {
839                                    literal.get_data().as_ref().is_some_and(|scalar| {
840                                        let interval = scalar.as_interval();
841                                        interval.months() != 0 || interval.days() != 0
842                                    })
843                                })
844                            {
845                                None
846                            } else {
847                                Some((input_ref.index(), Some((expr_type, rhs))))
848                            }
849                        } else {
850                            None
851                        }
852                    }
853                    _ => None,
854                }
855            }
856            _ => None,
857        }
858    }
859
860    pub fn as_eq_const(&self) -> Option<(InputRef, ExprImpl)> {
861        if let ExprImpl::FunctionCall(function_call) = self
862            && function_call.func_type() == ExprType::Equal
863        {
864            match function_call.clone().decompose_as_binary() {
865                (_, ExprImpl::InputRef(x), y) if y.is_const() => Some((*x, y)),
866                (_, x, ExprImpl::InputRef(y)) if x.is_const() => Some((*y, x)),
867                _ => None,
868            }
869        } else {
870            None
871        }
872    }
873
874    pub fn as_eq_correlated_input_ref(&self) -> Option<(InputRef, CorrelatedInputRef)> {
875        if let ExprImpl::FunctionCall(function_call) = self
876            && function_call.func_type() == ExprType::Equal
877        {
878            match function_call.clone().decompose_as_binary() {
879                (_, ExprImpl::InputRef(x), ExprImpl::CorrelatedInputRef(y)) => Some((*x, *y)),
880                (_, ExprImpl::CorrelatedInputRef(x), ExprImpl::InputRef(y)) => Some((*y, *x)),
881                _ => None,
882            }
883        } else {
884            None
885        }
886    }
887
888    pub fn as_is_null(&self) -> Option<InputRef> {
889        if let ExprImpl::FunctionCall(function_call) = self
890            && function_call.func_type() == ExprType::IsNull
891        {
892            match function_call.clone().decompose_as_unary() {
893                (_, ExprImpl::InputRef(x)) => Some(*x),
894                _ => None,
895            }
896        } else {
897            None
898        }
899    }
900
901    pub fn as_comparison_const(&self) -> Option<(InputRef, ExprType, ExprImpl)> {
902        fn reverse_comparison(comparison: ExprType) -> ExprType {
903            match comparison {
904                ExprType::LessThan => ExprType::GreaterThan,
905                ExprType::LessThanOrEqual => ExprType::GreaterThanOrEqual,
906                ExprType::GreaterThan => ExprType::LessThan,
907                ExprType::GreaterThanOrEqual => ExprType::LessThanOrEqual,
908                _ => unreachable!(),
909            }
910        }
911
912        if let ExprImpl::FunctionCall(function_call) = self {
913            match function_call.func_type() {
914                ty @ (ExprType::LessThan
915                | ExprType::LessThanOrEqual
916                | ExprType::GreaterThan
917                | ExprType::GreaterThanOrEqual) => {
918                    let (_, op1, op2) = function_call.clone().decompose_as_binary();
919                    match (op1, op2) {
920                        (ExprImpl::InputRef(x), y) if y.is_const() => Some((*x, ty, y)),
921                        (x, ExprImpl::InputRef(y)) if x.is_const() => {
922                            Some((*y, reverse_comparison(ty), x))
923                        }
924                        _ => None,
925                    }
926                }
927                _ => None,
928            }
929        } else {
930            None
931        }
932    }
933
934    pub fn as_in_const_list(&self) -> Option<(InputRef, Vec<ExprImpl>)> {
935        if let ExprImpl::FunctionCall(function_call) = self
936            && function_call.func_type() == ExprType::In
937        {
938            let (input, list) = function_call.inputs().split_first()?;
939            let input_ref = match input {
940                ExprImpl::InputRef(i) => i.as_ref().clone(),
941                _ => return None,
942            };
943            if !list.iter().all(ExprImpl::is_const) {
944                return None;
945            }
946
947            Some((input_ref, list.to_vec()))
948        } else {
949            None
950        }
951    }
952
953    pub fn as_some_eq_const_list(&self) -> Option<(InputRef, Vec<ExprImpl>)> {
954        if let ExprImpl::FunctionCall(function_call) = self
955            && function_call.func_type() == ExprType::Some
956        {
957            let (_, inner) = function_call.clone().decompose_as_unary();
958            let ExprImpl::FunctionCall(inner_call) = inner else {
959                return None;
960            };
961            if inner_call.func_type() != ExprType::Equal {
962                return None;
963            }
964
965            let (_, left, right) = inner_call.decompose_as_binary();
966            let (input_ref, list_expr) = match (left, right) {
967                (ExprImpl::InputRef(input_ref), list_expr) if list_expr.is_const() => {
968                    (input_ref.as_ref().clone(), list_expr)
969                }
970                (list_expr, ExprImpl::InputRef(input_ref)) if list_expr.is_const() => {
971                    (input_ref.as_ref().clone(), list_expr)
972                }
973                _ => return None,
974            };
975
976            let literal = list_expr.as_literal()?;
977            let DataType::List(list_type) = literal.return_type() else {
978                return None;
979            };
980            let list = literal
981                .get_data()
982                .as_ref()?
983                .as_list()
984                .iter()
985                .map(|datum| {
986                    ExprImpl::Literal(Box::new(Literal::new(
987                        datum.to_owned_datum(),
988                        list_type.elem().clone(),
989                    )))
990                })
991                .collect();
992
993            Some((input_ref, list))
994        } else {
995            None
996        }
997    }
998
999    pub fn as_or_disjunctions(&self) -> Option<Vec<ExprImpl>> {
1000        if let ExprImpl::FunctionCall(function_call) = self
1001            && function_call.func_type() == ExprType::Or
1002        {
1003            Some(to_disjunctions(self.clone()))
1004        } else {
1005            None
1006        }
1007    }
1008
1009    pub fn to_project_set_select_item_proto(&self) -> ProjectSetSelectItem {
1010        use risingwave_pb::expr::project_set_select_item::SelectItem::*;
1011
1012        ProjectSetSelectItem {
1013            select_item: Some(match self {
1014                ExprImpl::TableFunction(tf) => TableFunction(tf.to_protobuf()),
1015                expr => Expr(expr.to_expr_proto()),
1016            }),
1017        }
1018    }
1019
1020    /// Serialize the expression. Returns an error if this will result in an impure expression on a
1021    /// retract stream, which may lead to inconsistent results.
1022    pub fn to_project_set_select_item_proto_checked_pure(
1023        &self,
1024        retract: bool,
1025    ) -> crate::error::Result<ProjectSetSelectItem> {
1026        use risingwave_pb::expr::project_set_select_item::SelectItem::*;
1027
1028        Ok(ProjectSetSelectItem {
1029            select_item: Some(match self {
1030                ExprImpl::TableFunction(tf) => TableFunction(tf.to_protobuf_checked_pure(retract)?),
1031                expr => Expr(expr.to_expr_proto_checked_pure(retract, "SELECT list")?),
1032            }),
1033        })
1034    }
1035
1036    pub fn from_expr_proto(proto: &ExprNode) -> RwResult<Self> {
1037        let rex_node = proto.get_rex_node()?;
1038        let ret_type = proto.get_return_type()?.into();
1039
1040        Ok(match rex_node {
1041            RexNode::InputRef(column_index) => Self::InputRef(Box::new(InputRef::from_expr_proto(
1042                *column_index as _,
1043                ret_type,
1044            )?)),
1045            RexNode::Constant(_) => Self::Literal(Box::new(Literal::from_expr_proto(proto)?)),
1046            RexNode::Udf(udf) => Self::UserDefinedFunction(Box::new(
1047                UserDefinedFunction::from_expr_proto(udf, ret_type)?,
1048            )),
1049            RexNode::FuncCall(function_call) => {
1050                Self::FunctionCall(Box::new(FunctionCall::from_expr_proto(
1051                    function_call,
1052                    proto.get_function_type()?, // only interpret if it's a function call
1053                    ret_type,
1054                )?))
1055            }
1056            RexNode::Now(_) => Self::Now(Box::new(Now {})),
1057            RexNode::SecretRef(sr) => Self::SecretRef(Box::new(SecretRef {
1058                secret_id: sr.secret_id.into(),
1059                ref_as: risingwave_pb::secret::secret_ref::RefAsType::try_from(sr.ref_as)
1060                    .unwrap_or(risingwave_pb::secret::secret_ref::RefAsType::Text),
1061                secret_name: format!("<secret:{}>", sr.secret_id),
1062            })),
1063        })
1064    }
1065}
1066
1067impl From<Condition> for ExprImpl {
1068    fn from(c: Condition) -> Self {
1069        ExprImpl::and(c.conjunctions)
1070    }
1071}
1072
1073/// A custom Debug implementation that is more concise and suitable to use with
1074/// [`std::fmt::Formatter::debug_list`] in plan nodes. If the verbose output is preferred, it is
1075/// still available via `{:#?}`.
1076impl std::fmt::Debug for ExprImpl {
1077    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1078        if f.alternate() {
1079            return match self {
1080                Self::InputRef(arg0) => f.debug_tuple("InputRef").field(arg0).finish(),
1081                Self::Literal(arg0) => f.debug_tuple("Literal").field(arg0).finish(),
1082                Self::FunctionCall(arg0) => f.debug_tuple("FunctionCall").field(arg0).finish(),
1083                Self::FunctionCallWithLambda(arg0) => {
1084                    f.debug_tuple("FunctionCallWithLambda").field(arg0).finish()
1085                }
1086                Self::AggCall(arg0) => f.debug_tuple("AggCall").field(arg0).finish(),
1087                Self::Subquery(arg0) => f.debug_tuple("Subquery").field(arg0).finish(),
1088                Self::CorrelatedInputRef(arg0) => {
1089                    f.debug_tuple("CorrelatedInputRef").field(arg0).finish()
1090                }
1091                Self::TableFunction(arg0) => f.debug_tuple("TableFunction").field(arg0).finish(),
1092                Self::WindowFunction(arg0) => f.debug_tuple("WindowFunction").field(arg0).finish(),
1093                Self::UserDefinedFunction(arg0) => {
1094                    f.debug_tuple("UserDefinedFunction").field(arg0).finish()
1095                }
1096                Self::Parameter(arg0) => f.debug_tuple("Parameter").field(arg0).finish(),
1097                Self::Now(_) => f.debug_tuple("Now").finish(),
1098                Self::SecretRef(arg0) => f.debug_tuple("SecretRef").field(arg0).finish(),
1099            };
1100        }
1101        match self {
1102            Self::InputRef(x) => write!(f, "{:?}", x),
1103            Self::Literal(x) => write!(f, "{:?}", x),
1104            Self::FunctionCall(x) => write!(f, "{:?}", x),
1105            Self::FunctionCallWithLambda(x) => write!(f, "{:?}", x),
1106            Self::AggCall(x) => write!(f, "{:?}", x),
1107            Self::Subquery(x) => write!(f, "{:?}", x),
1108            Self::CorrelatedInputRef(x) => write!(f, "{:?}", x),
1109            Self::TableFunction(x) => write!(f, "{:?}", x),
1110            Self::WindowFunction(x) => write!(f, "{:?}", x),
1111            Self::UserDefinedFunction(x) => write!(f, "{:?}", x),
1112            Self::Parameter(x) => write!(f, "{:?}", x),
1113            Self::Now(x) => write!(f, "{:?}", x),
1114            Self::SecretRef(x) => write!(f, "Secret({})", x.secret_name),
1115        }
1116    }
1117}
1118
1119pub struct ExprDisplay<'a> {
1120    pub expr: &'a ExprImpl,
1121    pub input_schema: &'a Schema,
1122}
1123
1124impl std::fmt::Debug for ExprDisplay<'_> {
1125    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1126        let that = self.expr;
1127        match that {
1128            ExprImpl::InputRef(x) => write!(
1129                f,
1130                "{:?}",
1131                InputRefDisplay {
1132                    input_ref: x,
1133                    input_schema: self.input_schema
1134                }
1135            ),
1136            ExprImpl::Literal(x) => write!(f, "{:?}", x),
1137            ExprImpl::FunctionCall(x) => write!(
1138                f,
1139                "{:?}",
1140                FunctionCallDisplay {
1141                    function_call: x,
1142                    input_schema: self.input_schema
1143                }
1144            ),
1145            ExprImpl::FunctionCallWithLambda(x) => write!(
1146                f,
1147                "{:?}",
1148                FunctionCallDisplay {
1149                    function_call: &x.to_full_function_call(),
1150                    input_schema: self.input_schema
1151                }
1152            ),
1153            ExprImpl::AggCall(x) => write!(f, "{:?}", x),
1154            ExprImpl::Subquery(x) => write!(f, "{:?}", x),
1155            ExprImpl::CorrelatedInputRef(x) => write!(f, "{:?}", x),
1156            ExprImpl::TableFunction(x) => {
1157                // TODO: TableFunctionCallVerboseDisplay
1158                write!(f, "{:?}", x)
1159            }
1160            ExprImpl::WindowFunction(x) => {
1161                // TODO: WindowFunctionCallVerboseDisplay
1162                write!(f, "{:?}", x)
1163            }
1164            ExprImpl::UserDefinedFunction(x) => {
1165                write!(
1166                    f,
1167                    "{:?}",
1168                    UserDefinedFunctionDisplay {
1169                        func_call: x,
1170                        input_schema: self.input_schema
1171                    }
1172                )
1173            }
1174            ExprImpl::Parameter(x) => write!(f, "{:?}", x),
1175            ExprImpl::Now(x) => write!(f, "{:?}", x),
1176            ExprImpl::SecretRef(x) => write!(f, "Secret({})", x.secret_name),
1177        }
1178    }
1179}
1180
1181impl std::fmt::Display for ExprDisplay<'_> {
1182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1183        (self as &dyn std::fmt::Debug).fmt(f)
1184    }
1185}
1186
1187#[cfg(test)]
1188/// Asserts that the expression is an [`InputRef`] with the given index.
1189macro_rules! assert_eq_input_ref {
1190    ($e:expr, $index:expr) => {
1191        match $e {
1192            ExprImpl::InputRef(i) => assert_eq!(i.index(), $index),
1193            _ => assert!(false, "Expected input ref, found {:?}", $e),
1194        }
1195    };
1196}
1197
1198#[cfg(test)]
1199pub(crate) use assert_eq_input_ref;
1200use risingwave_common::bail;
1201use risingwave_common::catalog::Schema;
1202use risingwave_common::row::OwnedRow;
1203
1204use crate::utils::Condition;
1205
1206#[cfg(test)]
1207mod tests {
1208    use risingwave_pb::secret::secret_ref::RefAsType;
1209
1210    use super::*;
1211
1212    #[test]
1213    fn test_expr_debug_alternate() {
1214        let mut e = InputRef::new(1, DataType::Boolean).into();
1215        e = FunctionCall::new(ExprType::Not, vec![e]).unwrap().into();
1216        let s = format!("{:#?}", e);
1217        assert!(s.contains("return_type: Boolean"))
1218    }
1219
1220    #[test]
1221    fn test_secret_ref_display_contains_name() {
1222        let expr = ExprImpl::SecretRef(Box::new(SecretRef {
1223            secret_id: 42.into(),
1224            ref_as: RefAsType::Text,
1225            secret_name: "test_secret".to_owned(),
1226        }));
1227
1228        assert_eq!(format!("{expr:?}"), "Secret(test_secret)");
1229        assert_eq!(
1230            format!(
1231                "{:?}",
1232                ExprDisplay {
1233                    expr: &expr,
1234                    input_schema: Schema::empty(),
1235                }
1236            ),
1237            "Secret(test_secret)"
1238        );
1239        assert!(format!("{expr:#?}").contains("test_secret"));
1240    }
1241}