Skip to content

Date32 values are not correctly read from PostgreSQL #73084

Closed

Description

@PedroTadim

Describe the bug
For larger Date values, the postgresql function should read them as Date32.

How to reproduce
In PostgreSQL run:

CREATE TABLE t0 (c0 DATE);

Then in ClickHouse:

CREATE TABLE t0 (c0 Date32) ENGINE = Memory();
INSERT INTO TABLE t0 (c0) VALUES ('2276-11-21'), ('2269-07-01');
INSERT INTO TABLE FUNCTION postgresql('<host:port>', '<database>', 't0', '<user>', '<password>', '<schema>') SELECT c0 FROM t0;
SELECT tx.c0 FROM postgresql('<host:port>', '<database>', 't0', '<user>', '<password>', '<schema>') tx ORDER BY ALL;
--2090-01-24 wrong
--2097-06-16 wrong

PostgreSQL supports Date32 type ranges.

Version info

Activity

  1. added
    unexpected behaviour
    fuzz
    potential bug
    on Dec 10, 2024
  2. added
    comp-postgresql
    on Dec 10, 2024
  3. bikeshedder commented on Jun 4, 2025

    @bikeshedder

    I ran into the exact same problem where the date 1950-01-01 stored in PostgreSQL is read as 2129-06-07 in ClickHouse. I guess ClickHouse would just need to default to Date32 whenever dealing with a PostgreSQL DATE column.

  4. bikeshedder commented on Jun 4, 2025

    @bikeshedder

    I'm storing dates in the range 1950-01-01 ... 2100-01-01 in my PostgreSQL database. It's not pretty but this workaround works for me at the moment:

    SELECT
        if(
            date_column > makeDate(2100, 1, 1),
            (date_column::int - 65536)::date32,
            date_column
        ) AS date_column
    FROM
        pg.table_name

    It's far from pretty, but it works. I got lucky that my date ranges fit in a 16 bit date value so I can use that workaround.

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

    comp-postgresqlfuzzpotential bugunexpected behaviour

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions