1use std::cmp::Ordering;
16use std::collections::{BTreeMap, HashSet};
17use std::fmt::{self, Debug};
18use std::ops::Bound;
19use std::sync::LazyLock;
20
21use fixedbitset::FixedBitSet;
22use itertools::Itertools;
23use risingwave_common::catalog::Schema;
24use risingwave_common::types::{DataType, DefaultOrd, ScalarImpl};
25use risingwave_common::util::iter_util::ZipEqFast;
26use risingwave_common::util::scan_range::{ScanRange, is_full_range};
27use risingwave_common::util::sort_util::{OrderType, cmp_rows};
28
29use crate::TableCatalog;
30use crate::error::Result;
31use crate::expr::{
32 ExprDisplay, ExprImpl, ExprMutator, ExprRewriter, ExprType, ExprVisitor, FunctionCall,
33 InequalityInputPair, InputRef, collect_input_refs, column_self_eq_eliminate,
34 factorization_expr, fold_boolean_constant, push_down_not, to_conjunctions,
35 try_get_bool_constant,
36};
37use crate::utils::condition::cast_compare::{ResultForCmp, ResultForEq};
38
39#[derive(Debug, Clone, PartialEq, Eq, Hash)]
40pub struct Condition {
41 pub conjunctions: Vec<ExprImpl>,
43}
44
45impl IntoIterator for Condition {
46 type IntoIter = std::vec::IntoIter<ExprImpl>;
47 type Item = ExprImpl;
48
49 fn into_iter(self) -> Self::IntoIter {
50 self.conjunctions.into_iter()
51 }
52}
53
54impl fmt::Display for Condition {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 let mut conjunctions = self.conjunctions.iter();
57 if let Some(expr) = conjunctions.next() {
58 write!(f, "{:?}", expr)?;
59 }
60 if self.always_true() {
61 write!(f, "true")?;
62 } else {
63 for expr in conjunctions {
64 write!(f, " AND {:?}", expr)?;
65 }
66 }
67 Ok(())
68 }
69}
70
71impl Condition {
72 pub fn with_expr(expr: ExprImpl) -> Self {
73 let conjunctions = to_conjunctions(expr);
74
75 Self { conjunctions }.simplify()
76 }
77
78 pub fn true_cond() -> Self {
79 Self {
80 conjunctions: vec![],
81 }
82 }
83
84 pub fn false_cond() -> Self {
85 Self {
86 conjunctions: vec![ExprImpl::literal_bool(false)],
87 }
88 }
89
90 pub fn always_true(&self) -> bool {
91 self.conjunctions.is_empty()
92 }
93
94 pub fn always_false(&self) -> bool {
95 static FALSE: LazyLock<ExprImpl> = LazyLock::new(|| ExprImpl::literal_bool(false));
96 !self.conjunctions.is_empty() && self.conjunctions.contains(&*FALSE)
98 }
99
100 pub fn as_expr_unless_true(&self) -> Option<ExprImpl> {
102 if self.always_true() {
103 None
104 } else {
105 Some(self.clone().into())
106 }
107 }
108
109 #[must_use]
110 pub fn and(self, other: Self) -> Self {
111 let mut ret = self;
112 ret.conjunctions.extend(other.conjunctions);
113 ret.simplify()
114 }
115
116 #[must_use]
117 pub fn or(self, other: Self) -> Self {
118 let or_expr = ExprImpl::FunctionCall(
119 FunctionCall::new_unchecked(
120 ExprType::Or,
121 vec![self.into(), other.into()],
122 DataType::Boolean,
123 )
124 .into(),
125 );
126 let ret = Self::with_expr(or_expr);
127 ret.simplify()
128 }
129
130 #[must_use]
132 pub fn split(self, left_col_num: usize, right_col_num: usize) -> (Self, Self, Self) {
133 let left_bit_map = FixedBitSet::from_iter(0..left_col_num);
134 let right_bit_map = FixedBitSet::from_iter(left_col_num..left_col_num + right_col_num);
135
136 self.group_by::<_, 3>(|expr| {
137 let input_bits = expr.collect_input_refs(left_col_num + right_col_num);
138 if input_bits.is_subset(&left_bit_map) {
139 0
140 } else if input_bits.is_subset(&right_bit_map) {
141 1
142 } else {
143 2
144 }
145 })
146 .into_iter()
147 .next_tuple()
148 .unwrap()
149 }
150
151 pub fn collect_input_refs(&self, input_col_num: usize) -> FixedBitSet {
156 collect_input_refs(input_col_num, &self.conjunctions)
157 }
158
159 #[must_use]
173 pub fn split_by_input_col_nums(
174 self,
175 input_col_nums: &[usize],
176 only_eq: bool,
177 ) -> (BTreeMap<(usize, usize), Self>, Self) {
178 let mut bitmaps = Vec::with_capacity(input_col_nums.len());
179 let mut cols_seen = 0;
180 for cols in input_col_nums {
181 bitmaps.push(FixedBitSet::from_iter(cols_seen..cols_seen + cols));
182 cols_seen += cols;
183 }
184
185 let mut pairwise_conditions = BTreeMap::new();
186 let mut non_eq_join = vec![];
187
188 for expr in self.conjunctions {
189 let input_bits = expr.collect_input_refs(cols_seen);
190 let mut subset_indices = Vec::with_capacity(input_col_nums.len());
191 for (idx, bitmap) in bitmaps.iter().enumerate() {
192 if !input_bits.is_disjoint(bitmap) {
193 subset_indices.push(idx);
194 }
195 }
196 if subset_indices.len() != 2 || (only_eq && expr.as_eq_cond().is_none()) {
197 non_eq_join.push(expr);
198 } else {
199 let key = if subset_indices[0] < subset_indices[1] {
201 (subset_indices[0], subset_indices[1])
202 } else {
203 (subset_indices[1], subset_indices[0])
204 };
205 let e = pairwise_conditions
206 .entry(key)
207 .or_insert_with(Condition::true_cond);
208 e.conjunctions.push(expr);
209 }
210 }
211 (
212 pairwise_conditions,
213 Condition {
214 conjunctions: non_eq_join,
215 },
216 )
217 }
218
219 #[must_use]
220 pub fn split_eq_keys(
227 self,
228 left_col_num: usize,
229 right_col_num: usize,
230 ) -> (Vec<(InputRef, InputRef, bool)>, Self) {
231 let left_bit_map = FixedBitSet::from_iter(0..left_col_num);
232 let right_bit_map = FixedBitSet::from_iter(left_col_num..left_col_num + right_col_num);
233
234 let (mut eq_keys, mut others) = (vec![], vec![]);
235 self.conjunctions.into_iter().for_each(|expr| {
236 let input_bits = expr.collect_input_refs(left_col_num + right_col_num);
237 if input_bits.is_disjoint(&left_bit_map) || input_bits.is_disjoint(&right_bit_map) {
238 others.push(expr)
239 } else if let Some(columns) = expr.as_eq_cond() {
240 eq_keys.push((columns.0, columns.1, false));
241 } else if let Some(columns) = expr.as_is_not_distinct_from_cond() {
242 eq_keys.push((columns.0, columns.1, true));
243 } else {
244 others.push(expr)
245 }
246 });
247
248 (
249 eq_keys,
250 Condition {
251 conjunctions: others,
252 },
253 )
254 }
255
256 pub(crate) fn extract_inequality_keys(
263 &self,
264 left_col_num: usize,
265 right_col_num: usize,
266 ) -> Vec<(usize, InequalityInputPair)> {
267 let left_bit_map = FixedBitSet::from_iter(0..left_col_num);
268 let right_bit_map = FixedBitSet::from_iter(left_col_num..left_col_num + right_col_num);
269
270 self.conjunctions
271 .iter()
272 .enumerate()
273 .filter_map(|(conjunction_idx, expr)| {
274 let input_bits = expr.collect_input_refs(left_col_num + right_col_num);
275 if input_bits.is_disjoint(&left_bit_map) || input_bits.is_disjoint(&right_bit_map) {
276 None
277 } else {
278 expr.as_input_comparison_cond()
279 .map(|inequality_pair| (conjunction_idx, inequality_pair))
280 }
281 })
282 .collect_vec()
283 }
284
285 #[must_use]
288 pub fn split_disjoint(self, columns: &FixedBitSet) -> (Self, Self) {
289 self.group_by::<_, 2>(|expr| {
290 let input_bits = expr.collect_input_refs(columns.len());
291 input_bits.is_disjoint(columns) as usize
292 })
293 .into_iter()
294 .next_tuple()
295 .unwrap()
296 }
297
298 fn disjunctions_to_scan_ranges(
302 table: &TableCatalog,
303 max_split_range_gap: u64,
304 disjunctions: Vec<ExprImpl>,
305 ) -> Result<Option<(Vec<ScanRange>, bool)>> {
306 let disjunctions_result: Result<Vec<(Vec<ScanRange>, Self)>> = disjunctions
307 .into_iter()
308 .map(|x| {
309 Condition {
310 conjunctions: to_conjunctions(x),
311 }
312 .split_to_scan_ranges(table, max_split_range_gap)
313 })
314 .collect();
315
316 let disjunctions_result = disjunctions_result?;
318
319 let all_equal = disjunctions_result
322 .iter()
323 .all(|(scan_ranges, other_condition)| {
324 other_condition.always_true()
325 && scan_ranges
326 .iter()
327 .all(|x| !x.eq_conds.is_empty() && is_full_range(&x.range))
328 });
329
330 if all_equal {
331 let scan_ranges = disjunctions_result
335 .into_iter()
336 .flat_map(|(scan_ranges, _)| scan_ranges)
337 .sorted_by(|a, b| a.eq_conds.len().cmp(&b.eq_conds.len()))
339 .collect_vec();
340 let mut non_overlap_scan_ranges: Vec<ScanRange> = vec![];
342 for s1 in &scan_ranges {
343 let overlap = non_overlap_scan_ranges.iter().any(|s2| {
344 #[allow(clippy::disallowed_methods)]
345 s1.eq_conds
346 .iter()
347 .zip(s2.eq_conds.iter())
348 .all(|(a, b)| a == b)
349 });
350 if !overlap {
354 non_overlap_scan_ranges.push(s1.clone());
355 }
356 }
357
358 Ok(Some((non_overlap_scan_ranges, false)))
359 } else {
360 let mut scan_ranges = vec![];
361 for (scan_ranges_chunk, _) in disjunctions_result {
362 if scan_ranges_chunk.is_empty() {
363 return Ok(None);
365 }
366
367 scan_ranges.extend(scan_ranges_chunk);
368 }
369
370 let order_types = table
371 .pk
372 .iter()
373 .cloned()
374 .map(|x| {
375 if x.order_type.is_descending() {
376 x.order_type.reverse()
377 } else {
378 x.order_type
379 }
380 })
381 .collect_vec();
382 scan_ranges.sort_by(|left, right| {
383 let (left_start, _left_end) = &left.convert_to_range();
384 let (right_start, _right_end) = &right.convert_to_range();
385
386 let left_start_vec = match &left_start {
387 Bound::Included(vec) | Bound::Excluded(vec) => vec,
388 _ => &vec![],
389 };
390 let right_start_vec = match &right_start {
391 Bound::Included(vec) | Bound::Excluded(vec) => vec,
392 _ => &vec![],
393 };
394
395 if left_start_vec.is_empty() && right_start_vec.is_empty() {
396 return Ordering::Less;
397 }
398
399 if left_start_vec.is_empty() {
400 return Ordering::Less;
401 }
402
403 if right_start_vec.is_empty() {
404 return Ordering::Greater;
405 }
406
407 let cmp_column_len = left_start_vec.len().min(right_start_vec.len());
408 cmp_rows(
409 &left_start_vec[0..cmp_column_len],
410 &right_start_vec[0..cmp_column_len],
411 &order_types[0..cmp_column_len],
412 )
413 });
414
415 if scan_ranges.is_empty() {
416 return Ok(None);
417 }
418
419 if scan_ranges.len() == 1 {
420 return Ok(Some((scan_ranges, true)));
421 }
422
423 let mut output_scan_ranges: Vec<ScanRange> = vec![];
424 output_scan_ranges.push(scan_ranges[0].clone());
425 let mut idx = 1;
426 loop {
427 if idx >= scan_ranges.len() {
428 break;
429 }
430
431 let scan_range_left = output_scan_ranges.last_mut().unwrap();
432 let scan_range_right = &scan_ranges[idx];
433
434 if scan_range_left.eq_conds == scan_range_right.eq_conds {
435 if !ScanRange::is_overlap(scan_range_left, scan_range_right, &order_types) {
438 output_scan_ranges.push(scan_range_right.clone());
440 idx += 1;
441 continue;
442 }
443
444 fn merge_bound(
446 left_scan_range: &Bound<Vec<Option<ScalarImpl>>>,
447 right_scan_range: &Bound<Vec<Option<ScalarImpl>>>,
448 order_types: &[OrderType],
449 left_bound: bool,
450 ) -> Bound<Vec<Option<ScalarImpl>>> {
451 let left_scan_range = match left_scan_range {
452 Bound::Included(vec) | Bound::Excluded(vec) => vec,
453 Bound::Unbounded => return Bound::Unbounded,
454 };
455
456 let right_scan_range = match right_scan_range {
457 Bound::Included(vec) | Bound::Excluded(vec) => vec,
458 Bound::Unbounded => return Bound::Unbounded,
459 };
460
461 let cmp_len = left_scan_range.len().min(right_scan_range.len());
462
463 let cmp = cmp_rows(
464 &left_scan_range[..cmp_len],
465 &right_scan_range[..cmp_len],
466 &order_types[..cmp_len],
467 );
468
469 let bound = {
470 if (cmp.is_le() && left_bound) || (cmp.is_ge() && !left_bound) {
471 left_scan_range.to_vec()
472 } else {
473 right_scan_range.to_vec()
474 }
475 };
476
477 Bound::Included(bound)
479 }
480
481 scan_range_left.range.0 = merge_bound(
482 &scan_range_left.range.0,
483 &scan_range_right.range.0,
484 &order_types,
485 true,
486 );
487
488 scan_range_left.range.1 = merge_bound(
489 &scan_range_left.range.1,
490 &scan_range_right.range.1,
491 &order_types,
492 false,
493 );
494
495 if scan_range_left.is_full_table_scan() {
496 return Ok(None);
497 }
498 } else {
499 output_scan_ranges.push(scan_range_right.clone());
500 }
501
502 idx += 1;
503 }
504
505 Ok(Some((output_scan_ranges, true)))
506 }
507 }
508
509 fn split_row_cmp_to_scan_ranges(
510 &self,
511 table: &TableCatalog,
512 ) -> Result<Option<(Vec<ScanRange>, Self)>> {
513 let (mut row_conjunctions, row_conjunctions_without_struct): (Vec<_>, Vec<_>) =
514 self.conjunctions.clone().into_iter().partition(|expr| {
515 if let Some(f) = expr.as_function_call() {
516 if let Some(left_input) = f.inputs().get(0)
517 && let Some(left_input) = left_input.as_function_call()
518 && matches!(left_input.func_type(), ExprType::Row)
519 && left_input.inputs().iter().all(|x| x.is_input_ref())
520 && let Some(right_input) = f.inputs().get(1)
521 && right_input.is_literal()
522 {
523 true
524 } else {
525 false
526 }
527 } else {
528 false
529 }
530 });
531 if row_conjunctions.len() == 1 {
536 let row_conjunction = row_conjunctions.pop().unwrap();
537 let row_left_inputs = row_conjunction
538 .as_function_call()
539 .unwrap()
540 .inputs()
541 .get(0)
542 .unwrap()
543 .as_function_call()
544 .unwrap()
545 .inputs();
546 let row_right_literal = row_conjunction
547 .as_function_call()
548 .unwrap()
549 .inputs()
550 .get(1)
551 .unwrap()
552 .as_literal()
553 .unwrap();
554 if !matches!(row_right_literal.get_data(), Some(ScalarImpl::Struct(_))) {
555 return Ok(None);
556 }
557 let row_right_literal_data = row_right_literal.get_data().clone().unwrap();
558 let right_iter = row_right_literal_data.as_struct().fields();
559 let func_type = row_conjunction.as_function_call().unwrap().func_type();
560 if row_left_inputs.len() > 1
561 && (matches!(func_type, ExprType::LessThan)
562 || matches!(func_type, ExprType::GreaterThan))
563 {
564 let mut pk_struct = vec![];
565 let mut order_type = None;
566 let mut all_added = true;
567 let mut iter = row_left_inputs.iter().zip_eq_fast(right_iter);
568 for column_order in &table.pk {
569 if let Some((left_expr, right_expr)) = iter.next() {
570 if left_expr.as_input_ref().unwrap().index != column_order.column_index {
571 all_added = false;
572 break;
573 }
574 match order_type {
575 Some(o) => {
576 if o != column_order.order_type {
577 all_added = false;
578 break;
579 }
580 }
581 None => order_type = Some(column_order.order_type),
582 }
583 pk_struct.push(right_expr.clone());
584 }
585 }
586
587 if !pk_struct.is_empty() {
589 if !all_added {
590 let scan_range = ScanRange {
591 eq_conds: vec![],
592 range: match func_type {
593 ExprType::GreaterThan => {
594 (Bound::Included(pk_struct), Bound::Unbounded)
595 }
596 ExprType::LessThan => {
597 (Bound::Unbounded, Bound::Included(pk_struct))
598 }
599 _ => unreachable!(),
600 },
601 };
602 return Ok(Some((
603 vec![scan_range],
604 Condition {
605 conjunctions: self.conjunctions.clone(),
606 },
607 )));
608 } else {
609 let scan_range = ScanRange {
610 eq_conds: vec![],
611 range: match func_type {
612 ExprType::GreaterThan => {
613 (Bound::Excluded(pk_struct), Bound::Unbounded)
614 }
615 ExprType::LessThan => {
616 (Bound::Unbounded, Bound::Excluded(pk_struct))
617 }
618 _ => unreachable!(),
619 },
620 };
621 return Ok(Some((
622 vec![scan_range],
623 Condition {
624 conjunctions: row_conjunctions_without_struct,
625 },
626 )));
627 }
628 }
629 }
630 }
631 Ok(None)
632 }
633
634 pub fn get_eq_const_input_refs(&self) -> Vec<InputRef> {
636 self.conjunctions
637 .iter()
638 .filter_map(|expr| expr.as_eq_const().map(|(input_ref, _)| input_ref))
639 .collect()
640 }
641
642 pub fn split_to_scan_ranges(
644 self,
645 table: &TableCatalog,
646 max_split_range_gap: u64,
647 ) -> Result<(Vec<ScanRange>, Self)> {
648 fn false_cond() -> (Vec<ScanRange>, Condition) {
649 (vec![], Condition::false_cond())
650 }
651
652 if self.conjunctions.len() == 1
654 && let Some(disjunctions) = self.conjunctions[0].as_or_disjunctions()
655 {
656 if let Some((scan_ranges, maintaining_condition)) =
657 Self::disjunctions_to_scan_ranges(table, max_split_range_gap, disjunctions)?
658 {
659 if maintaining_condition {
660 return Ok((scan_ranges, self));
661 } else {
662 return Ok((scan_ranges, Condition::true_cond()));
663 }
664 } else {
665 return Ok((vec![], self));
666 }
667 }
668 if let Some((scan_ranges, other_condition)) = self.split_row_cmp_to_scan_ranges(table)? {
669 return Ok((scan_ranges, other_condition));
670 }
671
672 let mut groups = Self::classify_conjunctions_by_pk(self.conjunctions, table);
673 let mut other_conds = groups.pop().unwrap();
674
675 let mut scan_range = ScanRange::full_table_scan();
677 for i in 0..table.pk.len() {
678 let group = std::mem::take(&mut groups[i]);
679 if group.is_empty() {
680 groups.push(other_conds);
681 return Ok((
682 if scan_range.is_full_table_scan() {
683 vec![]
684 } else {
685 vec![scan_range]
686 },
687 Self {
688 conjunctions: groups[i + 1..].concat(),
689 },
690 ));
691 }
692
693 let Some((
694 lower_bound_conjunctions,
695 upper_bound_conjunctions,
696 eq_conds,
697 part_of_other_conds,
698 )) = Self::analyze_group(group)?
699 else {
700 return Ok(false_cond());
701 };
702 other_conds.extend(part_of_other_conds.into_iter());
703
704 let lower_bound = Self::merge_lower_bound_conjunctions(lower_bound_conjunctions);
705 let upper_bound = Self::merge_upper_bound_conjunctions(upper_bound_conjunctions);
706
707 if Self::is_invalid_range(&lower_bound, &upper_bound) {
708 return Ok(false_cond());
709 }
710
711 match eq_conds.len() {
713 1 => {
714 let eq_conds =
715 Self::extract_eq_conds_within_range(eq_conds, &upper_bound, &lower_bound);
716 if eq_conds.is_empty() {
717 return Ok(false_cond());
718 }
719 scan_range.eq_conds.extend(eq_conds.into_iter());
720 }
721 0 => {
722 let convert = |bound| match bound {
723 Bound::Included(l) => Bound::Included(vec![Some(l)]),
724 Bound::Excluded(l) => Bound::Excluded(vec![Some(l)]),
725 Bound::Unbounded => Bound::Unbounded,
726 };
727 scan_range.range = (convert(lower_bound), convert(upper_bound));
728 other_conds.extend(groups[i + 1..].iter().flatten().cloned());
729 break;
730 }
731 _ => {
732 let eq_conds =
739 Self::extract_eq_conds_within_range(eq_conds, &upper_bound, &lower_bound);
740 if eq_conds.is_empty() {
741 return Ok(false_cond());
742 }
743 other_conds.extend(groups[i + 1..].iter().flatten().cloned());
744 let scan_ranges = eq_conds
745 .into_iter()
746 .map(|lit| {
747 let mut scan_range = scan_range.clone();
748 scan_range.eq_conds.push(lit);
749 scan_range
750 })
751 .collect();
752 return Ok((
753 scan_ranges,
754 Self {
755 conjunctions: other_conds,
756 },
757 ));
758 }
759 }
760 }
761
762 Ok((
763 if scan_range.is_full_table_scan() {
764 vec![]
765 } else if table.columns[table.pk[0].column_index].data_type.is_int() {
766 match scan_range.split_small_range(max_split_range_gap) {
767 Some(scan_ranges) => scan_ranges,
768 None => vec![scan_range],
769 }
770 } else {
771 vec![scan_range]
772 },
773 Self {
774 conjunctions: other_conds,
775 },
776 ))
777 }
778
779 fn classify_conjunctions_by_pk(
783 conjunctions: Vec<ExprImpl>,
784 table: &TableCatalog,
785 ) -> Vec<Vec<ExprImpl>> {
786 let pk_cols_num = table.pk.len();
787 let cols_num = table.columns.len();
788
789 let mut col_idx_to_pk_idx = vec![None; cols_num];
790 table
791 .order_column_indices()
792 .enumerate()
793 .for_each(|(idx, pk_idx)| {
794 col_idx_to_pk_idx[pk_idx] = Some(idx);
795 });
796
797 let mut groups = vec![vec![]; pk_cols_num + 1];
798 for (key, group) in &conjunctions.into_iter().chunk_by(|expr| {
799 let input_bits = expr.collect_input_refs(cols_num);
800 if input_bits.count_ones(..) == 1 {
801 let col_idx = input_bits.ones().next().unwrap();
802 col_idx_to_pk_idx[col_idx].unwrap_or(pk_cols_num)
803 } else {
804 pk_cols_num
805 }
806 }) {
807 groups[key].extend(group);
808 }
809
810 groups
811 }
812
813 #[allow(clippy::type_complexity)]
821 fn analyze_group(
822 group: Vec<ExprImpl>,
823 ) -> Result<
824 Option<(
825 Vec<Bound<ScalarImpl>>,
826 Vec<Bound<ScalarImpl>>,
827 Vec<Option<ScalarImpl>>,
828 Vec<ExprImpl>,
829 )>,
830 > {
831 let mut lower_bound_conjunctions = vec![];
832 let mut upper_bound_conjunctions = vec![];
833 let mut eq_conds = vec![];
835 let mut other_conds = vec![];
836
837 for expr in group {
839 if let Some((input_ref, const_expr)) = expr.as_eq_const() {
840 let new_expr = if let Ok(expr) =
841 const_expr.clone().cast_implicit(&input_ref.data_type)
842 {
843 expr
844 } else {
845 match self::cast_compare::cast_compare_for_eq(const_expr, input_ref.data_type) {
846 Ok(ResultForEq::Success(expr)) => expr,
847 Ok(ResultForEq::NeverEqual) => {
848 return Ok(None);
849 }
850 Err(_) => {
851 other_conds.push(expr);
852 continue;
853 }
854 }
855 };
856
857 let Some(new_cond) = new_expr.fold_const()? else {
858 return Ok(None);
860 };
861 if Self::mutual_exclusive_with_eq_conds(&new_cond, &eq_conds) {
862 return Ok(None);
863 }
864 eq_conds = vec![Some(new_cond)];
865 } else if expr.as_is_null().is_some() {
866 if !eq_conds.is_empty() && eq_conds.into_iter().all(|l| l.is_some()) {
867 return Ok(None);
868 }
869 eq_conds = vec![None];
870 } else if let Some((input_ref, in_const_list)) = expr.as_in_const_list() {
871 let mut scalars = HashSet::new();
872 for const_expr in in_const_list {
873 let const_expr = const_expr.cast_implicit(&input_ref.data_type).unwrap();
876 let value = const_expr.fold_const()?;
877 let Some(value) = value else {
878 continue;
879 };
880 scalars.insert(Some(value));
881 }
882 if scalars.is_empty() {
883 return Ok(None);
885 }
886 if !eq_conds.is_empty() {
887 scalars = scalars
888 .intersection(&HashSet::from_iter(eq_conds))
889 .cloned()
890 .collect();
891 if scalars.is_empty() {
892 return Ok(None);
893 }
894 }
895 eq_conds = scalars
897 .into_iter()
898 .sorted_by(DefaultOrd::default_cmp)
899 .collect();
900 } else if let Some((input_ref, op, const_expr)) = expr.as_comparison_const() {
901 let new_expr =
902 if let Ok(expr) = const_expr.clone().cast_implicit(&input_ref.data_type) {
903 expr
904 } else {
905 match self::cast_compare::cast_compare_for_cmp(
906 const_expr,
907 input_ref.data_type,
908 op,
909 ) {
910 Ok(ResultForCmp::Success(expr)) => expr,
911 _ => {
912 other_conds.push(expr);
913 continue;
914 }
915 }
916 };
917 let Some(value) = new_expr.fold_const()? else {
918 return Ok(None);
920 };
921 match op {
922 ExprType::LessThan => {
923 upper_bound_conjunctions.push(Bound::Excluded(value));
924 }
925 ExprType::LessThanOrEqual => {
926 upper_bound_conjunctions.push(Bound::Included(value));
927 }
928 ExprType::GreaterThan => {
929 lower_bound_conjunctions.push(Bound::Excluded(value));
930 }
931 ExprType::GreaterThanOrEqual => {
932 lower_bound_conjunctions.push(Bound::Included(value));
933 }
934 _ => unreachable!(),
935 }
936 } else {
937 other_conds.push(expr);
938 }
939 }
940 Ok(Some((
941 lower_bound_conjunctions,
942 upper_bound_conjunctions,
943 eq_conds,
944 other_conds,
945 )))
946 }
947
948 fn mutual_exclusive_with_eq_conds(
949 new_conds: &ScalarImpl,
950 eq_conds: &[Option<ScalarImpl>],
951 ) -> bool {
952 !eq_conds.is_empty()
953 && eq_conds.iter().all(|l| {
954 if let Some(l) = l {
955 l != new_conds
956 } else {
957 true
958 }
959 })
960 }
961
962 fn merge_lower_bound_conjunctions(lb: Vec<Bound<ScalarImpl>>) -> Bound<ScalarImpl> {
963 lb.into_iter()
964 .max_by(|a, b| {
965 match (a, b) {
967 (Bound::Included(_), Bound::Unbounded) => std::cmp::Ordering::Greater,
968 (Bound::Excluded(_), Bound::Unbounded) => std::cmp::Ordering::Greater,
969 (Bound::Unbounded, Bound::Included(_)) => std::cmp::Ordering::Less,
970 (Bound::Unbounded, Bound::Excluded(_)) => std::cmp::Ordering::Less,
971 (Bound::Unbounded, Bound::Unbounded) => std::cmp::Ordering::Equal,
972 (Bound::Included(a), Bound::Included(b)) => a.default_cmp(b),
973 (Bound::Excluded(a), Bound::Excluded(b)) => a.default_cmp(b),
974 (Bound::Included(a), Bound::Excluded(b)) => match a.default_cmp(b) {
976 std::cmp::Ordering::Equal => std::cmp::Ordering::Less,
977 other => other,
978 },
979 (Bound::Excluded(a), Bound::Included(b)) => match a.default_cmp(b) {
980 std::cmp::Ordering::Equal => std::cmp::Ordering::Greater,
981 other => other,
982 },
983 }
984 })
985 .unwrap_or(Bound::Unbounded)
986 }
987
988 fn merge_upper_bound_conjunctions(ub: Vec<Bound<ScalarImpl>>) -> Bound<ScalarImpl> {
989 ub.into_iter()
990 .min_by(|a, b| {
991 match (a, b) {
993 (Bound::Included(_), Bound::Unbounded) => std::cmp::Ordering::Less,
994 (Bound::Excluded(_), Bound::Unbounded) => std::cmp::Ordering::Less,
995 (Bound::Unbounded, Bound::Included(_)) => std::cmp::Ordering::Greater,
996 (Bound::Unbounded, Bound::Excluded(_)) => std::cmp::Ordering::Greater,
997 (Bound::Unbounded, Bound::Unbounded) => std::cmp::Ordering::Equal,
998 (Bound::Included(a), Bound::Included(b)) => a.default_cmp(b),
999 (Bound::Excluded(a), Bound::Excluded(b)) => a.default_cmp(b),
1000 (Bound::Included(a), Bound::Excluded(b)) => match a.default_cmp(b) {
1002 std::cmp::Ordering::Equal => std::cmp::Ordering::Greater,
1003 other => other,
1004 },
1005 (Bound::Excluded(a), Bound::Included(b)) => match a.default_cmp(b) {
1006 std::cmp::Ordering::Equal => std::cmp::Ordering::Less,
1007 other => other,
1008 },
1009 }
1010 })
1011 .unwrap_or(Bound::Unbounded)
1012 }
1013
1014 fn is_invalid_range(lower_bound: &Bound<ScalarImpl>, upper_bound: &Bound<ScalarImpl>) -> bool {
1015 match (lower_bound, upper_bound) {
1016 (Bound::Included(l), Bound::Included(u)) => l.default_cmp(u).is_gt(), (Bound::Included(l), Bound::Excluded(u)) => l.default_cmp(u).is_ge(), (Bound::Excluded(l), Bound::Included(u)) => l.default_cmp(u).is_ge(), (Bound::Excluded(l), Bound::Excluded(u)) => l.default_cmp(u).is_ge(), _ => false,
1021 }
1022 }
1023
1024 fn extract_eq_conds_within_range(
1025 eq_conds: Vec<Option<ScalarImpl>>,
1026 upper_bound: &Bound<ScalarImpl>,
1027 lower_bound: &Bound<ScalarImpl>,
1028 ) -> Vec<Option<ScalarImpl>> {
1029 if Self::is_invalid_range(lower_bound, upper_bound) {
1032 return vec![];
1033 }
1034
1035 let is_extract_null = upper_bound == &Bound::Unbounded && lower_bound == &Bound::Unbounded;
1036
1037 eq_conds
1038 .into_iter()
1039 .filter(|cond| {
1040 if let Some(cond) = cond {
1041 match lower_bound {
1042 Bound::Included(val) => {
1043 if cond.default_cmp(val).is_lt() {
1044 return false;
1046 }
1047 }
1048 Bound::Excluded(val) => {
1049 if cond.default_cmp(val).is_le() {
1050 return false;
1052 }
1053 }
1054 Bound::Unbounded => {}
1055 }
1056 match upper_bound {
1057 Bound::Included(val) => {
1058 if cond.default_cmp(val).is_gt() {
1059 return false;
1061 }
1062 }
1063 Bound::Excluded(val) => {
1064 if cond.default_cmp(val).is_ge() {
1065 return false;
1067 }
1068 }
1069 Bound::Unbounded => {}
1070 }
1071 true
1072 } else {
1073 is_extract_null
1074 }
1075 })
1076 .collect()
1077 }
1078
1079 #[must_use]
1085 pub fn group_by<F, const N: usize>(self, f: F) -> [Self; N]
1086 where
1087 F: Fn(&ExprImpl) -> usize,
1088 {
1089 const EMPTY: Vec<ExprImpl> = vec![];
1090 let mut groups = [EMPTY; N];
1091 for (key, group) in &self.conjunctions.into_iter().chunk_by(|expr| {
1092 let i = f(expr);
1094 assert!(i < N);
1095 i
1096 }) {
1097 groups[key].extend(group);
1098 }
1099
1100 groups.map(|group| Condition {
1101 conjunctions: group,
1102 })
1103 }
1104
1105 #[must_use]
1106 pub fn rewrite_expr(self, rewriter: &mut (impl ExprRewriter + ?Sized)) -> Self {
1107 Self {
1108 conjunctions: self
1109 .conjunctions
1110 .into_iter()
1111 .map(|expr| rewriter.rewrite_expr(expr))
1112 .collect(),
1113 }
1114 .simplify()
1115 }
1116
1117 pub fn visit_expr<V: ExprVisitor + ?Sized>(&self, visitor: &mut V) {
1118 self.conjunctions
1119 .iter()
1120 .for_each(|expr| visitor.visit_expr(expr));
1121 }
1122
1123 pub fn visit_expr_mut(&mut self, mutator: &mut (impl ExprMutator + ?Sized)) {
1124 self.conjunctions
1125 .iter_mut()
1126 .for_each(|expr| mutator.visit_expr(expr))
1127 }
1128
1129 fn simplify(self) -> Self {
1132 let conjunctions: Vec<_> = self
1134 .conjunctions
1135 .into_iter()
1136 .map(push_down_not)
1137 .map(fold_boolean_constant)
1138 .map(column_self_eq_eliminate)
1139 .flat_map(to_conjunctions)
1140 .collect();
1141 let mut res: Vec<ExprImpl> = Vec::new();
1142 let mut visited: HashSet<ExprImpl> = HashSet::new();
1143 for expr in conjunctions {
1144 if !expr.has_subquery() {
1146 let results_of_factorization = factorization_expr(expr);
1147 res.extend(
1148 results_of_factorization
1149 .clone()
1150 .into_iter()
1151 .filter(|expr| !visited.contains(expr)),
1152 );
1153 visited.extend(results_of_factorization);
1154 } else {
1155 res.push(expr);
1157 }
1158 }
1159 res.retain(|expr| {
1161 if let Some(v) = try_get_bool_constant(expr)
1162 && v
1163 {
1164 false
1165 } else {
1166 true
1167 }
1168 });
1169 for expr in &mut res {
1171 if let Some(v) = try_get_bool_constant(expr)
1172 && !v
1173 {
1174 res.clear();
1175 res.push(ExprImpl::literal_bool(false));
1176 break;
1177 }
1178 }
1179 Self { conjunctions: res }
1180 }
1181}
1182
1183pub struct ConditionDisplay<'a> {
1184 pub condition: &'a Condition,
1185 pub input_schema: &'a Schema,
1186}
1187
1188impl ConditionDisplay<'_> {
1189 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1190 if self.condition.always_true() {
1191 write!(f, "true")
1192 } else {
1193 write!(
1194 f,
1195 "{}",
1196 self.condition
1197 .conjunctions
1198 .iter()
1199 .format_with(" AND ", |expr, f| {
1200 f(&ExprDisplay {
1201 expr,
1202 input_schema: self.input_schema,
1203 })
1204 })
1205 )
1206 }
1207 }
1208}
1209
1210impl fmt::Display for ConditionDisplay<'_> {
1211 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1212 self.fmt(f)
1213 }
1214}
1215
1216impl fmt::Debug for ConditionDisplay<'_> {
1217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1218 self.fmt(f)
1219 }
1220}
1221
1222mod cast_compare {
1227 use risingwave_common::types::DataType;
1228
1229 use crate::expr::{Expr, ExprImpl, ExprType};
1230
1231 enum ShrinkResult {
1232 OutUpperBound,
1233 OutLowerBound,
1234 InRange(ExprImpl),
1235 }
1236
1237 pub enum ResultForEq {
1238 Success(ExprImpl),
1239 NeverEqual,
1240 }
1241
1242 pub enum ResultForCmp {
1243 Success(ExprImpl),
1244 OutUpperBound,
1245 OutLowerBound,
1246 }
1247
1248 pub fn cast_compare_for_eq(const_expr: ExprImpl, target: DataType) -> Result<ResultForEq, ()> {
1249 match (const_expr.return_type(), &target) {
1250 (DataType::Int64, DataType::Int32)
1251 | (DataType::Int64, DataType::Int16)
1252 | (DataType::Int32, DataType::Int16) => match shrink_integral(const_expr, target)? {
1253 ShrinkResult::InRange(expr) => Ok(ResultForEq::Success(expr)),
1254 ShrinkResult::OutUpperBound | ShrinkResult::OutLowerBound => {
1255 Ok(ResultForEq::NeverEqual)
1256 }
1257 },
1258 _ => Err(()),
1259 }
1260 }
1261
1262 pub fn cast_compare_for_cmp(
1263 const_expr: ExprImpl,
1264 target: DataType,
1265 _op: ExprType,
1266 ) -> Result<ResultForCmp, ()> {
1267 match (const_expr.return_type(), &target) {
1268 (DataType::Int64, DataType::Int32)
1269 | (DataType::Int64, DataType::Int16)
1270 | (DataType::Int32, DataType::Int16) => match shrink_integral(const_expr, target)? {
1271 ShrinkResult::InRange(expr) => Ok(ResultForCmp::Success(expr)),
1272 ShrinkResult::OutUpperBound => Ok(ResultForCmp::OutUpperBound),
1273 ShrinkResult::OutLowerBound => Ok(ResultForCmp::OutLowerBound),
1274 },
1275 _ => Err(()),
1276 }
1277 }
1278
1279 fn shrink_integral(const_expr: ExprImpl, target: DataType) -> Result<ShrinkResult, ()> {
1280 let (upper_bound, lower_bound) = match (const_expr.return_type(), &target) {
1281 (DataType::Int64, DataType::Int32) => (i32::MAX as i64, i32::MIN as i64),
1282 (DataType::Int64, DataType::Int16) | (DataType::Int32, DataType::Int16) => {
1283 (i16::MAX as i64, i16::MIN as i64)
1284 }
1285 _ => unreachable!(),
1286 };
1287 match const_expr.fold_const().map_err(|_| ())? {
1288 Some(scalar) => {
1289 let value = scalar.as_integral();
1290 if value > upper_bound {
1291 Ok(ShrinkResult::OutUpperBound)
1292 } else if value < lower_bound {
1293 Ok(ShrinkResult::OutLowerBound)
1294 } else {
1295 Ok(ShrinkResult::InRange(
1296 const_expr.cast_explicit(&target).unwrap(),
1297 ))
1298 }
1299 }
1300 None => Ok(ShrinkResult::InRange(
1301 const_expr.cast_explicit(&target).unwrap(),
1302 )),
1303 }
1304 }
1305}
1306
1307#[cfg(test)]
1308mod tests {
1309 use rand::Rng;
1310
1311 use super::*;
1312
1313 #[test]
1314 fn test_split() {
1315 let left_col_num = 3;
1316 let right_col_num = 2;
1317
1318 let ty = DataType::Int32;
1319
1320 let mut rng = rand::rng();
1321
1322 let left: ExprImpl = FunctionCall::new(
1323 ExprType::LessThanOrEqual,
1324 vec![
1325 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1326 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1327 ],
1328 )
1329 .unwrap()
1330 .into();
1331
1332 let right: ExprImpl = FunctionCall::new(
1333 ExprType::LessThan,
1334 vec![
1335 InputRef::new(
1336 rng.random_range(left_col_num..left_col_num + right_col_num),
1337 ty.clone(),
1338 )
1339 .into(),
1340 InputRef::new(
1341 rng.random_range(left_col_num..left_col_num + right_col_num),
1342 ty.clone(),
1343 )
1344 .into(),
1345 ],
1346 )
1347 .unwrap()
1348 .into();
1349
1350 let other: ExprImpl = FunctionCall::new(
1351 ExprType::GreaterThan,
1352 vec![
1353 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1354 InputRef::new(
1355 rng.random_range(left_col_num..left_col_num + right_col_num),
1356 ty,
1357 )
1358 .into(),
1359 ],
1360 )
1361 .unwrap()
1362 .into();
1363
1364 let cond = Condition::with_expr(other.clone())
1365 .and(Condition::with_expr(right.clone()))
1366 .and(Condition::with_expr(left.clone()));
1367
1368 let res = cond.split(left_col_num, right_col_num);
1369
1370 assert_eq!(res.0.conjunctions, vec![left]);
1371 assert_eq!(res.1.conjunctions, vec![right]);
1372 assert_eq!(res.2.conjunctions, vec![other]);
1373 }
1374
1375 #[test]
1376 fn test_self_eq_eliminate() {
1377 let left_col_num = 3;
1378 let right_col_num = 2;
1379
1380 let ty = DataType::Int32;
1381
1382 let mut rng = rand::rng();
1383
1384 let x: ExprImpl = InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into();
1385
1386 let left: ExprImpl = FunctionCall::new(ExprType::Equal, vec![x.clone(), x.clone()])
1387 .unwrap()
1388 .into();
1389
1390 let right: ExprImpl = FunctionCall::new(
1391 ExprType::LessThan,
1392 vec![
1393 InputRef::new(
1394 rng.random_range(left_col_num..left_col_num + right_col_num),
1395 ty.clone(),
1396 )
1397 .into(),
1398 InputRef::new(
1399 rng.random_range(left_col_num..left_col_num + right_col_num),
1400 ty.clone(),
1401 )
1402 .into(),
1403 ],
1404 )
1405 .unwrap()
1406 .into();
1407
1408 let other: ExprImpl = FunctionCall::new(
1409 ExprType::GreaterThan,
1410 vec![
1411 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1412 InputRef::new(
1413 rng.random_range(left_col_num..left_col_num + right_col_num),
1414 ty,
1415 )
1416 .into(),
1417 ],
1418 )
1419 .unwrap()
1420 .into();
1421
1422 let cond = Condition::with_expr(other.clone())
1423 .and(Condition::with_expr(right.clone()))
1424 .and(Condition::with_expr(left.clone()));
1425
1426 let res = cond.split(left_col_num, right_col_num);
1427
1428 let left_res = FunctionCall::new(ExprType::IsNotNull, vec![x])
1429 .unwrap()
1430 .into();
1431
1432 assert_eq!(res.0.conjunctions, vec![left_res]);
1433 assert_eq!(res.1.conjunctions, vec![right]);
1434 assert_eq!(res.2.conjunctions, vec![other]);
1435 }
1436}