Ramin Gharib created FLINK-41740:
------------------------------------

             Summary: TO_TIMESTAMP_LTZ keeps more fractional digits than its 
precision
                 Key: FLINK-41740
                 URL: https://issues.apache.org/jira/browse/FLINK-41740
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Runtime
    Affects Versions: 2.3.0
            Reporter: Ramin Gharib
            Assignee: Ramin Gharib


When the input string has more fractional digits than the format, 
{{TO_TIMESTAMP_LTZ}} returns a value with more digits than its declared 
precision. The printed value looks right, but comparisons see the extra digits.

{code:sql}
SELECT
  TO_TIMESTAMP_LTZ(s, 'yyyy-MM-dd HH:mm:ss.SSSS') AS ts,
  TO_TIMESTAMP_LTZ(s, 'yyyy-MM-dd HH:mm:ss.SSSS')
    = TO_TIMESTAMP_LTZ('2023-01-01 12:30:00.1234', 'yyyy-MM-dd HH:mm:ss.SSSS') 
AS is_equal
FROM (VALUES ('2023-01-01 12:30:00.12348')) AS t(s);
{code}

||Column||Expected||Actual||
|ts|2023-01-01 12:30:00.1234|2023-01-01 12:30:00.1234|
|is_equal|TRUE|FALSE|

{{ts}} is a {{TIMESTAMP_LTZ(4)}}, but it holds {{2023-01-01 12:30:00.12348}}. 
Batch and streaming mode give the same result on master. A literal input hides 
the bug, because the planner folds the call into a {{TIMESTAMP_LTZ(4)}} literal.

*Cause*

{{DateTimeUtils.parseTimestampData(String, String, int)}} parses with 
{{DateTimeFormatter.ofPattern(format)}}. {{SSSS}} accepts exactly 4 digits, so 
5 digits fail the parse. The fallback then calls {{Timestamp.valueOf}}, which 
keeps up to 9 digits and ignores the precision. The bug shows since FLINK-39244 
added precisions 4 to 9. A binary row stores a timestamp of precision 3 or 
lower as milliseconds only, which drops the extra digits.

*Proposal*

Truncate the fallback result to the precision, as the strict parse and {{CAST(s 
AS TIMESTAMP(4))}} already do. PostgreSQL rounds instead, and Spark and DuckDB 
reject the input. Truncation keeps {{TO_TIMESTAMP_LTZ}} consistent with Flink's 
own casts.

{{TO_TIMESTAMP}} shares the helper and gets precisions above 3 in FLINK-14925, 
so the fix goes into apache/flink#27793 as a separate commit.



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

Reply via email to