Ramin Gharib created FLINK-41786:
------------------------------------

             Summary: SIGN and TRUNCATE fail to compile for TINYINT and SMALLINT
                 Key: FLINK-41786
                 URL: https://issues.apache.org/jira/browse/FLINK-41786
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.3.0
            Reporter: Ramin Gharib


SIGN and TRUNCATE fail with a compile error when the argument is a TINYINT or a 
SMALLINT. ROUND works for the same types.

{code:sql}
SELECT n, SIGN(n) AS s FROM (VALUES (CAST(-5 AS TINYINT)), (CAST(3 AS 
TINYINT))) AS t(n);
SELECT n, TRUNCATE(n, -1) AS r FROM (VALUES (CAST(1259 AS SMALLINT)), 
(CAST(-1259 AS SMALLINT))) AS t(n);
{code}

||Call||Expected||Actual||
|{{SIGN(CAST(-5 AS TINYINT))}}|-1|Table program cannot be compiled|
|{{SIGN(CAST(-5 AS SMALLINT))}}|-1|Table program cannot be compiled|
|{{TRUNCATE(CAST(125 AS TINYINT), -1)}}|120|Table program cannot be compiled|
|{{TRUNCATE(CAST(1259 AS SMALLINT), -1)}}|1250|Table program cannot be compiled|
|{{ROUND(CAST(1259 AS SMALLINT), -1)}}|1260|1260|

The root cause is a {{CompileException}}, such as {{Assignment conversion not 
possible from type "int" to type "short"}} for TRUNCATE. For SIGN, the source 
type in the message changes between runs, {{"int"}} or {{"float"}}. The same in 
batch and streaming mode.

h3. Cause
{{FunctionGenerator}} registers ROUND for TINYINT and SMALLINT ({{ROUND_BYTE}}, 
{{ROUND_SHORT}}), but SIGN only for INTEGER, BIGINT, DOUBLE and DECIMAL (lines 
123-130), and TRUNCATE only for INTEGER, BIGINT, DECIMAL, FLOAT and DOUBLE 
(lines 411-427). For a TINYINT or SMALLINT, the lookup picks a method of a 
wider type, and the generated code assigns its result to a {{byte}} or 
{{short}} without a cast.

h3. Proposal
Register TINYINT and SMALLINT for SIGN and TRUNCATE, as for ROUND, so the 
result keeps the type of the argument.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to