Skip to content

Commit ecb85c5

Browse files
Merge pull request ClickHouse#80301 from ClickHouse/fix-postgres-date
Fix DateTime and DateTime64 for PostgreSQL storage
2 parents b9f0bc5 + 5eb61d0 commit ecb85c5

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/Analyzer/ConstantNode.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ QueryTreeNodePtr ConstantNode::cloneImpl() const
199199

200200
ASTPtr ConstantNode::toASTImpl(const ConvertToASTOptions & options) const
201201
{
202+
if (!options.add_cast_for_constants)
203+
return std::make_shared<ASTLiteral>(getFieldFromColumnForASTLiteral(constant_value.getColumn(), 0, constant_value.getType()));
204+
202205
const auto & constant_value_type = constant_value.getType();
203206
auto constant_value_ast = std::make_shared<ASTLiteral>(getValue());
204207

205-
if (!options.add_cast_for_constants)
206-
return constant_value_ast;
207-
208208
// Add cast if constant was created as a result of constant folding.
209209
// Constant folding may lead to type transformation and literal on shard
210210
// may have a different type.

src/Storages/tests/gtest_transform_query_for_external_database.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ TEST(TransformQueryForExternalDatabase, Issue7245)
301301

302302
check(state, 1, {"apply_id", "apply_type", "apply_status", "create_time"},
303303
"SELECT apply_id FROM test.table WHERE apply_type = 2 AND create_time > addDays(toDateTime('2019-01-01 01:02:03', 'UTC'),-7) AND apply_status IN (3,4)",
304-
R"(SELECT "apply_id", "apply_type", "apply_status", "create_time" FROM "test"."table" WHERE ("apply_type" = 2) AND ("create_time" > '2018-12-25 01:02:03') AND ("apply_status" IN (3, 4)))",
305-
R"(SELECT "apply_id", "apply_type", "apply_status", "create_time" FROM "test"."table" WHERE ("apply_type" = 2) AND ("create_time" > 1545699723) AND ("apply_status" IN (3, 4)))");
304+
R"(SELECT "apply_id", "apply_type", "apply_status", "create_time" FROM "test"."table" WHERE ("apply_type" = 2) AND ("create_time" > '2018-12-25 01:02:03') AND ("apply_status" IN (3, 4)))");
306305
}
307306

308307
TEST(TransformQueryForExternalDatabase, Aliases)
@@ -396,8 +395,7 @@ TEST(TransformQueryForExternalDatabase, ToDate)
396395

397396
check(state, 1, {"a", "b", "foo"},
398397
"SELECT foo FROM table WHERE a=10 AND b=toDate('2019-10-05', 'UTC')",
399-
R"(SELECT "a", "b", "foo" FROM "test"."table" WHERE ("a" = 10) AND ("b" = '2019-10-05'))",
400-
R"(SELECT "a", "b", "foo" FROM "test"."table" WHERE ("a" = 10) AND ("b" = 18174))");
398+
R"(SELECT "a", "b", "foo" FROM "test"."table" WHERE ("a" = 10) AND ("b" = '2019-10-05'))");
401399
}
402400

403401
TEST(TransformQueryForExternalDatabase, Analyzer)
@@ -422,8 +420,7 @@ TEST(TransformQueryForExternalDatabase, Analyzer)
422420

423421
check(state, 1, {"is_value"},
424422
"SELECT is_value FROM table WHERE is_value = true",
425-
R"(SELECT "is_value" FROM "test"."table" WHERE "is_value" = true)",
426-
R"(SELECT "is_value" FROM "test"."table" WHERE "is_value" = 1)");
423+
R"(SELECT "is_value" FROM "test"."table" WHERE "is_value" = true)");
427424

428425
check(state, 1, {"is_value"},
429426
"SELECT is_value FROM table WHERE is_value = 1",

tests/integration/test_storage_postgresql/test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,29 @@ def _create_and_fill_table(table):
869869
cursor.execute(f'DROP TABLE "{table}\'"')
870870

871871

872+
def test_postgres_datetime(started_cluster):
873+
cursor = started_cluster.postgres_conn.cursor()
874+
cursor.execute(f"DROP TABLE IF EXISTS test_datetime")
875+
cursor.execute("CREATE TABLE test_datetime AS (SELECT '2025-01-02 03:04:05.678900'::timestamptz AS ts, '2025-01-02'::date as d)")
876+
877+
node1.query("DROP TABLE IF EXISTS test_datetime")
878+
node1.query(
879+
f"CREATE TABLE test_datetime (ts DateTime64(6, 'UTC'), d Date) ENGINE = PostgreSQL('postgres1:5432', 'postgres', 'test_datetime', 'postgres', '{pg_pass}')"
880+
)
881+
882+
result = node1.query("SELECT ts FROM test_datetime WHERE ts > '2025-01-01'::DateTime")
883+
assert result == "2025-01-02 03:04:05.678900\n"
884+
885+
result = node1.query("SELECT ts FROM test_datetime WHERE ts > '2025-01-01'::DateTime64")
886+
assert result == "2025-01-02 03:04:05.678900\n"
887+
888+
result = node1.query("SELECT ts FROM test_datetime WHERE ts > '2025-01-01'::Nullable(DateTime)")
889+
assert result == "2025-01-02 03:04:05.678900\n"
890+
891+
result = node1.query("SELECT ts FROM test_datetime WHERE ts > '2025-01-01'::Nullable(DateTime64)")
892+
assert result == "2025-01-02 03:04:05.678900\n"
893+
894+
872895
if __name__ == "__main__":
873896
cluster.start()
874897
input("Cluster created, press any key to destroy...")

0 commit comments

Comments
 (0)