1use std::cmp::Ordering;
16use std::collections::{BTreeMap, HashSet};
17use std::fmt::{self, Debug};
18use std::ops::Bound;
19use std::rc::Rc;
20use std::sync::LazyLock;
21
22use fixedbitset::FixedBitSet;
23use itertools::Itertools;
24use risingwave_common::catalog::{Schema, TableDesc};
25use risingwave_common::types::{DataType, DefaultOrd, ScalarImpl};
26use risingwave_common::util::iter_util::ZipEqFast;
27use risingwave_common::util::scan_range::{ScanRange, is_full_range};
28use risingwave_common::util::sort_util::{OrderType, cmp_rows};
29
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_desc: Rc<TableDesc>,
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_desc.clone(), 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_desc
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_desc: Rc<TableDesc>,
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_desc.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_desc: Rc<TableDesc>,
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_desc, 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)) =
669 self.split_row_cmp_to_scan_ranges(table_desc.clone())?
670 {
671 return Ok((scan_ranges, other_condition));
672 }
673
674 let mut groups = Self::classify_conjunctions_by_pk(self.conjunctions, &table_desc);
675 let mut other_conds = groups.pop().unwrap();
676
677 let mut scan_range = ScanRange::full_table_scan();
679 for i in 0..table_desc.order_column_indices().len() {
680 let group = std::mem::take(&mut groups[i]);
681 if group.is_empty() {
682 groups.push(other_conds);
683 return Ok((
684 if scan_range.is_full_table_scan() {
685 vec![]
686 } else {
687 vec![scan_range]
688 },
689 Self {
690 conjunctions: groups[i + 1..].concat(),
691 },
692 ));
693 }
694
695 let Some((
696 lower_bound_conjunctions,
697 upper_bound_conjunctions,
698 eq_conds,
699 part_of_other_conds,
700 )) = Self::analyze_group(group)?
701 else {
702 return Ok(false_cond());
703 };
704 other_conds.extend(part_of_other_conds.into_iter());
705
706 let lower_bound = Self::merge_lower_bound_conjunctions(lower_bound_conjunctions);
707 let upper_bound = Self::merge_upper_bound_conjunctions(upper_bound_conjunctions);
708
709 if Self::is_invalid_range(&lower_bound, &upper_bound) {
710 return Ok(false_cond());
711 }
712
713 match eq_conds.len() {
715 1 => {
716 let eq_conds =
717 Self::extract_eq_conds_within_range(eq_conds, &upper_bound, &lower_bound);
718 if eq_conds.is_empty() {
719 return Ok(false_cond());
720 }
721 scan_range.eq_conds.extend(eq_conds.into_iter());
722 }
723 0 => {
724 let convert = |bound| match bound {
725 Bound::Included(l) => Bound::Included(vec![Some(l)]),
726 Bound::Excluded(l) => Bound::Excluded(vec![Some(l)]),
727 Bound::Unbounded => Bound::Unbounded,
728 };
729 scan_range.range = (convert(lower_bound), convert(upper_bound));
730 other_conds.extend(groups[i + 1..].iter().flatten().cloned());
731 break;
732 }
733 _ => {
734 let eq_conds =
741 Self::extract_eq_conds_within_range(eq_conds, &upper_bound, &lower_bound);
742 if eq_conds.is_empty() {
743 return Ok(false_cond());
744 }
745 other_conds.extend(groups[i + 1..].iter().flatten().cloned());
746 let scan_ranges = eq_conds
747 .into_iter()
748 .map(|lit| {
749 let mut scan_range = scan_range.clone();
750 scan_range.eq_conds.push(lit);
751 scan_range
752 })
753 .collect();
754 return Ok((
755 scan_ranges,
756 Self {
757 conjunctions: other_conds,
758 },
759 ));
760 }
761 }
762 }
763
764 Ok((
765 if scan_range.is_full_table_scan() {
766 vec![]
767 } else if table_desc.columns[table_desc.order_column_indices()[0]]
768 .data_type
769 .is_int()
770 {
771 match scan_range.split_small_range(max_split_range_gap) {
772 Some(scan_ranges) => scan_ranges,
773 None => vec![scan_range],
774 }
775 } else {
776 vec![scan_range]
777 },
778 Self {
779 conjunctions: other_conds,
780 },
781 ))
782 }
783
784 fn classify_conjunctions_by_pk(
788 conjunctions: Vec<ExprImpl>,
789 table_desc: &Rc<TableDesc>,
790 ) -> Vec<Vec<ExprImpl>> {
791 let pk_column_ids = &table_desc.order_column_indices();
792 let pk_cols_num = pk_column_ids.len();
793 let cols_num = table_desc.columns.len();
794
795 let mut col_idx_to_pk_idx = vec![None; cols_num];
796 pk_column_ids.iter().enumerate().for_each(|(idx, pk_idx)| {
797 col_idx_to_pk_idx[*pk_idx] = Some(idx);
798 });
799
800 let mut groups = vec![vec![]; pk_cols_num + 1];
801 for (key, group) in &conjunctions.into_iter().chunk_by(|expr| {
802 let input_bits = expr.collect_input_refs(cols_num);
803 if input_bits.count_ones(..) == 1 {
804 let col_idx = input_bits.ones().next().unwrap();
805 col_idx_to_pk_idx[col_idx].unwrap_or(pk_cols_num)
806 } else {
807 pk_cols_num
808 }
809 }) {
810 groups[key].extend(group);
811 }
812
813 groups
814 }
815
816 #[allow(clippy::type_complexity)]
824 fn analyze_group(
825 group: Vec<ExprImpl>,
826 ) -> Result<
827 Option<(
828 Vec<Bound<ScalarImpl>>,
829 Vec<Bound<ScalarImpl>>,
830 Vec<Option<ScalarImpl>>,
831 Vec<ExprImpl>,
832 )>,
833 > {
834 let mut lower_bound_conjunctions = vec![];
835 let mut upper_bound_conjunctions = vec![];
836 let mut eq_conds = vec![];
838 let mut other_conds = vec![];
839
840 for expr in group {
842 if let Some((input_ref, const_expr)) = expr.as_eq_const() {
843 let new_expr = if let Ok(expr) = const_expr
844 .clone()
845 .cast_implicit(input_ref.data_type.clone())
846 {
847 expr
848 } else {
849 match self::cast_compare::cast_compare_for_eq(const_expr, input_ref.data_type) {
850 Ok(ResultForEq::Success(expr)) => expr,
851 Ok(ResultForEq::NeverEqual) => {
852 return Ok(None);
853 }
854 Err(_) => {
855 other_conds.push(expr);
856 continue;
857 }
858 }
859 };
860
861 let Some(new_cond) = new_expr.fold_const()? else {
862 return Ok(None);
864 };
865 if Self::mutual_exclusive_with_eq_conds(&new_cond, &eq_conds) {
866 return Ok(None);
867 }
868 eq_conds = vec![Some(new_cond)];
869 } else if expr.as_is_null().is_some() {
870 if !eq_conds.is_empty() && eq_conds.into_iter().all(|l| l.is_some()) {
871 return Ok(None);
872 }
873 eq_conds = vec![None];
874 } else if let Some((input_ref, in_const_list)) = expr.as_in_const_list() {
875 let mut scalars = HashSet::new();
876 for const_expr in in_const_list {
877 let const_expr = const_expr
880 .cast_implicit(input_ref.data_type.clone())
881 .unwrap();
882 let value = const_expr.fold_const()?;
883 let Some(value) = value else {
884 continue;
885 };
886 scalars.insert(Some(value));
887 }
888 if scalars.is_empty() {
889 return Ok(None);
891 }
892 if !eq_conds.is_empty() {
893 scalars = scalars
894 .intersection(&HashSet::from_iter(eq_conds))
895 .cloned()
896 .collect();
897 if scalars.is_empty() {
898 return Ok(None);
899 }
900 }
901 eq_conds = scalars
903 .into_iter()
904 .sorted_by(DefaultOrd::default_cmp)
905 .collect();
906 } else if let Some((input_ref, op, const_expr)) = expr.as_comparison_const() {
907 let new_expr = if let Ok(expr) = const_expr
908 .clone()
909 .cast_implicit(input_ref.data_type.clone())
910 {
911 expr
912 } else {
913 match self::cast_compare::cast_compare_for_cmp(
914 const_expr,
915 input_ref.data_type,
916 op,
917 ) {
918 Ok(ResultForCmp::Success(expr)) => expr,
919 _ => {
920 other_conds.push(expr);
921 continue;
922 }
923 }
924 };
925 let Some(value) = new_expr.fold_const()? else {
926 return Ok(None);
928 };
929 match op {
930 ExprType::LessThan => {
931 upper_bound_conjunctions.push(Bound::Excluded(value));
932 }
933 ExprType::LessThanOrEqual => {
934 upper_bound_conjunctions.push(Bound::Included(value));
935 }
936 ExprType::GreaterThan => {
937 lower_bound_conjunctions.push(Bound::Excluded(value));
938 }
939 ExprType::GreaterThanOrEqual => {
940 lower_bound_conjunctions.push(Bound::Included(value));
941 }
942 _ => unreachable!(),
943 }
944 } else {
945 other_conds.push(expr);
946 }
947 }
948 Ok(Some((
949 lower_bound_conjunctions,
950 upper_bound_conjunctions,
951 eq_conds,
952 other_conds,
953 )))
954 }
955
956 fn mutual_exclusive_with_eq_conds(
957 new_conds: &ScalarImpl,
958 eq_conds: &[Option<ScalarImpl>],
959 ) -> bool {
960 !eq_conds.is_empty()
961 && eq_conds.iter().all(|l| {
962 if let Some(l) = l {
963 l != new_conds
964 } else {
965 true
966 }
967 })
968 }
969
970 fn merge_lower_bound_conjunctions(lb: Vec<Bound<ScalarImpl>>) -> Bound<ScalarImpl> {
971 lb.into_iter()
972 .max_by(|a, b| {
973 match (a, b) {
975 (Bound::Included(_), Bound::Unbounded) => std::cmp::Ordering::Greater,
976 (Bound::Excluded(_), Bound::Unbounded) => std::cmp::Ordering::Greater,
977 (Bound::Unbounded, Bound::Included(_)) => std::cmp::Ordering::Less,
978 (Bound::Unbounded, Bound::Excluded(_)) => std::cmp::Ordering::Less,
979 (Bound::Unbounded, Bound::Unbounded) => std::cmp::Ordering::Equal,
980 (Bound::Included(a), Bound::Included(b)) => a.default_cmp(b),
981 (Bound::Excluded(a), Bound::Excluded(b)) => a.default_cmp(b),
982 (Bound::Included(a), Bound::Excluded(b)) => match a.default_cmp(b) {
984 std::cmp::Ordering::Equal => std::cmp::Ordering::Less,
985 other => other,
986 },
987 (Bound::Excluded(a), Bound::Included(b)) => match a.default_cmp(b) {
988 std::cmp::Ordering::Equal => std::cmp::Ordering::Greater,
989 other => other,
990 },
991 }
992 })
993 .unwrap_or(Bound::Unbounded)
994 }
995
996 fn merge_upper_bound_conjunctions(ub: Vec<Bound<ScalarImpl>>) -> Bound<ScalarImpl> {
997 ub.into_iter()
998 .min_by(|a, b| {
999 match (a, b) {
1001 (Bound::Included(_), Bound::Unbounded) => std::cmp::Ordering::Less,
1002 (Bound::Excluded(_), Bound::Unbounded) => std::cmp::Ordering::Less,
1003 (Bound::Unbounded, Bound::Included(_)) => std::cmp::Ordering::Greater,
1004 (Bound::Unbounded, Bound::Excluded(_)) => std::cmp::Ordering::Greater,
1005 (Bound::Unbounded, Bound::Unbounded) => std::cmp::Ordering::Equal,
1006 (Bound::Included(a), Bound::Included(b)) => a.default_cmp(b),
1007 (Bound::Excluded(a), Bound::Excluded(b)) => a.default_cmp(b),
1008 (Bound::Included(a), Bound::Excluded(b)) => match a.default_cmp(b) {
1010 std::cmp::Ordering::Equal => std::cmp::Ordering::Greater,
1011 other => other,
1012 },
1013 (Bound::Excluded(a), Bound::Included(b)) => match a.default_cmp(b) {
1014 std::cmp::Ordering::Equal => std::cmp::Ordering::Less,
1015 other => other,
1016 },
1017 }
1018 })
1019 .unwrap_or(Bound::Unbounded)
1020 }
1021
1022 fn is_invalid_range(lower_bound: &Bound<ScalarImpl>, upper_bound: &Bound<ScalarImpl>) -> bool {
1023 match (lower_bound, upper_bound) {
1024 (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,
1029 }
1030 }
1031
1032 fn extract_eq_conds_within_range(
1033 eq_conds: Vec<Option<ScalarImpl>>,
1034 upper_bound: &Bound<ScalarImpl>,
1035 lower_bound: &Bound<ScalarImpl>,
1036 ) -> Vec<Option<ScalarImpl>> {
1037 if Self::is_invalid_range(lower_bound, upper_bound) {
1040 return vec![];
1041 }
1042
1043 let is_extract_null = upper_bound == &Bound::Unbounded && lower_bound == &Bound::Unbounded;
1044
1045 eq_conds
1046 .into_iter()
1047 .filter(|cond| {
1048 if let Some(cond) = cond {
1049 match lower_bound {
1050 Bound::Included(val) => {
1051 if cond.default_cmp(val).is_lt() {
1052 return false;
1054 }
1055 }
1056 Bound::Excluded(val) => {
1057 if cond.default_cmp(val).is_le() {
1058 return false;
1060 }
1061 }
1062 Bound::Unbounded => {}
1063 }
1064 match upper_bound {
1065 Bound::Included(val) => {
1066 if cond.default_cmp(val).is_gt() {
1067 return false;
1069 }
1070 }
1071 Bound::Excluded(val) => {
1072 if cond.default_cmp(val).is_ge() {
1073 return false;
1075 }
1076 }
1077 Bound::Unbounded => {}
1078 }
1079 true
1080 } else {
1081 is_extract_null
1082 }
1083 })
1084 .collect()
1085 }
1086
1087 #[must_use]
1093 pub fn group_by<F, const N: usize>(self, f: F) -> [Self; N]
1094 where
1095 F: Fn(&ExprImpl) -> usize,
1096 {
1097 const EMPTY: Vec<ExprImpl> = vec![];
1098 let mut groups = [EMPTY; N];
1099 for (key, group) in &self.conjunctions.into_iter().chunk_by(|expr| {
1100 let i = f(expr);
1102 assert!(i < N);
1103 i
1104 }) {
1105 groups[key].extend(group);
1106 }
1107
1108 groups.map(|group| Condition {
1109 conjunctions: group,
1110 })
1111 }
1112
1113 #[must_use]
1114 pub fn rewrite_expr(self, rewriter: &mut (impl ExprRewriter + ?Sized)) -> Self {
1115 Self {
1116 conjunctions: self
1117 .conjunctions
1118 .into_iter()
1119 .map(|expr| rewriter.rewrite_expr(expr))
1120 .collect(),
1121 }
1122 .simplify()
1123 }
1124
1125 pub fn visit_expr<V: ExprVisitor + ?Sized>(&self, visitor: &mut V) {
1126 self.conjunctions
1127 .iter()
1128 .for_each(|expr| visitor.visit_expr(expr));
1129 }
1130
1131 pub fn visit_expr_mut(&mut self, mutator: &mut (impl ExprMutator + ?Sized)) {
1132 self.conjunctions
1133 .iter_mut()
1134 .for_each(|expr| mutator.visit_expr(expr))
1135 }
1136
1137 fn simplify(self) -> Self {
1140 let conjunctions: Vec<_> = self
1142 .conjunctions
1143 .into_iter()
1144 .map(push_down_not)
1145 .map(fold_boolean_constant)
1146 .map(column_self_eq_eliminate)
1147 .flat_map(to_conjunctions)
1148 .collect();
1149 let mut res: Vec<ExprImpl> = Vec::new();
1150 let mut visited: HashSet<ExprImpl> = HashSet::new();
1151 for expr in conjunctions {
1152 if !expr.has_subquery() {
1154 let results_of_factorization = factorization_expr(expr);
1155 res.extend(
1156 results_of_factorization
1157 .clone()
1158 .into_iter()
1159 .filter(|expr| !visited.contains(expr)),
1160 );
1161 visited.extend(results_of_factorization);
1162 } else {
1163 res.push(expr);
1165 }
1166 }
1167 res.retain(|expr| {
1169 if let Some(v) = try_get_bool_constant(expr)
1170 && v
1171 {
1172 false
1173 } else {
1174 true
1175 }
1176 });
1177 for expr in &mut res {
1179 if let Some(v) = try_get_bool_constant(expr)
1180 && !v
1181 {
1182 res.clear();
1183 res.push(ExprImpl::literal_bool(false));
1184 break;
1185 }
1186 }
1187 Self { conjunctions: res }
1188 }
1189}
1190
1191pub struct ConditionDisplay<'a> {
1192 pub condition: &'a Condition,
1193 pub input_schema: &'a Schema,
1194}
1195
1196impl ConditionDisplay<'_> {
1197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1198 if self.condition.always_true() {
1199 write!(f, "true")
1200 } else {
1201 write!(
1202 f,
1203 "{}",
1204 self.condition
1205 .conjunctions
1206 .iter()
1207 .format_with(" AND ", |expr, f| {
1208 f(&ExprDisplay {
1209 expr,
1210 input_schema: self.input_schema,
1211 })
1212 })
1213 )
1214 }
1215 }
1216}
1217
1218impl fmt::Display for ConditionDisplay<'_> {
1219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1220 self.fmt(f)
1221 }
1222}
1223
1224impl fmt::Debug for ConditionDisplay<'_> {
1225 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1226 self.fmt(f)
1227 }
1228}
1229
1230mod cast_compare {
1235 use risingwave_common::types::DataType;
1236
1237 use crate::expr::{Expr, ExprImpl, ExprType};
1238
1239 enum ShrinkResult {
1240 OutUpperBound,
1241 OutLowerBound,
1242 InRange(ExprImpl),
1243 }
1244
1245 pub enum ResultForEq {
1246 Success(ExprImpl),
1247 NeverEqual,
1248 }
1249
1250 pub enum ResultForCmp {
1251 Success(ExprImpl),
1252 OutUpperBound,
1253 OutLowerBound,
1254 }
1255
1256 pub fn cast_compare_for_eq(const_expr: ExprImpl, target: DataType) -> Result<ResultForEq, ()> {
1257 match (const_expr.return_type(), &target) {
1258 (DataType::Int64, DataType::Int32)
1259 | (DataType::Int64, DataType::Int16)
1260 | (DataType::Int32, DataType::Int16) => match shrink_integral(const_expr, target)? {
1261 ShrinkResult::InRange(expr) => Ok(ResultForEq::Success(expr)),
1262 ShrinkResult::OutUpperBound | ShrinkResult::OutLowerBound => {
1263 Ok(ResultForEq::NeverEqual)
1264 }
1265 },
1266 _ => Err(()),
1267 }
1268 }
1269
1270 pub fn cast_compare_for_cmp(
1271 const_expr: ExprImpl,
1272 target: DataType,
1273 _op: ExprType,
1274 ) -> Result<ResultForCmp, ()> {
1275 match (const_expr.return_type(), &target) {
1276 (DataType::Int64, DataType::Int32)
1277 | (DataType::Int64, DataType::Int16)
1278 | (DataType::Int32, DataType::Int16) => match shrink_integral(const_expr, target)? {
1279 ShrinkResult::InRange(expr) => Ok(ResultForCmp::Success(expr)),
1280 ShrinkResult::OutUpperBound => Ok(ResultForCmp::OutUpperBound),
1281 ShrinkResult::OutLowerBound => Ok(ResultForCmp::OutLowerBound),
1282 },
1283 _ => Err(()),
1284 }
1285 }
1286
1287 fn shrink_integral(const_expr: ExprImpl, target: DataType) -> Result<ShrinkResult, ()> {
1288 let (upper_bound, lower_bound) = match (const_expr.return_type(), &target) {
1289 (DataType::Int64, DataType::Int32) => (i32::MAX as i64, i32::MIN as i64),
1290 (DataType::Int64, DataType::Int16) | (DataType::Int32, DataType::Int16) => {
1291 (i16::MAX as i64, i16::MIN as i64)
1292 }
1293 _ => unreachable!(),
1294 };
1295 match const_expr.fold_const().map_err(|_| ())? {
1296 Some(scalar) => {
1297 let value = scalar.as_integral();
1298 if value > upper_bound {
1299 Ok(ShrinkResult::OutUpperBound)
1300 } else if value < lower_bound {
1301 Ok(ShrinkResult::OutLowerBound)
1302 } else {
1303 Ok(ShrinkResult::InRange(
1304 const_expr.cast_explicit(target).unwrap(),
1305 ))
1306 }
1307 }
1308 None => Ok(ShrinkResult::InRange(
1309 const_expr.cast_explicit(target).unwrap(),
1310 )),
1311 }
1312 }
1313}
1314
1315#[cfg(test)]
1316mod tests {
1317 use rand::Rng;
1318
1319 use super::*;
1320
1321 #[test]
1322 fn test_split() {
1323 let left_col_num = 3;
1324 let right_col_num = 2;
1325
1326 let ty = DataType::Int32;
1327
1328 let mut rng = rand::rng();
1329
1330 let left: ExprImpl = FunctionCall::new(
1331 ExprType::LessThanOrEqual,
1332 vec![
1333 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1334 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1335 ],
1336 )
1337 .unwrap()
1338 .into();
1339
1340 let right: ExprImpl = FunctionCall::new(
1341 ExprType::LessThan,
1342 vec![
1343 InputRef::new(
1344 rng.random_range(left_col_num..left_col_num + right_col_num),
1345 ty.clone(),
1346 )
1347 .into(),
1348 InputRef::new(
1349 rng.random_range(left_col_num..left_col_num + right_col_num),
1350 ty.clone(),
1351 )
1352 .into(),
1353 ],
1354 )
1355 .unwrap()
1356 .into();
1357
1358 let other: ExprImpl = FunctionCall::new(
1359 ExprType::GreaterThan,
1360 vec![
1361 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1362 InputRef::new(
1363 rng.random_range(left_col_num..left_col_num + right_col_num),
1364 ty,
1365 )
1366 .into(),
1367 ],
1368 )
1369 .unwrap()
1370 .into();
1371
1372 let cond = Condition::with_expr(other.clone())
1373 .and(Condition::with_expr(right.clone()))
1374 .and(Condition::with_expr(left.clone()));
1375
1376 let res = cond.split(left_col_num, right_col_num);
1377
1378 assert_eq!(res.0.conjunctions, vec![left]);
1379 assert_eq!(res.1.conjunctions, vec![right]);
1380 assert_eq!(res.2.conjunctions, vec![other]);
1381 }
1382
1383 #[test]
1384 fn test_self_eq_eliminate() {
1385 let left_col_num = 3;
1386 let right_col_num = 2;
1387
1388 let ty = DataType::Int32;
1389
1390 let mut rng = rand::rng();
1391
1392 let x: ExprImpl = InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into();
1393
1394 let left: ExprImpl = FunctionCall::new(ExprType::Equal, vec![x.clone(), x.clone()])
1395 .unwrap()
1396 .into();
1397
1398 let right: ExprImpl = FunctionCall::new(
1399 ExprType::LessThan,
1400 vec![
1401 InputRef::new(
1402 rng.random_range(left_col_num..left_col_num + right_col_num),
1403 ty.clone(),
1404 )
1405 .into(),
1406 InputRef::new(
1407 rng.random_range(left_col_num..left_col_num + right_col_num),
1408 ty.clone(),
1409 )
1410 .into(),
1411 ],
1412 )
1413 .unwrap()
1414 .into();
1415
1416 let other: ExprImpl = FunctionCall::new(
1417 ExprType::GreaterThan,
1418 vec![
1419 InputRef::new(rng.random_range(0..left_col_num), ty.clone()).into(),
1420 InputRef::new(
1421 rng.random_range(left_col_num..left_col_num + right_col_num),
1422 ty,
1423 )
1424 .into(),
1425 ],
1426 )
1427 .unwrap()
1428 .into();
1429
1430 let cond = Condition::with_expr(other.clone())
1431 .and(Condition::with_expr(right.clone()))
1432 .and(Condition::with_expr(left.clone()));
1433
1434 let res = cond.split(left_col_num, right_col_num);
1435
1436 let left_res = FunctionCall::new(ExprType::IsNotNull, vec![x])
1437 .unwrap()
1438 .into();
1439
1440 assert_eq!(res.0.conjunctions, vec![left_res]);
1441 assert_eq!(res.1.conjunctions, vec![right]);
1442 assert_eq!(res.2.conjunctions, vec![other]);
1443 }
1444}