Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,7 @@ impl ScalarUDFImpl for CastToI64UDF {
arg
} else {
// need to use an actual cast to get the correct type
Expr::Cast(datafusion_expr::Cast {
expr: Box::new(arg),
data_type: DataType::Int64,
})
Expr::Cast(datafusion_expr::Cast::new(Box::new(arg), DataType::Int64))
};
// return the newly written argument to DataFusion
Ok(ExprSimplifyResult::Simplified(new_expr))
Expand Down
52 changes: 45 additions & 7 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,13 +1012,23 @@ pub struct Cast {
/// The expression being cast
pub expr: Box<Expr>,
/// The `DataType` the expression will yield
pub data_type: DataType,
pub data_type: FieldRef,
}

impl Cast {
/// Create a new Cast expression
pub fn new(expr: Box<Expr>, data_type: DataType) -> Self {
Self { expr, data_type }
Self {
expr,
data_type: Field::new("", data_type, true).into(),
}
}

pub fn new_from_field(expr: Box<Expr>, field: FieldRef) -> Self {
Self {
expr,
data_type: field,
}
}
}

Expand All @@ -1028,13 +1038,23 @@ pub struct TryCast {
/// The expression being cast
pub expr: Box<Expr>,
/// The `DataType` the expression will yield
pub data_type: DataType,
pub data_type: FieldRef,
}

impl TryCast {
/// Create a new TryCast expression
pub fn new(expr: Box<Expr>, data_type: DataType) -> Self {
Self { expr, data_type }
Self {
expr,
data_type: Field::new("", data_type, true).into(),
}
}

pub fn new_from_field(expr: Box<Expr>, field: FieldRef) -> Self {
Self {
expr,
data_type: field,
}
}
}

