This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 796c7d1dae feat: support f16 in coercion logic (#18944)
796c7d1dae is described below
commit 796c7d1daea5acd65702eef2884bdac0daf83876
Author: Jeffrey Vo <[email protected]>
AuthorDate: Wed Feb 4 22:53:31 2026 +0900
feat: support f16 in coercion logic (#18944)
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->
- Closes #18943
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
Originally:
> As pointed out by @martin-g, even though we plan to remove `NUMERICS`
(see #18092) we should probably add f16 first so we don't conflate
adding new functionality with refactoring changes.
Updated:
> #19727 removes `NUMERICS` for us, which surfaced a bug where f16
wasn't being coerced to f64. Turns out we didn't have f16 support in the
logic calculating the potential coercions. Fixing this so f16 input to a
signature expected f64 is now allowed and coerced.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
Support coercion of f16 to f64 as specified by signature.
Add tests for regr, percentile & covar functions.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Added tests.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
No.
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
datafusion/expr-common/src/signature.rs | 1 +
.../expr-common/src/type_coercion/aggregates.rs | 1 +
datafusion/expr/src/type_coercion/functions.rs | 6 +++-
datafusion/optimizer/src/analyzer/type_coercion.rs | 2 +-
datafusion/sqllogictest/test_files/aggregate.slt | 35 +++++++++++++++++++++-
5 files changed, 42 insertions(+), 3 deletions(-)
diff --git a/datafusion/expr-common/src/signature.rs
b/datafusion/expr-common/src/signature.rs
index 54bb84f03d..4c766b2cc5 100644
--- a/datafusion/expr-common/src/signature.rs
+++ b/datafusion/expr-common/src/signature.rs
@@ -1585,6 +1585,7 @@ mod tests {
vec![DataType::UInt16, DataType::UInt16],
vec![DataType::UInt32, DataType::UInt32],
vec![DataType::UInt64, DataType::UInt64],
+ vec![DataType::Float16, DataType::Float16],
vec![DataType::Float32, DataType::Float32],
vec![DataType::Float64, DataType::Float64]
]
diff --git a/datafusion/expr-common/src/type_coercion/aggregates.rs
b/datafusion/expr-common/src/type_coercion/aggregates.rs
index 01d093950d..ab4d086e4c 100644
--- a/datafusion/expr-common/src/type_coercion/aggregates.rs
+++ b/datafusion/expr-common/src/type_coercion/aggregates.rs
@@ -42,6 +42,7 @@ pub static NUMERICS: &[DataType] = &[
DataType::UInt16,
DataType::UInt32,
DataType::UInt64,
+ DataType::Float16,
DataType::Float32,
DataType::Float64,
];
diff --git a/datafusion/expr/src/type_coercion/functions.rs
b/datafusion/expr/src/type_coercion/functions.rs
index d839d84c66..90c137de24 100644
--- a/datafusion/expr/src/type_coercion/functions.rs
+++ b/datafusion/expr/src/type_coercion/functions.rs
@@ -852,10 +852,13 @@ fn coerced_from<'a>(
(UInt16, Null | UInt8 | UInt16) => Some(type_into.clone()),
(UInt32, Null | UInt8 | UInt16 | UInt32) => Some(type_into.clone()),
(UInt64, Null | UInt8 | UInt16 | UInt32 | UInt64) =>
Some(type_into.clone()),
+ (Float16, Null | Int8 | Int16 | UInt8 | UInt16 | Float16) => {
+ Some(type_into.clone())
+ }
(
Float32,
Null | Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 |
UInt64
- | Float32,
+ | Float16 | Float32,
) => Some(type_into.clone()),
(
Float64,
@@ -868,6 +871,7 @@ fn coerced_from<'a>(
| UInt16
| UInt32
| UInt64
+ | Float16
| Float32
| Float64
| Decimal32(_, _)
diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs
b/datafusion/optimizer/src/analyzer/type_coercion.rs
index 1e59ceb972..a98678f7cf 100644
--- a/datafusion/optimizer/src/analyzer/type_coercion.rs
+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs
@@ -1892,7 +1892,7 @@ mod test {
.err()
.unwrap()
.strip_backtrace();
- assert!(err.starts_with("Error during planning: Failed to coerce
arguments to satisfy a call to 'avg' function: coercion from Utf8 to the
signature Uniform(1, [Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64,
Float32, Float64]) failed"));
+ assert!(err.starts_with("Error during planning: Failed to coerce
arguments to satisfy a call to 'avg' function: coercion from Utf8 to the
signature Uniform(1, [Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64,
Float16, Float32, Float64]) failed"));
Ok(())
}
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt
b/datafusion/sqllogictest/test_files/aggregate.slt
index ab217b192b..b819fd3477 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -571,6 +571,16 @@ SELECT covar(c2, c12) FROM aggregate_test_100
----
-0.079969012479
+query R
+SELECT covar_pop(arrow_cast(c2, 'Float16'), arrow_cast(c12, 'Float16')) FROM
aggregate_test_100
+----
+-0.079163311005
+
+query R
+SELECT covar(arrow_cast(c2, 'Float16'), arrow_cast(c12, 'Float16')) FROM
aggregate_test_100
+----
+-0.079962940409
+
# single_row_query_covar_1
query R
select covar_samp(sq.column1, sq.column2) from (values (1.1, 2.2)) as sq
@@ -1313,6 +1323,24 @@ select approx_median(arrow_cast(col_f32, 'Float16')),
arrow_typeof(approx_median
----
2.75 Float16
+# This shouldn't be NaN, see:
+# https://github.com/apache/datafusion/issues/18945
+query RT
+select
+ percentile_cont(0.5) within group (order by arrow_cast(col_f32, 'Float16')),
+ arrow_typeof(percentile_cont(0.5) within group (order by arrow_cast(col_f32,
'Float16')))
+from median_table;
+----
+NaN Float16
+
+query RT
+select
+ approx_percentile_cont(0.5) within group (order by arrow_cast(col_f32,
'Float16')),
+ arrow_typeof(approx_percentile_cont(0.5) within group (order by
arrow_cast(col_f32, 'Float16')))
+from median_table;
+----
+2.75 Float16
+
query ?T
select approx_median(NULL), arrow_typeof(approx_median(NULL)) from
median_table;
----
@@ -6719,7 +6747,12 @@ from aggregate_test_100;
----
0.051534002628 0.48427355347 100 0.001929150558 0.479274948239 0.508972509913
6.707779292571 9.234223721582 0.345678715695
-
+query R
+select
+ regr_slope(arrow_cast(c12, 'Float16'), arrow_cast(c11, 'Float16'))
+from aggregate_test_100;
+----
+0.051477733249
# regr_*() functions ignore NULLs
query RRIRRRRRR
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]