MCPcopy Create free account
hub / github.com/apache/datafusion / cast_subquery

Function cast_subquery

datafusion/expr/src/expr_schema.rs:762–792  ·  view source on GitHub ↗

Cast subquery in InSubquery/ScalarSubquery to a given type. 1. **Projection plan**: If the subquery is a projection (i.e. a SELECT statement with specific columns), it casts the first expression in the projection to the target type and creates a new projection with the casted expression. 2. **Non-projection plan**: If the subquery isn't a projection, it adds a projection to the plan with the cast

(subquery: Subquery, cast_to_type: &DataType)

Source from the content-addressed store, hash-verified

760/// 2. **Non-projection plan**: If the subquery isn't a projection, it adds a projection to the plan
761/// with the casted first column.
762pub fn cast_subquery(subquery: Subquery, cast_to_type: &DataType) -> Result<Subquery> {
763 if subquery.subquery.schema().field(0).data_type() == cast_to_type {
764 return Ok(subquery);
765 }
766
767 let plan = subquery.subquery.as_ref();
768 let new_plan = match plan {
769 LogicalPlan::Projection(projection) => {
770 let cast_expr = projection.expr[0]
771 .clone()
772 .cast_to(cast_to_type, projection.input.schema())?;
773 LogicalPlan::Projection(Projection::try_new(
774 vec![cast_expr],
775 Arc::clone(&projection.input),
776 )?)
777 }
778 _ => {
779 let cast_expr = Expr::Column(Column::from(plan.schema().qualified_field(0)))
780 .cast_to(cast_to_type, subquery.subquery.schema())?;
781 LogicalPlan::Projection(Projection::try_new(
782 vec![cast_expr],
783 subquery.subquery,
784 )?)
785 }
786 };
787 Ok(Subquery {
788 subquery: Arc::new(new_plan),
789 outer_ref_columns: subquery.outer_ref_columns,
790 spans: Spans::new(),
791 })
792}
793
794#[cfg(test)]
795mod tests {

Callers 2

cast_toMethod · 0.85
f_upMethod · 0.85

Calls 10

ProjectionClass · 0.85
newFunction · 0.85
qualified_fieldMethod · 0.80
ColumnClass · 0.50
data_typeMethod · 0.45
fieldMethod · 0.45
schemaMethod · 0.45
as_refMethod · 0.45
cast_toMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…