Expand Down Expand Up @@ -3488,10 +3508,28 @@ impl Display for Expr {
write!(f, "END")
}
Expr::Cast(Cast { expr, data_type }) => {
write!(f, "CAST({expr} AS {data_type})")
if data_type.metadata().is_empty() {
write!(f, "CAST({expr} AS {})", data_type.data_type())
} else {
write!(
f,
"CAST({expr} AS {}<{:?}>)",
data_type.data_type(),
data_type.metadata()
)
}
Comment on lines +3511 to +3520
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a utility function for this in #17986 (how to print a type represented by a DataType and possibly metadata in a user-facing error or explain plan). The idea is to keep existing plan explain strings the same unless somebody actually puts metadata here.

}
Expr::TryCast(TryCast { expr, data_type }) => {
write!(f, "TRY_CAST({expr} AS {data_type})")
if data_type.metadata().is_empty() {
write!(f, "TRY_CAST({expr} AS {})", data_type.data_type())
} else {
write!(
f,
"TRY_CAST({expr} AS {}<{:?}>)",
data_type.data_type(),
data_type.metadata()
)
}
}
Expr::Not(expr) => write!(f, "NOT {expr}"),
Expr::Negative(expr) => write!(f, "(- {expr})"),
Expand Down Expand Up @@ -3844,7 +3882,7 @@ mod test {
fn format_cast() -> Result<()> {
let expr = Expr::Cast(Cast {
expr: Box::new(Expr::Literal(ScalarValue::Float32(Some(1.23)), None)),
data_type: DataType::Utf8,
data_type: Field::new("", DataType::Utf8, true).into(),
});
let expected_canonical = "CAST(Float32(1.23) AS Utf8)";
assert_eq!(expected_canonical, format!("{expr}"));
Expand Down
13 changes: 11 additions & 2 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ impl ExprSchemable for Expr {
.map_or(Ok(DataType::Null), |e| e.get_type(schema))
}
Expr::Cast(Cast { data_type, .. })
| Expr::TryCast(TryCast { data_type, .. }) => Ok(data_type.clone()),
| Expr::TryCast(TryCast { data_type, .. }) => {
Ok(data_type.data_type().clone())
}
Expr::Unnest(Unnest { expr }) => {
let arg_data_type = expr.get_type(schema)?;
// Unnest's output type is the inner type of the list
Expand Down Expand Up @@ -592,7 +594,14 @@ impl ExprSchemable for Expr {
// _ => Ok((self.get_type(schema)?, self.nullable(schema)?)),
Expr::Cast(Cast { expr, data_type }) => expr
.to_field(schema)
.map(|(_, f)| f.as_ref().clone().with_data_type(data_type.clone()))
.map(|(_, f)| {
f.as_ref()
.clone()
.with_data_type(data_type.data_type().clone())
.with_metadata(f.metadata().clone())
// TODO: should nullability be overridden here or derived from the
// input expression?
Comment on lines +598 to +603
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this type of cast should be able to express nullability or not.

})
.map(Arc::new),
Expr::Like(_)
| Expr::SimilarTo(_)
Expand Down
4 changes: 2 additions & 2 deletions datafusion/expr/src/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ impl TreeNode for Expr {
}),
Expr::Cast(Cast { expr, data_type }) => expr
.map_elements(f)?
.update_data(|be| Expr::Cast(Cast::new(be, data_type))),
.update_data(|be| Expr::Cast(Cast::new_from_field(be, data_type))),
Expr::TryCast(TryCast { expr, data_type }) => expr
.map_elements(f)?
.update_data(|be| Expr::TryCast(TryCast::new(be, data_type))),
.update_data(|be| Expr::TryCast(TryCast::new_from_field(be, data_type))),
Expr::ScalarFunction(ScalarFunction { func, args }) => {
args.map_elements(f)?.map_data(|new_args| {
Ok(Expr::ScalarFunction(ScalarFunction::new_udf(
Expand Down
2 changes: 1 addition & 1 deletion datafusion/functions/src/core/arrow_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl ScalarUDFImpl for ArrowCastFunc {
// Use an actual cast to get the correct type
Expr::Cast(datafusion_expr::Cast {
expr: Box::new(arg),
data_type: target_type,
data_type: Field::new("", target_type, true).into(),
})
};
// return the newly written argument to DataFusion
Expand Down
6 changes: 4 additions & 2 deletions datafusion/physical-expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,14 @@ pub fn create_physical_expr(
Expr::Cast(Cast { expr, data_type }) => expressions::cast(
create_physical_expr(expr, input_dfschema, execution_props)?,
input_schema,
data_type.clone(),
// TODO: this drops extension metadata associated with the cast
data_type.data_type().clone(),
Comment on lines -294 to +295
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually need physical expressions to be able to cast things...my vauge plan is to use a logical plan transformation or perhaps optimizer rule to replace casts to extension types with a ScalarUDF call. This should possibly error if there is mismatched metadata between the input and destination (i.e., a physical cast would only ever represent a storage cast, which is usually OK).

),
Expr::TryCast(TryCast { expr, data_type }) => expressions::try_cast(
create_physical_expr(expr, input_dfschema, execution_props)?,
input_schema,
data_type.clone(),
// TODO: this drops extension metadata associated with the cast
data_type.data_type().clone(),
),
Expr::Not(expr) => {
expressions::not(create_physical_expr(expr, input_dfschema, execution_props)?)
Expand Down
4 changes: 4 additions & 0 deletions datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,15 @@ message WhenThen {
message CastNode {
LogicalExprNode expr = 1;
datafusion_common.ArrowType arrow_type = 2;
map<string, string> metadata = 3;
optional bool nullable = 4;
}

message TryCastNode {
LogicalExprNode expr = 1;
datafusion_common.ArrowType arrow_type = 2;
map<string, string> metadata = 3;
optional bool nullable = 4;
}

message SortExprNode {
Expand Down
72 changes: 72 additions & 0 deletions datafusion/proto/src/generated/pbjson.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions datafusion/proto/src/generated/prost.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading