Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup logical optimizer rules. #7919

Merged
merged 16 commits into from
Oct 25, 2023
Merged
8 changes: 8 additions & 0 deletions datafusion/common/src/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ impl DFSchema {
.zip(iter2)
.all(|((t1, f1), (t2, f2))| t1 == t2 && Self::field_is_semantically_equal(f1, f2))
}
(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During schema check, we were missing out these cases as equal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @viirya this may be of interest to you

DataType::Decimal128(_l_precision, _l_scale),
DataType::Decimal128(_r_precision, _r_scale),
) => true,
(
DataType::Decimal256(_l_precision, _l_scale),
DataType::Decimal256(_r_precision, _r_scale),
) => true,
_ => dt1 == dt2,
}
}
Expand Down
17 changes: 17 additions & 0 deletions datafusion/common/src/functional_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,21 @@ mod tests {
assert_eq!(iter.next(), Some(&Constraint::Unique(vec![20])));
assert_eq!(iter.next(), None);
}

#[test]
fn test_get_updated_id_keys() {
let fund_dependencies =
FunctionalDependencies::new(vec![FunctionalDependence::new(
vec![1],
vec![0, 1, 2],
true,
)]);
let res = fund_dependencies.project_functional_dependencies(&[1, 2], 2);
let expected = FunctionalDependencies::new(vec![FunctionalDependence::new(
vec![0],
vec![0, 1],
true,
)]);
assert_eq!(res, expected);
}
}
14 changes: 7 additions & 7 deletions datafusion/core/tests/sql/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ async fn group_by_dictionary() {
.expect("ran plan correctly");

let expected = [
"+-------+------------------------+",
"| t.val | COUNT(DISTINCT t.dict) |",
"+-------+------------------------+",
"| 1 | 2 |",
"| 2 | 2 |",
"| 4 | 1 |",
"+-------+------------------------+",
"+-----+------------------------+",
"| val | COUNT(DISTINCT t.dict) |",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

"+-----+------------------------+",
"| 1 | 2 |",
"| 2 | 2 |",
"| 4 | 1 |",
"+-----+------------------------+",
];
assert_batches_sorted_eq!(expected, &results);
}
Expand Down
45 changes: 26 additions & 19 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@

//! Built-in functions module contains all the built-in functions definitions.

use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, OnceLock};

use crate::nullif::SUPPORTED_NULLIF_TYPES;
use crate::signature::TIMEZONE_WILDCARD;
use crate::type_coercion::functions::data_types;
use crate::{
conditional_expressions, struct_expressions, utils, FuncMonotonicity, Signature,
TypeSignature, Volatility,
};

use arrow::datatypes::{DataType, Field, Fields, IntervalUnit, TimeUnit};
use datafusion_common::{
internal_err, plan_datafusion_err, plan_err, DataFusionError, Result,
};
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, OnceLock};

use crate::type_coercion::binary::get_wider_type;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

Expand Down Expand Up @@ -468,18 +473,14 @@ impl BuiltinScalarFunction {
/// * `List(Int64)` has dimension 2
/// * `List(List(Int64))` has dimension 3
/// * etc.
fn return_dimension(self, input_expr_type: DataType) -> u64 {
let mut res: u64 = 1;
fn return_dimension(self, input_expr_type: &DataType) -> u64 {
let mut result: u64 = 1;
let mut current_data_type = input_expr_type;
loop {
match current_data_type {
DataType::List(field) => {
current_data_type = field.data_type().clone();
res += 1;
}
_ => return res,
}
while let DataType::List(field) = current_data_type {
current_data_type = field.data_type();
result += 1;
}
result
}

/// Returns the output [`DataType`] of this function
Expand Down Expand Up @@ -538,11 +539,17 @@ impl BuiltinScalarFunction {
match input_expr_type {
List(field) => {
if !field.data_type().equals_datatype(&Null) {
let dims = self.return_dimension(input_expr_type.clone());
if max_dims < dims {
max_dims = dims;
expr_type = input_expr_type.clone();
}
let dims = self.return_dimension(input_expr_type);
expr_type = match max_dims.cmp(&dims) {
Ordering::Greater => expr_type,
Ordering::Equal => {
get_wider_type(&expr_type, input_expr_type)?
}
Ordering::Less => {
max_dims = dims;
input_expr_type.clone()
}
};
}
}
_ => {
Expand Down
Loading