Skip to content

Commit

Permalink
[fix](catalog) Fix ClickHouse DataTime64 precision parsing (apache#26977
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zy-kkk authored Nov 15, 2023
1 parent 15c43d8 commit df867a1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,13 @@ CREATE TABLE doris_test.ts
ts UInt64
)
ENGINE = MergeTree
ORDER BY id;

CREATE TABLE doris_test.dt_with_tz
(
id Int64,
dt1 DateTime('Asia/Shanghai'),
dt2 DateTime64(6, 'Asia/Shanghai')
)
ENGINE = MergeTree
ORDER BY id;
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ INSERT INTO doris_test.final_test Values (1, 'second');

INSERT INTO doris_test.ts values (1,1694438743);


INSERT INTO doris_test.dt_with_tz values(1, '2022-01-02 00:00:00','2022-01-02 00:00:00.000000');
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ private ColumnValueConverter getOutputConverter(
}
if (OffsetDateTime.class.equals(clz)) {
return createConverter(
input -> ((Timestamp) input).toLocalDateTime(), LocalDateTime.class);
input -> ((OffsetDateTime) input).toLocalDateTime(),
LocalDateTime.class);
}
if (oracle.sql.TIMESTAMP.class.equals(clz)) {
return createConverter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,14 @@ protected Type jdbcTypeToDoris(JdbcFieldSchema fieldSchema) {
if (ckType.startsWith("DateTime(") || ckType.equals("DateTime")) {
return ScalarType.createDatetimeV2Type(0);
} else {
int indexStart = ckType.indexOf('(');
int indexEnd = ckType.indexOf(')');
if (indexStart != -1 && indexEnd != -1) {
String scaleStr = ckType.substring(indexStart + 1, indexEnd);
int scale = Integer.parseInt(scaleStr);
if (scale > JDBC_DATETIME_SCALE) {
scale = JDBC_DATETIME_SCALE;
}
// return with the actual scale
return ScalarType.createDatetimeV2Type(scale);
} else {
// default precision if not specified
return ScalarType.createDatetimeV2Type(JDBC_DATETIME_SCALE);
// DateTime64 with millisecond precision
// Datetime64(6) / DateTime64(6, 'Asia/Shanghai')
String[] accuracy = ckType.substring(11, ckType.length() - 1).split(", ");
int precision = Integer.parseInt(accuracy[0]);
if (precision > 6) {
precision = JDBC_DATETIME_SCALE;
}
return ScalarType.createDatetimeV2Type(precision);
}
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ suite("test_clickhouse_jdbc_catalog", "p0,external,clickhouse,external_docker,ex
contains """QUERY: SELECT "id", "ts" FROM "doris_test"."ts" WHERE ("ts" <= toUnixTimestamp(FROM_UNIXTIME(ts, '%Y-%m-%d')))"""
}

order_qt_dt_with_tz """ select * from dt_with_tz order by id; """

sql """ drop catalog if exists ${catalog_name} """
}
}

0 comments on commit df867a1

Please sign in to comment.