Skip to content

Commit

Permalink
[minor]: remove same util functions from the code base. (#13026)
Browse files Browse the repository at this point in the history
* Initial commit

* Resolve linter errors

* Decrease diff
  • Loading branch information
akurmustafa authored Oct 22, 2024
1 parent 465d660 commit 755ba91
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 349 deletions.
173 changes: 172 additions & 1 deletion datafusion/core/tests/fuzz_cases/equivalence/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

use crate::fuzz_cases::equivalence::utils::{
convert_to_orderings, create_random_schema, create_test_schema_2,
convert_to_orderings, create_random_schema, create_test_params, create_test_schema_2,
generate_table_for_eq_properties, generate_table_for_orderings,
is_table_same_after_sort, TestScalarUDF,
};
Expand Down Expand Up @@ -160,6 +160,177 @@ fn test_ordering_satisfy_with_equivalence_complex_random() -> Result<()> {
Ok(())
}

#[test]
fn test_ordering_satisfy_with_equivalence() -> Result<()> {
// Schema satisfies following orderings:
// [a ASC], [d ASC, b ASC], [e DESC, f ASC, g ASC]
// and
// Column [a=c] (e.g they are aliases).
let (test_schema, eq_properties) = create_test_params()?;
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 col_g = &col("g", &test_schema)?;

let option_asc = SortOptions {
descending: false,
nulls_first: false,
};

let option_desc = SortOptions {
descending: true,
nulls_first: true,
};
let table_data_with_properties =
generate_table_for_eq_properties(&eq_properties, 625, 5)?;

// First element in the tuple stores vector of requirement, second element is the expected return value for ordering_satisfy function
let requirements = vec![
// `a ASC NULLS LAST`, expects `ordering_satisfy` to be `true`, since existing ordering `a ASC NULLS LAST, b ASC NULLS LAST` satisfies it
(vec![(col_a, option_asc)], true),
(vec![(col_a, option_desc)], false),
// Test whether equivalence works as expected
(vec![(col_c, option_asc)], true),
(vec![(col_c, option_desc)], false),
// Test whether ordering equivalence works as expected
(vec![(col_d, option_asc)], true),
(vec![(col_d, option_asc), (col_b, option_asc)], true),
(vec![(col_d, option_desc), (col_b, option_asc)], false),
(
vec![
(col_e, option_desc),
(col_f, option_asc),
(col_g, option_asc),
],
true,
),
(vec![(col_e, option_desc), (col_f, option_asc)], true),
(vec![(col_e, option_asc), (col_f, option_asc)], false),
(vec![(col_e, option_desc), (col_b, option_asc)], false),
(vec![(col_e, option_asc), (col_b, option_asc)], false),
(
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_d, option_asc),
(col_b, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_e, option_desc),
(col_f, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_e, option_desc),
(col_b, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_d, option_desc),
(col_b, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_e, option_asc),
(col_f, option_asc),
],
false,
),
(
vec![
(col_d, option_asc),
(col_b, option_asc),
(col_e, option_asc),
(col_b, option_asc),
],
false,
),
(vec![(col_d, option_asc), (col_e, option_desc)], true),
(
vec![
(col_d, option_asc),
(col_c, option_asc),
(col_b, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_e, option_desc),
(col_f, option_asc),
(col_b, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_e, option_desc),
(col_c, option_asc),
(col_b, option_asc),
],
true,
),
(
vec![
(col_d, option_asc),
(col_e, option_desc),
(col_b, option_asc),
(col_f, option_asc),
],
true,
),
];

for (cols, expected) in requirements {
let err_msg = format!("Error in test case:{cols:?}");
let required = cols
.into_iter()
.map(|(expr, options)| PhysicalSortExpr {
expr: Arc::clone(expr),
options,
})
.collect::<Vec<_>>();

// Check expected result with experimental result.
assert_eq!(
is_table_same_after_sort(
required.clone(),
table_data_with_properties.clone()
)?,
expected
);
assert_eq!(
eq_properties.ordering_satisfy(&required),
expected,
"{err_msg}"
);
}

Ok(())
}

// This test checks given a table is ordered with `[a ASC, b ASC, c ASC, d ASC]` and `[a ASC, c ASC, b ASC, d ASC]`
// whether the table is also ordered with `[a ASC, b ASC, d ASC]` and `[a ASC, c ASC, d ASC]`
// Since these orderings cannot be deduced, these orderings shouldn't be satisfied by the table generated.
Expand Down
57 changes: 57 additions & 0 deletions datafusion/core/tests/fuzz_cases/equivalence/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,63 @@ fn get_representative_arr(
None
}

// Generate a schema which consists of 8 columns (a, b, c, d, e, f, g, h)
pub fn create_test_schema() -> Result<SchemaRef> {
let a = Field::new("a", DataType::Int32, true);
let b = Field::new("b", DataType::Int32, true);
let c = Field::new("c", DataType::Int32, true);
let d = Field::new("d", DataType::Int32, true);
let e = Field::new("e", DataType::Int32, true);
let f = Field::new("f", DataType::Int32, true);
let g = Field::new("g", DataType::Int32, true);
let h = Field::new("h", DataType::Int32, true);
let schema = Arc::new(Schema::new(vec![a, b, c, d, e, f, g, h]));

Ok(schema)
}

/// Construct a schema with following properties
/// Schema satisfies following orderings:
/// [a ASC], [d ASC, b ASC], [e DESC, f ASC, g ASC]
/// and
/// Column [a=c] (e.g they are aliases).
pub fn create_test_params() -> Result<(SchemaRef, EquivalenceProperties)> {
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 col_g = &col("g", &test_schema)?;
let mut eq_properties = EquivalenceProperties::new(Arc::clone(&test_schema));
eq_properties.add_equal_conditions(col_a, col_c)?;

let option_asc = SortOptions {
descending: false,
nulls_first: false,
};
let option_desc = SortOptions {
descending: true,
nulls_first: true,
};
let orderings = vec![
// [a ASC]
vec![(col_a, option_asc)],
// [d ASC, b ASC]
vec![(col_d, option_asc), (col_b, option_asc)],
// [e DESC, f ASC, g ASC]
vec![
(col_e, option_desc),
(col_f, option_asc),
(col_g, option_asc),
],
];
let orderings = convert_to_orderings(&orderings);
eq_properties.add_new_orderings(orderings);
Ok((test_schema, eq_properties))
}

// Generate a table that satisfies the given equivalence properties; i.e.
// equivalences, ordering equivalences, and constants.
pub fn generate_table_for_eq_properties(
Expand Down
Loading

0 comments on commit 755ba91

Please sign in to comment.