use std::fmt;
use std::hash::Hash;
use std::sync::Arc;
use crate::physical_expr::PhysicalExpr;
use arrow::compute::{CastOptions, can_cast_types};
use arrow::datatypes::{DataType, DataType::*, Field, FieldRef, Metadata, Schema};
use arrow::record_batch::RecordBatch;
use arrow::util::display::{ArrayFormatterFactory, DurationFormat, FormatOptions};
use arrow_schema::extension::{EXTENSION_TYPE_METADATA_KEY, EXTENSION_TYPE_NAME_KEY};
use datafusion_common::datatype::DataTypeExt;
use datafusion_common::format::DEFAULT_FORMAT_OPTIONS;
use datafusion_common::nested_struct::{
requires_nested_struct_cast, validate_data_type_compatibility,
};
use datafusion_common::{Result, not_impl_err};
use datafusion_expr_common::columnar_value::ColumnarValue;
use datafusion_expr_common::interval_arithmetic::Interval;
use datafusion_expr_common::sort_properties::ExprProperties;
const DEFAULT_CAST_OPTIONS: CastOptions<'static> = CastOptions {
safe: false,
format_options: DEFAULT_FORMAT_OPTIONS,
};
const DEFAULT_SAFE_CAST_OPTIONS: CastOptions<'static> = CastOptions {
safe: true,
format_options: DEFAULT_FORMAT_OPTIONS,
};
#[derive(Debug, Clone)]
struct OwnedCastOptions {
safe: bool,
format_safe: bool,
null: String,
date_format: Option<String>,
datetime_format: Option<String>,
timestamp_format: Option<String>,
timestamp_tz_format: Option<String>,
time_format: Option<String>,
duration_format: DurationFormat,
types_info: bool,
quoted_strings: bool,
formatter_factory: Option<&'static dyn ArrayFormatterFactory>,
}
impl From<CastOptions<'static>> for OwnedCastOptions {
fn from(options: CastOptions<'static>) -> Self {
let CastOptions {
safe,
format_options,
} = options;
Self {
safe,
format_safe: format_options.safe(),
null: format_options.null().to_owned(),
date_format: format_options.date_format().map(str::to_owned),
datetime_format: format_options.datetime_format().map(str::to_owned),
timestamp_format: format_options.timestamp_format().map(str::to_owned),
timestamp_tz_format: format_options.timestamp_tz_format().map(str::to_owned),
time_format: format_options.time_format().map(str::to_owned),
duration_format: format_options.duration_format(),
types_info: format_options.types_info(),
quoted_strings: format_options.quoted_strings(),
formatter_factory: format_options.formatter_factory(),
}
}
}
impl OwnedCastOptions {
fn as_arrow(&self) -> CastOptions<'_> {
let Self {
safe,
format_safe,
null,
date_format,
datetime_format,
timestamp_format,
timestamp_tz_format,
time_format,
duration_format,
types_info,
quoted_strings,
formatter_factory,
} = self;
CastOptions {
safe: *safe,
format_options: FormatOptions::new()
.with_display_error(*format_safe)
.with_null(null)
.with_date_format(date_format.as_deref())
.with_datetime_format(datetime_format.as_deref())
.with_timestamp_format(timestamp_format.as_deref())
.with_timestamp_tz_format(timestamp_tz_format.as_deref())
.with_time_format(time_format.as_deref())
.with_duration_format(*duration_format)
.with_types_info(*types_info)
.with_quoted_strings(*quoted_strings)
.with_formatter_factory(*formatter_factory),
}
}
}
impl PartialEq for OwnedCastOptions {
fn eq(&self, other: &Self) -> bool {
self.as_arrow() == other.as_arrow()
}
}
impl Eq for OwnedCastOptions {}
impl Hash for OwnedCastOptions {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_arrow().hash(state);
}
}
fn can_cast_named_struct_types(source: &DataType, target: &DataType) -> bool {
validate_data_type_compatibility("", source, target).is_ok()
}
#[derive(Debug, Clone, Eq)]
pub struct CastExpr {
pub expr: Arc<dyn PhysicalExpr>,
target_field: FieldRef,
explicit_target: bool,
cast_options: OwnedCastOptions,
}
impl PartialEq for CastExpr {
fn eq(&self, other: &Self) -> bool {
self.expr.eq(&other.expr)
&& self.cast_type().eq(other.cast_type())
&& self.target_metadata().eq(&other.target_metadata())
&& self.target_nullable().eq(&other.target_nullable())
&& self.cast_options.eq(&other.cast_options)
}
}
impl Hash for CastExpr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.expr.hash(state);
self.cast_type().hash(state);
if let Some(metadata) = self.target_metadata() {
let mut entries: Vec<_> = metadata.iter().collect();
entries.sort_by_key(|(k, _)| *k);
for (k, v) in entries {
k.hash(state);
v.hash(state);
}
}
self.target_nullable().hash(state);
self.cast_options.hash(state);
}
}
impl CastExpr {
pub fn new(
expr: Arc<dyn PhysicalExpr>,
cast_type: DataType,
cast_options: Option<CastOptions<'static>>,
) -> Self {
Self {
expr,
target_field: cast_type.into_nullable_field_ref(),
explicit_target: false,
cast_options: cast_options.unwrap_or(DEFAULT_CAST_OPTIONS).into(),
}
}
pub fn new_with_target_field(
expr: Arc<dyn PhysicalExpr>,
target_field: FieldRef,
cast_options: Option<CastOptions<'static>>,
) -> Self {
Self {
expr,
target_field,
explicit_target: true,
cast_options: cast_options.unwrap_or(DEFAULT_CAST_OPTIONS).into(),
}
}
pub fn expr(&self) -> &Arc<dyn PhysicalExpr> {
&self.expr
}
pub fn cast_type(&self) -> &DataType {
self.target_field.data_type()
}
pub fn target_metadata(&self) -> Option<&Metadata> {
self.explicit_target.then(|| self.target_field.metadata())
}
pub fn target_nullable(&self) -> Option<bool> {
self.explicit_target
.then(|| self.target_field.is_nullable())
}
pub fn target_field(&self) -> &FieldRef {
&self.target_field
}
pub fn cast_options(&self) -> CastOptions<'_> {
self.cast_options.as_arrow()
}
pub fn with_new_expr_and_target_field(
&self,
expr: Arc<dyn PhysicalExpr>,
target_field: FieldRef,
) -> Self {
Self {
expr,
target_field,
explicit_target: true,
cast_options: self.cast_options.clone(),
}
}
pub fn has_explicit_metadata(&self) -> bool {
self.explicit_target
}
pub fn has_explicit_nullability(&self) -> bool {
self.explicit_target
}
fn resolved_target_field(&self, input_schema: &Schema) -> Result<FieldRef> {
let source_result = self.expr.return_field(input_schema);
if self.explicit_target {
let name = source_result
.as_ref()
.map(|f| f.name().to_string())
.unwrap_or_default();
return Ok(Arc::new(
Field::new(
name,
self.cast_type().clone(),
self.target_field.is_nullable(),
)
.with_metadata(self.target_field.metadata().clone()),
));
}
source_result.map(|source_field| {
let mut metadata = source_field.metadata().clone();
metadata.remove(EXTENSION_TYPE_NAME_KEY);
metadata.remove(EXTENSION_TYPE_METADATA_KEY);
Arc::new(
source_field
.as_ref()
.clone()
.with_data_type(self.cast_type().clone())
.with_metadata(metadata),
)
})
}
pub fn check_bigger_cast(cast_type: &DataType, src: &DataType) -> bool {
if cast_type.eq(src) {
return true;
}
matches!(
(src, cast_type),
(Int8, Int16 | Int32 | Int64)
| (Int16, Int32 | Int64)
| (Int32, Int64)
| (Int32, Date32)
| (Date32, Int32)
| (Int64, Date64)
| (Date64, Int64)
| (UInt8, UInt16 | UInt32 | UInt64 | Int16 | Int32 | Int64)
| (UInt16, UInt32 | UInt64 | Int32 | Int64)
| (UInt32, UInt64 | Int64)
| (Int8 | Int16 | UInt8 | UInt16, Float32)
| (Int8 | Int16 | Int32 | UInt8 | UInt16 | UInt32, Float64)
| (Utf8, LargeUtf8 | Utf8View)
| (Binary, LargeBinary | BinaryView)
)
}
pub fn is_bigger_cast(&self, src: &DataType) -> bool {
Self::check_bigger_cast(self.cast_type(), src)
}
}
fn is_fixed_offset(tz: &str) -> bool {
tz == "UTC" || tz.starts_with(['+', '-'])
}
fn is_order_preserving_cast(source_type: &DataType, target_type: &DataType) -> bool {
use arrow::datatypes::TimeUnit::*;
if source_type == target_type
|| (source_type.is_numeric() || *source_type == Boolean)
&& target_type.is_numeric()
{
return true;
}
match (source_type, target_type) {
(Date32 | Date64, Date32 | Date64)
| (Date32 | Date64, Timestamp(_, None))
| (Timestamp(_, None), Date32) => true,
(Timestamp(_, Some(tz)), Date32) => is_fixed_offset(tz),
(Timestamp(_, _), Date64) => true,
(Time32(Second | Millisecond), Time32(Second | Millisecond))
| (Time32(Second | Millisecond), Time64(Microsecond | Nanosecond))
| (Time64(Nanosecond), Time64(Microsecond))
| (Duration(_), Duration(_)) => true,
(Timestamp(_, from_tz), Timestamp(_, to_tz)) => {
from_tz.is_some() || to_tz.as_deref().is_none_or(is_fixed_offset)
}
_ => false,
}
}
pub(crate) fn cast_expr_properties(
child: &ExprProperties,
target_type: &DataType,
null_on_failure: bool,
) -> Result<ExprProperties> {
let unbounded = Interval::make_unbounded(target_type)?;
let source_type = child.range.data_type();
let bigger_cast = CastExpr::check_bigger_cast(target_type, &source_type);
if bigger_cast
|| (!null_on_failure && is_order_preserving_cast(&source_type, target_type))
{
Ok(child
.clone()
.with_range(unbounded)
.with_strictly_order_preserving(
child.strictly_order_preserving && bigger_cast,
))
} else {
Ok(ExprProperties::new_unknown().with_range(unbounded))
}
}
#[cfg(feature = "proto")]
fn serialize_cast_options(
options: &OwnedCastOptions,
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalCastOptions>> {
use datafusion_proto_models::protobuf;
let options = options.as_arrow();
if options.format_options.formatter_factory().is_some() {
return not_impl_err!(
"CastExpr serialization does not support a custom formatter_factory"
);
}
if options == DEFAULT_CAST_OPTIONS {
return Ok(None);
}
let CastOptions {
safe,
format_options,
} = options;
let duration_format = match format_options.duration_format() {
DurationFormat::ISO8601 => protobuf::PhysicalDurationFormat::Iso8601,
DurationFormat::Pretty => protobuf::PhysicalDurationFormat::Pretty,
_ => {
return not_impl_err!(
"CastExpr serialization does not support this duration format"
);
}
};
Ok(Some(protobuf::PhysicalCastOptions {
safe,
format_options: Some(protobuf::PhysicalFormatOptions {
safe: format_options.safe(),
null: format_options.null().to_owned(),
date_format: format_options.date_format().map(str::to_owned),
datetime_format: format_options.datetime_format().map(str::to_owned),
timestamp_format: format_options.timestamp_format().map(str::to_owned),
timestamp_tz_format: format_options.timestamp_tz_format().map(str::to_owned),
time_format: format_options.time_format().map(str::to_owned),
duration_format: duration_format.into(),
types_info: format_options.types_info(),
quoted_strings: format_options.quoted_strings(),
}),
}))
}
#[cfg(feature = "proto")]
fn deserialize_cast_options(
options: Option<&datafusion_proto_models::protobuf::PhysicalCastOptions>,
) -> Result<OwnedCastOptions> {
use datafusion_common::internal_datafusion_err;
use datafusion_proto_models::protobuf;
let Some(options) = options else {
return Ok(DEFAULT_CAST_OPTIONS.into());
};
let protobuf::PhysicalCastOptions {
safe,
format_options,
} = options;
let format_options = format_options.as_ref().ok_or_else(|| {
internal_datafusion_err!(
"CastExpr cast_options is missing required field 'format_options'"
)
})?;
let protobuf::PhysicalFormatOptions {
safe: format_safe,
null,
date_format,
datetime_format,
timestamp_format,
timestamp_tz_format,
time_format,
duration_format,
types_info,
quoted_strings,
} = format_options;
let duration_format = match protobuf::PhysicalDurationFormat::try_from(
*duration_format,
)
.map_err(|_| {
internal_datafusion_err!(
"CastExpr has invalid duration format value {duration_format}"
)
})? {
protobuf::PhysicalDurationFormat::Iso8601 => DurationFormat::ISO8601,
protobuf::PhysicalDurationFormat::Pretty => DurationFormat::Pretty,
};
Ok(OwnedCastOptions {
safe: *safe,
format_safe: *format_safe,
null: null.clone(),
date_format: date_format.clone(),
datetime_format: datetime_format.clone(),
timestamp_format: timestamp_format.clone(),
timestamp_tz_format: timestamp_tz_format.clone(),
time_format: time_format.clone(),
duration_format,
types_info: *types_info,
quoted_strings: *quoted_strings,
formatter_factory: None,
})
}
impl fmt::Display for CastExpr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CAST({} AS {}", self.expr, self.cast_type())?;
if self.explicit_target && self.target_field.is_nullable() {
write!(f, ";N")?;
}
write!(f, ")")
}
}
impl PhysicalExpr for CastExpr {
fn data_type(&self, _input_schema: &Schema) -> Result<DataType> {
Ok(self.cast_type().clone())
}
fn nullable(&self, input_schema: &Schema) -> Result<bool> {
let child_nullable = self.expr.nullable(input_schema)?;
let target_nullable = self.resolved_target_field(input_schema)?.is_nullable();
Ok(child_nullable || target_nullable)
}
fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {
let value = self.expr.evaluate(batch)?;
let cast_options = self.cast_options();
value
.cast_to(self.cast_type(), Some(&cast_options))
.map_err(|error| {
let source = self
.expr
.return_field(batch.schema().as_ref())
.ok()
.filter(|field| !field.name().is_empty())
.map(|field| format!("field '{}'", field.name()))
.unwrap_or_else(|| format!("expression '{}'", self.expr));
error.context(format!(
"Failed to cast {source} from {} to {}",
value.data_type(),
self.cast_type()
))
})
}
fn return_field(&self, input_schema: &Schema) -> Result<FieldRef> {
self.resolved_target_field(input_schema)
}
fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
vec![&self.expr]
}
fn with_new_children(
self: Arc<Self>,
children: Vec<Arc<dyn PhysicalExpr>>,
) -> Result<Arc<dyn PhysicalExpr>> {
Ok(Arc::new(CastExpr {
expr: Arc::clone(&children[0]),
target_field: Arc::clone(&self.target_field),
explicit_target: self.explicit_target,
cast_options: self.cast_options.clone(),
}))
}
fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> {
children[0].cast_to(self.cast_type(), &self.cast_options())
}
fn propagate_constraints(
&self,
interval: &Interval,
children: &[&Interval],
) -> Result<Option<Vec<Interval>>> {
let source_type = children[0].data_type();
let target_type = self.cast_type();
if !can_propagate_cast_constraints(&source_type, target_type) {
return Ok(Some(vec![]));
}
Ok(Some(vec![
interval.cast_to(&source_type, &DEFAULT_SAFE_CAST_OPTIONS)?,
]))
}
fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> {
cast_expr_properties(&children[0], self.cast_type(), self.cast_options.safe)
}
fn fmt_sql(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CAST(")?;
self.expr.fmt_sql(f)?;
write!(f, " AS {:?}", self.cast_type())?;
write!(f, ")")
}
#[cfg(feature = "proto")]
fn try_to_proto(
&self,
ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>,
) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> {
use datafusion_proto_models::protobuf;
let Self {
expr,
target_field,
explicit_target,
cast_options,
} = self;
let target_field_proto = if *explicit_target {
Some(target_field.as_ref().try_into()?)
} else {
None
};
let cast_options_proto = serialize_cast_options(cast_options)?;
Ok(Some(protobuf::PhysicalExprNode {
expr_id: None,
expr_type: Some(protobuf::physical_expr_node::ExprType::Cast(Box::new(
protobuf::PhysicalCastNode {
expr: Some(Box::new(ctx.encode_child(expr)?)),
arrow_type: Some(target_field.data_type().try_into()?),
target_field: target_field_proto,
cast_options: cast_options_proto,
},
))),
}))
}
}
fn can_propagate_cast_constraints(source: &DataType, target: &DataType) -> bool {
CastExpr::check_bigger_cast(target, source)
|| (source.is_integer() && target.is_integer())
|| (*source == Float32 && *target == Float64)
}
#[cfg(feature = "proto")]
impl CastExpr {
pub fn try_from_proto(
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
) -> Result<Arc<dyn PhysicalExpr>> {
use datafusion_common::internal_err;
use datafusion_physical_expr_common::expect_expr_variant;
use datafusion_physical_expr_common::physical_expr::proto_decode::require_proto_field;
use datafusion_proto_models::protobuf;
let cast_expr = expect_expr_variant!(
node,
protobuf::physical_expr_node::ExprType::Cast,
"CastExpr",
);
let protobuf::PhysicalCastNode {
expr,
arrow_type,
target_field,
cast_options,
} = &**cast_expr;
let expr = ctx.decode_required_expression(expr.as_deref(), "CastExpr", "expr")?;
let arrow_type =
require_proto_field(arrow_type.as_ref(), "CastExpr", "arrow_type")?;
let cast_type: DataType = arrow_type.try_into()?;
let target_field = target_field
.as_ref()
.map(|field| {
let field: Field = field.try_into()?;
if field.data_type() != &cast_type {
return internal_err!(
"CastExpr target_field type does not match arrow_type"
);
}
Ok(Arc::new(field))
})
.transpose()?;
let cast_options = deserialize_cast_options(cast_options.as_ref())?;
let (target_field, explicit_target) = match target_field {
Some(target_field) => (target_field, true),
None => (cast_type.into_nullable_field_ref(), false),
};
Ok(Arc::new(CastExpr {
expr,
target_field,
explicit_target,
cast_options,
}))
}
}
pub fn cast_with_options(
expr: Arc<dyn PhysicalExpr>,
input_schema: &Schema,
cast_type: DataType,
cast_options: Option<CastOptions<'static>>,
) -> Result<Arc<dyn PhysicalExpr>> {
let expr_type = expr.data_type(input_schema)?;
if expr_type == cast_type {
return Ok(Arc::clone(&expr));
}
let can_build_cast = if requires_nested_struct_cast(&expr_type, &cast_type) {
can_cast_named_struct_types(&expr_type, &cast_type)
} else {
can_cast_types(&expr_type, &cast_type)
};
if !can_build_cast {
return not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}");
}
Ok(Arc::new(CastExpr::new(expr, cast_type, cast_options)))
}
pub fn cast_with_target_field(
expr: Arc<dyn PhysicalExpr>,
input_schema: &Schema,
target_field: &FieldRef,
cast_options: Option<CastOptions<'static>>,
) -> Result<Arc<dyn PhysicalExpr>> {
let expr_type = expr.data_type(input_schema)?;
let cast_type = target_field.data_type();
let is_type_only = target_field.name().is_empty()
&& target_field.is_nullable()
&& target_field.metadata().is_empty();
if expr_type == *cast_type && is_type_only {
let source_field = expr.return_field(input_schema)?;
let has_extension_metadata = source_field
.metadata()
.contains_key(EXTENSION_TYPE_NAME_KEY);
if !has_extension_metadata {
return Ok(Arc::clone(&expr));
}
}
let can_build_cast = if requires_nested_struct_cast(&expr_type, cast_type) {
can_cast_named_struct_types(&expr_type, cast_type)
} else {
can_cast_types(&expr_type, cast_type)
};
if !can_build_cast {
return not_impl_err!("Unsupported CAST from {expr_type} to {cast_type}");
}
if is_type_only {
Ok(Arc::new(CastExpr::new(
expr,
cast_type.clone(),
cast_options,
)))
} else {
Ok(Arc::new(CastExpr::new_with_target_field(
expr,
Arc::clone(target_field),
cast_options,
)))
}
}
pub fn cast(
expr: Arc<dyn PhysicalExpr>,
input_schema: &Schema,
cast_type: DataType,
) -> Result<Arc<dyn PhysicalExpr>> {
cast_with_options(expr, input_schema, cast_type, None)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::expressions::column::col;
use arrow::{
array::{
Array, ArrayRef, Decimal128Array, Float32Array, Float64Array, Int8Array,
Int16Array, Int32Array, Int64Array, StringArray, StructArray,
Time64NanosecondArray, TimestampNanosecondArray, UInt32Array,
},
datatypes::*,
};
use datafusion_common::ScalarValue;
use datafusion_common::cast::{
as_boolean_array, as_int64_array, as_string_array, as_struct_array,
as_uint8_array,
};
use datafusion_common::rounding::{next_down, next_up};
use datafusion_physical_expr_common::physical_expr::fmt_sql;
use insta::assert_snapshot;
use std::collections::HashMap;
#[test]
fn test_cast_constraint_propagation() -> Result<()> {
for (source, target, propagates) in [
(Utf8, Int32, false),
(Utf8View, Int32, false),
(Timestamp(TimeUnit::Nanosecond, None), Date32, false),
(Int32, Date32, true),
(Date32, Int32, true),
(Utf8, LargeUtf8, true),
(Utf8, Utf8, true),
(Float64, Int32, false),
(Int64, Float32, false),
(Float64, Float32, false),
(Decimal128(4, 1), Decimal128(4, 0), false),
(Decimal128(4, 1), Int32, false),
(Float64, Decimal128(4, 1), false),
(Int8, Int64, true),
(Int64, Int8, true),
(Int32, UInt32, true),
(UInt32, Int32, true),
(Int32, Float64, true),
(Float32, Float64, true),
(Decimal128(4, 1), Decimal128(4, 1), true),
] {
let schema = Schema::new(vec![Field::new("x", source.clone(), true)]);
let expr = CastExpr::new(col("x", &schema)?, target.clone(), None);
let input = Interval::make_unbounded(&source)?;
let value = ScalarValue::Int32(Some(0)).cast_to(&target)?;
let output = Interval::from(&value);
let expected = if propagates {
vec![Interval::from(&value.cast_to(&source)?)]
} else {
vec![]
};
assert_eq!(
expr.propagate_constraints(&output, &[&input])?,
Some(expected),
"{source} -> {target}"
);
}
Ok(())
}
#[test]
fn test_float_widening_constraint_boundaries() -> Result<()> {
let mut values = vec![
f32::NEG_INFINITY,
-f32::MAX,
-1.0,
-f32::MIN_POSITIVE,
-f32::from_bits(1),
-0.0,
0.0,
f32::from_bits(1),
f32::MIN_POSITIVE,
1.0,
f32::MAX,
f32::INFINITY,
];
values.extend(
[0x7f800001, 0x7f800002, 0x7fc00001, 0xff800001, 0xffc00001]
.map(f32::from_bits),
);
values.extend([next_down(1.0f32), next_up(1.0f32)]);
let schema = Arc::new(Schema::new(vec![Field::new("x", Float32, false)]));
let batch = RecordBatch::try_new(
Arc::clone(&schema),
vec![Arc::new(Float32Array::from(values.clone()))],
)?;
let expr = CastExpr::new(col("x", &schema)?, Float64, None);
let array = expr.evaluate(&batch)?.into_array(values.len())?;
let widened = array.as_any().downcast_ref::<Float64Array>().unwrap();
let mut bounds = vec![-f64::MAX, f64::MAX];
for value in widened.values() {
bounds.extend([next_down(*value), *value, next_up(*value)]);
}
bounds.extend([
f64::from(f32::from_bits(1)) / 2.0,
-f64::from(f32::from_bits(1)) / 2.0,
f64::midpoint(1.0, f64::from(next_up(1.0f32))),
]);
bounds.sort_by(f64::total_cmp);
bounds.dedup_by(|a, b| a.to_bits() == b.to_bits());
let input = Interval::make_unbounded(&Float32)?;
for (i, lower) in bounds.iter().enumerate() {
for upper in &bounds[i..] {
let output = Interval::make(Some(*lower), Some(*upper))?;
let propagated = expr.propagate_constraints(&output, &[&input])?.unwrap();
assert_eq!(propagated.len(), 1);
for (index, value) in values.iter().enumerate() {
if output.contains_value(ScalarValue::Float64(Some(
widened.value(index),
)))? {
assert!(
propagated[0]
.contains_value(ScalarValue::Float32(Some(*value)))?,
"input bits={:08x}, output={output}, propagated={:?}",
value.to_bits(),
propagated[0]
);
}
}
}
}
Ok(())
}
fn make_struct_array(fields: Fields, arrays: Vec<ArrayRef>) -> StructArray {
StructArray::new(fields, arrays, None)
}
fn cast_struct_array(
column: &str,
input_field: Field,
target_field: Field,
input_array: StructArray,
) -> Result<StructArray> {
let schema = Arc::new(Schema::new(vec![input_field]));
let batch = RecordBatch::try_new(
Arc::clone(&schema),
vec![Arc::new(input_array) as ArrayRef],
)?;
let expr = CastExpr::new_with_target_field(
col(column, schema.as_ref())?,
Arc::new(target_field),
None,
);
let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
Ok(as_struct_array(result.as_ref())?.clone())
}
macro_rules! generic_decimal_to_other_test_cast {
($DECIMAL_ARRAY:ident, $A_TYPE:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr,$CAST_OPTIONS:expr) => {{
let schema = Schema::new(vec![Field::new("a", $A_TYPE, true)]);
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new($DECIMAL_ARRAY)],
)?;
let expression =
cast_with_options(col("a", &schema)?, &schema, $TYPE, $CAST_OPTIONS)?;
assert_eq!(format!("CAST(a@0 AS {})", $TYPE), format!("{}", expression));
assert_eq!(expression.data_type(&schema)?, $TYPE);
let result = expression
.evaluate(&batch)?
.into_array(batch.num_rows())
.expect("Failed to convert to array");
assert_eq!(*result.data_type(), $TYPE);
let result = result
.as_any()
.downcast_ref::<$TYPEARRAY>()
.expect("failed to downcast");
for (i, x) in $VEC.iter().enumerate() {
match x {
Some(x) => assert_eq!(result.value(i), *x),
None => assert!(result.is_null(i)),
}
}
}};
}
macro_rules! generic_test_cast {
($A_ARRAY:ident, $A_TYPE:expr, $A_VEC:expr, $TYPEARRAY:ident, $TYPE:expr, $VEC:expr, $CAST_OPTIONS:expr) => {{
let schema = Schema::new(vec![Field::new("a", $A_TYPE, true)]);
let a_vec_len = $A_VEC.len();
let a = $A_ARRAY::from($A_VEC);
let batch =
RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
let expression =
cast_with_options(col("a", &schema)?, &schema, $TYPE, $CAST_OPTIONS)?;
assert_eq!(format!("CAST(a@0 AS {})", $TYPE), format!("{}", expression));
assert_eq!(expression.data_type(&schema)?, $TYPE);
let result = expression
.evaluate(&batch)?
.into_array(batch.num_rows())
.expect("Failed to convert to array");
assert_eq!(*result.data_type(), $TYPE);
assert_eq!(result.len(), a_vec_len);
let result = result
.as_any()
.downcast_ref::<$TYPEARRAY>()
.expect("failed to downcast");
for (i, x) in $VEC.iter().enumerate() {
match x {
Some(x) => assert_eq!(result.value(i), *x),
None => assert!(result.is_null(i)),
}
}
}};
}
#[test]
fn test_cast_decimal_to_decimal() -> Result<()> {
let array = vec![
Some(1234),
Some(2222),
Some(3),
Some(4000),
Some(5000),
None,
];
let decimal_array = array
.clone()
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 3)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 3),
Decimal128Array,
Decimal128(20, 6),
[
Some(1_234_000),
Some(2_222_000),
Some(3_000),
Some(4_000_000),
Some(5_000_000),
None
],
None
);
let decimal_array = array
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 3)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 3),
Decimal128Array,
Decimal128(10, 2),
[Some(123), Some(222), Some(0), Some(400), Some(500), None],
None
);
Ok(())
}
#[test]
fn test_cast_decimal_to_decimal_overflow() -> Result<()> {
let array = vec![Some(123456789)];
let decimal_array = array
.clone()
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 3)?;
let schema = Schema::new(vec![Field::new("a", Decimal128(10, 3), false)]);
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(decimal_array)],
)?;
let expression =
cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2), None)?;
let e = expression.evaluate(&batch).unwrap_err().strip_backtrace(); assert_snapshot!(e, @r"
Failed to cast field 'a' from Decimal128(10, 3) to Decimal128(6, 2)
caused by
Arrow error: Invalid argument error: 123456.79 is too large to store in a Decimal128 of precision 6. Max is 9999.99
");
let expression_safe = cast_with_options(
col("a", &schema)?,
&schema,
Decimal128(6, 2),
Some(DEFAULT_SAFE_CAST_OPTIONS),
)?;
let result_safe = expression_safe
.evaluate(&batch)?
.into_array(batch.num_rows())
.expect("failed to convert to array");
assert!(result_safe.is_null(0));
Ok(())
}
#[test]
fn test_cast_decimal_to_numeric() -> Result<()> {
let array = vec![Some(1), Some(2), Some(3), Some(4), Some(5), None];
let decimal_array = array
.clone()
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 0)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 0),
Int8Array,
Int8,
[
Some(1_i8),
Some(2_i8),
Some(3_i8),
Some(4_i8),
Some(5_i8),
None
],
None
);
let decimal_array = array
.clone()
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 0)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 0),
Int16Array,
Int16,
[
Some(1_i16),
Some(2_i16),
Some(3_i16),
Some(4_i16),
Some(5_i16),
None
],
None
);
let decimal_array = array
.clone()
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 0)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 0),
Int32Array,
Int32,
[
Some(1_i32),
Some(2_i32),
Some(3_i32),
Some(4_i32),
Some(5_i32),
None
],
None
);
let decimal_array = array
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 0)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 0),
Int64Array,
Int64,
[
Some(1_i64),
Some(2_i64),
Some(3_i64),
Some(4_i64),
Some(5_i64),
None
],
None
);
let array = vec![
Some(1234),
Some(2222),
Some(3),
Some(4000),
Some(5000),
None,
];
let decimal_array = array
.clone()
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(10, 3)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(10, 3),
Float32Array,
Float32,
[
Some(1.234_f32),
Some(2.222_f32),
Some(0.003_f32),
Some(4.0_f32),
Some(5.0_f32),
None
],
None
);
let decimal_array = array
.into_iter()
.collect::<Decimal128Array>()
.with_precision_and_scale(20, 6)?;
generic_decimal_to_other_test_cast!(
decimal_array,
Decimal128(20, 6),
Float64Array,
Float64,
[
Some(0.001234_f64),
Some(0.002222_f64),
Some(0.000003_f64),
Some(0.004_f64),
Some(0.005_f64),
None
],
None
);
Ok(())
}
#[test]
fn test_cast_numeric_to_decimal() -> Result<()> {
generic_test_cast!(
Int8Array,
Int8,
vec![1, 2, 3, 4, 5],
Decimal128Array,
Decimal128(3, 0),
[Some(1), Some(2), Some(3), Some(4), Some(5)],
None
);
generic_test_cast!(
Int16Array,
Int16,
vec![1, 2, 3, 4, 5],
Decimal128Array,
Decimal128(5, 0),
[Some(1), Some(2), Some(3), Some(4), Some(5)],
None
);
generic_test_cast!(
Int32Array,
Int32,
vec![1, 2, 3, 4, 5],
Decimal128Array,
Decimal128(10, 0),
[Some(1), Some(2), Some(3), Some(4), Some(5)],
None
);
generic_test_cast!(
Int64Array,
Int64,
vec![1, 2, 3, 4, 5],
Decimal128Array,
Decimal128(20, 0),
[Some(1), Some(2), Some(3), Some(4), Some(5)],
None
);
generic_test_cast!(
Int64Array,
Int64,
vec![1, 2, 3, 4, 5],
Decimal128Array,
Decimal128(20, 2),
[Some(100), Some(200), Some(300), Some(400), Some(500)],
None
);
generic_test_cast!(
Float32Array,
Float32,
vec![1.5, 2.5, 3.0, 1.123_456_8, 5.50],
Decimal128Array,
Decimal128(10, 2),
[Some(150), Some(250), Some(300), Some(112), Some(550)],
None
);
generic_test_cast!(
Float64Array,
Float64,
vec![1.5, 2.5, 3.0, 1.123_456_8, 5.50],
Decimal128Array,
Decimal128(20, 4),
[
Some(15000),
Some(25000),
Some(30000),
Some(11235),
Some(55000)
],
None
);
Ok(())
}
#[test]
fn test_cast_i32_u32() -> Result<()> {
generic_test_cast!(
Int32Array,
Int32,
vec![1, 2, 3, 4, 5],
UInt32Array,
UInt32,
[
Some(1_u32),
Some(2_u32),
Some(3_u32),
Some(4_u32),
Some(5_u32)
],
None
);
Ok(())
}
#[test]
fn test_cast_i32_utf8() -> Result<()> {
generic_test_cast!(
Int32Array,
Int32,
vec![1, 2, 3, 4, 5],
StringArray,
Utf8,
[Some("1"), Some("2"), Some("3"), Some("4"), Some("5")],
None
);
Ok(())
}
#[test]
fn test_cast_i64_t64() -> Result<()> {
let original = vec![1, 2, 3, 4, 5];
let expected: Vec<Option<i64>> = original
.iter()
.map(|i| Some(Time64NanosecondArray::from(vec![*i]).value(0)))
.collect();
generic_test_cast!(
Int64Array,
Int64,
original,
TimestampNanosecondArray,
Timestamp(TimeUnit::Nanosecond, None),
expected,
None
);
Ok(())
}
#[test]
fn invalid_cast() {
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
let result = cast(
col("a", &schema).unwrap(),
&schema,
Interval(IntervalUnit::MonthDayNano),
);
result.expect_err("expected Invalid CAST");
}
#[test]
fn invalid_cast_with_options_error() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
let a = StringArray::from(vec!["9.1"]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
let expression = cast_with_options(col("a", &schema)?, &schema, Int32, None)?;
let result = expression.evaluate(&batch);
let error = result.expect_err("expected error").strip_backtrace();
assert_eq!(
error,
"Failed to cast field 'a' from Utf8 to Int32\n\
caused by\n\
Arrow error: Cast error: Cannot cast string '9.1' to value of Int32 type"
);
Ok(())
}
#[test]
fn invalid_cast_with_empty_field_name_uses_expression() {
let schema = Schema::new(vec![Field::new("", Utf8, false)]);
let batch = RecordBatch::try_new(
Arc::new(schema.clone()),
vec![Arc::new(StringArray::from(vec!["9.1"]))],
)
.expect("valid record batch");
let expression = cast_with_options(
col("", &schema).expect("valid column"),
&schema,
Int32,
None,
)
.expect("valid cast expression");
let error = expression
.evaluate(&batch)
.expect_err("expected error")
.strip_backtrace();
assert_eq!(
error,
"Failed to cast expression '@0' from Utf8 to Int32\n\
caused by\n\
Arrow error: Cast error: Cannot cast string '9.1' to value of Int32 type"
);
}
#[test]
fn field_aware_cast_preserves_target_field_semantics() -> Result<()> {
let metadata = HashMap::from([("target_meta".to_string(), "1".to_string())]);
for (child_nullable, target_nullable) in [(true, false), (false, true)] {
let schema = Schema::new(vec![Field::new("a", Int32, child_nullable)]);
let target_field = Arc::new(
Field::new("cast_target", Int64, target_nullable)
.with_metadata(metadata.clone()),
);
let expr = CastExpr::new_with_target_field(
col("a", &schema)?,
Arc::clone(&target_field),
None,
);
let field = expr.return_field(&schema)?;
assert_eq!(field.name(), "a");
assert_eq!(field.data_type(), &Int64);
assert_eq!(field.is_nullable(), target_nullable);
assert_eq!(
field.metadata().get("target_meta"),
Some(&"1".to_string()),
"Target metadata should be preserved exactly"
);
assert_eq!(expr.nullable(&schema)?, child_nullable || target_nullable);
}
Ok(())
}
#[test]
fn target_field_accessor_returns_the_constructed_field() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", Int32, true)]);
let metadata = HashMap::from([("target_meta".to_string(), "1".to_string())]);
let target_field =
Arc::new(Field::new("cast_target", Int64, false).with_metadata(metadata));
let expr = CastExpr::new_with_target_field(
col("a", &schema)?,
Arc::clone(&target_field),
None,
);
assert_eq!(expr.target_field(), &target_field);
assert_eq!(expr.cast_type(), &Int64);
assert_eq!(expr.target_metadata(), Some(target_field.metadata()));
assert_eq!(expr.target_nullable(), Some(false));
assert!(expr.has_explicit_metadata());
assert!(expr.has_explicit_nullability());
let type_only = CastExpr::new(col("a", &schema)?, Int64, None);
assert_eq!(type_only.cast_type(), &Int64);
assert_eq!(type_only.target_metadata(), None);
assert_eq!(type_only.target_nullable(), None);
assert!(!type_only.has_explicit_metadata());
assert!(!type_only.has_explicit_nullability());
Ok(())
}
#[test]
fn type_only_cast_preserves_legacy_field_name_and_nullability() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
let expr = CastExpr::new(col("a", &schema)?, Int64, None);
let field = expr.return_field(&schema)?;
assert_eq!(field.name(), "a");
assert_eq!(field.data_type(), &Int64);
assert!(!field.is_nullable());
assert!(!expr.nullable(&schema)?);
Ok(())
}
#[test]
fn struct_cast_validation_uses_nested_target_fields() -> Result<()> {
let source_type = Struct(Fields::from(vec![
Arc::new(Field::new("x", Int32, true)),
Arc::new(Field::new("y", Utf8, true)),
]));
let schema = Schema::new(vec![Field::new("a", source_type.clone(), true)]);
let valid_target = Struct(Fields::from(vec![
Arc::new(Field::new("y", Utf8, true)),
Arc::new(Field::new("x", Int64, true)),
]));
cast_with_options(col("a", &schema)?, &schema, valid_target, None)?;
let invalid_target = Struct(Fields::from(vec![
Arc::new(Field::new("y", Utf8, true)),
Arc::new(Field::new("missing", Int64, false)),
]));
let err = cast_with_options(col("a", &schema)?, &schema, invalid_target, None)
.expect_err("missing required struct field should fail");
assert!(err.to_string().contains("Unsupported CAST"));
Ok(())
}
#[test]
fn field_aware_cast_struct_array_missing_child() -> Result<()> {
let source_a = Field::new("a", Int32, true);
let source_b = Field::new("b", Utf8, true);
let target_field = Field::new(
"s",
Struct(
vec![
Arc::new(Field::new("a", Int64, true)),
Arc::new(Field::new("c", Utf8, true)),
]
.into(),
),
true,
);
let struct_array = cast_struct_array(
"s",
Field::new(
"s",
Struct(
vec![Arc::new(source_a.clone()), Arc::new(source_b.clone())].into(),
),
true,
),
target_field,
make_struct_array(
vec![Arc::new(source_a), Arc::new(source_b)].into(),
vec![
Arc::new(Int32Array::from(vec![Some(1), None])) as ArrayRef,
Arc::new(StringArray::from(vec![Some("alpha"), Some("beta")]))
as ArrayRef,
],
),
)?;
let cast_a = as_int64_array(struct_array.column_by_name("a").unwrap().as_ref())?;
assert_eq!(cast_a.value(0), 1);
assert!(cast_a.is_null(1));
let cast_c = as_string_array(struct_array.column_by_name("c").unwrap().as_ref())?;
assert!(cast_c.is_null(0));
assert!(cast_c.is_null(1));
Ok(())
}
#[test]
fn field_aware_cast_nested_struct_array() -> Result<()> {
let inner_source = Field::new(
"inner",
Struct(vec![Arc::new(Field::new("x", Int32, true))].into()),
true,
);
let inner_target = Field::new(
"inner",
Struct(
vec![
Arc::new(Field::new("x", Int64, true)),
Arc::new(Field::new("y", Boolean, true)),
]
.into(),
),
true,
);
let target_field =
Field::new("root", Struct(vec![Arc::new(inner_target)].into()), true);
let inner_struct = make_struct_array(
vec![Arc::new(Field::new("x", Int32, true))].into(),
vec![Arc::new(Int32Array::from(vec![Some(7), None])) as ArrayRef],
);
let outer_struct = make_struct_array(
vec![Arc::new(inner_source.clone())].into(),
vec![Arc::new(inner_struct) as ArrayRef],
);
let struct_array = cast_struct_array(
"root",
Field::new("root", Struct(vec![Arc::new(inner_source)].into()), true),
target_field,
outer_struct,
)?;
let inner =
as_struct_array(struct_array.column_by_name("inner").unwrap().as_ref())?;
let x = as_int64_array(inner.column_by_name("x").unwrap().as_ref())?;
assert_eq!(x.value(0), 7);
assert!(x.is_null(1));
let y = as_boolean_array(inner.column_by_name("y").unwrap().as_ref())?;
assert!(y.is_null(0));
assert!(y.is_null(1));
Ok(())
}
#[test]
fn field_aware_cast_struct_scalar() -> Result<()> {
let source_field = Field::new("a", Int32, true);
let target_field = Field::new(
"s",
Struct(vec![Arc::new(Field::new("a", UInt8, true))].into()),
true,
);
let schema = Arc::new(Schema::new(vec![Field::new(
"s",
Struct(vec![Arc::new(source_field.clone())].into()),
true,
)]));
let scalar_struct = make_struct_array(
vec![Arc::new(source_field)].into(),
vec![Arc::new(Int32Array::from(vec![Some(9)])) as ArrayRef],
);
let literal = Arc::new(crate::expressions::Literal::new(ScalarValue::Struct(
Arc::new(scalar_struct),
)));
let target_field = Arc::new(target_field);
let expr = CastExpr::new_with_target_field(literal, target_field, None);
let batch = RecordBatch::new_empty(schema);
let result = expr.evaluate(&batch)?;
let ColumnarValue::Scalar(ScalarValue::Struct(array)) = result else {
panic!("expected struct scalar");
};
let casted = as_uint8_array(array.column_by_name("a").unwrap().as_ref())?;
assert_eq!(casted.value(0), 9);
Ok(())
}
#[test]
#[ignore = "TODO: https://github.com/apache/datafusion/issues/5396"]
fn test_cast_decimal() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", Int64, false)]);
let a = Int64Array::from(vec![100]);
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![Arc::new(a)])?;
let expression =
cast_with_options(col("a", &schema)?, &schema, Decimal128(38, 38), None)?;
expression.evaluate(&batch)?;
Ok(())
}
#[test]
fn test_fmt_sql() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", Int32, true)]);
let expr = cast(col("a", &schema)?, &schema, Int64)?;
let display_string = expr.to_string();
assert_eq!(display_string, "CAST(a@0 AS Int64)");
let sql_string = fmt_sql(expr.as_ref()).to_string();
assert_eq!(sql_string, "CAST(a AS Int64)");
let schema = Schema::new(vec![Field::new("b", Utf8, true)]);
let expr = cast(col("b", &schema)?, &schema, Int32)?;
let display_string = expr.to_string();
assert_eq!(display_string, "CAST(b@0 AS Int32)");
let sql_string = fmt_sql(expr.as_ref()).to_string();
assert_eq!(sql_string, "CAST(b AS Int32)");
Ok(())
}
#[test]
fn test_display_explicit_target_nullability() -> Result<()> {
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
let expr = CastExpr::new_with_target_field(
col("a", &schema)?,
Arc::new(Field::new("a", Int32, true)),
None,
);
assert_eq!(expr.to_string(), "CAST(a@0 AS Int32;N)");
assert_eq!(fmt_sql(&expr).to_string(), "CAST(a AS Int32)");
let expr = CastExpr::new_with_target_field(
col("a", &schema)?,
Arc::new(Field::new("a", Int32, false)),
None,
);
assert_eq!(expr.to_string(), "CAST(a@0 AS Int32)");
let expr = CastExpr::new(col("a", &schema)?, Int64, None);
assert_eq!(expr.to_string(), "CAST(a@0 AS Int64)");
Ok(())
}
#[test]
fn type_only_cast_strips_extension_metadata() -> Result<()> {
let source_meta = HashMap::from([
(
EXTENSION_TYPE_NAME_KEY.to_string(),
"arrow.uuid".to_string(),
),
("custom_key".to_string(), "custom_value".to_string()),
]);
let schema = Schema::new(vec![
Field::new("a", FixedSizeBinary(16), false).with_metadata(source_meta),
]);
let expr = CastExpr::new(col("a", &schema)?, Utf8, None);
let field = expr.return_field(&schema)?;
assert!(
field.metadata().get(EXTENSION_TYPE_NAME_KEY).is_none(),
"Type-only cast should strip extension type name from source"
);
assert_eq!(
field.metadata().get("custom_key"),
Some(&"custom_value".to_string()),
"Type-only cast should preserve non-extension metadata"
);
Ok(())
}
#[test]
fn field_aware_cast_uses_exact_target_metadata() -> Result<()> {
let source_meta = HashMap::from([
(
EXTENSION_TYPE_NAME_KEY.to_string(),
"source.type".to_string(),
),
("source_key".to_string(), "source_value".to_string()),
]);
let target_meta = HashMap::from([
(
EXTENSION_TYPE_NAME_KEY.to_string(),
"target.type".to_string(),
),
(
EXTENSION_TYPE_METADATA_KEY.to_string(),
"target_ext_meta".to_string(),
),
("target_key".to_string(), "target_value".to_string()),
]);
let schema = Schema::new(vec![
Field::new("a", FixedSizeBinary(16), false).with_metadata(source_meta),
]);
let target_field =
Arc::new(Field::new("b", Utf8, true).with_metadata(target_meta));
let expr = CastExpr::new_with_target_field(
col("a", &schema)?,
Arc::clone(&target_field),
None,
);
let field = expr.return_field(&schema)?;
assert_eq!(
field.metadata().get(EXTENSION_TYPE_NAME_KEY),
Some(&"target.type".to_string()),
"Field-aware cast should use target's extension type name"
);
assert_eq!(
field.metadata().get(EXTENSION_TYPE_METADATA_KEY),
Some(&"target_ext_meta".to_string()),
"Field-aware cast should use target's extension type metadata"
);
assert!(
field.metadata().get("source_key").is_none(),
"Field-aware cast should NOT preserve source metadata"
);
assert_eq!(
field.metadata().get("target_key"),
Some(&"target_value".to_string()),
"Field-aware cast should preserve target's non-extension metadata"
);
Ok(())
}
#[test]
fn test_integer_date_cast_preserves_values_and_ordering() {
use arrow::array::{Date32Array, Date64Array};
use arrow::compute::SortOptions;
use datafusion_expr_common::sort_properties::SortProperties;
let values = vec![None, Some(i32::MIN), Some(-1), Some(0), Some(i32::MAX)];
let integers: ArrayRef = Arc::new(Int32Array::from(values.clone()));
let dates: ArrayRef = Arc::new(Date32Array::from(values));
let values = vec![None, Some(i64::MIN), Some(-1), Some(0), Some(i64::MAX)];
let integers64: ArrayRef = Arc::new(Int64Array::from(values.clone()));
let dates64: ArrayRef = Arc::new(Date64Array::from(values));
for (input, expected) in [
(Arc::clone(&integers), Arc::clone(&dates)),
(dates, integers),
(Arc::clone(&integers64), Arc::clone(&dates64)),
(dates64, integers64),
] {
let schema = Arc::new(Schema::new(vec![Field::new(
"a",
input.data_type().clone(),
true,
)]));
let expr = CastExpr::new(
col("a", &schema).expect("column exists"),
expected.data_type().clone(),
None,
);
assert!(expr.is_bigger_cast(input.data_type()));
let child = ExprProperties::new_unknown()
.with_range(
Interval::make_unbounded(input.data_type())
.expect("supported interval type"),
)
.with_order(SortProperties::Ordered(SortOptions::default()))
.with_strictly_order_preserving(true);
let properties = expr
.get_properties(std::slice::from_ref(&child))
.expect("cast properties");
assert_eq!(properties.sort_properties, child.sort_properties);
assert!(properties.strictly_order_preserving);
assert_eq!(properties.range.data_type(), *expected.data_type());
let batch =
RecordBatch::try_new(schema, vec![input]).expect("valid input batch");
let actual = expr
.evaluate(&batch)
.expect("cast succeeds")
.into_array(batch.num_rows())
.expect("array result");
assert_eq!(actual.as_ref(), expected.as_ref());
}
}
#[test]
fn test_byte_representation_cast_preserves_values_and_ordering() -> Result<()> {
use arrow::array::{
BinaryArray, BinaryViewArray, LargeBinaryArray, StringViewArray,
};
use arrow::compute::SortOptions;
use datafusion_expr_common::sort_properties::SortProperties;
let strings = vec![
None,
Some(""),
Some("a"),
Some("a longer shared string"),
Some("a longer shared string"),
Some("🦀"),
];
let bytes: Vec<Option<&[u8]>> = vec![
None,
Some(b""),
Some(b"\0"),
Some(b"a longer shared byte string"),
Some(b"a longer shared byte string"),
Some(b"\xff"),
];
let binary: ArrayRef = Arc::new(BinaryArray::from(bytes.clone()));
let cases: [(ArrayRef, ArrayRef); 3] = [
(
Arc::new(StringArray::from(strings.clone())),
Arc::new(StringViewArray::from(strings)),
),
(
Arc::clone(&binary),
Arc::new(LargeBinaryArray::from(bytes.clone())),
),
(binary, Arc::new(BinaryViewArray::from(bytes))),
];
for (input, expected) in cases {
let schema = Arc::new(Schema::new(vec![Field::new(
"a",
input.data_type().clone(),
true,
)]));
let expr =
CastExpr::new(col("a", &schema)?, expected.data_type().clone(), None);
assert!(expr.is_bigger_cast(input.data_type()));
for descending in [false, true] {
for nulls_first in [false, true] {
let child = ExprProperties::new_unknown()
.with_range(Interval::make_unbounded(input.data_type())?)
.with_order(SortProperties::Ordered(SortOptions {
descending,
nulls_first,
}))
.with_strictly_order_preserving(true);
let properties = expr.get_properties(std::slice::from_ref(&child))?;
assert_eq!(properties.sort_properties, child.sort_properties);
assert!(properties.strictly_order_preserving);
assert_eq!(properties.range.data_type(), *expected.data_type());
}
}
let batch = RecordBatch::try_new(schema, vec![input])?;
let actual = expr.evaluate(&batch)?.into_array(batch.num_rows())?;
assert_eq!(actual.as_ref(), expected.as_ref());
}
for (source, target) in [
(Utf8View, Utf8),
(LargeBinary, Binary),
(BinaryView, Binary),
] {
assert!(!CastExpr::check_bigger_cast(&target, &source));
}
Ok(())
}
#[test]
fn test_unsigned_to_signed_cast_preserves_values_and_ordering() {
use arrow::array::{UInt8Array, UInt16Array, UInt32Array};
use arrow::compute::SortOptions;
use datafusion_expr_common::sort_properties::SortProperties;
let inputs: [(ArrayRef, Vec<DataType>, i64); 3] = [
(
Arc::new(UInt8Array::from(vec![
None,
Some(0),
Some(1),
Some(u8::MAX),
])),
vec![Int16, Int32, Int64],
i64::from(u8::MAX),
),
(
Arc::new(UInt16Array::from(vec![
None,
Some(0),
Some(1),
Some(u16::MAX),
])),
vec![Int32, Int64],
i64::from(u16::MAX),
),
(
Arc::new(UInt32Array::from(vec![
None,
Some(0),
Some(1),
Some(u32::MAX),
])),
vec![Int64],
i64::from(u32::MAX),
),
];
for (input, target_types, max) in inputs {
let schema = Arc::new(Schema::new(vec![Field::new(
"a",
input.data_type().clone(),
true,
)]));
let batch =
RecordBatch::try_new(Arc::clone(&schema), vec![Arc::clone(&input)])
.expect("valid input batch");
for target_type in target_types {
let expr =
CastExpr::new(col("a", &schema).unwrap(), target_type.clone(), None);
let actual = expr
.evaluate(&batch)
.unwrap()
.into_array(batch.num_rows())
.unwrap();
for (index, value) in
[None, Some(0), Some(1), Some(max)].into_iter().enumerate()
{
let expected = match target_type {
Int16 => {
ScalarValue::Int16(value.map(|v| i16::try_from(v).unwrap()))
}
Int32 => {
ScalarValue::Int32(value.map(|v| i32::try_from(v).unwrap()))
}
Int64 => ScalarValue::Int64(value),
_ => unreachable!(),
};
assert_eq!(
ScalarValue::try_from_array(&actual, index).unwrap(),
expected
);
}
for descending in [false, true] {
for nulls_first in [false, true] {
let child = ExprProperties::new_unknown()
.with_range(
Interval::make_unbounded(input.data_type()).unwrap(),
)
.with_order(SortProperties::Ordered(SortOptions {
descending,
nulls_first,
}))
.with_strictly_order_preserving(true);
let properties =
expr.get_properties(std::slice::from_ref(&child)).unwrap();
assert_eq!(properties.sort_properties, child.sort_properties);
assert!(properties.strictly_order_preserving);
assert_eq!(properties.range.data_type(), target_type);
}
}
}
}
}
#[test]
fn test_temporal_cast_ordering() {
use TimeUnit::*;
use arrow::compute::SortOptions;
use datafusion_expr_common::sort_properties::SortProperties;
let timezone = Some("America/Goose_Bay".into());
let cases = [
(Date32, Date64, true),
(Date64, Date32, true),
(Date32, Timestamp(Nanosecond, None), true),
(Date64, Timestamp(Second, None), true),
(Timestamp(Second, None), Date32, true),
(Timestamp(Second, Some("UTC".into())), Date32, true),
(Timestamp(Second, Some("+08:00".into())), Date32, true),
(Timestamp(Second, timezone.clone()), Date32, false),
(Timestamp(Second, timezone.clone()), Date64, true),
(Timestamp(Second, None), Timestamp(Nanosecond, None), true),
(Timestamp(Nanosecond, None), Timestamp(Second, None), true),
(
Timestamp(Second, None),
Timestamp(Second, timezone.clone()),
false,
),
(
Timestamp(Second, None),
Timestamp(Second, Some("+08:00".into())),
true,
),
(
Timestamp(Second, timezone.clone()),
Timestamp(Second, None),
true,
),
(
Timestamp(Second, timezone.clone()),
Timestamp(Millisecond, Some("UTC".into())),
true,
),
(Timestamp(Second, None), Time32(Second), false),
(Timestamp(Nanosecond, timezone), Time64(Nanosecond), false),
(Time32(Second), Time32(Millisecond), true),
(Time32(Millisecond), Time32(Second), true),
(Time32(Second), Time64(Nanosecond), true),
(Time64(Nanosecond), Time64(Microsecond), true),
(Time64(Microsecond), Time64(Nanosecond), false),
(Time64(Nanosecond), Time32(Second), false),
(Duration(Second), Duration(Nanosecond), true),
(Duration(Nanosecond), Duration(Second), true),
(Null, Timestamp(Second, None), false),
];
for (source, target, preserves_order) in cases {
let schema = Schema::new(vec![Field::new("a", source.clone(), true)]);
let expr = CastExpr::new(
col("a", &schema).expect("column exists"),
target.clone(),
None,
);
for descending in [false, true] {
for nulls_first in [false, true] {
let ordered = SortProperties::Ordered(SortOptions {
descending,
nulls_first,
});
let child = ExprProperties::new_unknown()
.with_range(
Interval::make_unbounded(&source)
.expect("supported interval type"),
)
.with_order(ordered)
.with_strictly_order_preserving(true);
let properties =
expr.get_properties(&[child]).expect("cast properties");
assert_eq!(
properties.sort_properties,
if preserves_order {
ordered
} else {
SortProperties::Unordered
},
"{source} -> {target}, descending={descending}, nulls_first={nulls_first}"
);
assert_eq!(properties.range.data_type(), target);
assert!(!properties.strictly_order_preserving);
}
}
}
}
#[test]
fn test_cast_ordering_with_null_on_failure() {
use arrow::array::TimestampSecondArray;
use arrow::compute::SortOptions;
use datafusion_expr_common::sort_properties::SortProperties;
let source = Timestamp(TimeUnit::Second, None);
let target = Timestamp(TimeUnit::Nanosecond, None);
let schema = Arc::new(Schema::new(vec![Field::new("a", source.clone(), true)]));
let batch = RecordBatch::try_new(
Arc::clone(&schema),
vec![Arc::new(TimestampSecondArray::from(vec![
None,
Some(0),
Some(i64::MAX),
]))],
)
.expect("valid input batch");
let child = ExprProperties::new_unknown()
.with_range(
Interval::make_unbounded(&source).expect("supported interval type"),
)
.with_order(SortProperties::Ordered(SortOptions::default()))
.with_strictly_order_preserving(true);
let expr = CastExpr::new(
col("a", &schema).expect("column exists"),
target.clone(),
Some(DEFAULT_SAFE_CAST_OPTIONS),
);
let actual = expr
.evaluate(&batch)
.expect("safe cast succeeds")
.into_array(batch.num_rows())
.expect("array result");
let expected = TimestampNanosecondArray::from(vec![None, Some(0), None]);
assert_eq!(actual.as_ref(), &expected);
let properties = expr
.get_properties(std::slice::from_ref(&child))
.expect("safe cast properties");
assert_eq!(properties.sort_properties, SortProperties::Unordered);
assert_eq!(properties.range.data_type(), target);
assert!(!properties.strictly_order_preserving);
let expr = CastExpr::new(col("a", &schema).expect("column exists"), target, None);
assert!(expr.evaluate(&batch).is_err());
assert_eq!(
expr.get_properties(&[child])
.expect("cast properties")
.sort_properties,
SortProperties::Ordered(SortOptions::default())
);
let schema = Schema::new(vec![Field::new("a", Int32, true)]);
let child = ExprProperties::new_unknown()
.with_range(
Interval::make_unbounded(&Int32).expect("supported interval type"),
)
.with_order(SortProperties::Ordered(SortOptions::default()))
.with_strictly_order_preserving(true);
let expr = CastExpr::new(
col("a", &schema).expect("column exists"),
Int64,
Some(DEFAULT_SAFE_CAST_OPTIONS),
);
let properties = expr
.get_properties(std::slice::from_ref(&child))
.expect("safe lossless cast properties");
assert_eq!(properties.sort_properties, child.sort_properties);
assert!(properties.strictly_order_preserving);
}
#[test]
fn test_check_bigger_cast_precision_loss() {
use DataType::*;
assert!(CastExpr::check_bigger_cast(&Int16, &Int8));
assert!(CastExpr::check_bigger_cast(&Int64, &Int32));
assert!(CastExpr::check_bigger_cast(&Float32, &Int16));
assert!(CastExpr::check_bigger_cast(&Float32, &UInt16));
assert!(CastExpr::check_bigger_cast(&Float64, &Int32));
assert!(CastExpr::check_bigger_cast(&Float64, &UInt32));
assert!(CastExpr::check_bigger_cast(&LargeUtf8, &Utf8));
assert!(!CastExpr::check_bigger_cast(&Float32, &Int32));
assert!(!CastExpr::check_bigger_cast(&Float32, &UInt32));
assert!(!CastExpr::check_bigger_cast(&Float64, &Int64));
assert!(!CastExpr::check_bigger_cast(&Float64, &UInt64));
assert!(!CastExpr::check_bigger_cast(&UInt16, &Int8));
assert!(!CastExpr::check_bigger_cast(&UInt32, &Int16));
assert!(!CastExpr::check_bigger_cast(&Int8, &UInt8));
assert!(!CastExpr::check_bigger_cast(&Int16, &UInt16));
assert!(!CastExpr::check_bigger_cast(&Int32, &UInt32));
assert!(!CastExpr::check_bigger_cast(&Int64, &UInt64));
assert!(!CastExpr::check_bigger_cast(&Int8, &UInt16));
}
}
#[cfg(all(test, feature = "proto"))]
mod proto_tests {
use super::*;
use crate::expressions::{Column, col};
use crate::proto_test_util::{
StubDecoder, StubEncoder, UnreachableDecoder, column_node,
};
use arrow::datatypes::Field;
use datafusion_common::DataFusionError;
use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx;
use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx;
use datafusion_proto_models::datafusion_common::ArrowType;
use datafusion_proto_models::protobuf::{
PhysicalCastNode, PhysicalExprNode, physical_expr_node,
};
#[derive(Debug)]
struct TestFormatterFactory;
impl ArrayFormatterFactory for TestFormatterFactory {
fn create_array_formatter<'formatter>(
&self,
_array: &'formatter dyn arrow::array::Array,
_options: &FormatOptions<'formatter>,
_field: Option<&'formatter Field>,
) -> std::result::Result<
Option<arrow::util::display::ArrayFormatter<'formatter>>,
arrow::error::ArrowError,
> {
Ok(None)
}
}
static TEST_FORMATTER_FACTORY: TestFormatterFactory = TestFormatterFactory;
fn proto_cast_fixture() -> CastExpr {
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
CastExpr::new(col("a", &schema).unwrap(), Int64, None)
}
fn proto_int64_arrow_type() -> ArrowType {
(&Int64).try_into().unwrap()
}
fn proto_cast_node(
expr: Option<Box<PhysicalExprNode>>,
arrow_type: Option<ArrowType>,
) -> PhysicalExprNode {
PhysicalExprNode {
expr_id: None,
expr_type: Some(physical_expr_node::ExprType::Cast(Box::new(
PhysicalCastNode {
expr,
arrow_type,
target_field: None,
cast_options: None,
},
))),
}
}
fn encode_cast(cast: &CastExpr) -> PhysicalCastNode {
let encoder = StubEncoder::ok();
let node = cast
.try_to_proto(&PhysicalExprEncodeCtx::new(&encoder))
.unwrap()
.expect("CastExpr should encode to Some(node)");
match node.expr_type {
Some(physical_expr_node::ExprType::Cast(cast)) => *cast,
other => panic!("expected a Cast node, got {other:?}"),
}
}
fn round_trip_cast(cast: &CastExpr, schema: &Schema) -> CastExpr {
let encoder = StubEncoder::ok();
let node = cast
.try_to_proto(&PhysicalExprEncodeCtx::new(&encoder))
.unwrap()
.expect("CastExpr should encode to Some(node)");
let decoder = StubDecoder::ok();
CastExpr::try_from_proto(&node, &PhysicalExprDecodeCtx::new(schema, &decoder))
.unwrap()
.downcast_ref::<CastExpr>()
.expect("decoded expr should be a CastExpr")
.clone()
}
fn non_default_cast_options() -> CastOptions<'static> {
CastOptions {
safe: true,
format_options: FormatOptions::new()
.with_display_error(false)
.with_null("NULL")
.with_date_format(Some("%d/%m/%Y"))
.with_datetime_format(Some("%d/%m/%Y %H:%M:%S"))
.with_timestamp_format(Some("%s"))
.with_timestamp_tz_format(Some("%+"))
.with_time_format(Some("%H-%M-%S"))
.with_duration_format(DurationFormat::ISO8601)
.with_types_info(true)
.with_quoted_strings(true),
}
}
#[test]
fn try_to_proto_encodes_cast_expr() {
let cast = proto_cast_fixture();
let encoder = StubEncoder::ok();
let ctx = PhysicalExprEncodeCtx::new(&encoder);
let node = cast
.try_to_proto(&ctx)
.unwrap()
.expect("CastExpr should encode to Some(node)");
assert!(node.expr_id.is_none());
let cast_node = match node.expr_type {
Some(physical_expr_node::ExprType::Cast(cast_node)) => *cast_node,
other => panic!("expected a Cast node, got {other:?}"),
};
assert!(cast_node.expr.is_some());
let arrow_type = cast_node
.arrow_type
.as_ref()
.expect("cast type should be encoded");
let data_type: DataType = arrow_type.try_into().unwrap();
assert_eq!(data_type, Int64);
assert!(cast_node.target_field.is_none());
assert!(cast_node.cast_options.is_none());
}
#[test]
fn cast_target_field_survives_proto_round_trip() {
let schema = Schema::new(vec![Field::new("a", Int32, true)]);
let target = Arc::new(Field::new("target", Int64, false).with_metadata(
Metadata::from([("extension".to_string(), "value".to_string())]),
));
let cast = CastExpr::new_with_target_field(
col("a", &schema).unwrap(),
Arc::clone(&target),
None,
);
assert!(encode_cast(&cast).target_field.is_some());
let decoded = round_trip_cast(&cast, &schema);
assert_eq!(decoded.target_field(), &target);
assert_eq!(decoded.target_metadata(), Some(target.metadata()));
assert_eq!(decoded.target_nullable(), Some(false));
assert!(!decoded.return_field(&schema).unwrap().is_nullable());
}
#[test]
fn explicit_default_shaped_cast_target_is_encoded() {
let schema = Schema::new(vec![Field::new("a", Int32, false).with_metadata(
Metadata::from([("source".to_string(), "value".to_string())]),
)]);
let cast = CastExpr::new_with_target_field(
col("a", &schema).unwrap(),
Int64.into_nullable_field_ref(),
None,
);
assert!(encode_cast(&cast).target_field.is_some());
let decoded = round_trip_cast(&cast, &schema);
assert_eq!(decoded.target_metadata(), Some(&Metadata::default()));
assert_eq!(decoded.target_nullable(), Some(true));
let output = decoded.return_field(&schema).unwrap();
assert!(output.metadata().is_empty());
assert!(output.is_nullable());
}
#[test]
fn type_only_cast_target_is_omitted_and_remains_type_only() {
let metadata = Metadata::from([("source".to_string(), "value".to_string())]);
let schema = Schema::new(vec![
Field::new("a", Int32, false).with_metadata(metadata.clone()),
]);
let cast = CastExpr::new(col("a", &schema).unwrap(), Int64, None);
assert!(encode_cast(&cast).target_field.is_none());
let decoded = round_trip_cast(&cast, &schema);
assert_eq!(decoded.target_metadata(), None);
assert_eq!(decoded.target_nullable(), None);
let output = decoded.return_field(&schema).unwrap();
assert_eq!(output.metadata(), &metadata);
assert!(!output.is_nullable());
}
#[test]
fn cast_options_survive_proto_round_trip() {
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
let options = non_default_cast_options();
let cast =
CastExpr::new(col("a", &schema).unwrap(), Int64, Some(options.clone()));
assert!(encode_cast(&cast).cast_options.is_some());
assert_eq!(round_trip_cast(&cast, &schema).cast_options(), options);
}
#[test]
fn custom_formatter_factory_is_rejected() {
let schema = Schema::new(vec![Field::new("a", Int32, false)]);
let cast = CastExpr::new(
col("a", &schema).unwrap(),
Int64,
Some(CastOptions {
safe: false,
format_options: DEFAULT_FORMAT_OPTIONS
.with_formatter_factory(Some(&TEST_FORMATTER_FACTORY)),
}),
);
let encoder = StubEncoder::ok();
let err = cast
.try_to_proto(&PhysicalExprEncodeCtx::new(&encoder))
.unwrap_err();
assert!(matches!(
err,
DataFusionError::NotImplemented(msg)
if msg.contains("custom formatter_factory")
));
}
#[test]
fn try_to_proto_propagates_child_encode_error() {
let cast = proto_cast_fixture();
let encoder = StubEncoder::failing_on(1);
let ctx = PhysicalExprEncodeCtx::new(&encoder);
let err = cast.try_to_proto(&ctx).unwrap_err();
assert!(matches!(
err,
DataFusionError::Internal(msg) if msg.contains("call 1")
));
}
#[test]
fn try_from_proto_decodes_legacy_cast_expr() {
let node = proto_cast_node(
Some(Box::new(column_node("a"))),
Some(proto_int64_arrow_type()),
);
let schema = Schema::empty();
let decoder = StubDecoder::ok();
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
let decoded = CastExpr::try_from_proto(&node, &ctx).unwrap();
let cast = decoded
.downcast_ref::<CastExpr>()
.expect("decoded expr should be a CastExpr");
assert_eq!(cast.cast_type(), &Int64);
assert!(cast.expr().downcast_ref::<Column>().is_some());
assert_eq!(cast.target_metadata(), None);
assert_eq!(cast.target_nullable(), None);
assert_eq!(cast.cast_options(), DEFAULT_CAST_OPTIONS);
}
#[test]
fn try_from_proto_rejects_mismatched_target_field_type() {
let mut node = proto_cast_node(
Some(Box::new(column_node("a"))),
Some(proto_int64_arrow_type()),
);
let Some(physical_expr_node::ExprType::Cast(cast)) = node.expr_type.as_mut()
else {
unreachable!()
};
cast.target_field =
Some((&Field::new("target", Int32, true)).try_into().unwrap());
let schema = Schema::empty();
let decoder = StubDecoder::ok();
let err = CastExpr::try_from_proto(
&node,
&PhysicalExprDecodeCtx::new(&schema, &decoder),
)
.unwrap_err();
assert!(err.to_string().contains("target_field type"));
}
#[test]
fn try_from_proto_rejects_non_cast_node() {
let node = column_node("a");
let schema = Schema::empty();
let decoder = UnreachableDecoder;
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
assert!(matches!(
err,
DataFusionError::Internal(msg)
if msg.contains("PhysicalExprNode is not a CastExpr")
));
}
#[test]
fn try_from_proto_rejects_missing_expr() {
let node = proto_cast_node(None, Some(proto_int64_arrow_type()));
let schema = Schema::empty();
let decoder = UnreachableDecoder;
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
assert!(matches!(
err,
DataFusionError::Internal(msg)
if msg.contains("CastExpr is missing required field 'expr'")
));
}
#[test]
fn try_from_proto_rejects_missing_arrow_type() {
let node = proto_cast_node(Some(Box::new(column_node("a"))), None);
let schema = Schema::empty();
let decoder = StubDecoder::ok();
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
assert!(matches!(
err,
DataFusionError::Internal(msg)
if msg.contains("CastExpr is missing required field 'arrow_type'")
));
}
#[test]
fn try_from_proto_propagates_child_decode_error() {
let node = proto_cast_node(
Some(Box::new(column_node("a"))),
Some(proto_int64_arrow_type()),
);
let schema = Schema::empty();
let decoder = StubDecoder::failing_on(1);
let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
let err = CastExpr::try_from_proto(&node, &ctx).unwrap_err();
assert!(matches!(
err,
DataFusionError::Internal(msg) if msg.contains("call 1")
));
}
}