Yan Yan created SPARK-62371:
-------------------------------

             Summary: Keep the instants of TIMESTAMP existence defaults in 
every session time zone
                 Key: SPARK-62371
                 URL: https://issues.apache.org/jira/browse/SPARK-62371
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
            Reporter: Yan Yan


When a data file lacks a column, Spark fills it from the column's 
EXISTS_DEFAULT metadata. This should be the fixed value evaluated when the 
column was created or added.

For TIMESTAMP columns, Spark serializes that value through Literal.sql as a 
wall-clock timestamp in the DDL session's time zone, without an offset. A 
reader in another session time zone interprets the stored text as a different 
instant.

h3. Reproduction

{code:sql}
SET spark.sql.timestampType=TIMESTAMP_LTZ;
SET spark.sql.session.timeZone=America/Los_Angeles;
CREATE TABLE t (id INT) USING parquet;
INSERT INTO t VALUES (1);
ALTER TABLE t ADD COLUMNS (
  ts TIMESTAMP DEFAULT TIMESTAMP '2024-06-01 09:00:00'
);
INSERT INTO t (id) VALUES (2);
SET spark.sql.session.timeZone=UTC;
SELECT id, ts FROM t ORDER BY id;
{code}

The ALTER statement and the second INSERT both run in America/Los_Angeles, so 
both rows should represent 2024-06-01 16:00:00Z.

Actual result in UTC:
{code}
1  2024-06-01 09:00:00
2  2024-06-01 16:00:00
{code}

Expected result in UTC:
{code}
1  2024-06-01 16:00:00
2  2024-06-01 16:00:00
{code}

The first row reads its timestamp from EXISTS_DEFAULT because its file predates 
the column. The second row reads a physically stored timestamp. Equality 
filters and joins can consequently miss the first row.

h3. Daylight-saving overlap

The problem also occurs without changing the session time zone. In 
America/Los_Angeles, DEFAULT TIMESTAMP '2024-11-03 09:30:00Z' denotes the 
second occurrence of 01:30, in PST. Literal.sql stores only TIMESTAMP 
'2024-11-03 01:30:00'. Parsing that text in the same time zone selects the 
first occurrence, in PDT, and changes the instant to 08:30:00Z.

h3. Proposed fix

Persist newly generated TIMESTAMP existence defaults as UTC literals with an 
explicit offset, for example TIMESTAMP '2024-06-01 16:00:00Z'. Apply the same 
rendering to TIMESTAMP_LTZ(p) and timestamps nested in arrays, structs and 
maps. CURRENT_DEFAULT evaluation for new inserts is unchanged.

Regression tests cover Parquet, ORC and JSON; three reader time zones; both 
timestampType settings; the daylight-saving overlap; and V1/V2 column 
conversions.

Pull request: https://github.com/apache/spark/pull/60825




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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to