liukun4515 commented on a change in pull request #1483: URL: https://github.com/apache/arrow-datafusion/pull/1483#discussion_r785622084
########## File path: datafusion/src/physical_plan/coercion_rule/binary_rule.rs ########## @@ -0,0 +1,229 @@ +// 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. + +//! Support the coercion rule for binary operation + +use crate::arrow::datatypes::DataType; +use crate::error::{DataFusionError, Result}; +use crate::logical_plan::Operator; +use crate::physical_plan::expressions::coercion::{ + dictionary_coercion, eq_coercion, is_numeric, like_coercion, numerical_coercion, + string_coercion, temporal_coercion, +}; + +/// Coercion rules for all binary operators. Returns the output type +/// of applying `op` to an argument of `lhs_type` and `rhs_type`. +pub(crate) fn coerce_types( + lhs_type: &DataType, + op: &Operator, + rhs_type: &DataType, +) -> Result<DataType> { + // This result MUST be compatible with `binary_coerce` + let result = match op { + Operator::And | Operator::Or => match (lhs_type, rhs_type) { + // logical binary boolean operators can only be evaluated in bools + (DataType::Boolean, DataType::Boolean) => Some(DataType::Boolean), + _ => None, + }, + // logical equality operators have their own rules, and always return a boolean + Operator::Eq | Operator::NotEq => comparison_eq_coercion(lhs_type, rhs_type), + // order-comparison operators have their own rules + Operator::Lt | Operator::Gt | Operator::GtEq | Operator::LtEq => { + comparison_order_coercion(lhs_type, rhs_type) + } + // "like" operators operate on strings and always return a boolean + Operator::Like | Operator::NotLike => like_coercion(lhs_type, rhs_type), + // for math expressions, the final value of the coercion is also the return type + // because coercion favours higher information types + // TODO: support decimal data type Review comment: https://github.com/apache/arrow-datafusion/pull/1554/files done this comments -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
