datafusion_physical_expr/equivalence/ordering.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::fmt::Display;
use std::hash::Hash;
use std::sync::Arc;
use std::vec::IntoIter;
use crate::equivalence::add_offset_to_expr;
use crate::{LexOrdering, PhysicalExpr};
use arrow_schema::SortOptions;
/// An `OrderingEquivalenceClass` object keeps track of different alternative
/// orderings than can describe a schema. For example, consider the following table:
///
/// ```text
/// |a|b|c|d|
/// |1|4|3|1|
/// |2|3|3|2|
/// |3|1|2|2|
/// |3|2|1|3|
/// ```
///
/// Here, both `vec![a ASC, b ASC]` and `vec![c DESC, d ASC]` describe the table
/// ordering. In this case, we say that these orderings are equivalent.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
pub struct OrderingEquivalenceClass {
pub orderings: Vec<LexOrdering>,
}
impl OrderingEquivalenceClass {
/// Creates new empty ordering equivalence class.
pub fn empty() -> Self {
Default::default()
}
/// Clears (empties) this ordering equivalence class.
pub fn clear(&mut self) {
self.orderings.clear();
}
/// Creates new ordering equivalence class from the given orderings.
pub fn new(orderings: Vec<LexOrdering>) -> Self {
let mut result = Self { orderings };
result.remove_redundant_entries();
result
}
/// Checks whether `ordering` is a member of this equivalence class.
pub fn contains(&self, ordering: &LexOrdering) -> bool {
self.orderings.contains(ordering)
}
/// Adds `ordering` to this equivalence class.
#[allow(dead_code)]
fn push(&mut self, ordering: LexOrdering) {
self.orderings.push(ordering);
// Make sure that there are no redundant orderings:
self.remove_redundant_entries();
}
/// Checks whether this ordering equivalence class is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns an iterator over the equivalent orderings in this class.
pub fn iter(&self) -> impl Iterator<Item = &LexOrdering> {
self.orderings.iter()
}
/// Returns how many equivalent orderings there are in this class.
pub fn len(&self) -> usize {
self.orderings.len()
}
/// Extend this ordering equivalence class with the `other` class.
pub fn extend(&mut self, other: Self) {
self.orderings.extend(other.orderings);
// Make sure that there are no redundant orderings:
self.remove_redundant_entries();
}
/// Adds new orderings into this ordering equivalence class.
pub fn add_new_orderings(
&mut self,
orderings: impl IntoIterator<Item = LexOrdering>,
) {
self.orderings.extend(orderings);
// Make sure that there are no redundant orderings:
self.remove_redundant_entries();
}
/// Adds a single ordering to the existing ordering equivalence class.
pub fn add_new_ordering(&mut self, ordering: LexOrdering) {
self.add_new_orderings([ordering]);
}
/// Removes redundant orderings from this equivalence class. For instance,
/// if we already have the ordering `[a ASC, b ASC, c DESC]`, then there is
/// no need to keep ordering `[a ASC, b ASC]` in the state.
fn remove_redundant_entries(&mut self) {
let mut work = true;
while work {
work = false;
let mut idx = 0;
while idx < self.orderings.len() {
let mut ordering_idx = idx + 1;
let mut removal = self.orderings[idx].is_empty();
while ordering_idx < self.orderings.len() {
work |= resolve_overlap(&mut self.orderings, idx, ordering_idx);
if self.orderings[idx].is_empty() {
removal = true;
break;
}
work |= resolve_overlap(&mut self.orderings, ordering_idx, idx);
if self.orderings[ordering_idx].is_empty() {
self.orderings.swap_remove(ordering_idx);
} else {
ordering_idx += 1;
}
}
if removal {
self.orderings.swap_remove(idx);
} else {
idx += 1;
}
}
}
}
/// Returns the concatenation of all the orderings. This enables merge
/// operations to preserve all equivalent orderings simultaneously.
pub fn output_ordering(&self) -> Option<LexOrdering> {
let output_ordering = self
.orderings
.iter()
.flat_map(|ordering| ordering.as_ref())
.cloned()
.collect();
let output_ordering = collapse_lex_ordering(output_ordering);
(!output_ordering.is_empty()).then_some(output_ordering)
}
// Append orderings in `other` to all existing orderings in this equivalence
// class.
pub fn join_suffix(mut self, other: &Self) -> Self {
let n_ordering = self.orderings.len();
// Replicate entries before cross product
let n_cross = std::cmp::max(n_ordering, other.len() * n_ordering);
self.orderings = self
.orderings
.iter()
.cloned()
.cycle()
.take(n_cross)
.collect();
// Suffix orderings of other to the current orderings.
for (outer_idx, ordering) in other.iter().enumerate() {
for idx in 0..n_ordering {
// Calculate cross product index
let idx = outer_idx * n_ordering + idx;
self.orderings[idx].inner.extend(ordering.iter().cloned());
}
}
self
}
/// Adds `offset` value to the index of each expression inside this
/// ordering equivalence class.
pub fn add_offset(&mut self, offset: usize) {
for ordering in self.orderings.iter_mut() {
for sort_expr in ordering.inner.iter_mut() {
sort_expr.expr = add_offset_to_expr(Arc::clone(&sort_expr.expr), offset);
}
}
}
/// Gets sort options associated with this expression if it is a leading
/// ordering expression. Otherwise, returns `None`.
pub fn get_options(&self, expr: &Arc<dyn PhysicalExpr>) -> Option<SortOptions> {
for ordering in self.iter() {
let leading_ordering = &ordering[0];
if leading_ordering.expr.eq(expr) {
return Some(leading_ordering.options);
}
}
None
}
}
impl IntoIterator for OrderingEquivalenceClass {
type Item = LexOrdering;
type IntoIter = IntoIter<LexOrdering>;
fn into_iter(self) -> Self::IntoIter {
self.orderings.into_iter()
}
}
/// This function constructs a duplicate-free `LexOrdering` by filtering out
/// duplicate entries that have same physical expression inside. For example,
/// `vec![a ASC, a DESC]` collapses to `vec![a ASC]`.
pub fn collapse_lex_ordering(input: LexOrdering) -> LexOrdering {
let mut output = LexOrdering::default();
for item in input.iter() {
if !output.iter().any(|req| req.expr.eq(&item.expr)) {
output.push(item.clone());
}
}
output
}
/// Trims `orderings[idx]` if some suffix of it overlaps with a prefix of
/// `orderings[pre_idx]`. Returns `true` if there is any overlap, `false` otherwise.
fn resolve_overlap(orderings: &mut [LexOrdering], idx: usize, pre_idx: usize) -> bool {
let length = orderings[idx].len();
let other_length = orderings[pre_idx].len();
for overlap in 1..=length.min(other_length) {
if orderings[idx][length - overlap..] == orderings[pre_idx][..overlap] {
orderings[idx].truncate(length - overlap);
return true;
}
}
false
}
impl Display for OrderingEquivalenceClass {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[")?;
let mut iter = self.orderings.iter();
if let Some(ordering) = iter.next() {
write!(f, "[{}]", ordering)?;
}
for ordering in iter {
write!(f, ", [{}]", ordering)?;
}
write!(f, "]")?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::equivalence::tests::{
convert_to_orderings, convert_to_sort_exprs, create_test_schema,
};
use crate::equivalence::{
EquivalenceClass, EquivalenceGroup, EquivalenceProperties,
OrderingEquivalenceClass,
};
use crate::expressions::{col, BinaryExpr, Column};
use crate::utils::tests::TestScalarUDF;
use crate::{ConstExpr, PhysicalExpr, PhysicalSortExpr};
use arrow::datatypes::{DataType, Field, Schema};
use arrow_schema::SortOptions;
use datafusion_common::{DFSchema, Result};
use datafusion_expr::{Operator, ScalarUDF};
use datafusion_physical_expr_common::sort_expr::LexOrdering;
#[test]
fn test_ordering_satisfy() -> Result<()> {
let input_schema = Arc::new(Schema::new(vec![
Field::new("a", DataType::Int64, true),
Field::new("b", DataType::Int64, true),
]));
let crude = LexOrdering::new(vec![PhysicalSortExpr {
expr: Arc::new(Column::new("a", 0)),
options: SortOptions::default(),
}]);
let finer = LexOrdering::new(vec![
PhysicalSortExpr {
expr: Arc::new(Column::new("a", 0)),
options: SortOptions::default(),
},
PhysicalSortExpr {
expr: Arc::new(Column::new("b", 1)),
options: SortOptions::default(),
},
]);
// finer ordering satisfies, crude ordering should return true
let mut eq_properties_finer =
EquivalenceProperties::new(Arc::clone(&input_schema));
eq_properties_finer.oeq_class.push(finer.clone());
assert!(eq_properties_finer.ordering_satisfy(crude.as_ref()));
// Crude ordering doesn't satisfy finer ordering. should return false
let mut eq_properties_crude =
EquivalenceProperties::new(Arc::clone(&input_schema));
eq_properties_crude.oeq_class.push(crude);
assert!(!eq_properties_crude.ordering_satisfy(finer.as_ref()));
Ok(())
}
#[test]
fn test_ordering_satisfy_with_equivalence2() -> Result<()> {
let test_schema = create_test_schema()?;
let col_a = &col("a", &test_schema)?;
let col_b = &col("b", &test_schema)?;
let col_c = &col("c", &test_schema)?;
let col_d = &col("d", &test_schema)?;
let col_e = &col("e", &test_schema)?;
let col_f = &col("f", &test_schema)?;
let test_fun = ScalarUDF::new_from_impl(TestScalarUDF::new());
let floor_a = &crate::udf::create_physical_expr(
&test_fun,
&[col("a", &test_schema)?],
&test_schema,
&[],
&DFSchema::empty(),
)?;
let floor_f = &crate::udf::create_physical_expr(
&test_fun,
&[col("f", &test_schema)?],
&test_schema,
&[],
&DFSchema::empty(),
)?;
let exp_a = &crate::udf::create_physical_expr(
&test_fun,
&[col("a", &test_schema)?],
&test_schema,
&[],
&DFSchema::empty(),
)?;
let a_plus_b = Arc::new(BinaryExpr::new(
Arc::clone(col_a),
Operator::Plus,
Arc::clone(col_b),
)) as Arc<dyn PhysicalExpr>;
let options = SortOptions {
descending: false,
nulls_first: false,
};
let test_cases = vec![
// ------------ TEST CASE 1 ------------
(
// orderings
vec![
// [a ASC, d ASC, b ASC]
vec![(col_a, options), (col_d, options), (col_b, options)],
// [c ASC]
vec![(col_c, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [a ASC, b ASC], requirement is not satisfied.
vec![(col_a, options), (col_b, options)],
// expected: requirement is not satisfied.
false,
),
// ------------ TEST CASE 2 ------------
(
// orderings
vec![
// [a ASC, c ASC, b ASC]
vec![(col_a, options), (col_c, options), (col_b, options)],
// [d ASC]
vec![(col_d, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [floor(a) ASC],
vec![(floor_a, options)],
// expected: requirement is satisfied.
true,
),
// ------------ TEST CASE 2.1 ------------
(
// orderings
vec![
// [a ASC, c ASC, b ASC]
vec![(col_a, options), (col_c, options), (col_b, options)],
// [d ASC]
vec![(col_d, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [floor(f) ASC], (Please note that a=f)
vec![(floor_f, options)],
// expected: requirement is satisfied.
true,
),
// ------------ TEST CASE 3 ------------
(
// orderings
vec![
// [a ASC, c ASC, b ASC]
vec![(col_a, options), (col_c, options), (col_b, options)],
// [d ASC]
vec![(col_d, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [a ASC, c ASC, a+b ASC],
vec![(col_a, options), (col_c, options), (&a_plus_b, options)],
// expected: requirement is satisfied.
true,
),
// ------------ TEST CASE 4 ------------
(
// orderings
vec![
// [a ASC, b ASC, c ASC, d ASC]
vec![
(col_a, options),
(col_b, options),
(col_c, options),
(col_d, options),
],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [floor(a) ASC, a+b ASC],
vec![(floor_a, options), (&a_plus_b, options)],
// expected: requirement is satisfied.
false,
),
// ------------ TEST CASE 5 ------------
(
// orderings
vec![
// [a ASC, b ASC, c ASC, d ASC]
vec![
(col_a, options),
(col_b, options),
(col_c, options),
(col_d, options),
],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [exp(a) ASC, a+b ASC],
vec![(exp_a, options), (&a_plus_b, options)],
// expected: requirement is not satisfied.
// TODO: If we know that exp function is 1-to-1 function.
// we could have deduced that above requirement is satisfied.
false,
),
// ------------ TEST CASE 6 ------------
(
// orderings
vec![
// [a ASC, d ASC, b ASC]
vec![(col_a, options), (col_d, options), (col_b, options)],
// [c ASC]
vec![(col_c, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [a ASC, d ASC, floor(a) ASC],
vec![(col_a, options), (col_d, options), (floor_a, options)],
// expected: requirement is satisfied.
true,
),
// ------------ TEST CASE 7 ------------
(
// orderings
vec![
// [a ASC, c ASC, b ASC]
vec![(col_a, options), (col_c, options), (col_b, options)],
// [d ASC]
vec![(col_d, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [a ASC, floor(a) ASC, a + b ASC],
vec![(col_a, options), (floor_a, options), (&a_plus_b, options)],
// expected: requirement is not satisfied.
false,
),
// ------------ TEST CASE 8 ------------
(
// orderings
vec![
// [a ASC, b ASC, c ASC]
vec![(col_a, options), (col_b, options), (col_c, options)],
// [d ASC]
vec![(col_d, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [a ASC, c ASC, floor(a) ASC, a + b ASC],
vec![
(col_a, options),
(col_c, options),
(floor_a, options),
(&a_plus_b, options),
],
// expected: requirement is not satisfied.
false,
),
// ------------ TEST CASE 9 ------------
(
// orderings
vec![
// [a ASC, b ASC, c ASC, d ASC]
vec![
(col_a, options),
(col_b, options),
(col_c, options),
(col_d, options),
],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [a ASC, b ASC, c ASC, floor(a) ASC],
vec![
(col_a, options),
(col_b, options),
(col_c, options),
(floor_a, options),
],
// expected: requirement is satisfied.
true,
),
// ------------ TEST CASE 10 ------------
(
// orderings
vec![
// [d ASC, b ASC]
vec![(col_d, options), (col_b, options)],
// [c ASC, a ASC]
vec![(col_c, options), (col_a, options)],
],
// equivalence classes
vec![vec![col_a, col_f]],
// constants
vec![col_e],
// requirement [c ASC, d ASC, a + b ASC],
vec![(col_c, options), (col_d, options), (&a_plus_b, options)],
// expected: requirement is satisfied.
true,
),
];
for (orderings, eq_group, constants, reqs, expected) in test_cases {
let err_msg =
format!("error in test orderings: {orderings:?}, eq_group: {eq_group:?}, constants: {constants:?}, reqs: {reqs:?}, expected: {expected:?}");
let mut eq_properties = EquivalenceProperties::new(Arc::clone(&test_schema));
let orderings = convert_to_orderings(&orderings);
eq_properties.add_new_orderings(orderings);
let eq_group = eq_group
.into_iter()
.map(|eq_class| {
let eq_classes = eq_class.into_iter().cloned().collect::<Vec<_>>();
EquivalenceClass::new(eq_classes)
})
.collect::<Vec<_>>();
let eq_group = EquivalenceGroup::new(eq_group);
eq_properties.add_equivalence_group(eq_group);
let constants = constants
.into_iter()
.map(|expr| ConstExpr::from(expr).with_across_partitions(true));
eq_properties = eq_properties.with_constants(constants);
let reqs = convert_to_sort_exprs(&reqs);
assert_eq!(
eq_properties.ordering_satisfy(reqs.as_ref()),
expected,
"{}",
err_msg
);
}
Ok(())
}
#[test]
fn test_ordering_satisfy_different_lengths() -> Result<()> {
let test_schema = create_test_schema()?;
let col_a = &col("a", &test_schema)?;
let col_b = &col("b", &test_schema)?;
let col_c = &col("c", &test_schema)?;
let col_d = &col("d", &test_schema)?;
let col_e = &col("e", &test_schema)?;
let col_f = &col("f", &test_schema)?;
let options = SortOptions {
descending: false,
nulls_first: false,
};
// a=c (e.g they are aliases).
let mut eq_properties = EquivalenceProperties::new(test_schema);
eq_properties.add_equal_conditions(col_a, col_c)?;
let orderings = vec![
vec![(col_a, options)],
vec![(col_e, options)],
vec![(col_d, options), (col_f, options)],
];
let orderings = convert_to_orderings(&orderings);
// Column [a ASC], [e ASC], [d ASC, f ASC] are all valid orderings for the schema.
eq_properties.add_new_orderings(orderings);
// First entry in the tuple is required ordering, second entry is the expected flag
// that indicates whether this required ordering is satisfied.
// ([a ASC], true) indicate a ASC requirement is already satisfied by existing orderings.
let test_cases = vec![
// [c ASC, a ASC, e ASC], expected represents this requirement is satisfied
(
vec![(col_c, options), (col_a, options), (col_e, options)],
true,
),
(vec![(col_c, options), (col_b, options)], false),
(vec![(col_c, options), (col_d, options)], true),
(
vec![(col_d, options), (col_f, options), (col_b, options)],
false,
),
(vec![(col_d, options), (col_f, options)], true),
];
for (reqs, expected) in test_cases {
let err_msg =
format!("error in test reqs: {:?}, expected: {:?}", reqs, expected,);
let reqs = convert_to_sort_exprs(&reqs);
assert_eq!(
eq_properties.ordering_satisfy(reqs.as_ref()),
expected,
"{}",
err_msg
);
}
Ok(())
}
#[test]
fn test_remove_redundant_entries_oeq_class() -> Result<()> {
let schema = create_test_schema()?;
let col_a = &col("a", &schema)?;
let col_b = &col("b", &schema)?;
let col_c = &col("c", &schema)?;
let col_d = &col("d", &schema)?;
let col_e = &col("e", &schema)?;
let option_asc = SortOptions {
descending: false,
nulls_first: false,
};
let option_desc = SortOptions {
descending: true,
nulls_first: true,
};
// First entry in the tuple is the given orderings for the table
// Second entry is the simplest version of the given orderings that is functionally equivalent.
let test_cases = vec![
// ------- TEST CASE 1 ---------
(
// ORDERINGS GIVEN
vec![
// [a ASC, b ASC]
vec![(col_a, option_asc), (col_b, option_asc)],
],
// EXPECTED orderings that is succinct.
vec![
// [a ASC, b ASC]
vec![(col_a, option_asc), (col_b, option_asc)],
],
),
// ------- TEST CASE 2 ---------
(
// ORDERINGS GIVEN
vec![
// [a ASC, b ASC]
vec![(col_a, option_asc), (col_b, option_asc)],
// [a ASC, b ASC, c ASC]
vec![
(col_a, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
],
// EXPECTED orderings that is succinct.
vec![
// [a ASC, b ASC, c ASC]
vec![
(col_a, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
],
),
// ------- TEST CASE 3 ---------
(
// ORDERINGS GIVEN
vec![
// [a ASC, b DESC]
vec![(col_a, option_asc), (col_b, option_desc)],
// [a ASC]
vec![(col_a, option_asc)],
// [a ASC, c ASC]
vec![(col_a, option_asc), (col_c, option_asc)],
],
// EXPECTED orderings that is succinct.
vec![
// [a ASC, b DESC]
vec![(col_a, option_asc), (col_b, option_desc)],
// [a ASC, c ASC]
vec![(col_a, option_asc), (col_c, option_asc)],
],
),
// ------- TEST CASE 4 ---------
(
// ORDERINGS GIVEN
vec![
// [a ASC, b ASC]
vec![(col_a, option_asc), (col_b, option_asc)],
// [a ASC, b ASC, c ASC]
vec![
(col_a, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
// [a ASC]
vec![(col_a, option_asc)],
],
// EXPECTED orderings that is succinct.
vec![
// [a ASC, b ASC, c ASC]
vec![
(col_a, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
],
),
// ------- TEST CASE 5 ---------
// Empty ordering
(
vec![vec![]],
// No ordering in the state (empty ordering is ignored).
vec![],
),
// ------- TEST CASE 6 ---------
(
// ORDERINGS GIVEN
vec![
// [a ASC, b ASC]
vec![(col_a, option_asc), (col_b, option_asc)],
// [b ASC]
vec![(col_b, option_asc)],
],
// EXPECTED orderings that is succinct.
vec![
// [a ASC]
vec![(col_a, option_asc)],
// [b ASC]
vec![(col_b, option_asc)],
],
),
// ------- TEST CASE 7 ---------
// b, a
// c, a
// d, b, c
(
// ORDERINGS GIVEN
vec![
// [b ASC, a ASC]
vec![(col_b, option_asc), (col_a, option_asc)],
// [c ASC, a ASC]
vec![(col_c, option_asc), (col_a, option_asc)],
// [d ASC, b ASC, c ASC]
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
],
// EXPECTED orderings that is succinct.
vec![
// [b ASC, a ASC]
vec![(col_b, option_asc), (col_a, option_asc)],
// [c ASC, a ASC]
vec![(col_c, option_asc), (col_a, option_asc)],
// [d ASC]
vec![(col_d, option_asc)],
],
),
// ------- TEST CASE 8 ---------
// b, e
// c, a
// d, b, e, c, a
(
// ORDERINGS GIVEN
vec![
// [b ASC, e ASC]
vec![(col_b, option_asc), (col_e, option_asc)],
// [c ASC, a ASC]
vec![(col_c, option_asc), (col_a, option_asc)],
// [d ASC, b ASC, e ASC, c ASC, a ASC]
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_e, option_asc),
(col_c, option_asc),
(col_a, option_asc),
],
],
// EXPECTED orderings that is succinct.
vec![
// [b ASC, e ASC]
vec![(col_b, option_asc), (col_e, option_asc)],
// [c ASC, a ASC]
vec![(col_c, option_asc), (col_a, option_asc)],
// [d ASC]
vec![(col_d, option_asc)],
],
),
// ------- TEST CASE 9 ---------
// b
// a, b, c
// d, a, b
(
// ORDERINGS GIVEN
vec![
// [b ASC]
vec![(col_b, option_asc)],
// [a ASC, b ASC, c ASC]
vec![
(col_a, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
// [d ASC, a ASC, b ASC]
vec![
(col_d, option_asc),
(col_a, option_asc),
(col_b, option_asc),
],
],
// EXPECTED orderings that is succinct.
vec![
// [b ASC]
vec![(col_b, option_asc)],
// [a ASC, b ASC, c ASC]
vec![
(col_a, option_asc),
(col_b, option_asc),
(col_c, option_asc),
],
// [d ASC]
vec![(col_d, option_asc)],
],
),
];
for (orderings, expected) in test_cases {
let orderings = convert_to_orderings(&orderings);
let expected = convert_to_orderings(&expected);
let actual = OrderingEquivalenceClass::new(orderings.clone());
let actual = actual.orderings;
let err_msg = format!(
"orderings: {:?}, expected: {:?}, actual :{:?}",
orderings, expected, actual
);
assert_eq!(actual.len(), expected.len(), "{}", err_msg);
for elem in actual {
assert!(expected.contains(&elem), "{}", err_msg);
}
}
Ok(())
}
}