Apache DataFusion SQL Query Engine
fix: Add coercion rules for Float16 types (#15816)
Ed Seidl3fa111e67b9f
diff --git a/datafusion/expr-common/src/type_coercion/binary.rs b/datafusion/expr-common/src/type_coercion/binary.rsindex 491e3c2f8b..9ab83dd0bf 100644--- a/datafusion/expr-common/src/type_coercion/binary.rs+++ b/datafusion/expr-common/src/type_coercion/binary.rs@@ -931,6 +931,7 @@ fn coerce_numeric_type_to_decimal(numeric_type: &DataType) -> Option<DataType> {Int32 | UInt32 => Some(Decimal128(10, 0)),Int64 | UInt64 => Some(Decimal128(20, 0)),// TODO if we convert the floating-point data to the decimal type, it maybe overflow.+ Float16 => Some(Decimal128(6, 3)),Float32 => Some(Decimal128(14, 7)),Float64 => Some(Decimal128(30, 15)),_ => None,@@ -949,6 +950,7 @@ fn coerce_numeric_type_to_decimal256(numeric_type: &DataType) -> Option<DataTypeInt32 | UInt32 => Some(Decimal256(10, 0)),Int64 | UInt64 => Some(Decimal256(20, 0)),// TODO if we convert the floating-point data to the decimal type, it maybe overflow.+ Float16 => Some(Decimal256(6, 3)),Float32 => Some(Decimal256(14, 7)),Float64 => Some(Decimal256(30, 15)),_ => None,@@ -1044,6 +1046,7 @@ fn numerical_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataTymatch (lhs_type, rhs_type) {(Float64, _) | (_, Float64) => Some(Float64),(_, Float32) | (Float32, _) => Some(Float32),+ (_, Float16) | (Float16, _) => Some(Float16),// The following match arms encode the following logic: Given the two// integral types, we choose the narrowest possible integral type that// accommodates all values of both types. Note that to avoid information@@ -1578,6 +1581,10 @@ mod tests {coerce_numeric_type_to_decimal(&DataType::Int64).unwrap(),DataType::Decimal128(20, 0));+ assert_eq!(+ coerce_numeric_type_to_decimal(&DataType::Float16).unwrap(),+ DataType::Decimal128(6, 3)+ );assert_eq!(coerce_numeric_type_to_decimal(&DataType::Float32).unwrap(),DataType::Decimal128(14, 7)@@ -2056,6 +2063,13 @@ mod tests {Operator::Plus,Float32);+ // (_, Float16) | (Float16, _) => Some(Float16),+ test_coercion_binary_rule_multiple!(+ Float16,+ [Float16, Int64, UInt64, Int32, UInt32, Int16, UInt16, Int8, UInt8],+ Operator::Plus,+ Float16+ );// (UInt64, Int64 | Int32 | Int16 | Int8) | (Int64 | Int32 | Int16 | Int8, UInt64) => Some(Decimal128(20, 0)),test_coercion_binary_rule_multiple!(UInt64,@@ -2194,6 +2208,18 @@ mod tests {DataType::Boolean);// float+ test_coercion_binary_rule!(+ DataType::Float16,+ DataType::Int64,+ Operator::Eq,+ DataType::Float16+ );+ test_coercion_binary_rule!(+ DataType::Float16,+ DataType::Float64,+ Operator::Eq,+ DataType::Float64+ );test_coercion_binary_rule!(DataType::Float32,DataType::Int64,diff --git a/datafusion/sqllogictest/test_files/float16.slt b/datafusion/sqllogictest/test_files/float16.sltnew file mode 100644index 0000000000..5e59c730f0--- /dev/null+++ b/datafusion/sqllogictest/test_files/float16.slt@@ -0,0 +1,92 @@+# 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.++# Basic tests Tests for Float16 Type++statement ok+create table floats as values (1.0), (2.0), (3.0), (NULL), ('Nan');++statement ok+create table float16s as select arrow_cast(column1, 'Float16') as column1 from floats;++query RT+select column1, arrow_typeof(column1) as type from float16s;+----+1 Float16+2 Float16+3 Float16+NULL Float16+NaN Float16++# Test coercions with arithmetic++query RRRRRR+SELECT+ column1 + 1::tinyint as column1_plus_int8,+ column1 + 1::smallint as column1_plus_int16,+ column1 + 1::int as column1_plus_int32,+ column1 + 1::bigint as column1_plus_int64,+ column1 + 1.0::float as column1_plus_float32,+ column1 + 1.0 as column1_plus_float64+FROM float16s;+----+2 2 2 2 2 2+3 3 3 3 3 3+4 4 4 4 4 4+NULL NULL NULL NULL NULL NULL+NaN NaN NaN NaN NaN NaN++# Try coercing with literal NULL+query error+select column1 + NULL from float16s;+----+DataFusion error: type_coercion+caused by+Error during planning: Cannot automatically convert Null to Float16+++# Test coercions with equality+query BBBBBB+SELECT+ column1 = 1::tinyint as column1_equals_int8,+ column1 = 1::smallint as column1_equals_int16,+ column1 = 1::int as column1_equals_int32,+ column1 = 1::bigint as column1_equals_int64,+ column1 = 1.0::float as column1_equals_float32,+ column1 = 1.0 as column1_equals_float64+FROM float16s;+----+true true true true true true+false false false false false false+false false false false false false+NULL NULL NULL NULL NULL NULL+false false false false false false+++# Try coercing with literal NULL+query error+select column1 = NULL from float16s;+----+DataFusion error: Error during planning: Cannot infer common argument type for comparison operation Float16 = Null+++# Cleanup+statement ok+drop table floats;++statement ok+drop table float16s;