Skip to content

Commit b16cd93

Browse files
mustafasrepoberkaysynnadaozankabak
authored
Cleanup logical optimizer rules. (#7919)
* Initial commit * Address todos * Update comments * Simplifications * Minor simplifications * Address reviews * Add TableScan constructor * Minor changes * make try_new_with_schema method of Aggregate private * Use projection try_new instead of try_new_schema * Simplifications, add comment * Review changes * Improve comments * Move get_wider_type to type_coercion module * Clean up type coercion file --------- Co-authored-by: berkaysynnada <berkay.sahin@synnada.ai> Co-authored-by: Mehmet Ozan Kabak <ozankabak@gmail.com>
1 parent af2cda9 commit b16cd93

File tree

16 files changed

+451
-403
lines changed

16 files changed

+451
-403
lines changed

datafusion/common/src/dfschema.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,14 @@ impl DFSchema {
444444
.zip(iter2)
445445
.all(|((t1, f1), (t2, f2))| t1 == t2 && Self::field_is_semantically_equal(f1, f2))
446446
}
447+
(
448+
DataType::Decimal128(_l_precision, _l_scale),
449+
DataType::Decimal128(_r_precision, _r_scale),
450+
) => true,
451+
(
452+
DataType::Decimal256(_l_precision, _l_scale),
453+
DataType::Decimal256(_r_precision, _r_scale),
454+
) => true,
447455
_ => dt1 == dt2,
448456
}
449457
}

datafusion/common/src/functional_dependencies.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,21 @@ mod tests {
558558
assert_eq!(iter.next(), Some(&Constraint::Unique(vec![20])));
559559
assert_eq!(iter.next(), None);
560560
}
561+
562+
#[test]
563+
fn test_get_updated_id_keys() {
564+
let fund_dependencies =
565+
FunctionalDependencies::new(vec![FunctionalDependence::new(
566+
vec![1],
567+
vec![0, 1, 2],
568+
true,
569+
)]);
570+
let res = fund_dependencies.project_functional_dependencies(&[1, 2], 2);
571+
let expected = FunctionalDependencies::new(vec![FunctionalDependence::new(
572+
vec![0],
573+
vec![0, 1],
574+
true,
575+
)]);
576+
assert_eq!(res, expected);
577+
}
561578
}

datafusion/core/tests/sql/group_by.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ async fn group_by_dictionary() {
231231
.expect("ran plan correctly");
232232

233233
let expected = [
234-
"+-------+------------------------+",
235-
"| t.val | COUNT(DISTINCT t.dict) |",
236-
"+-------+------------------------+",
237-
"| 1 | 2 |",
238-
"| 2 | 2 |",
239-
"| 4 | 1 |",
240-
"+-------+------------------------+",
234+
"+-----+------------------------+",
235+
"| val | COUNT(DISTINCT t.dict) |",
236+
"+-----+------------------------+",
237+
"| 1 | 2 |",
238+
"| 2 | 2 |",
239+
"| 4 | 1 |",
240+
"+-----+------------------------+",
241241
];
242242
assert_batches_sorted_eq!(expected, &results);
243243
}

datafusion/expr/src/built_in_function.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@
1717

1818
//! Built-in functions module contains all the built-in functions definitions.
1919
20+
use std::cmp::Ordering;
21+
use std::collections::HashMap;
22+
use std::fmt;
23+
use std::str::FromStr;
24+
use std::sync::{Arc, OnceLock};
25+
2026
use crate::nullif::SUPPORTED_NULLIF_TYPES;
2127
use crate::signature::TIMEZONE_WILDCARD;
28+
use crate::type_coercion::binary::get_wider_type;
2229
use crate::type_coercion::functions::data_types;
2330
use crate::{
2431
conditional_expressions, struct_expressions, utils, FuncMonotonicity, Signature,
2532
TypeSignature, Volatility,
2633
};
34+
2735
use arrow::datatypes::{DataType, Field, Fields, IntervalUnit, TimeUnit};
2836
use datafusion_common::{
2937
internal_err, plan_datafusion_err, plan_err, DataFusionError, Result,
3038
};
31-
use std::collections::HashMap;
32-
use std::fmt;
33-
use std::str::FromStr;
34-
use std::sync::{Arc, OnceLock};
39+
3540
use strum::IntoEnumIterator;
3641
use strum_macros::EnumIter;
3742

@@ -468,18 +473,14 @@ impl BuiltinScalarFunction {
468473
/// * `List(Int64)` has dimension 2
469474
/// * `List(List(Int64))` has dimension 3
470475
/// * etc.
471-
fn return_dimension(self, input_expr_type: DataType) -> u64 {
472-
let mut res: u64 = 1;
476+
fn return_dimension(self, input_expr_type: &DataType) -> u64 {
477+
let mut result: u64 = 1;
473478
let mut current_data_type = input_expr_type;
474-
loop {
475-
match current_data_type {
476-
DataType::List(field) => {
477-
current_data_type = field.data_type().clone();
478-
res += 1;
479-
}
480-
_ => return res,
481-
}
479+
while let DataType::List(field) = current_data_type {
480+
current_data_type = field.data_type();
481+
result += 1;
482482
}
483+
result
483484
}
484485

485486
/// Returns the output [`DataType`] of this function
@@ -538,11 +539,17 @@ impl BuiltinScalarFunction {
538539
match input_expr_type {
539540
List(field) => {
540541
if !field.data_type().equals_datatype(&Null) {
541-
let dims = self.return_dimension(input_expr_type.clone());
542-
if max_dims < dims {
543-
max_dims = dims;
544-
expr_type = input_expr_type.clone();
545-
}
542+
let dims = self.return_dimension(input_expr_type);
543+
expr_type = match max_dims.cmp(&dims) {
544+
Ordering::Greater => expr_type,
545+
Ordering::Equal => {
546+
get_wider_type(&expr_type, input_expr_type)?
547+
}
548+
Ordering::Less => {
549+
max_dims = dims;
550+
input_expr_type.clone()
551+
}
552+
};
546553
}
547554
}
548555
_ => {

0 commit comments

Comments
 (0)