-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split expr type and null info to be expr-schemable
- Loading branch information
Showing
7 changed files
with
191 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use super::Expr; | ||
use crate::field_util::get_indexed_field; | ||
use crate::physical_plan::{ | ||
aggregates, expressions::binary_operator_data_type, functions, window_functions, | ||
}; | ||
use arrow::datatypes::DataType; | ||
use datafusion_common::{DataFusionError, ExprSchema, Result}; | ||
|
||
/// trait to allow expr to typable with respect to a schema | ||
pub trait ExprSchemable { | ||
/// given a schema, return the type of the expr | ||
fn get_type<S: ExprSchema>(&self, schema: &S) -> Result<DataType>; | ||
|
||
/// given a schema, return the nullability of the expr | ||
fn nullable<S: ExprSchema>(&self, input_schema: &S) -> Result<bool>; | ||
} | ||
|
||
impl ExprSchemable for Expr { | ||
/// Returns the [arrow::datatypes::DataType] of the expression | ||
/// based on [ExprSchema] | ||
/// | ||
/// Note: [DFSchema] implements [ExprSchema]. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function errors when it is not possible to compute its | ||
/// [arrow::datatypes::DataType]. This happens when e.g. the | ||
/// expression refers to a column that does not exist in the | ||
/// schema, or when the expression is incorrectly typed | ||
/// (e.g. `[utf8] + [bool]`). | ||
fn get_type<S: ExprSchema>(&self, schema: &S) -> Result<DataType> { | ||
match self { | ||
Expr::Alias(expr, _) | Expr::Sort { expr, .. } | Expr::Negative(expr) => { | ||
expr.get_type(schema) | ||
} | ||
Expr::Column(c) => Ok(schema.data_type(c)?.clone()), | ||
Expr::ScalarVariable(_) => Ok(DataType::Utf8), | ||
Expr::Literal(l) => Ok(l.get_datatype()), | ||
Expr::Case { when_then_expr, .. } => when_then_expr[0].1.get_type(schema), | ||
Expr::Cast { data_type, .. } | Expr::TryCast { data_type, .. } => { | ||
Ok(data_type.clone()) | ||
} | ||
Expr::ScalarUDF { fun, args } => { | ||
let data_types = args | ||
.iter() | ||
.map(|e| e.get_type(schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
Ok((fun.return_type)(&data_types)?.as_ref().clone()) | ||
} | ||
Expr::ScalarFunction { fun, args } => { | ||
let data_types = args | ||
.iter() | ||
.map(|e| e.get_type(schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
functions::return_type(fun, &data_types) | ||
} | ||
Expr::WindowFunction { fun, args, .. } => { | ||
let data_types = args | ||
.iter() | ||
.map(|e| e.get_type(schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
window_functions::return_type(fun, &data_types) | ||
} | ||
Expr::AggregateFunction { fun, args, .. } => { | ||
let data_types = args | ||
.iter() | ||
.map(|e| e.get_type(schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
aggregates::return_type(fun, &data_types) | ||
} | ||
Expr::AggregateUDF { fun, args, .. } => { | ||
let data_types = args | ||
.iter() | ||
.map(|e| e.get_type(schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
Ok((fun.return_type)(&data_types)?.as_ref().clone()) | ||
} | ||
Expr::Not(_) | ||
| Expr::IsNull(_) | ||
| Expr::Between { .. } | ||
| Expr::InList { .. } | ||
| Expr::IsNotNull(_) => Ok(DataType::Boolean), | ||
Expr::BinaryExpr { | ||
ref left, | ||
ref right, | ||
ref op, | ||
} => binary_operator_data_type( | ||
&left.get_type(schema)?, | ||
op, | ||
&right.get_type(schema)?, | ||
), | ||
Expr::Wildcard => Err(DataFusionError::Internal( | ||
"Wildcard expressions are not valid in a logical query plan".to_owned(), | ||
)), | ||
Expr::GetIndexedField { ref expr, key } => { | ||
let data_type = expr.get_type(schema)?; | ||
|
||
get_indexed_field(&data_type, key).map(|x| x.data_type().clone()) | ||
} | ||
} | ||
} | ||
|
||
/// Returns the nullability of the expression based on [ExprSchema]. | ||
/// | ||
/// Note: [DFSchema] implements [ExprSchema]. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function errors when it is not possible to compute its | ||
/// nullability. This happens when the expression refers to a | ||
/// column that does not exist in the schema. | ||
fn nullable<S: ExprSchema>(&self, input_schema: &S) -> Result<bool> { | ||
match self { | ||
Expr::Alias(expr, _) | ||
| Expr::Not(expr) | ||
| Expr::Negative(expr) | ||
| Expr::Sort { expr, .. } | ||
| Expr::Between { expr, .. } | ||
| Expr::InList { expr, .. } => expr.nullable(input_schema), | ||
Expr::Column(c) => input_schema.nullable(c), | ||
Expr::Literal(value) => Ok(value.is_null()), | ||
Expr::Case { | ||
when_then_expr, | ||
else_expr, | ||
.. | ||
} => { | ||
// this expression is nullable if any of the input expressions are nullable | ||
let then_nullable = when_then_expr | ||
.iter() | ||
.map(|(_, t)| t.nullable(input_schema)) | ||
.collect::<Result<Vec<_>>>()?; | ||
if then_nullable.contains(&true) { | ||
Ok(true) | ||
} else if let Some(e) = else_expr { | ||
e.nullable(input_schema) | ||
} else { | ||
Ok(false) | ||
} | ||
} | ||
Expr::Cast { expr, .. } => expr.nullable(input_schema), | ||
Expr::ScalarVariable(_) | ||
| Expr::TryCast { .. } | ||
| Expr::ScalarFunction { .. } | ||
| Expr::ScalarUDF { .. } | ||
| Expr::WindowFunction { .. } | ||
| Expr::AggregateFunction { .. } | ||
| Expr::AggregateUDF { .. } => Ok(true), | ||
Expr::IsNull(_) | Expr::IsNotNull(_) => Ok(false), | ||
Expr::BinaryExpr { | ||
ref left, | ||
ref right, | ||
.. | ||
} => Ok(left.nullable(input_schema)? || right.nullable(input_schema)?), | ||
Expr::Wildcard => Err(DataFusionError::Internal( | ||
"Wildcard expressions are not valid in a logical query plan".to_owned(), | ||
)), | ||
Expr::GetIndexedField { ref expr, key } => { | ||
let data_type = expr.get_type(input_schema)?; | ||
get_indexed_field(&data_type, key).map(|x| x.is_nullable()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.