From 7e08f2d40ae249c531cd74ef9fb19361761bb8ce Mon Sep 17 00:00:00 2001 From: Nuttiiya Seekhao Date: Thu, 26 Jan 2023 07:52:22 -0800 Subject: [PATCH 1/3] Add NULL literal support for decimal and integers Modified test --- datafusion/substrait/src/consumer.rs | 30 +++++++++++- datafusion/substrait/src/producer.rs | 64 ++++++++++++++++++++++--- datafusion/substrait/tests/roundtrip.rs | 18 ++++++- 3 files changed, 103 insertions(+), 9 deletions(-) diff --git a/datafusion/substrait/src/consumer.rs b/datafusion/substrait/src/consumer.rs index a650592d5636f..a7b849a008917 100644 --- a/datafusion/substrait/src/consumer.rs +++ b/datafusion/substrait/src/consumer.rs @@ -39,10 +39,11 @@ use substrait::proto::{ extensions::simple_extension_declaration::MappingType, function_argument::ArgType, join_rel, plan_rel, + r#type, read_rel::ReadType, rel::RelType, sort_field::{SortDirection, SortKind::*}, - AggregateFunction, Expression, Plan, Rel, + AggregateFunction, Expression, Plan, Rel, Type }; use datafusion::logical_expr::expr::Sort; @@ -655,6 +656,9 @@ pub async fn from_substrait_rex( Some(LiteralType::Binary(b)) => Ok(Arc::new(Expr::Literal( ScalarValue::Binary(Some(b.clone())), ))), + Some(LiteralType::Null(ntype)) => { + Ok(Arc::new(Expr::Literal(from_substrait_null(ntype)?))) + } _ => { return Err(DataFusionError::NotImplemented(format!( "Unsupported literal_type: {:?}", @@ -668,3 +672,27 @@ pub async fn from_substrait_rex( )), } } + +fn from_substrait_null(null_type: &Type) -> Result { + if let Some(kind) = &null_type.kind { + match kind { + r#type::Kind::I8(_) => Ok(ScalarValue::Int8(None)), + r#type::Kind::I16(_) => Ok(ScalarValue::Int16(None)), + r#type::Kind::I32(_) => Ok(ScalarValue::Int32(None)), + r#type::Kind::I64(_) => Ok(ScalarValue::Int64(None)), + r#type::Kind::Decimal(d) => Ok(ScalarValue::Decimal128( + None, + d.precision as u8, + d.scale as i8, + )), + _ => Err(DataFusionError::NotImplemented(format!( + "Unsupported null kind: {:?}", + kind + ))), + } + } else { + return Err(DataFusionError::NotImplemented( + "Null type without kind is not supported".to_string(), + )); + } +} \ No newline at end of file diff --git a/datafusion/substrait/src/producer.rs b/datafusion/substrait/src/producer.rs index 0b05f1dc14885..72eb13fb7fb2b 100644 --- a/datafusion/substrait/src/producer.rs +++ b/datafusion/substrait/src/producer.rs @@ -46,6 +46,7 @@ use substrait::proto::{ }, function_argument::ArgType, join_rel, plan_rel, + r#type, read_rel::{NamedTable, ReadType}, rel::RelType, sort_field::{SortDirection, SortKind}, @@ -380,9 +381,18 @@ pub fn to_substrait_agg_measure( None => None } }) - }, + } + Expr::Alias(expr, _name) => { + to_substrait_agg_measure(expr, schema, extension_info) + } _ => Err(DataFusionError::Internal(format!( +<<<<<<< HEAD "Expression must be compatible with aggregation. Unsupported expression: {expr:?}", +======= + "Expression must be compatible with aggregation. Unsupported expression: {:?}. ExpressionType: {:?}", + expr, + expr.variant_name() +>>>>>>> Add NULL literal support for decimal and integers ))), } } @@ -583,17 +593,13 @@ pub fn to_substrait_rex( precision: *p as i32, scale: *s as i32, })) - } + }, ScalarValue::Utf8(Some(s)) => Some(LiteralType::String(s.clone())), ScalarValue::LargeUtf8(Some(s)) => Some(LiteralType::String(s.clone())), ScalarValue::Binary(Some(b)) => Some(LiteralType::Binary(b.clone())), ScalarValue::LargeBinary(Some(b)) => Some(LiteralType::Binary(b.clone())), ScalarValue::Date32(Some(d)) => Some(LiteralType::Date(*d)), - _ => { - return Err(DataFusionError::NotImplemented(format!( - "Unsupported literal: {value:?}", - ))) - } + _ => Some(try_to_substrait_null(value)?), }; Ok(Expression { rex_type: Some(RexType::Literal(Literal { @@ -610,6 +616,50 @@ pub fn to_substrait_rex( } } +fn try_to_substrait_null(v: &ScalarValue) -> Result { + let default_type_ref = 0; + let default_nullability = r#type::Nullability::Nullable as i32; + match v { + ScalarValue::Int8(None) => Ok(LiteralType::Null(substrait::proto::Type { + kind: Some(r#type::Kind::I8(r#type::I8 { + type_variation_reference: default_type_ref, + nullability: default_nullability, + })), + })), + ScalarValue::Int16(None) => Ok(LiteralType::Null(substrait::proto::Type { + kind: Some(r#type::Kind::I16(r#type::I16 { + type_variation_reference: default_type_ref, + nullability: default_nullability, + })), + })), + ScalarValue::Int32(None) => Ok(LiteralType::Null(substrait::proto::Type { + kind: Some(r#type::Kind::I32(r#type::I32 { + type_variation_reference: default_type_ref, + nullability: default_nullability, + })), + })), + ScalarValue::Int64(None) => Ok(LiteralType::Null(substrait::proto::Type { + kind: Some(r#type::Kind::I64(r#type::I64 { + type_variation_reference: default_type_ref, + nullability: default_nullability, + })), + })), + ScalarValue::Decimal128(None, p, s) => Ok(LiteralType::Null(substrait::proto::Type { + kind: Some(r#type::Kind::Decimal(r#type::Decimal { + scale: *s as i32, + precision: *p as i32, + type_variation_reference: default_type_ref, + nullability: default_nullability, + })), + })), + // TODO: Extend support for remaining data types + _ => Err(DataFusionError::NotImplemented(format!( + "Unsupported literal: {:?}", + v + ))), + } +} + fn substrait_sort_field( expr: &Expr, schema: &DFSchemaRef, diff --git a/datafusion/substrait/tests/roundtrip.rs b/datafusion/substrait/tests/roundtrip.rs index fcf01f6c6eca9..727aa421a381f 100644 --- a/datafusion/substrait/tests/roundtrip.rs +++ b/datafusion/substrait/tests/roundtrip.rs @@ -101,6 +101,11 @@ mod tests { roundtrip("SELECT * FROM data WHERE b > 2.5").await } + #[tokio::test] + async fn null_decimal_literal() -> Result<()> { + roundtrip("SELECT * FROM data WHERE b = NULL").await + } + #[tokio::test] async fn simple_distinct() -> Result<()> { test_alias( @@ -171,6 +176,17 @@ mod tests { .await } + #[tokio::test] + async fn aggregate_case() -> Result<()> { + assert_expected_plan( + "SELECT SUM(CASE WHEN a > 0 THEN 1 ELSE NULL END) FROM data", + "Projection: SUM(CASE WHEN data.a > Int64(0) THEN Int64(1) ELSE Int64(NULL) END)\ + \n Aggregate: groupBy=[[]], aggr=[[SUM(CASE WHEN data.a > Int64(0) THEN Int64(1) ELSE Int64(NULL) END)]]\ + \n TableScan: data projection=[a]", + ) + .await + } + #[tokio::test] async fn roundtrip_inner_join() -> Result<()> { roundtrip("SELECT data.a FROM data JOIN data2 ON data.a = data2.a").await @@ -306,7 +322,7 @@ mod tests { explicit_options.schema = Some(&schema); ctx.register_csv("data", "tests/testdata/data.csv", explicit_options.clone()) .await?; - ctx.register_csv("data2", "tests/testdata/data.csv", explicit_options) + ctx.register_csv("data2", "tests/testdata/data.csv", explicit_options.clone()) .await?; Ok(ctx) } From ecb1f5986cbd2ea3f7ed89f4afde3c4f000db282 Mon Sep 17 00:00:00 2001 From: Nuttiiya Seekhao Date: Thu, 26 Jan 2023 09:27:56 -0800 Subject: [PATCH 2/3] Cargo fmt --- datafusion/substrait/src/consumer.rs | 7 +++---- datafusion/substrait/src/producer.rs | 23 ++++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/datafusion/substrait/src/consumer.rs b/datafusion/substrait/src/consumer.rs index a7b849a008917..1905345bb30ad 100644 --- a/datafusion/substrait/src/consumer.rs +++ b/datafusion/substrait/src/consumer.rs @@ -38,12 +38,11 @@ use substrait::proto::{ }, extensions::simple_extension_declaration::MappingType, function_argument::ArgType, - join_rel, plan_rel, - r#type, + join_rel, plan_rel, r#type, read_rel::ReadType, rel::RelType, sort_field::{SortDirection, SortKind::*}, - AggregateFunction, Expression, Plan, Rel, Type + AggregateFunction, Expression, Plan, Rel, Type, }; use datafusion::logical_expr::expr::Sort; @@ -695,4 +694,4 @@ fn from_substrait_null(null_type: &Type) -> Result { "Null type without kind is not supported".to_string(), )); } -} \ No newline at end of file +} diff --git a/datafusion/substrait/src/producer.rs b/datafusion/substrait/src/producer.rs index 72eb13fb7fb2b..fab1c75947001 100644 --- a/datafusion/substrait/src/producer.rs +++ b/datafusion/substrait/src/producer.rs @@ -45,8 +45,7 @@ use substrait::proto::{ simple_extension_declaration::{ExtensionFunction, MappingType}, }, function_argument::ArgType, - join_rel, plan_rel, - r#type, + join_rel, plan_rel, r#type, read_rel::{NamedTable, ReadType}, rel::RelType, sort_field::{SortDirection, SortKind}, @@ -593,7 +592,7 @@ pub fn to_substrait_rex( precision: *p as i32, scale: *s as i32, })) - }, + } ScalarValue::Utf8(Some(s)) => Some(LiteralType::String(s.clone())), ScalarValue::LargeUtf8(Some(s)) => Some(LiteralType::String(s.clone())), ScalarValue::Binary(Some(b)) => Some(LiteralType::Binary(b.clone())), @@ -644,14 +643,16 @@ fn try_to_substrait_null(v: &ScalarValue) -> Result { nullability: default_nullability, })), })), - ScalarValue::Decimal128(None, p, s) => Ok(LiteralType::Null(substrait::proto::Type { - kind: Some(r#type::Kind::Decimal(r#type::Decimal { - scale: *s as i32, - precision: *p as i32, - type_variation_reference: default_type_ref, - nullability: default_nullability, - })), - })), + ScalarValue::Decimal128(None, p, s) => { + Ok(LiteralType::Null(substrait::proto::Type { + kind: Some(r#type::Kind::Decimal(r#type::Decimal { + scale: *s as i32, + precision: *p as i32, + type_variation_reference: default_type_ref, + nullability: default_nullability, + })), + })) + } // TODO: Extend support for remaining data types _ => Err(DataFusionError::NotImplemented(format!( "Unsupported literal: {:?}", From 7e04fdb5d1868ca203ef083566d78b93e33bc659 Mon Sep 17 00:00:00 2001 From: Nuttiiya Seekhao Date: Thu, 26 Jan 2023 11:38:50 -0800 Subject: [PATCH 3/3] Addressed PR comment Cargo clippy fix Resolved merge conflict --- datafusion/substrait/src/consumer.rs | 21 ++++++++++----------- datafusion/substrait/src/producer.rs | 9 ++------- datafusion/substrait/tests/roundtrip.rs | 4 ++-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/datafusion/substrait/src/consumer.rs b/datafusion/substrait/src/consumer.rs index 1905345bb30ad..e7303e21cd597 100644 --- a/datafusion/substrait/src/consumer.rs +++ b/datafusion/substrait/src/consumer.rs @@ -78,7 +78,7 @@ pub fn name_to_op(name: &str) -> Result { "bitwise_shift_right" => Ok(Operator::BitwiseShiftRight), "bitwise_shift_left" => Ok(Operator::BitwiseShiftLeft), _ => Err(DataFusionError::NotImplemented(format!( - "Unsupported function name: {name:?}", + "Unsupported function name: {name:?}" ))), } } @@ -98,7 +98,7 @@ pub async fn from_substrait_plan( Ok((ext_f.function_anchor, &ext_f.name)) } _ => Err(DataFusionError::NotImplemented(format!( - "Extension type not supported: {ext:?}", + "Extension type not supported: {ext:?}" ))), }, None => Err(DataFusionError::NotImplemented( @@ -429,13 +429,13 @@ fn from_substrait_jointype(join_type: i32) -> Result { join_rel::JoinType::Semi => Ok(JoinType::LeftSemi), _ => { return Err(DataFusionError::Internal(format!( - "unsupported join type {substrait_join_type:?}", + "unsupported join type {substrait_join_type:?}" ))) } } } else { return Err(DataFusionError::Internal(format!( - "invalid join type variant {join_type:?}", + "invalid join type variant {join_type:?}" ))); } } @@ -597,7 +597,7 @@ pub async fn from_substrait_rex( }))) } (l, r) => Err(DataFusionError::NotImplemented(format!( - "Invalid arguments for binary expression: {l:?} and {r:?}", + "Invalid arguments for binary expression: {l:?} and {r:?}" ))), } } @@ -635,12 +635,12 @@ pub async fn from_substrait_rex( ))?; let p = d.precision.try_into().map_err(|e| { DataFusionError::Substrait(format!( - "Failed to parse decimal precision: {e}", + "Failed to parse decimal precision: {e}" )) })?; let s = d.scale.try_into().map_err(|e| { DataFusionError::Substrait(format!( - "Failed to parse decimal scale: {e}", + "Failed to parse decimal scale: {e}" )) })?; Ok(Arc::new(Expr::Literal(ScalarValue::Decimal128( @@ -685,13 +685,12 @@ fn from_substrait_null(null_type: &Type) -> Result { d.scale as i8, )), _ => Err(DataFusionError::NotImplemented(format!( - "Unsupported null kind: {:?}", - kind + "Unsupported null kind: {kind:?}" ))), } } else { - return Err(DataFusionError::NotImplemented( + Err(DataFusionError::NotImplemented( "Null type without kind is not supported".to_string(), - )); + )) } } diff --git a/datafusion/substrait/src/producer.rs b/datafusion/substrait/src/producer.rs index fab1c75947001..3a9c4923b763a 100644 --- a/datafusion/substrait/src/producer.rs +++ b/datafusion/substrait/src/producer.rs @@ -297,7 +297,7 @@ pub fn to_substrait_rel( to_substrait_rel(alias.input.as_ref(), extension_info) } _ => Err(DataFusionError::NotImplemented(format!( - "Unsupported operator: {plan:?}", + "Unsupported operator: {plan:?}" ))), } } @@ -385,13 +385,9 @@ pub fn to_substrait_agg_measure( to_substrait_agg_measure(expr, schema, extension_info) } _ => Err(DataFusionError::Internal(format!( -<<<<<<< HEAD - "Expression must be compatible with aggregation. Unsupported expression: {expr:?}", -======= "Expression must be compatible with aggregation. Unsupported expression: {:?}. ExpressionType: {:?}", expr, expr.variant_name() ->>>>>>> Add NULL literal support for decimal and integers ))), } } @@ -655,8 +651,7 @@ fn try_to_substrait_null(v: &ScalarValue) -> Result { } // TODO: Extend support for remaining data types _ => Err(DataFusionError::NotImplemented(format!( - "Unsupported literal: {:?}", - v + "Unsupported literal: {v:?}" ))), } } diff --git a/datafusion/substrait/tests/roundtrip.rs b/datafusion/substrait/tests/roundtrip.rs index 727aa421a381f..ba4997f4ba871 100644 --- a/datafusion/substrait/tests/roundtrip.rs +++ b/datafusion/substrait/tests/roundtrip.rs @@ -320,9 +320,9 @@ mod tests { Field::new("d", DataType::Boolean, true), ]); explicit_options.schema = Some(&schema); - ctx.register_csv("data", "tests/testdata/data.csv", explicit_options.clone()) + ctx.register_csv("data", "tests/testdata/data.csv", explicit_options) .await?; - ctx.register_csv("data2", "tests/testdata/data.csv", explicit_options.clone()) + ctx.register_csv("data2", "tests/testdata/data.csv", CsvReadOptions::new()) .await?; Ok(ctx) }