@@ -22,12 +22,13 @@ use crate::{OptimizerConfig, OptimizerRule};
2222use arrow:: datatypes:: {
2323 DataType , MAX_DECIMAL_FOR_EACH_PRECISION , MIN_DECIMAL_FOR_EACH_PRECISION ,
2424} ;
25- use datafusion_common:: { DFSchemaRef , DataFusionError , Result , ScalarValue } ;
25+ use datafusion_common:: { DFSchema , DFSchemaRef , DataFusionError , Result , ScalarValue } ;
2626use datafusion_expr:: expr_rewriter:: { ExprRewritable , ExprRewriter , RewriteRecursion } ;
2727use datafusion_expr:: utils:: from_plan;
2828use datafusion_expr:: {
2929 binary_expr, in_list, lit, Expr , ExprSchemable , LogicalPlan , Operator ,
3030} ;
31+ use std:: sync:: Arc ;
3132
3233/// The rule can be used to the numeric binary comparison with literal expr, like below pattern:
3334/// `cast(left_expr as data_type) comparison_op literal_expr` or `literal_expr comparison_op cast(right_expr as data_type)`.
@@ -79,10 +80,18 @@ fn optimize(plan: &LogicalPlan) -> Result<LogicalPlan> {
7980 . map ( |input| optimize ( input) )
8081 . collect :: < Result < Vec < _ > > > ( ) ?;
8182
82- let schema = plan. schema ( ) ;
83+ let mut schema = new_inputs. iter ( ) . map ( |input| input. schema ( ) ) . fold (
84+ DFSchema :: empty ( ) ,
85+ |mut lhs, rhs| {
86+ lhs. merge ( rhs) ;
87+ lhs
88+ } ,
89+ ) ;
90+
91+ schema. merge ( plan. schema ( ) ) ;
8392
8493 let mut expr_rewriter = UnwrapCastExprRewriter {
85- schema : schema . clone ( ) ,
94+ schema : Arc :: new ( schema ) ,
8695 } ;
8796
8897 let new_exprs = plan
@@ -273,6 +282,11 @@ fn try_cast_literal_to_type(
273282 lit_value : & ScalarValue ,
274283 target_type : & DataType ,
275284) -> Result < Option < ScalarValue > > {
285+ let lit_data_type = lit_value. get_datatype ( ) ;
286+ // the rule just support the signed numeric data type now
287+ if !is_support_data_type ( & lit_data_type) || !is_support_data_type ( target_type) {
288+ return Ok ( None ) ;
289+ }
276290 if lit_value. is_null ( ) {
277291 // null value can be cast to any type of null value
278292 return Ok ( Some ( ScalarValue :: try_from ( target_type) ?) ) ;
@@ -373,7 +387,7 @@ mod tests {
373387 use arrow:: datatypes:: DataType ;
374388 use datafusion_common:: { DFField , DFSchema , DFSchemaRef , ScalarValue } ;
375389 use datafusion_expr:: expr_rewriter:: ExprRewritable ;
376- use datafusion_expr:: { cast, col, lit, try_cast, Expr } ;
390+ use datafusion_expr:: { cast, col, in_list , lit, try_cast, Expr } ;
377391 use std:: collections:: HashMap ;
378392 use std:: sync:: Arc ;
379393
@@ -585,6 +599,21 @@ mod tests {
585599 assert_eq ! ( optimize_test( expr_lt, & schema) , expected) ;
586600 }
587601
602+ #[ test]
603+ fn test_not_support_data_type ( ) {
604+ // "c6 > 0" will be cast to `cast(c6 as int64) > 0
605+ // but the type of c6 is uint32
606+ // the rewriter will not throw error and just return the original expr
607+ let schema = expr_test_schema ( ) ;
608+ let expr_input = cast ( col ( "c6" ) , DataType :: Int64 ) . eq ( lit ( 0i64 ) ) ;
609+ assert_eq ! ( optimize_test( expr_input. clone( ) , & schema) , expr_input) ;
610+
611+ // inlist for unsupported data type
612+ let expr_input =
613+ in_list ( cast ( col ( "c6" ) , DataType :: Int64 ) , vec ! [ lit( 0i64 ) ] , false ) ;
614+ assert_eq ! ( optimize_test( expr_input. clone( ) , & schema) , expr_input) ;
615+ }
616+
588617 fn optimize_test ( expr : Expr , schema : & DFSchemaRef ) -> Expr {
589618 let mut expr_rewriter = UnwrapCastExprRewriter {
590619 schema : schema. clone ( ) ,
@@ -601,6 +630,7 @@ mod tests {
601630 DFField :: new( None , "c3" , DataType :: Decimal128 ( 18 , 2 ) , false ) ,
602631 DFField :: new( None , "c4" , DataType :: Decimal128 ( 38 , 37 ) , false ) ,
603632 DFField :: new( None , "c5" , DataType :: Float32 , false ) ,
633+ DFField :: new( None , "c6" , DataType :: UInt32 , false ) ,
604634 ] ,
605635 HashMap :: new ( ) ,
606636 )
0 commit comments