Skip to content

Commit 7a13970

Browse files
authored
Merge pull request #2276 from ClickHouse/issue-2218
[jdbc-v2] Fix issue #2218 Broken Result with Nullable(FixedString(N))
2 parents 6a27f85 + 2c86850 commit 7a13970

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOExce
103103

104104
ClickHouseColumn actualColumn = column.getDataType() == ClickHouseDataType.Dynamic ? readDynamicData() : column;
105105
ClickHouseDataType dataType = actualColumn.getDataType();
106-
int estimatedLen = actualColumn.getEstimatedLength();
107106
int precision = actualColumn.getPrecision();
108107
int scale = actualColumn.getScale();
109108
TimeZone timezone = actualColumn.getTimeZoneOrDefault(timeZone);
@@ -112,10 +111,10 @@ public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOExce
112111
switch (dataType) {
113112
// Primitives
114113
case FixedString: {
115-
byte[] bytes = estimatedLen > STRING_BUFF.length ?
116-
new byte[estimatedLen] : STRING_BUFF;
117-
readNBytes(input, bytes, 0, estimatedLen);
118-
return (T) new String(bytes, 0, estimatedLen, StandardCharsets.UTF_8);
114+
byte[] bytes = precision > STRING_BUFF.length ?
115+
new byte[precision] : STRING_BUFF;
116+
readNBytes(input, bytes, 0, precision);
117+
return (T) new String(bytes, 0, precision, StandardCharsets.UTF_8);
119118
}
120119
case String: {
121120
return (T) readString();

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ public void testSwitchDatabase() throws Exception {
573573
}
574574
}
575575
}
576-
576+
577+
577578
@Test(groups = { "integration" })
578579
public void testNewLineSQLParsing() throws Exception {
579580
try (Connection conn = getJdbcConnection()) {
@@ -637,4 +638,37 @@ public void testNewLineSQLParsing() throws Exception {
637638
}
638639
}
639640
}
641+
642+
643+
@Test(groups = { "integration" })
644+
public void testNullableFixedStringType() throws Exception {
645+
try (Connection conn = getJdbcConnection()) {
646+
String sqlCreate = "CREATE TABLE `data_types` (`f1` FixedString(4),`f2` LowCardinality(FixedString(4)), `f3` Nullable(FixedString(4)), `f4` LowCardinality(Nullable(FixedString(4))) ) ENGINE Memory;";
647+
try (Statement stmt = conn.createStatement()) {
648+
int r = stmt.executeUpdate(sqlCreate);
649+
assertEquals(r, 0);
650+
}
651+
try(Statement stmt = conn.createStatement()) {
652+
String sqlInsert = "INSERT INTO `data_types` VALUES ('val1', 'val2', 'val3', 'val4')";
653+
int r = stmt.executeUpdate(sqlInsert);
654+
assertEquals(r, 1);
655+
}
656+
try(Statement stmt = conn.createStatement()) {
657+
String sqlSelect = "SELECT * FROM `data_types`";
658+
ResultSet rs = stmt.executeQuery(sqlSelect);
659+
assertTrue(rs.next());
660+
assertEquals(rs.getString(1), "val1");
661+
assertEquals(rs.getString(2), "val2");
662+
assertEquals(rs.getString(3), "val3");
663+
assertEquals(rs.getString(4), "val4");
664+
assertFalse(rs.next());
665+
}
666+
try(Statement stmt = conn.createStatement()) {
667+
String sqlSelect = "SELECT f4 FROM `data_types`";
668+
ResultSet rs = stmt.executeQuery(sqlSelect);
669+
assertTrue(rs.next());
670+
assertEquals(rs.getString(1), "val4");
671+
}
672+
}
673+
}
640674
}

0 commit comments

Comments
 (0)