|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::logical_plan::producer::{to_substrait_type, SubstraitProducer}; |
| 19 | +use crate::variation_const::DEFAULT_TYPE_VARIATION_REF; |
| 20 | +use datafusion::common::{DFSchemaRef, ScalarValue}; |
| 21 | +use datafusion::logical_expr::{Cast, Expr, TryCast}; |
| 22 | +use substrait::proto::expression::cast::FailureBehavior; |
| 23 | +use substrait::proto::expression::literal::LiteralType; |
| 24 | +use substrait::proto::expression::{Literal, RexType}; |
| 25 | +use substrait::proto::Expression; |
| 26 | + |
| 27 | +pub fn from_cast( |
| 28 | + producer: &mut impl SubstraitProducer, |
| 29 | + cast: &Cast, |
| 30 | + schema: &DFSchemaRef, |
| 31 | +) -> datafusion::common::Result<Expression> { |
| 32 | + let Cast { expr, data_type } = cast; |
| 33 | + // since substrait Null must be typed, so if we see a cast(null, dt), we make it a typed null |
| 34 | + if let Expr::Literal(lit) = expr.as_ref() { |
| 35 | + // only the untyped(a null scalar value) null literal need this special handling |
| 36 | + // since all other kind of nulls are already typed and can be handled by substrait |
| 37 | + // e.g. null::<Int32Type> or null::<Utf8Type> |
| 38 | + if matches!(lit, ScalarValue::Null) { |
| 39 | + let lit = Literal { |
| 40 | + nullable: true, |
| 41 | + type_variation_reference: DEFAULT_TYPE_VARIATION_REF, |
| 42 | + literal_type: Some(LiteralType::Null(to_substrait_type( |
| 43 | + data_type, true, |
| 44 | + )?)), |
| 45 | + }; |
| 46 | + return Ok(Expression { |
| 47 | + rex_type: Some(RexType::Literal(lit)), |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + Ok(Expression { |
| 52 | + rex_type: Some(RexType::Cast(Box::new( |
| 53 | + substrait::proto::expression::Cast { |
| 54 | + r#type: Some(to_substrait_type(data_type, true)?), |
| 55 | + input: Some(Box::new(producer.handle_expr(expr, schema)?)), |
| 56 | + failure_behavior: FailureBehavior::ThrowException.into(), |
| 57 | + }, |
| 58 | + ))), |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +pub fn from_try_cast( |
| 63 | + producer: &mut impl SubstraitProducer, |
| 64 | + cast: &TryCast, |
| 65 | + schema: &DFSchemaRef, |
| 66 | +) -> datafusion::common::Result<Expression> { |
| 67 | + let TryCast { expr, data_type } = cast; |
| 68 | + Ok(Expression { |
| 69 | + rex_type: Some(RexType::Cast(Box::new( |
| 70 | + substrait::proto::expression::Cast { |
| 71 | + r#type: Some(to_substrait_type(data_type, true)?), |
| 72 | + input: Some(Box::new(producer.handle_expr(expr, schema)?)), |
| 73 | + failure_behavior: FailureBehavior::ReturnNull.into(), |
| 74 | + }, |
| 75 | + ))), |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + use crate::logical_plan::producer::to_substrait_extended_expr; |
| 83 | + use datafusion::arrow::datatypes::{DataType, Field}; |
| 84 | + use datafusion::common::DFSchema; |
| 85 | + use datafusion::execution::SessionStateBuilder; |
| 86 | + use datafusion::logical_expr::ExprSchemable; |
| 87 | + use substrait::proto::expression_reference::ExprType; |
| 88 | + |
| 89 | + #[tokio::test] |
| 90 | + async fn fold_cast_null() { |
| 91 | + let state = SessionStateBuilder::default().build(); |
| 92 | + let empty_schema = DFSchemaRef::new(DFSchema::empty()); |
| 93 | + let field = Field::new("out", DataType::Int32, false); |
| 94 | + |
| 95 | + let expr = Expr::Literal(ScalarValue::Null) |
| 96 | + .cast_to(&DataType::Int32, &empty_schema) |
| 97 | + .unwrap(); |
| 98 | + |
| 99 | + let typed_null = |
| 100 | + to_substrait_extended_expr(&[(&expr, &field)], &empty_schema, &state) |
| 101 | + .unwrap(); |
| 102 | + |
| 103 | + if let ExprType::Expression(expr) = |
| 104 | + typed_null.referred_expr[0].expr_type.as_ref().unwrap() |
| 105 | + { |
| 106 | + let lit = Literal { |
| 107 | + nullable: true, |
| 108 | + type_variation_reference: DEFAULT_TYPE_VARIATION_REF, |
| 109 | + literal_type: Some(LiteralType::Null( |
| 110 | + to_substrait_type(&DataType::Int32, true).unwrap(), |
| 111 | + )), |
| 112 | + }; |
| 113 | + let expected = Expression { |
| 114 | + rex_type: Some(RexType::Literal(lit)), |
| 115 | + }; |
| 116 | + assert_eq!(*expr, expected); |
| 117 | + } else { |
| 118 | + panic!("Expected expression type"); |
| 119 | + } |
| 120 | + |
| 121 | + // a typed null should not be folded |
| 122 | + let expr = Expr::Literal(ScalarValue::Int64(None)) |
| 123 | + .cast_to(&DataType::Int32, &empty_schema) |
| 124 | + .unwrap(); |
| 125 | + |
| 126 | + let typed_null = |
| 127 | + to_substrait_extended_expr(&[(&expr, &field)], &empty_schema, &state) |
| 128 | + .unwrap(); |
| 129 | + |
| 130 | + if let ExprType::Expression(expr) = |
| 131 | + typed_null.referred_expr[0].expr_type.as_ref().unwrap() |
| 132 | + { |
| 133 | + let cast_expr = substrait::proto::expression::Cast { |
| 134 | + r#type: Some(to_substrait_type(&DataType::Int32, true).unwrap()), |
| 135 | + input: Some(Box::new(Expression { |
| 136 | + rex_type: Some(RexType::Literal(Literal { |
| 137 | + nullable: true, |
| 138 | + type_variation_reference: DEFAULT_TYPE_VARIATION_REF, |
| 139 | + literal_type: Some(LiteralType::Null( |
| 140 | + to_substrait_type(&DataType::Int64, true).unwrap(), |
| 141 | + )), |
| 142 | + })), |
| 143 | + })), |
| 144 | + failure_behavior: FailureBehavior::ThrowException as i32, |
| 145 | + }; |
| 146 | + let expected = Expression { |
| 147 | + rex_type: Some(RexType::Cast(Box::new(cast_expr))), |
| 148 | + }; |
| 149 | + assert_eq!(*expr, expected); |
| 150 | + } else { |
| 151 | + panic!("Expected expression type"); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments