[client-v2, jdbc-v2] A Time value that is negative or 24 h or more is read as a time of day (-01:02:03 becomes 22:57:57)

Đang mở
#3,447 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java

Hướng nghiên cứu

Start with the named conversion paths in DataTypeConverter.java, AbstractBinaryFormatReader.java, and JdbcUtils.java, then inspect the jdbc-time-as-sql-time tests in tests/clickhouse-client/allowlist.txt and related issue #3419. Reproduce the negative and 24-hour-or-more cases with the supplied Java program. Done means affected reads return the full server value or report that it cannot be represented, and never silently return a different time; the issue leaves the conversion behavior for java.sql.Time and LocalTime to a maintainer decision.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug client data-types jdbc

Description

A ClickHouse Time or Time64 value can be from -999:59:59 to 999:59:59. client-v2 and jdbc-v2 read a value that is negative or 24 hours or more as its time of day, and report no error: -01:02:03 becomes 22:57:57, and 25:00:00 becomes 01:00:00. Values from 00:00:00 to 23:59:59 are correct.

These reads are affected:

  • client-v2: getString and getLocalTime of GenericRecord (queryAll) and of ClickHouseBinaryFormatReader, also getString of an Array(Time) value ([22:57:57]);
  • jdbc-v2: ResultSet.getString, getObject(col) (the default mapping java.sql.Time, which holds only the time of day: 82677000 ms for -01:02:03), and getObject(col, LocalTime.class).

jdbc-v2 getTime(col) keeps the full value (-3723000 ms for -01:02:03). For Time64, getObject(col) also drops the fraction (01:00:00 for 25:00:00.5).

The same defect was reported for ch-go in https://github.com/ClickHouse/ch-go/issues/1249 and for clickhouse-odbc in https://github.com/ClickHouse/clickhouse-odbc/issues/643.

Steps to reproduce
  1. Start a ClickHouse server.
  2. Run the program in "Code Example" in a JVM with the time zone UTC, with the clickhouse-jdbc -all jar and slf4j-api on the classpath.
Error Log or Exception StackTrace
toTime(-3723)              server=-01:02:03   client-v2: getString=22:57:57 getLocalTime=22:57:57 reader.getString=22:57:57 reader.getLocalTime=22:57:57
toTime(90000)              server=25:00:00    client-v2: getString=01:00:00 getLocalTime=01:00 reader.getString=01:00:00 reader.getLocalTime=01:00
toTime64('25:00:00.5', 1)  server=25:00:00.5  client-v2: getString=01:00:00.5 getLocalTime=01:00:00.500 reader.getString=01:00:00.5 reader.getLocalTime=01:00:00.500
toTime(3723)               server=01:02:03    client-v2: getString=01:02:03 getLocalTime=01:02:03 reader.getString=01:02:03 reader.getLocalTime=01:02:03
[toTime(-3723)]            server=['-01:02:03'] client-v2: getString=[22:57:57]
toTime(-3723)              server=-01:02:03   jdbc-v2: getString=22:57:57 getObject=22:57:57 (82677000 ms) getObject(LocalTime)=22:57:57 | getTime=-3723000 ms
toTime(90000)              server=25:00:00    jdbc-v2: getString=01:00:00 getObject=01:00:00 (3600000 ms) getObject(LocalTime)=01:00 | getTime=90000000 ms
toTime64('25:00:00.5', 1)  server=25:00:00.5  jdbc-v2: getString=01:00:00.5 getObject=01:00:00 (3600000 ms) getObject(LocalTime)=01:00:00.500 | getTime=90000500 ms
toTime(3723)               server=01:02:03    jdbc-v2: getString=01:02:03 getObject=01:02:03 (3723000 ms) getObject(LocalTime)=01:02:03 | getTime=3723000 ms
[toTime(-3723)]            server=['-01:02:03'] jdbc-v2: getString=[22:57:57]
Expected Behaviour

The value that the server gives (toString(t), the server= column above; curl with FORMAT TSV gives the same text): -01:02:03, 25:00:00, 25:00:00.5, ['-01:02:03']. getString must return this text. A LocalTime cannot hold these values, and a java.sql.Time made from a LocalTime cannot either: these reads must return the full value (as getTime(col) does), or throw an error that says that the value does not fit. They must not return a different time.

