Ramin Gharib created FLINK-41788:
------------------------------------
Summary: Arithmetic of a FLOAT with a DOUBLE or DECIMAL gets an
inconsistent result type
Key: FLINK-41788
URL: https://issues.apache.org/jira/browse/FLINK-41788
Project: Flink
Issue Type: Bug
Components: Table SQL / API, Table SQL / Planner
Affects Versions: 2.3.0
Reporter: Ramin Gharib
In SQL, {{f * x}} for a FLOAT {{f}} and a DOUBLE {{x}} is a FLOAT, while {{x *
f}} is a DOUBLE. The FLOAT result loses precision and overflows to Infinity.
{code:sql}
SELECT f, x, f * x AS fx, x * f AS xf, TYPEOF(f * x) AS tfx, TYPEOF(x * f) AS
txf
FROM (VALUES (CAST(0.1 AS FLOAT), CAST(0.1 AS DOUBLE)), (CAST(1.5 AS FLOAT),
CAST(1E300 AS DOUBLE))) AS t(f, x);
{code}
||f||x||Expected f * x||Actual f * x||x * f||
|0.1|0.1|0.010000000149011612|0.010000001|0.010000000149011612|
|1.5|1.0E300|1.5E300|Infinity|1.5E300|
{{TYPEOF(f * x)}} is {{FLOAT NOT NULL}} and {{TYPEOF(x * f)}} is {{DOUBLE NOT
NULL}}. The same for {{+}} and {{-}}, in batch and streaming mode.
The Table API disagrees with SQL:
* {{$("f").times($("x"))}} declares a DOUBLE, but the value is still computed
as a FLOAT. It returns {{Infinity}} for {{3.0E38}} times {{10}}, where
{{$("x").times($("f"))}} returns {{3.000000005497756E39}}.
* {{$("d").plus($("f"))}} for a DECIMAL {{d}} fails when the statement runs,
also with {{times}}. {{$("f").plus($("d"))}} and SQL {{d + f}} give a DOUBLE.
{noformat}
Column types of query result and sink for '*anonymous_collect$1*' do not match.
Cause: Incompatible types for sink column 'd_f' at position 1.
Query schema: [d_f: DOUBLE]
Sink schema: [d_f: FLOAT]
{noformat}
DuckDB, PostgreSQL, Trino and Spark give a DOUBLE for a FLOAT with a DOUBLE in
either order.
h3. Proposal
A FLOAT with a DOUBLE gives a DOUBLE in either order and is computed as a
DOUBLE, and a DECIMAL with a FLOAT gives a DOUBLE, in SQL and the Table API
alike. Compiled plans store the result type, so the fix needs to keep the
current type for plans compiled before it.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)