Skip to main content

risingwave_frontend/expr/
pure.rs

1// Copyright 2023 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::borrow::Cow;
16
17use expr_node::Type;
18use risingwave_pb::expr::expr_node;
19
20use super::{ExprImpl, ExprVisitor};
21use crate::expr::FunctionCall;
22
23#[derive(Default)]
24pub(crate) struct ImpureAnalyzer {
25    impure: Option<Cow<'static, str>>,
26}
27
28impl ImpureAnalyzer {
29    /// Returns `true` if the expression is impure.
30    ///
31    /// Only call this method after visiting the expression.
32    pub fn is_impure(&self) -> bool {
33        self.impure.is_some()
34    }
35
36    /// Returns the description of the impure expression if it is impure, for error reporting.
37    /// `None` if the expression is pure.
38    ///
39    /// Only call this method after visiting the expression.
40    pub fn impure_expr_desc(&self) -> Option<&str> {
41        self.impure.as_deref()
42    }
43}
44
45impl ExprVisitor for ImpureAnalyzer {
46    fn visit_user_defined_function(&mut self, func_call: &super::UserDefinedFunction) {
47        let name = &func_call.catalog.name;
48        self.impure = Some(format!("user-defined function `{name}`").into());
49    }
50
51    fn visit_table_function(&mut self, func_call: &super::TableFunction) {
52        use crate::expr::table_function::TableFunctionType as Type;
53        let func_type = func_call.function_type;
54        match func_type {
55            Type::Unspecified => unreachable!(),
56
57            // deterministic
58            Type::GenerateSeries
59            | Type::Unnest
60            | Type::RegexpMatches
61            | Type::Range
62            | Type::GenerateSubscripts
63            | Type::PgExpandarray
64            | Type::JsonbArrayElements
65            | Type::JsonbArrayElementsText
66            | Type::JsonbEach
67            | Type::JsonbEachText
68            | Type::JsonbObjectKeys
69            | Type::JsonbPathQuery
70            | Type::JsonbPopulateRecordset
71            | Type::JsonbToRecordset => {
72                func_call.args.iter().for_each(|expr| self.visit_expr(expr));
73            }
74
75            // indeterministic
76            Type::FileScan
77            | Type::PostgresQuery
78            | Type::MysqlQuery
79            | Type::InternalBackfillProgress
80            | Type::InternalSourceBackfillProgress
81            | Type::InternalGetChannelDeltaStats
82            | Type::PgGetKeywords => {
83                self.impure = Some(func_type.as_str_name().into());
84            }
85            Type::UserDefined => {
86                let name = &func_call.user_defined.as_ref().unwrap().name;
87                self.impure = Some(format!("user-defined table function `{name}`").into());
88            }
89        }
90    }
91
92    fn visit_now(&mut self, _: &super::Now) {
93        self.impure = Some("NOW or PROCTIME".into());
94    }
95
96    fn visit_secret_ref(&mut self, secret_ref: &super::SecretRef) {
97        self.impure = Some(format!("secret reference `{}`", secret_ref.secret_name).into());
98    }
99
100    fn visit_function_call(&mut self, func_call: &super::FunctionCall) {
101        let func_type = func_call.func_type();
102        match func_type {
103            Type::Unspecified => unreachable!(),
104            #[expect(deprecated)]
105            Type::Add
106            | Type::Subtract
107            | Type::Multiply
108            | Type::Divide
109            | Type::Modulus
110            | Type::Equal
111            | Type::NotEqual
112            | Type::LessThan
113            | Type::LessThanOrEqual
114            | Type::GreaterThan
115            | Type::GreaterThanOrEqual
116            | Type::And
117            | Type::Or
118            | Type::Not
119            | Type::In
120            | Type::Some
121            | Type::All
122            | Type::BitwiseAnd
123            | Type::BitwiseOr
124            | Type::BitwiseXor
125            | Type::BitwiseNot
126            | Type::BitwiseShiftLeft
127            | Type::BitwiseShiftRight
128            | Type::Extract
129            | Type::DatePart
130            | Type::TumbleStart
131            | Type::SecToTimestamptz
132            | Type::AtTimeZone
133            | Type::DateTrunc
134            | Type::DateBin
135            | Type::MakeDate
136            | Type::MakeTime
137            | Type::MakeTimestamp
138            | Type::CharToTimestamptz
139            | Type::CharToDate
140            | Type::CastWithTimeZone
141            | Type::AddWithTimeZone
142            | Type::SubtractWithTimeZone
143            | Type::Cast
144            | Type::Substr
145            | Type::Length
146            | Type::Like
147            | Type::ILike
148            | Type::SimilarToEscape
149            | Type::Upper
150            | Type::Lower
151            | Type::Trim
152            | Type::Replace
153            | Type::Position
154            | Type::Ltrim
155            | Type::Rtrim
156            | Type::Case
157            | Type::ConstantLookup
158            | Type::RoundDigit
159            | Type::Round
160            | Type::Ascii
161            | Type::Translate
162            | Type::Coalesce
163            | Type::ConcatWs
164            | Type::ConcatWsVariadic
165            | Type::Abs
166            | Type::SplitPart
167            | Type::Ceil
168            | Type::Floor
169            | Type::Trunc
170            | Type::ToChar
171            | Type::Md5
172            | Type::CharLength
173            | Type::Repeat
174            | Type::ConcatOp
175            | Type::ByteaConcatOp
176            | Type::Concat
177            | Type::ConcatVariadic
178            | Type::BoolOut
179            | Type::OctetLength
180            | Type::BitLength
181            | Type::Overlay
182            | Type::RegexpMatch
183            | Type::RegexpReplace
184            | Type::RegexpCount
185            | Type::RegexpSplitToArray
186            | Type::RegexpEq
187            | Type::Pow
188            | Type::Exp
189            | Type::Ln
190            | Type::Log10
191            | Type::Chr
192            | Type::StartsWith
193            | Type::Initcap
194            | Type::Lpad
195            | Type::Rpad
196            | Type::Reverse
197            | Type::Strpos
198            | Type::ToAscii
199            | Type::ToHex
200            | Type::QuoteIdent
201            | Type::Sin
202            | Type::Cos
203            | Type::Tan
204            | Type::Cot
205            | Type::Asin
206            | Type::Acos
207            | Type::Acosd
208            | Type::Atan
209            | Type::Atan2
210            | Type::Atand
211            | Type::Atan2d
212            | Type::Sqrt
213            | Type::Cbrt
214            | Type::Sign
215            | Type::Scale
216            | Type::MinScale
217            | Type::TrimScale
218            | Type::Gamma
219            | Type::Lgamma
220            | Type::Left
221            | Type::Right
222            | Type::Degrees
223            | Type::Radians
224            | Type::IsTrue
225            | Type::IsNotTrue
226            | Type::IsFalse
227            | Type::IsNotFalse
228            | Type::IsNull
229            | Type::IsNotNull
230            | Type::IsDistinctFrom
231            | Type::IsNotDistinctFrom
232            | Type::Neg
233            | Type::Field
234            | Type::Array
235            | Type::ArrayAccess
236            | Type::ArrayRangeAccess
237            | Type::Row
238            | Type::ArrayToString
239            | Type::ArrayCat
240            | Type::ArrayMax
241            | Type::ArraySum
242            | Type::ArraySort
243            | Type::ArrayAppend
244            | Type::ArrayReverse
245            | Type::ArrayPrepend
246            | Type::FormatType
247            | Type::ArrayDistinct
248            | Type::ArrayMin
249            | Type::ArrayDims
250            | Type::ArrayLength
251            | Type::Cardinality
252            | Type::TrimArray
253            | Type::ArrayRemove
254            | Type::ArrayReplace
255            | Type::ArrayPosition
256            | Type::ArrayContains
257            | Type::ArrayContained
258            | Type::ArrayOverlaps
259            | Type::ArrayFlatten
260            | Type::HexToInt256
261            | Type::JsonbConcat
262            | Type::JsonbAccess
263            | Type::JsonbAccessStr
264            | Type::JsonbExtractPath
265            | Type::JsonbExtractPathVariadic
266            | Type::JsonbExtractPathText
267            | Type::JsonbExtractPathTextVariadic
268            | Type::JsonbTypeof
269            | Type::JsonbArrayLength
270            | Type::JsonbObject
271            | Type::JsonbPretty
272            | Type::JsonbDeletePath
273            | Type::JsonbContains
274            | Type::JsonbContained
275            | Type::JsonbExists
276            | Type::JsonbExistsAny
277            | Type::JsonbExistsAll
278            | Type::JsonbStripNulls
279            | Type::JsonbBuildArray
280            | Type::JsonbBuildArrayVariadic
281            | Type::JsonbBuildObject
282            | Type::JsonbPopulateRecord
283            | Type::JsonbToArray
284            | Type::JsonbToRecord
285            | Type::JsonbBuildObjectVariadic
286            | Type::JsonbPathExists
287            | Type::JsonbPathMatch
288            | Type::JsonbPathQueryArray
289            | Type::JsonbPathQueryFirst
290            | Type::JsonbSet
291            | Type::JsonbPopulateMap
292            | Type::IsJson
293            | Type::ToJsonb
294            | Type::ToVariant
295            | Type::VariantGet
296            | Type::TryVariantGet
297            | Type::VariantTypeof
298            | Type::Sind
299            | Type::Cosd
300            | Type::Cotd
301            | Type::Asind
302            | Type::Sinh
303            | Type::Cosh
304            | Type::Coth
305            | Type::Tanh
306            | Type::Atanh
307            | Type::Asinh
308            | Type::Acosh
309            | Type::Decode
310            | Type::Encode
311            | Type::GetBit
312            | Type::GetByte
313            | Type::SetBit
314            | Type::SetByte
315            | Type::BitCount
316            | Type::Sha1
317            | Type::Sha224
318            | Type::Sha256
319            | Type::Sha384
320            | Type::Sha512
321            | Type::Crc32
322            | Type::Crc32c
323            | Type::Hmac
324            | Type::SecureCompare
325            | Type::Decrypt
326            | Type::Encrypt
327            | Type::Tand
328            | Type::ArrayPositions
329            | Type::StringToArray
330            | Type::Format
331            | Type::FormatVariadic
332            | Type::PgwireSend
333            | Type::PgwireRecv
334            | Type::ArrayTransform
335            | Type::Greatest
336            | Type::Least
337            | Type::ConvertFrom
338            | Type::ConvertTo
339            | Type::IcebergTransform
340            | Type::InetNtoa
341            | Type::InetAton
342            | Type::QuoteLiteral
343            | Type::QuoteNullable
344            | Type::MapFromEntries
345            | Type::MapAccess
346            | Type::MapKeys
347            | Type::MapValues
348            | Type::MapEntries
349            | Type::MapFromKeyValues
350            | Type::MapCat
351            | Type::MapContains
352            | Type::MapDelete
353            | Type::MapFilter
354            | Type::MapInsert
355            | Type::MapLength
356            | Type::L2Distance
357            | Type::CosineDistance
358            | Type::L1Distance
359            | Type::InnerProduct
360            | Type::VecConcat
361            | Type::L2Norm
362            | Type::L2Normalize
363            | Type::Subvector
364            // TODO: `rw_vnode` is more like STABLE instead of IMMUTABLE, because even its result is
365            // deterministic, it needs to read the total vnode count from the context, which means that
366            // it cannot be evaluated during constant folding. We have to treat it pure here so it can be used
367            // internally without materialization.
368            | Type::Vnode
369            | Type::VnodeUser
370            | Type::RwEpochToTs
371            | Type::CheckNotNull
372            | Type::CompositeCast =>
373            // expression output is deterministic(same result for the same input)
374            {
375                func_call
376                    .inputs()
377                    .iter()
378                    .for_each(|expr| self.visit_expr(expr));
379            }
380            // expression output is not deterministic
381            Type::TestFeature
382            | Type::License
383            | Type::Proctime
384            | Type::PgSleep
385            | Type::PgSleepFor
386            | Type::PgSleepUntil
387            | Type::CastRegclass
388            | Type::PgGetIndexdef
389            | Type::ColDescription
390            | Type::PgGetViewdef
391            | Type::PgGetUserbyid
392            | Type::PgIndexesSize
393            | Type::PgRelationSize
394            | Type::PgGetSerialSequence
395            | Type::PgIndexColumnHasProperty
396            | Type::HasTablePrivilege
397            | Type::HasAnyColumnPrivilege
398            | Type::HasSchemaPrivilege
399            | Type::MakeTimestamptz
400            | Type::PgIsInRecovery
401            | Type::RwRecoveryStatus
402            | Type::RwClusterId
403            | Type::RwFragmentVnodes
404            | Type::RwActorVnodes
405            | Type::PgTableIsVisible
406            | Type::HasFunctionPrivilege
407            | Type::OpenaiEmbedding
408            | Type::HasDatabasePrivilege
409            | Type::Random
410            | Type::ClockTimestamp
411            | Type::GenRandomUuid => self.impure = Some(func_type.as_str_name().into()),
412        }
413    }
414}
415
416pub fn is_pure(expr: &ExprImpl) -> bool {
417    !is_impure(expr)
418}
419
420pub fn is_impure(expr: &ExprImpl) -> bool {
421    let mut a = ImpureAnalyzer::default();
422    a.visit_expr(expr);
423    a.is_impure()
424}
425
426pub fn is_impure_func_call(func_call: &FunctionCall) -> bool {
427    let mut a = ImpureAnalyzer::default();
428    a.visit_function_call(func_call);
429    a.is_impure()
430}
431
432/// Returns the description of the impure expression if it is impure, for error reporting.
433/// `None` if the expression is pure.
434pub fn impure_expr_desc(expr: &ExprImpl) -> Option<String> {
435    let mut a = ImpureAnalyzer::default();
436    a.visit_expr(expr);
437    a.impure_expr_desc().map(|s| s.to_owned())
438}
439
440#[cfg(test)]
441mod tests {
442    use risingwave_common::types::DataType;
443    use risingwave_pb::expr::expr_node::Type;
444
445    use crate::expr::{ExprImpl, FunctionCall, InputRef, is_impure, is_pure};
446
447    fn expect_pure(expr: &ExprImpl) {
448        assert!(is_pure(expr));
449        assert!(!is_impure(expr));
450    }
451
452    fn expect_impure(expr: &ExprImpl) {
453        assert!(!is_pure(expr));
454        assert!(is_impure(expr));
455    }
456
457    #[test]
458    fn test_pure_funcs() {
459        let e: ExprImpl = FunctionCall::new(
460            Type::Add,
461            vec![
462                InputRef::new(0, DataType::Int16).into(),
463                InputRef::new(0, DataType::Int16).into(),
464            ],
465        )
466        .unwrap()
467        .into();
468        expect_pure(&e);
469
470        let e: ExprImpl = FunctionCall::new(
471            Type::GreaterThan,
472            vec![
473                InputRef::new(0, DataType::Timestamptz).into(),
474                FunctionCall::new(Type::Proctime, vec![]).unwrap().into(),
475            ],
476        )
477        .unwrap()
478        .into();
479        expect_impure(&e);
480    }
481}