Root cause:

  • DataTypeConverter.timeToString formats the value with the time-of-day pattern HH:mm:ss (DataTypeConverter.java:156-168), and AbstractBinaryFormatReader.getString converts the value to LocalTime first (AbstractBinaryFormatReader.java:381).
  • client-v2 getLocalTime returns dt.toLocalTime() of the epoch-based LocalDateTime that the reader makes for Time (AbstractBinaryFormatReader.java:1015).
  • jdbc-v2 JdbcUtils.convert makes the java.sql.Time with Time.valueOf(LocalTime.from(value)) (JdbcUtils.java:426), and the LocalTime with LocalTime.from(value) (JdbcUtils.java:416).

DataTypeUtils.durationToTimeString already writes the signed text. The fix needs a maintainer decision for the time-of-day types: return the full value in the java.sql.Time (as getTime does) and throw for LocalTime, or throw for both.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related:

  • #3419 (open): a negative Time64 with a fraction is decoded wrong, in a different function.
  • tests/clickhouse-client/allowlist.txt, group jdbc-time-as-sql-time (from #3389): lists 43 jdbc-v2 core SQL tests for the getObject part ("Issue: not filed yet").
Code Example
Reproduction program (Java, 49 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.query.GenericRecord;
import com.clickhouse.client.api.query.QueryResponse;
import java.sql.*;
import java.time.LocalTime;
import java.util.Properties;

public class Repro {
    static final String[] CASES = {"toTime(-3723)", "toTime(90000)", "toTime64('25:00:00.5', 1)", "toTime(3723)"};

    public static void main(String[] args) throws Exception {
        String pw = System.getenv("CLICKHOUSE_PASSWORD");
        try (Client client = new Client.Builder().addEndpoint("http://clickhouse:8123")
                .setUsername("default").setPassword(pw).build()) {
            for (String c : CASES) {
                String sql = "SELECT " + c + " AS t, toString(t) AS s";
                GenericRecord r = client.queryAll(sql).get(0);
                try (QueryResponse resp = client.query(sql).get();
                     ClickHouseBinaryFormatReader rd = client.newBinaryFormatReader(resp)) {
                    rd.next();
                    System.out.printf("%-26s server=%-11s client-v2: getString=%s getLocalTime=%s reader.getString=%s reader.getLocalTime=%s%n",
                            c, r.getString("s"), r.getString("t"), r.getLocalTime("t"), rd.getString("t"), rd.getLocalTime("t"));
                }
            }
            GenericRecord a = client.queryAll("SELECT [toTime(-3723)] AS a, toString(a) AS s").get(0);
            System.out.printf("%-26s server=%-11s client-v2: getString=%s%n", "[toTime(-3723)]", a.getString("s"), a.getString("a"));
        }
        Properties p = new Properties();
        p.setProperty("user", "default");
        p.setProperty("password", pw);
        try (Connection cn = DriverManager.getConnection("jdbc:clickhouse://clickhouse:8123/default", p);
             Statement st = cn.createStatement()) {
            for (String c : CASES) {
                try (ResultSet rs = st.executeQuery("SELECT " + c + " AS t, toString(t) AS s")) {
                    rs.next();
                    Time t = (Time) rs.getObject("t");
                    System.out.printf("%-26s server=%-11s jdbc-v2: getString=%s getObject=%s (%d ms) getObject(LocalTime)=%s | getTime=%d ms%n",
                            c, rs.getString("s"), rs.getString("t"), t, t.getTime(), rs.getObject("t", LocalTime.class),
                            rs.getTime("t").getTime());
                }
            }
            try (ResultSet rs = st.executeQuery("SELECT [toTime(-3723)] AS a, toString(a) AS s")) {
                rs.next();
                System.out.printf("%-26s server=%-11s jdbc-v2: getString=%s%n", "[toTime(-3723)]", rs.getString("s"), rs.getString("a"));
            }
        }
    }
}
Configuration
Client Configuration
new Client.Builder().addEndpoint("http://clickhouse:8123").setUsername("default").setPassword(...).build();
DriverManager.getConnection("jdbc:clickhouse://clickhouse:8123/default", props); // user, password
Environment
  • Cloud
  • Client version: main (8480ca001): client-v2, and the clickhouse-jdbc -all jar (jdbc-v2)
  • Language version: Java 17.0.21
  • OS: Linux
ClickHouse Server
  • ClickHouse Server version: 26.11.2.4
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: none
  • Sample data for all these tables: none

Classification: impact: Data integrity · area: Data types & formats (Time, Time64)


Found by automated analysis (Polyglot AI) while checking ch-go#1249 and clickhouse-odbc#643 against the other client repos. Verified against a live server.

Ngôn ngữ chính
Java
Star
1.6k
Fork
657
Merge trung bình
2 ngày 6 giờ
Pull request đã merge (30 ngày)
36

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ClickHouse/clickhouse-java

Tất cả issue của ClickHouse/clickhouse-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.