Apache DataFusion SQL Query Engine
preserve casts in rewrite_sort_cols_by_aggs (#5611)
mpurins-coralogixb35b5cbd3a39
diff --git a/datafusion/expr/src/expr_rewriter/order_by.rs b/datafusion/expr/src/expr_rewriter/order_by.rsindex 84b7193e22..a26b3c6741 100644--- a/datafusion/expr/src/expr_rewriter/order_by.rs+++ b/datafusion/expr/src/expr_rewriter/order_by.rs@@ -19,7 +19,7 @@use crate::expr::Sort;use crate::expr_rewriter::{normalize_col, rewrite_expr};-use crate::{Expr, ExprSchemable, LogicalPlan};+use crate::{Cast, Expr, ExprSchemable, LogicalPlan, TryCast};use datafusion_common::{Column, Result};/// Rewrite sort on aggregate expressions to sort on the column of aggregate output@@ -116,7 +116,19 @@ fn rewrite_in_terms_of_projection(// look for the column named the same as this exprif let Some(found) = proj_exprs.iter().find(|a| expr_match(&search_col, a)) {- return Ok((*found).clone());+ let found = found.clone();+ let expr = match normalized_expr {+ Expr::Cast(Cast { expr: _, data_type }) => Expr::Cast(Cast {+ expr: Box::new(found),+ data_type,+ }),+ Expr::TryCast(TryCast { expr: _, data_type }) => Expr::TryCast(TryCast {+ expr: Box::new(found),+ data_type,+ }),+ _ => found,+ };+ return Ok(expr);}Ok(expr)})@@ -141,7 +153,8 @@ mod test {use arrow::datatypes::{DataType, Field, Schema};use crate::{- avg, col, lit, logical_plan::builder::LogicalTableSource, min, LogicalPlanBuilder,+ avg, cast, col, lit, logical_plan::builder::LogicalTableSource, min, try_cast,+ LogicalPlanBuilder,};use super::*;@@ -241,6 +254,34 @@ mod test {}}+ #[test]+ fn preserve_cast() {+ let plan = make_input()+ .project(vec![col("c2").alias("c2")])+ .unwrap()+ .project(vec![col("c2").alias("c2")])+ .unwrap()+ .build()+ .unwrap();++ let cases = vec![+ TestCase {+ desc: "Cast is preserved by rewrite_sort_cols_by_aggs",+ input: sort(cast(col("c2"), DataType::Int64)),+ expected: sort(cast(col("c2").alias("c2"), DataType::Int64)),+ },+ TestCase {+ desc: "TryCast is preserved by rewrite_sort_cols_by_aggs",+ input: sort(try_cast(col("c2"), DataType::Int64)),+ expected: sort(try_cast(col("c2").alias("c2"), DataType::Int64)),+ },+ ];++ for case in cases {+ case.run(&plan)+ }+ }+struct TestCase {desc: &'static str,input: Expr,diff --git a/datafusion/sql/tests/integration_test.rs b/datafusion/sql/tests/integration_test.rsindex 4d5d56fdf4..4e2f22caba 100644--- a/datafusion/sql/tests/integration_test.rs+++ b/datafusion/sql/tests/integration_test.rs@@ -3252,6 +3252,17 @@ fn test_select_distinct_order_by() {assert_eq!(err.to_string(), expected);}+#[test]+fn select_order_by_with_cast() {+ let sql =+ "SELECT first_name AS first_name FROM (SELECT first_name AS first_name FROM person) ORDER BY CAST(first_name as INT)";+ let expected = "Sort: CAST(first_name AS first_name AS Int32) ASC NULLS LAST\+ \n Projection: first_name AS first_name\+ \n Projection: person.first_name AS first_name\+ \n TableScan: person";+ quick_test(sql, expected);+}+#[test]fn test_duplicated_left_join_key_inner_join() {// person.id * 2 happen twice in left side.