risingwave_frontend/optimizer/plan_expr_visitor/
strong.rs1use fixedbitset::FixedBitSet;
16
17use crate::expr::{ExprImpl, ExprType, FunctionCall, InputRef};
18
19#[derive(Default)]
38pub struct Strong {
39 null_columns: FixedBitSet,
40}
41
42impl Strong {
43 fn new(null_columns: FixedBitSet) -> Self {
44 Self { null_columns }
45 }
46
47 pub fn is_null(expr: &ExprImpl, null_columns: FixedBitSet) -> bool {
51 let strong = Strong::new(null_columns);
52 strong.is_null_visit(expr)
53 }
54
55 fn is_input_ref_null(&self, input_ref: &InputRef) -> bool {
56 self.null_columns.contains(input_ref.index())
57 }
58
59 fn is_null_visit(&self, expr: &ExprImpl) -> bool {
60 match expr {
61 ExprImpl::InputRef(input_ref) => self.is_input_ref_null(input_ref),
62 ExprImpl::Literal(literal) => literal.get_data().is_none(),
63 ExprImpl::FunctionCall(func_call) => self.is_null_function_call(func_call),
64 ExprImpl::FunctionCallWithLambda(_) => false,
65 ExprImpl::AggCall(_) => false,
66 ExprImpl::Subquery(_) => false,
67 ExprImpl::CorrelatedInputRef(_) => false,
68 ExprImpl::TableFunction(_) => false,
69 ExprImpl::WindowFunction(_) => false,
70 ExprImpl::UserDefinedFunction(_) => false,
71 ExprImpl::Parameter(_) => false,
72 ExprImpl::Now(_) => false,
73 }
74 }
75
76 fn is_null_function_call(&self, func_call: &FunctionCall) -> bool {
77 match func_call.func_type() {
78 ExprType::IsNull
80 | ExprType::IsNotNull
81 | ExprType::IsDistinctFrom
82 | ExprType::IsNotDistinctFrom
83 | ExprType::IsTrue
84 | ExprType::QuoteNullable
85 | ExprType::IsNotTrue
86 | ExprType::IsFalse
87 | ExprType::IsNotFalse
88 | ExprType::CheckNotNull => false,
89 ExprType::Not
91 | ExprType::Equal
92 | ExprType::NotEqual
93 | ExprType::LessThan
94 | ExprType::LessThanOrEqual
95 | ExprType::GreaterThan
96 | ExprType::GreaterThanOrEqual
97 | ExprType::Like
98 | ExprType::Add
99 | ExprType::AddWithTimeZone
100 | ExprType::Subtract
101 | ExprType::Multiply
102 | ExprType::Modulus
103 | ExprType::Divide
104 | ExprType::Cast
105 | ExprType::Trim
106 | ExprType::Ltrim
107 | ExprType::Rtrim
108 | ExprType::Ceil
109 | ExprType::Floor
110 | ExprType::Extract
111 | ExprType::L2Distance
112 | ExprType::CosineDistance
113 | ExprType::L1Distance
114 | ExprType::InnerProduct
115 | ExprType::VecConcat
116 | ExprType::L2Norm
117 | ExprType::L2Normalize
118 | ExprType::Subvector
119 | ExprType::Greatest
120 | ExprType::Least => self.any_null(func_call),
121 ExprType::And | ExprType::Or | ExprType::Coalesce => self.all_null(func_call),
123 ExprType::In
126 | ExprType::Some
127 | ExprType::All
128 | ExprType::BitwiseAnd
129 | ExprType::BitwiseOr
130 | ExprType::BitwiseXor
131 | ExprType::BitwiseNot
132 | ExprType::BitwiseShiftLeft
133 | ExprType::BitwiseShiftRight
134 | ExprType::DatePart
135 | ExprType::TumbleStart
136 | ExprType::MakeDate
137 | ExprType::MakeTime
138 | ExprType::MakeTimestamp
139 | ExprType::SecToTimestamptz
140 | ExprType::AtTimeZone
141 | ExprType::DateTrunc
142 | ExprType::DateBin
143 | ExprType::CharToTimestamptz
144 | ExprType::CharToDate
145 | ExprType::CastWithTimeZone
146 | ExprType::SubtractWithTimeZone
147 | ExprType::MakeTimestamptz
148 | ExprType::Substr
149 | ExprType::Length
150 | ExprType::ILike
151 | ExprType::SimilarToEscape
152 | ExprType::Upper
153 | ExprType::Lower
154 | ExprType::Replace
155 | ExprType::Position
156 | ExprType::Case
157 | ExprType::ConstantLookup
158 | ExprType::RoundDigit
159 | ExprType::Round
160 | ExprType::Ascii
161 | ExprType::Translate
162 | ExprType::Concat
163 | ExprType::ConcatVariadic
164 | ExprType::ConcatWs
165 | ExprType::ConcatWsVariadic
166 | ExprType::Abs
167 | ExprType::SplitPart
168 | ExprType::ToChar
169 | ExprType::Md5
170 | ExprType::CharLength
171 | ExprType::Repeat
172 | ExprType::ConcatOp
173 | ExprType::ByteaConcatOp
174 | ExprType::BoolOut
175 | ExprType::OctetLength
176 | ExprType::BitLength
177 | ExprType::Overlay
178 | ExprType::RegexpMatch
179 | ExprType::RegexpReplace
180 | ExprType::RegexpCount
181 | ExprType::RegexpSplitToArray
182 | ExprType::RegexpEq
183 | ExprType::Pow
184 | ExprType::Exp
185 | ExprType::Chr
186 | ExprType::StartsWith
187 | ExprType::Initcap
188 | ExprType::Lpad
189 | ExprType::Rpad
190 | ExprType::Reverse
191 | ExprType::Strpos
192 | ExprType::ToAscii
193 | ExprType::ToHex
194 | ExprType::QuoteIdent
195 | ExprType::QuoteLiteral
196 | ExprType::Sin
197 | ExprType::Cos
198 | ExprType::Tan
199 | ExprType::Cot
200 | ExprType::Asin
201 | ExprType::Acos
202 | ExprType::Acosd
203 | ExprType::Atan
204 | ExprType::Atan2
205 | ExprType::Atand
206 | ExprType::Atan2d
207 | ExprType::Sind
208 | ExprType::Cosd
209 | ExprType::Cotd
210 | ExprType::Tand
211 | ExprType::Asind
212 | ExprType::Sqrt
213 | ExprType::Degrees
214 | ExprType::Radians
215 | ExprType::Cosh
216 | ExprType::Tanh
217 | ExprType::Coth
218 | ExprType::Asinh
219 | ExprType::Acosh
220 | ExprType::Atanh
221 | ExprType::Sinh
222 | ExprType::Trunc
223 | ExprType::Ln
224 | ExprType::Log10
225 | ExprType::Cbrt
226 | ExprType::Sign
227 | ExprType::Scale
228 | ExprType::MinScale
229 | ExprType::TrimScale
230 | ExprType::Encode
231 | ExprType::Decode
232 | ExprType::Sha1
233 | ExprType::Sha224
234 | ExprType::Sha256
235 | ExprType::Sha384
236 | ExprType::Sha512
237 | ExprType::GetBit
238 | ExprType::GetByte
239 | ExprType::SetBit
240 | ExprType::SetByte
241 | ExprType::BitCount
242 | ExprType::Hmac
243 | ExprType::SecureCompare
244 | ExprType::Left
245 | ExprType::Right
246 | ExprType::Format
247 | ExprType::FormatVariadic
248 | ExprType::PgwireSend
249 | ExprType::PgwireRecv
250 | ExprType::ConvertFrom
251 | ExprType::ConvertTo
252 | ExprType::Decrypt
253 | ExprType::Encrypt
254 | ExprType::Neg
255 | ExprType::Field
256 | ExprType::Array
257 | ExprType::ArrayAccess
258 | ExprType::Row
259 | ExprType::ArrayToString
260 | ExprType::ArrayRangeAccess
261 | ExprType::ArrayCat
262 | ExprType::ArrayAppend
263 | ExprType::ArrayPrepend
264 | ExprType::FormatType
265 | ExprType::ArrayDistinct
266 | ExprType::ArrayLength
267 | ExprType::Cardinality
268 | ExprType::ArrayRemove
269 | ExprType::ArrayPositions
270 | ExprType::TrimArray
271 | ExprType::StringToArray
272 | ExprType::ArrayPosition
273 | ExprType::ArrayReplace
274 | ExprType::ArrayDims
275 | ExprType::ArrayTransform
276 | ExprType::ArrayMin
277 | ExprType::ArrayMax
278 | ExprType::ArraySum
279 | ExprType::ArraySort
280 | ExprType::ArrayReverse
281 | ExprType::ArrayContains
282 | ExprType::ArrayContained
283 | ExprType::ArrayFlatten
284 | ExprType::HexToInt256
285 | ExprType::JsonbAccess
286 | ExprType::JsonbAccessStr
287 | ExprType::JsonbExtractPath
288 | ExprType::JsonbExtractPathVariadic
289 | ExprType::JsonbExtractPathText
290 | ExprType::JsonbExtractPathTextVariadic
291 | ExprType::JsonbTypeof
292 | ExprType::JsonbArrayLength
293 | ExprType::IsJson
294 | ExprType::JsonbConcat
295 | ExprType::JsonbObject
296 | ExprType::JsonbPretty
297 | ExprType::JsonbContains
298 | ExprType::JsonbContained
299 | ExprType::JsonbExists
300 | ExprType::JsonbExistsAny
301 | ExprType::JsonbExistsAll
302 | ExprType::JsonbDeletePath
303 | ExprType::JsonbStripNulls
304 | ExprType::ToJsonb
305 | ExprType::JsonbBuildArray
306 | ExprType::JsonbBuildArrayVariadic
307 | ExprType::JsonbBuildObject
308 | ExprType::JsonbBuildObjectVariadic
309 | ExprType::JsonbPathExists
310 | ExprType::JsonbPathMatch
311 | ExprType::JsonbPathQueryArray
312 | ExprType::JsonbPathQueryFirst
313 | ExprType::JsonbPopulateRecord
314 | ExprType::JsonbToArray
315 | ExprType::JsonbToRecord
316 | ExprType::JsonbSet
317 | ExprType::JsonbPopulateMap
318 | ExprType::MapFromEntries
319 | ExprType::MapAccess
320 | ExprType::MapKeys
321 | ExprType::MapValues
322 | ExprType::MapEntries
323 | ExprType::MapFromKeyValues
324 | ExprType::MapCat
325 | ExprType::MapContains
326 | ExprType::MapDelete
327 | ExprType::MapFilter
328 | ExprType::MapInsert
329 | ExprType::MapLength
330 | ExprType::Vnode
331 | ExprType::VnodeUser
332 | ExprType::TestFeature
333 | ExprType::License
334 | ExprType::Proctime
335 | ExprType::PgSleep
336 | ExprType::PgSleepFor
337 | ExprType::PgSleepUntil
338 | ExprType::CastRegclass
339 | ExprType::PgGetIndexdef
340 | ExprType::ColDescription
341 | ExprType::PgGetViewdef
342 | ExprType::PgGetUserbyid
343 | ExprType::PgIndexesSize
344 | ExprType::PgRelationSize
345 | ExprType::PgGetSerialSequence
346 | ExprType::PgIndexColumnHasProperty
347 | ExprType::PgIsInRecovery
348 | ExprType::PgTableIsVisible
349 | ExprType::RwRecoveryStatus
350 | ExprType::RwClusterId
351 | ExprType::RwFragmentVnodes
352 | ExprType::RwActorVnodes
353 | ExprType::IcebergTransform
354 | ExprType::HasTablePrivilege
355 | ExprType::HasFunctionPrivilege
356 | ExprType::HasAnyColumnPrivilege
357 | ExprType::HasSchemaPrivilege
358 | ExprType::InetAton
359 | ExprType::InetNtoa
360 | ExprType::CompositeCast
361 | ExprType::RwEpochToTs
362 | ExprType::OpenaiEmbedding
363 | ExprType::HasDatabasePrivilege
364 | ExprType::Random => false,
365 ExprType::Unspecified => unreachable!(),
366 }
367 }
368
369 fn any_null(&self, func_call: &FunctionCall) -> bool {
370 func_call
371 .inputs()
372 .iter()
373 .any(|expr| self.is_null_visit(expr))
374 }
375
376 fn all_null(&self, func_call: &FunctionCall) -> bool {
377 func_call
378 .inputs()
379 .iter()
380 .all(|expr| self.is_null_visit(expr))
381 }
382}
383
384#[cfg(test)]
385mod tests {
386 use risingwave_common::types::DataType;
387
388 use super::*;
389 use crate::expr::ExprImpl::Literal;
390
391 #[test]
392 fn test_literal() {
393 let null_columns = FixedBitSet::with_capacity(1);
394 let expr = Literal(crate::expr::Literal::new(None, DataType::Varchar).into());
395 assert!(Strong::is_null(&expr, null_columns.clone()));
396
397 let expr = Literal(
398 crate::expr::Literal::new(Some("test".to_owned().into()), DataType::Varchar).into(),
399 );
400 assert!(!Strong::is_null(&expr, null_columns));
401 }
402
403 #[test]
404 fn test_input_ref1() {
405 let null_columns = FixedBitSet::with_capacity(2);
406 let expr = InputRef::new(0, DataType::Varchar).into();
407 assert!(!Strong::is_null(&expr, null_columns.clone()));
408
409 let expr = InputRef::new(1, DataType::Varchar).into();
410 assert!(!Strong::is_null(&expr, null_columns));
411 }
412
413 #[test]
414 fn test_input_ref2() {
415 let mut null_columns = FixedBitSet::with_capacity(2);
416 null_columns.insert(0);
417 null_columns.insert(1);
418 let expr = InputRef::new(0, DataType::Varchar).into();
419 assert!(Strong::is_null(&expr, null_columns.clone()));
420
421 let expr = InputRef::new(1, DataType::Varchar).into();
422 assert!(Strong::is_null(&expr, null_columns));
423 }
424
425 #[test]
426 fn test_c1_equal_1_or_c2_is_null() {
427 let mut null_columns = FixedBitSet::with_capacity(2);
428 null_columns.insert(0);
429 let expr = FunctionCall::new_unchecked(
430 ExprType::Or,
431 vec![
432 FunctionCall::new_unchecked(
433 ExprType::Equal,
434 vec![
435 InputRef::new(0, DataType::Int64).into(),
436 Literal(crate::expr::Literal::new(Some(1.into()), DataType::Int32).into()),
437 ],
438 DataType::Boolean,
439 )
440 .into(),
441 FunctionCall::new_unchecked(
442 ExprType::IsNull,
443 vec![InputRef::new(1, DataType::Int64).into()],
444 DataType::Boolean,
445 )
446 .into(),
447 ],
448 DataType::Boolean,
449 )
450 .into();
451 assert!(!Strong::is_null(&expr, null_columns));
452 }
453
454 #[test]
455 fn test_divide() {
456 let mut null_columns = FixedBitSet::with_capacity(2);
457 null_columns.insert(0);
458 null_columns.insert(1);
459 let expr = FunctionCall::new_unchecked(
460 ExprType::Divide,
461 vec![
462 InputRef::new(0, DataType::Decimal).into(),
463 InputRef::new(1, DataType::Decimal).into(),
464 ],
465 DataType::Varchar,
466 )
467 .into();
468 assert!(Strong::is_null(&expr, null_columns));
469 }
470
471 #[test]
473 fn test_multiply_divide() {
474 let mut null_columns = FixedBitSet::with_capacity(2);
475 null_columns.insert(0);
476 let expr = FunctionCall::new_unchecked(
477 ExprType::Multiply,
478 vec![
479 Literal(crate::expr::Literal::new(Some(0.8f64.into()), DataType::Float64).into()),
480 FunctionCall::new_unchecked(
481 ExprType::Divide,
482 vec![
483 InputRef::new(0, DataType::Decimal).into(),
484 InputRef::new(1, DataType::Decimal).into(),
485 ],
486 DataType::Decimal,
487 )
488 .into(),
489 ],
490 DataType::Decimal,
491 )
492 .into();
493 assert!(Strong::is_null(&expr, null_columns));
494 }
495
496 macro_rules! gen_test {
498 ($func:ident, $expr:expr, $expected:expr) => {
499 #[test]
500 fn $func() {
501 let null_columns = FixedBitSet::with_capacity(2);
502 let expr = $expr;
503 assert_eq!(Strong::is_null(&expr, null_columns), $expected);
504 }
505 };
506 }
507
508 gen_test!(
509 test_is_not_null,
510 FunctionCall::new_unchecked(
511 ExprType::IsNotNull,
512 vec![InputRef::new(0, DataType::Varchar).into()],
513 DataType::Varchar
514 )
515 .into(),
516 false
517 );
518 gen_test!(
519 test_is_null,
520 FunctionCall::new_unchecked(
521 ExprType::IsNull,
522 vec![InputRef::new(0, DataType::Varchar).into()],
523 DataType::Varchar
524 )
525 .into(),
526 false
527 );
528 gen_test!(
529 test_is_distinct_from,
530 FunctionCall::new_unchecked(
531 ExprType::IsDistinctFrom,
532 vec![
533 InputRef::new(0, DataType::Varchar).into(),
534 InputRef::new(1, DataType::Varchar).into()
535 ],
536 DataType::Varchar
537 )
538 .into(),
539 false
540 );
541 gen_test!(
542 test_is_not_distinct_from,
543 FunctionCall::new_unchecked(
544 ExprType::IsNotDistinctFrom,
545 vec![
546 InputRef::new(0, DataType::Varchar).into(),
547 InputRef::new(1, DataType::Varchar).into()
548 ],
549 DataType::Varchar
550 )
551 .into(),
552 false
553 );
554 gen_test!(
555 test_is_true,
556 FunctionCall::new_unchecked(
557 ExprType::IsTrue,
558 vec![InputRef::new(0, DataType::Varchar).into()],
559 DataType::Varchar
560 )
561 .into(),
562 false
563 );
564 gen_test!(
565 test_is_not_true,
566 FunctionCall::new_unchecked(
567 ExprType::IsNotTrue,
568 vec![InputRef::new(0, DataType::Varchar).into()],
569 DataType::Varchar
570 )
571 .into(),
572 false
573 );
574 gen_test!(
575 test_is_false,
576 FunctionCall::new_unchecked(
577 ExprType::IsFalse,
578 vec![InputRef::new(0, DataType::Varchar).into()],
579 DataType::Varchar
580 )
581 .into(),
582 false
583 );
584}