Skip to content

Commit 812a3c1

Browse files
committed
fixed datetime64 formatting and added JDBC type casts tests
1 parent e3e6206 commit 812a3c1

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

client-v2/src/main/java/com/clickhouse/client/api/DataTypeUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public class DataTypeUtils {
3030
/**
3131
* Formatter for the DateTime type with nanoseconds.
3232
*/
33-
public static DateTimeFormatter DATETIME_WITH_NANOS_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.nnnnnnnnn");
33+
public static DateTimeFormatter DATETIME_WITH_NANOS_FORMATTER = new DateTimeFormatterBuilder().appendPattern("uuuu-MM-dd HH:mm:ss")
34+
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
35+
.toFormatter();
3436

3537
private static final DateTimeFormatter INSTANT_FORMATTER = new DateTimeFormatterBuilder()
3638
.appendValue(ChronoField.INSTANT_SECONDS)

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ public static String readAsString(Object value, ClickHouseColumn column) {
348348
case DateTime:
349349
case DateTime32:
350350
return zdt.format(DataTypeUtils.DATETIME_FORMATTER);
351+
case DateTime64:
352+
return zdt.format(DataTypeUtils.DATETIME_WITH_NANOS_FORMATTER);
351353
default:
352354
return value.toString();
353355
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,6 @@ public void testStatementsWithDatabaseInTableIdentifier() throws Exception {
11301130

11311131
for (int i = 0; i < tableIdentifier.length; i++) {
11321132
String tableId = tableIdentifier[i];
1133-
System.out.println(">> " + tableId);
11341133
final String insertStmt = "INSERT INTO " + tableId + " VALUES (?, ?)";
11351134
try (PreparedStatement stmt = conn.prepareStatement(insertStmt)) {
11361135
stmt.setInt(1, i + 10);
@@ -1300,6 +1299,39 @@ public static Object[][] testTypeCastsDP() {
13001299
{"ed0c77a3-2e4b-4954-98ee-22a4fdad9565", ClickHouseDataType.UUID, ClickHouseDataType.UUID},
13011300
{"::ffff:127.0.0.1", ClickHouseDataType.IPv6, ClickHouseDataType.IPv6},
13021301
{"116.253.40.133", ClickHouseDataType.IPv4, ClickHouseDataType.IPv4},
1302+
{100, JDBCType.TINYINT, ClickHouseDataType.Int8}
1303+
};
1304+
}
1305+
1306+
@Test(groups = {"integration"}, dataProvider = "testJDBCTypeCastDP")
1307+
public void testJDBCTypeCast(Object value, int targetType, ClickHouseDataType expectedType) throws Exception {
1308+
try (Connection conn = getJdbcConnection()) {
1309+
try (PreparedStatement stmt = conn.prepareStatement("select ?, toTypeName(?)")) {
1310+
stmt.setObject(1, value, targetType);
1311+
stmt.setObject(2, value, targetType);
1312+
1313+
try (ResultSet rs = stmt.executeQuery()) {
1314+
rs.next();
1315+
assertEquals(rs.getString(2), expectedType.getName());
1316+
switch (expectedType) {
1317+
case IPv4:
1318+
assertEquals(rs.getString(1), "/" + value);
1319+
break;
1320+
case IPv6:
1321+
// do not check
1322+
break;
1323+
default:
1324+
assertEquals(rs.getString(1), String.valueOf(value));
1325+
}
1326+
}
1327+
}
1328+
}
1329+
}
1330+
1331+
@DataProvider(name = "testJDBCTypeCastDP")
1332+
public static Object[][] testJDBCTypeCastDP() {
1333+
return new Object[][] {
1334+
{100, JDBCType.TINYINT.getVendorTypeNumber().intValue(), ClickHouseDataType.Int8}
13031335
};
13041336
}
13051337

@@ -1310,6 +1342,9 @@ public void testTypesInvalidForCast() throws Exception {
13101342
for (ClickHouseDataType type : JdbcUtils.INVALID_TARGET_TYPES) {
13111343
expectThrows(SQLException.class, ()->stmt.setObject(1, "", type));
13121344
}
1345+
1346+
expectThrows(SQLException.class, ()->stmt.setObject(1, "", JDBCType.OTHER.getVendorTypeNumber()));
1347+
expectThrows(SQLException.class, ()->stmt.setObject(1, "", ClickHouseDataType.DateTime64));
13131348
}
13141349
}
13151350
}
@@ -1335,7 +1370,7 @@ public static Object[][] testTypeCastWithScaleOrLengthDP() {
13351370
return new Object[][] {
13361371
{0.123456789, ClickHouseDataType.Decimal64, 3, "0.123", "Decimal(18, 3)"},
13371372
{"hello", ClickHouseDataType.FixedString, 5, "hello", "FixedString(5)"},
1338-
{"2017-10-02 10:20:30.333333", ClickHouseDataType.DateTime64, 3, "2017-10-02T10:20:30.333", "DateTime64(3)"}
1373+
{"2017-10-02 10:20:30.333333", ClickHouseDataType.DateTime64, 3, "2017-10-02 10:20:30.333", "DateTime64(3)"}
13391374
};
13401375
}
13411376

0 commit comments

Comments
 (0)