@@ -171,26 +171,26 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
171
171
) ) ) )
172
172
}
173
173
Expr :: Not ( expr) => Ok ( Transformed :: yes ( not ( get_casted_expr_for_bool_op (
174
- & expr,
174
+ * expr,
175
175
& self . schema ,
176
176
) ?) ) ) ,
177
177
Expr :: IsTrue ( expr) => Ok ( Transformed :: yes ( is_true (
178
- get_casted_expr_for_bool_op ( & expr, & self . schema ) ?,
178
+ get_casted_expr_for_bool_op ( * expr, & self . schema ) ?,
179
179
) ) ) ,
180
180
Expr :: IsNotTrue ( expr) => Ok ( Transformed :: yes ( is_not_true (
181
- get_casted_expr_for_bool_op ( & expr, & self . schema ) ?,
181
+ get_casted_expr_for_bool_op ( * expr, & self . schema ) ?,
182
182
) ) ) ,
183
183
Expr :: IsFalse ( expr) => Ok ( Transformed :: yes ( is_false (
184
- get_casted_expr_for_bool_op ( & expr, & self . schema ) ?,
184
+ get_casted_expr_for_bool_op ( * expr, & self . schema ) ?,
185
185
) ) ) ,
186
186
Expr :: IsNotFalse ( expr) => Ok ( Transformed :: yes ( is_not_false (
187
- get_casted_expr_for_bool_op ( & expr, & self . schema ) ?,
187
+ get_casted_expr_for_bool_op ( * expr, & self . schema ) ?,
188
188
) ) ) ,
189
189
Expr :: IsUnknown ( expr) => Ok ( Transformed :: yes ( is_unknown (
190
- get_casted_expr_for_bool_op ( & expr, & self . schema ) ?,
190
+ get_casted_expr_for_bool_op ( * expr, & self . schema ) ?,
191
191
) ) ) ,
192
192
Expr :: IsNotUnknown ( expr) => Ok ( Transformed :: yes ( is_not_unknown (
193
- get_casted_expr_for_bool_op ( & expr, & self . schema ) ?,
193
+ get_casted_expr_for_bool_op ( * expr, & self . schema ) ?,
194
194
) ) ) ,
195
195
Expr :: Like ( Like {
196
196
negated,
@@ -308,15 +308,12 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
308
308
Expr :: ScalarFunction ( ScalarFunction { func_def, args } ) => match func_def {
309
309
ScalarFunctionDefinition :: UDF ( fun) => {
310
310
let new_expr = coerce_arguments_for_signature (
311
- args. as_slice ( ) ,
311
+ args,
312
312
& self . schema ,
313
313
fun. signature ( ) ,
314
314
) ?;
315
- let new_expr = coerce_arguments_for_fun (
316
- new_expr. as_slice ( ) ,
317
- & self . schema ,
318
- & fun,
319
- ) ?;
315
+ let new_expr =
316
+ coerce_arguments_for_fun ( new_expr, & self . schema , & fun) ?;
320
317
Ok ( Transformed :: yes ( Expr :: ScalarFunction (
321
318
ScalarFunction :: new_udf ( fun, new_expr) ,
322
319
) ) )
@@ -336,7 +333,7 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
336
333
AggregateFunctionDefinition :: BuiltIn ( fun) => {
337
334
let new_expr = coerce_agg_exprs_for_signature (
338
335
& fun,
339
- & args,
336
+ args,
340
337
& self . schema ,
341
338
& fun. signature ( ) ,
342
339
) ?;
@@ -353,7 +350,7 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
353
350
}
354
351
AggregateFunctionDefinition :: UDF ( fun) => {
355
352
let new_expr = coerce_arguments_for_signature (
356
- args. as_slice ( ) ,
353
+ args,
357
354
& self . schema ,
358
355
fun. signature ( ) ,
359
356
) ?;
@@ -387,7 +384,7 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
387
384
expr:: WindowFunctionDefinition :: AggregateFunction ( fun) => {
388
385
coerce_agg_exprs_for_signature (
389
386
fun,
390
- & args,
387
+ args,
391
388
& self . schema ,
392
389
& fun. signature ( ) ,
393
390
) ?
@@ -454,12 +451,12 @@ fn coerce_scalar(target_type: &DataType, value: &ScalarValue) -> Result<ScalarVa
454
451
/// Downstream code uses this signal to treat these values as *unbounded*.
455
452
fn coerce_scalar_range_aware (
456
453
target_type : & DataType ,
457
- value : & ScalarValue ,
454
+ value : ScalarValue ,
458
455
) -> Result < ScalarValue > {
459
- coerce_scalar ( target_type, value) . or_else ( |err| {
456
+ coerce_scalar ( target_type, & value) . or_else ( |err| {
460
457
// If type coercion fails, check if the largest type in family works:
461
458
if let Some ( largest_type) = get_widest_type_in_family ( target_type) {
462
- coerce_scalar ( largest_type, value) . map_or_else (
459
+ coerce_scalar ( largest_type, & value) . map_or_else (
463
460
|_| exec_err ! ( "Cannot cast {value:?} to {target_type:?}" ) ,
464
461
|_| ScalarValue :: try_from ( target_type) ,
465
462
)
@@ -484,7 +481,7 @@ fn get_widest_type_in_family(given_type: &DataType) -> Option<&DataType> {
484
481
/// Coerces the given (window frame) `bound` to `target_type`.
485
482
fn coerce_frame_bound (
486
483
target_type : & DataType ,
487
- bound : & WindowFrameBound ,
484
+ bound : WindowFrameBound ,
488
485
) -> Result < WindowFrameBound > {
489
486
match bound {
490
487
WindowFrameBound :: Preceding ( v) => {
@@ -530,31 +527,30 @@ fn coerce_window_frame(
530
527
}
531
528
WindowFrameUnits :: Rows | WindowFrameUnits :: Groups => & DataType :: UInt64 ,
532
529
} ;
533
- window_frame. start_bound =
534
- coerce_frame_bound ( target_type, & window_frame. start_bound ) ?;
535
- window_frame. end_bound = coerce_frame_bound ( target_type, & window_frame. end_bound ) ?;
530
+ window_frame. start_bound = coerce_frame_bound ( target_type, window_frame. start_bound ) ?;
531
+ window_frame. end_bound = coerce_frame_bound ( target_type, window_frame. end_bound ) ?;
536
532
Ok ( window_frame)
537
533
}
538
534
539
535
// Support the `IsTrue` `IsNotTrue` `IsFalse` `IsNotFalse` type coercion.
540
536
// The above op will be rewrite to the binary op when creating the physical op.
541
- fn get_casted_expr_for_bool_op ( expr : & Expr , schema : & DFSchemaRef ) -> Result < Expr > {
537
+ fn get_casted_expr_for_bool_op ( expr : Expr , schema : & DFSchemaRef ) -> Result < Expr > {
542
538
let left_type = expr. get_type ( schema) ?;
543
539
get_input_types ( & left_type, & Operator :: IsDistinctFrom , & DataType :: Boolean ) ?;
544
- cast_expr ( expr, & DataType :: Boolean , schema)
540
+ expr. cast_to ( & DataType :: Boolean , schema)
545
541
}
546
542
547
543
/// Returns `expressions` coerced to types compatible with
548
544
/// `signature`, if possible.
549
545
///
550
546
/// See the module level documentation for more detail on coercion.
551
547
fn coerce_arguments_for_signature (
552
- expressions : & [ Expr ] ,
548
+ expressions : Vec < Expr > ,
553
549
schema : & DFSchema ,
554
550
signature : & Signature ,
555
551
) -> Result < Vec < Expr > > {
556
552
if expressions. is_empty ( ) {
557
- return Ok ( vec ! [ ] ) ;
553
+ return Ok ( expressions ) ;
558
554
}
559
555
560
556
let current_types = expressions
@@ -565,58 +561,47 @@ fn coerce_arguments_for_signature(
565
561
let new_types = data_types ( & current_types, signature) ?;
566
562
567
563
expressions
568
- . iter ( )
564
+ . into_iter ( )
569
565
. enumerate ( )
570
- . map ( |( i, expr) | cast_expr ( expr, & new_types[ i] , schema) )
571
- . collect :: < Result < Vec < _ > > > ( )
566
+ . map ( |( i, expr) | expr. cast_to ( & new_types[ i] , schema) )
567
+ . collect ( )
572
568
}
573
569
574
570
fn coerce_arguments_for_fun (
575
- expressions : & [ Expr ] ,
571
+ expressions : Vec < Expr > ,
576
572
schema : & DFSchema ,
577
573
fun : & Arc < ScalarUDF > ,
578
574
) -> Result < Vec < Expr > > {
579
- if expressions. is_empty ( ) {
580
- return Ok ( vec ! [ ] ) ;
581
- }
582
- let mut expressions: Vec < Expr > = expressions. to_vec ( ) ;
583
-
584
575
// Cast Fixedsizelist to List for array functions
585
576
if fun. name ( ) == "make_array" {
586
- expressions = expressions
577
+ expressions
587
578
. into_iter ( )
588
579
. map ( |expr| {
589
580
let data_type = expr. get_type ( schema) . unwrap ( ) ;
590
581
if let DataType :: FixedSizeList ( field, _) = data_type {
591
- let field = field. as_ref ( ) . clone ( ) ;
592
- let to_type = DataType :: List ( Arc :: new ( field) ) ;
582
+ let to_type = DataType :: List ( field. clone ( ) ) ;
593
583
expr. cast_to ( & to_type, schema)
594
584
} else {
595
585
Ok ( expr)
596
586
}
597
587
} )
598
- . collect :: < Result < Vec < _ > > > ( ) ?;
588
+ . collect ( )
589
+ } else {
590
+ Ok ( expressions)
599
591
}
600
-
601
- Ok ( expressions)
602
- }
603
-
604
- /// Cast `expr` to the specified type, if possible
605
- fn cast_expr ( expr : & Expr , to_type : & DataType , schema : & DFSchema ) -> Result < Expr > {
606
- expr. clone ( ) . cast_to ( to_type, schema)
607
592
}
608
593
609
594
/// Returns the coerced exprs for each `input_exprs`.
610
595
/// Get the coerced data type from `aggregate_rule::coerce_types` and add `try_cast` if the
611
596
/// data type of `input_exprs` need to be coerced.
612
597
fn coerce_agg_exprs_for_signature (
613
598
agg_fun : & AggregateFunction ,
614
- input_exprs : & [ Expr ] ,
599
+ input_exprs : Vec < Expr > ,
615
600
schema : & DFSchema ,
616
601
signature : & Signature ,
617
602
) -> Result < Vec < Expr > > {
618
603
if input_exprs. is_empty ( ) {
619
- return Ok ( vec ! [ ] ) ;
604
+ return Ok ( input_exprs ) ;
620
605
}
621
606
let current_types = input_exprs
622
607
. iter ( )
@@ -627,10 +612,10 @@ fn coerce_agg_exprs_for_signature(
627
612
type_coercion:: aggregates:: coerce_types ( agg_fun, & current_types, signature) ?;
628
613
629
614
input_exprs
630
- . iter ( )
615
+ . into_iter ( )
631
616
. enumerate ( )
632
- . map ( |( i, expr) | cast_expr ( expr, & coerced_types[ i] , schema) )
633
- . collect :: < Result < Vec < _ > > > ( )
617
+ . map ( |( i, expr) | expr. cast_to ( & coerced_types[ i] , schema) )
618
+ . collect ( )
634
619
}
635
620
636
621
fn coerce_case_expression ( case : Case , schema : & DFSchemaRef ) -> Result < Case > {
0 commit comments