Skip to content

Commit 3941d1d

Browse files
test(jdbc-v2): assert BFloat16 JDBC metadata is reported as FLOAT/Float
Add focused tests proving BFloat16 type information is reported correctly through the JDBC metadata surfaces, per review feedback: - JdbcDataTypeTests#testBFloat16ResultSetMetaData: ResultSetMetaData reports a BFloat16 (and Nullable(BFloat16)) result column as Types.FLOAT / java.lang.Float, with the ClickHouse type name preserved (BFloat16 / Nullable(BFloat16)). - DatabaseMetaDataTest#testGetColumnsBFloat16: DatabaseMetaData.getColumns reports a BFloat16 (and Nullable(BFloat16)) column as Types.FLOAT with the ClickHouse type name, plus a sibling Int32 contrast column. The BFloat16 -> Types.FLOAT / java.lang.Float mapping already lives in JdbcUtils.CLICKHOUSE_TO_SQL_TYPE_MAP; these tests pin it end-to-end. getTypeInfo BFloat16 coverage is already exercised by the existing DatabaseMetaDataTest#testGetTypeInfo. Implements: #2279
1 parent 7eea000 commit 3941d1d

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,31 @@ public void testBFloat16WriteAsFloat() throws SQLException {
610610
}
611611
}
612612

613+
@Test(groups = { "integration" })
614+
public void testBFloat16ResultSetMetaData() throws SQLException {
615+
if (ClickHouseVersion.of(getServerVersion()).check(BFLOAT16_UNSUPPORTED_VERSIONS)) {
616+
throw new SkipException("BFloat16 was introduced in ClickHouse 24.11");
617+
}
618+
619+
// ResultSetMetaData must report a BFloat16 result column - and a Nullable(BFloat16) one - as
620+
// java.sql.Types.FLOAT / java.lang.Float, with the ClickHouse type name preserved verbatim.
621+
try (Connection conn = getJdbcConnection();
622+
Statement stmt = conn.createStatement();
623+
ResultSet rs = stmt.executeQuery(
624+
"SELECT CAST(1.5 AS BFloat16) AS v, CAST(NULL AS Nullable(BFloat16)) AS vNull")) {
625+
ResultSetMetaData meta = rs.getMetaData();
626+
assertEquals(meta.getColumnCount(), 2);
627+
628+
assertEquals(meta.getColumnType(1), Types.FLOAT);
629+
assertEquals(meta.getColumnTypeName(1), "BFloat16");
630+
assertEquals(meta.getColumnClassName(1), Float.class.getName());
631+
632+
assertEquals(meta.getColumnType(2), Types.FLOAT);
633+
assertEquals(meta.getColumnTypeName(2), "Nullable(BFloat16)");
634+
assertEquals(meta.getColumnClassName(2), Float.class.getName());
635+
}
636+
}
637+
613638
// Text form of the exact BFloat16 whose bit pattern is {@code pattern}, suitable for use as a
614639
// SQL numeric literal. Non-finite patterns use ClickHouse's nan/inf/-inf tokens; every other
615640
// pattern uses the shortest round-trippable decimal of Float.intBitsToFloat(pattern << 16),

jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.clickhouse.jdbc.JdbcIntegrationTest;
99
import com.clickhouse.jdbc.internal.JdbcUtils;
1010
import org.testng.Assert;
11+
import org.testng.SkipException;
1112
import org.testng.annotations.Test;
1213

1314
import java.sql.Connection;
@@ -146,6 +147,48 @@ public void testGetColumns() throws Exception {
146147
}
147148
}
148149

150+
@Test(groups = { "integration" })
151+
public void testGetColumnsBFloat16() throws Exception {
152+
if (isVersionMatch("(,24.10]")) {
153+
throw new SkipException("BFloat16 was introduced in ClickHouse 24.11");
154+
}
155+
156+
try (Connection conn = getJdbcConnection()) {
157+
final String tableName = "get_columns_bfloat16_test";
158+
try (Statement stmt = conn.createStatement()) {
159+
stmt.executeUpdate("DROP TABLE IF EXISTS " + tableName);
160+
stmt.executeUpdate("CREATE TABLE " + tableName
161+
+ " (id Int32, v BFloat16, vNull Nullable(BFloat16)) ENGINE MergeTree ORDER BY id");
162+
}
163+
164+
DatabaseMetaData dbmd = conn.getMetaData();
165+
try (ResultSet rs = dbmd.getColumns(null, getDatabase(), tableName, null)) {
166+
// Sibling Int32 column: a contrast case confirming only the BFloat16 columns are affected.
167+
assertTrue(rs.next());
168+
assertEquals(rs.getString("TABLE_NAME"), tableName);
169+
assertEquals(rs.getString("COLUMN_NAME"), "id");
170+
assertEquals(rs.getInt("DATA_TYPE"), Types.INTEGER);
171+
assertEquals(rs.getString("TYPE_NAME"), "Int32");
172+
173+
// BFloat16 must be reported as java.sql.Types.FLOAT with the ClickHouse type name preserved.
174+
assertTrue(rs.next());
175+
assertEquals(rs.getString("COLUMN_NAME"), "v");
176+
assertEquals(rs.getInt("DATA_TYPE"), Types.FLOAT);
177+
assertEquals(rs.getObject("DATA_TYPE"), Types.FLOAT);
178+
assertEquals(rs.getString("TYPE_NAME"), "BFloat16");
179+
assertFalse(rs.getBoolean("NULLABLE"));
180+
181+
// Nullable(BFloat16): same SQL type, nullable, with the full ClickHouse type name.
182+
assertTrue(rs.next());
183+
assertEquals(rs.getString("COLUMN_NAME"), "vNull");
184+
assertEquals(rs.getInt("DATA_TYPE"), Types.FLOAT);
185+
assertEquals(rs.getObject("DATA_TYPE"), Types.FLOAT);
186+
assertEquals(rs.getString("TYPE_NAME"), "Nullable(BFloat16)");
187+
assertTrue(rs.getBoolean("NULLABLE"));
188+
}
189+
}
190+
}
191+
149192
@Test(groups = {"integration"})
150193
public void testSupportFlags() throws Exception {
151194
try (Connection conn = getJdbcConnection()) {

0 commit comments

Comments
 (0)