Wraps this expression in a cast to a target [arrow::datatypes::DataType]. # Errors This function errors when it is impossible to cast the expression to the target [arrow::datatypes::DataType].
(self, cast_to_type: &DataType, schema: &dyn ExprSchema)
| 684 | /// This function errors when it is impossible to cast the |
| 685 | /// expression to the target [arrow::datatypes::DataType]. |
| 686 | fn cast_to(self, cast_to_type: &DataType, schema: &dyn ExprSchema) -> Result<Expr> { |
| 687 | let this_type = self.get_type(schema)?; |
| 688 | if this_type == *cast_to_type { |
| 689 | return Ok(self); |
| 690 | } |
| 691 | |
| 692 | // TODO(kszucs): Most of the operations do not validate the type correctness |
| 693 | // like all of the binary expressions below. Perhaps Expr should track the |
| 694 | // type of the expression? |
| 695 | |
| 696 | // Special handling for struct-to-struct casts with name-based field matching |
| 697 | let can_cast = match (&this_type, cast_to_type) { |
| 698 | (DataType::Struct(_), DataType::Struct(_)) => { |
| 699 | // Always allow struct-to-struct casts; field matching happens at runtime |
| 700 | true |
| 701 | } |
| 702 | _ => can_cast_types(&this_type, cast_to_type), |
| 703 | }; |
| 704 | |
| 705 | if can_cast { |
| 706 | match self { |
| 707 | Expr::ScalarSubquery(subquery) => { |
| 708 | Ok(Expr::ScalarSubquery(cast_subquery(subquery, cast_to_type)?)) |
| 709 | } |
| 710 | _ => Ok(Expr::Cast(Cast::new(Box::new(self), cast_to_type.clone()))), |
| 711 | } |
| 712 | } else { |
| 713 | plan_err!("Cannot automatically convert {this_type} to {cast_to_type}") |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | /// Verify that function is invoked with correct number and type of arguments as |
no test coverage detected