Skip to content

EXTRACT(YEAR) comparisons return NULL for a year outside the column's range #25135

Description

@RyanLin5967

Describe the bug

Currently, date_part's preimage rewrite builds a NULL bound for an out-of-range January 1, so the comparison is NULL on every row.

To Reproduce

CREATE VIEW t AS SELECT * FROM (VALUES
  ('2020-06-01T12:00:00'::timestamp),
  ('1995-06-01T12:00:00'::timestamp)) s(ts);

SELECT count(*) FROM t WHERE EXTRACT(YEAR FROM ts) < 9999;
-- 0, should be 2

SELECT count(*) FROM t WHERE EXTRACT(YEAR FROM ts) >= 1000 OR EXTRACT(YEAR FROM ts) = 2020;
-- 1, should be 2, so a wrong non-zero count is reachable too

SELECT CASE WHEN EXTRACT(YEAR FROM ts) < 9999 THEN 'ok' ELSE 'BAD' END FROM t;
-- BAD BAD, so it is not confined to WHERE

SELECT EXTRACT(YEAR FROM ts), EXTRACT(YEAR FROM ts) < 9999 FROM t;
-- 2020 and 1995, both NULL

SELECT count(*) FROM t WHERE EXTRACT(YEAR FROM ts) <= 2262;
-- 0, since <= and > take January 1 of the next year as the bound

SELECT count(*) FROM t WHERE EXTRACT(YEAR FROM ts) >= 1678;
-- 2, so a bound inside 1678..2261 is unaffected

SELECT count(*) FROM (SELECT arrow_cast(ts,'Timestamp(Microsecond, None)') AS ts FROM t)
  WHERE EXTRACT(YEAR FROM ts) < 9999;
-- 2, so only the nanosecond unit's narrower range reaches it

Expected behavior

Each count returns 2 and the CASE returns ok ok. The years the projection prints are the ones the comparison should test.

Additional context

date_to_scalar returns Some(TimestampNanosecond(None, tz)), so the NULL passes the guard.

America/Lima reaches it at 1990 and 1994, where Peru began DST at midnight on January 1.

datafusion-cli 55.0.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions