Skip to content

Apache DataFusion SQL Query Engine

[substrait] support-try-cast (#13562)

Justin Zhu189a4bb9799f
diff --git a/datafusion/substrait/src/logical_plan/consumer.rs b/datafusion/substrait/src/logical_plan/consumer.rs
index c81fa3e29e..a9e411e35a 100644
--- a/datafusion/substrait/src/logical_plan/consumer.rs
+++ b/datafusion/substrait/src/logical_plan/consumer.rs
@@ -31,7 +31,7 @@ use datafusion::logical_expr::expr::{Exists, InSubquery, Sort};
use datafusion::logical_expr::{
Aggregate, BinaryExpr, Case, EmptyRelation, Expr, ExprSchemable, LogicalPlan,
- Operator, Projection, SortExpr, Values,
+ Operator, Projection, SortExpr, TryCast, Values,
};
use substrait::proto::aggregate_rel::Grouping;
use substrait::proto::expression::subquery::set_predicate::PredicateOp;
@@ -71,6 +71,7 @@ use datafusion::{
use std::collections::HashSet;
use std::sync::Arc;
use substrait::proto::exchange_rel::ExchangeKind;
+use substrait::proto::expression::cast::FailureBehavior::ReturnNull;
use substrait::proto::expression::literal::user_defined::Val;
use substrait::proto::expression::literal::{
interval_day_to_second, IntervalCompound, IntervalDayToSecond, IntervalYearToMonth,
@@ -1682,8 +1683,8 @@ pub async fn from_substrait_rex(
Ok(Expr::Literal(scalar_value))
}
Some(RexType::Cast(cast)) => match cast.as_ref().r#type.as_ref() {
- Some(output_type) => Ok(Expr::Cast(Cast::new(
- Box::new(
+ Some(output_type) => {
+ let input_expr = Box::new(
from_substrait_rex(
state,
cast.as_ref().input.as_ref().unwrap().as_ref(),
@@ -1691,9 +1692,15 @@ pub async fn from_substrait_rex(
extensions,
)
.await?,
- ),
- from_substrait_type_without_names(output_type, extensions)?,
- ))),
+ );
+ let data_type =
+ from_substrait_type_without_names(output_type, extensions)?;
+ if cast.failure_behavior() == ReturnNull {
+ Ok(Expr::TryCast(TryCast::new(input_expr, data_type)))
+ } else {
+ Ok(Expr::Cast(Cast::new(input_expr, data_type)))
+ }
+ }
None => substrait_err!("Cast expression without output type is not allowed"),
},
Some(RexType::WindowFunction(window)) => {
diff --git a/datafusion/substrait/src/logical_plan/producer.rs b/datafusion/substrait/src/logical_plan/producer.rs
index 29019dfd74..c432a14412 100644
--- a/datafusion/substrait/src/logical_plan/producer.rs
+++ b/datafusion/substrait/src/logical_plan/producer.rs
@@ -23,7 +23,7 @@ use substrait::proto::expression_reference::ExprType;
use datafusion::arrow::datatypes::{Field, IntervalUnit};
use datafusion::logical_expr::{
- Distinct, FetchType, Like, Partitioning, SkipType, WindowFrameUnits,
+ Distinct, FetchType, Like, Partitioning, SkipType, TryCast, WindowFrameUnits,
};
use datafusion::{
arrow::datatypes::{DataType, TimeUnit},
@@ -55,6 +55,7 @@ use datafusion::logical_expr::{expr, Between, JoinConstraint, LogicalPlan, Opera
use datafusion::prelude::Expr;
use pbjson_types::Any as ProtoAny;
use substrait::proto::exchange_rel::{ExchangeKind, RoundRobin, ScatterFields};
+use substrait::proto::expression::cast::FailureBehavior;
use substrait::proto::expression::literal::interval_day_to_second::PrecisionMode;
use substrait::proto::expression::literal::map::KeyValue;
use substrait::proto::expression::literal::{
@@ -1182,23 +1183,36 @@ pub fn to_substrait_rex(
rex_type: Some(RexType::IfThen(Box::new(IfThen { ifs, r#else }))),
})
}
- Expr::Cast(Cast { expr, data_type }) => {
- Ok(Expression {
- rex_type: Some(RexType::Cast(Box::new(
- substrait::proto::expression::Cast {
- r#type: Some(to_substrait_type(data_type, true)?),
- input: Some(Box::new(to_substrait_rex(
- state,
- expr,
- schema,
- col_ref_offset,
- extensions,
- )?)),
- failure_behavior: 0, // FAILURE_BEHAVIOR_UNSPECIFIED
- },
- ))),
- })
- }
+ Expr::Cast(Cast { expr, data_type }) => Ok(Expression {
+ rex_type: Some(RexType::Cast(Box::new(
+ substrait::proto::expression::Cast {
+ r#type: Some(to_substrait_type(data_type, true)?),
+ input: Some(Box::new(to_substrait_rex(
+ state,
+ expr,
+ schema,
+ col_ref_offset,
+ extensions,
+ )?)),
+ failure_behavior: FailureBehavior::ThrowException.into(),
+ },
+ ))),
+ }),
+ Expr::TryCast(TryCast { expr, data_type }) => Ok(Expression {
+ rex_type: Some(RexType::Cast(Box::new(
+ substrait::proto::expression::Cast {
+ r#type: Some(to_substrait_type(data_type, true)?),
+ input: Some(Box::new(to_substrait_rex(
+ state,
+ expr,
+ schema,
+ col_ref_offset,
+ extensions,
+ )?)),
+ failure_behavior: FailureBehavior::ReturnNull.into(),
+ },
+ ))),
+ }),
Expr::Literal(value) => to_substrait_literal_expr(value, extensions),
Expr::Alias(Alias { expr, .. }) => {
to_substrait_rex(state, expr, schema, col_ref_offset, extensions)
diff --git a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
index d03ab51820..0141e6a086 100644
--- a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
+++ b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs
@@ -411,6 +411,16 @@ async fn implicit_cast() -> Result<()> {
roundtrip("SELECT * FROM data WHERE a = b").await
}
+#[tokio::test]
+async fn try_cast_decimal_to_int() -> Result<()> {
+ roundtrip("SELECT * FROM data WHERE a = TRY_CAST(b AS int)").await
+}
+
+#[tokio::test]
+async fn try_cast_decimal_to_string() -> Result<()> {
+ roundtrip("SELECT * FROM data WHERE a = TRY_CAST(b AS string)").await
+}
+
#[tokio::test]
async fn aggregate_case() -> Result<()> {
assert_expected_plan(