Skip to content

Commit 83ba9a4

Browse files
authored
Merge pull request #2884 from ClickHouse/06/18/26/fix_type_info_v2
Added literal prefix and suffix to getTypeInfo() to be close to v1
2 parents f9d3c98 + 7156c2e commit 83ba9a4

2 files changed

Lines changed: 173 additions & 3 deletions

File tree

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataImpl.java

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.clickhouse.jdbc.internal.JdbcUtils;
1414
import com.clickhouse.logging.Logger;
1515
import com.clickhouse.logging.LoggerFactory;
16+
import com.google.common.collect.ImmutableMap;
1617

1718
import java.sql.Connection;
1819
import java.sql.JDBCType;
@@ -1344,9 +1345,129 @@ public ResultSet getTypeInfo() throws SQLException {
13441345
row.put("DATA_TYPE", type.getVendorTypeNumber());
13451346
};
13461347

1348+
private static class TypeLiteralInfo {
1349+
private final String prefix;
1350+
private final String suffix;
1351+
TypeLiteralInfo(String prefix, String suffix){
1352+
this.prefix = prefix;
1353+
this.suffix = suffix;
1354+
}
1355+
1356+
public String getPrefix() {
1357+
return prefix;
1358+
}
1359+
1360+
public String getSuffix() {
1361+
return suffix;
1362+
}
1363+
}
1364+
1365+
private static final Map<String, TypeLiteralInfo> TYPE_LITERAL_INFO_MAP;
1366+
static {
1367+
ImmutableMap.Builder<String, TypeLiteralInfo> mBuilder = ImmutableMap.builder();
1368+
// ---------------------------------------------------------
1369+
// Core String, Text & Binary Types
1370+
// ---------------------------------------------------------
1371+
mBuilder.put("String", new TypeLiteralInfo("'", "'"));
1372+
mBuilder.put("FixedString", new TypeLiteralInfo("'", "'"));
1373+
mBuilder.put("UUID", new TypeLiteralInfo("'", "'"));
1374+
1375+
// Aliased String/Binary Types from your list
1376+
mBuilder.put("BINARY", new TypeLiteralInfo("'", "'"));
1377+
mBuilder.put("NATIONAL CHAR VARYING", new TypeLiteralInfo("'", "'"));
1378+
mBuilder.put("BINARY VARYING", new TypeLiteralInfo("'", "'"));
1379+
mBuilder.put("NCHAR LARGE OBJECT", new TypeLiteralInfo("'", "'"));
1380+
mBuilder.put("NATIONAL CHARACTER VARYING", new TypeLiteralInfo("'", "'"));
1381+
mBuilder.put("NATIONAL CHARACTER LARGE OBJECT", new TypeLiteralInfo("'", "'"));
1382+
mBuilder.put("NATIONAL CHAR", new TypeLiteralInfo("'", "'"));
1383+
mBuilder.put("CHAR LARGE OBJECT", new TypeLiteralInfo("'", "'"));
1384+
mBuilder.put("CHARACTER VARYING", new TypeLiteralInfo("'", "'"));
1385+
mBuilder.put("NATIONAL CHARACTER", new TypeLiteralInfo("'", "'"));
1386+
mBuilder.put("LONGBLOB", new TypeLiteralInfo("'", "'"));
1387+
mBuilder.put("CHAR VARYING", new TypeLiteralInfo("'", "'"));
1388+
mBuilder.put("MEDIUMBLOB", new TypeLiteralInfo("'", "'"));
1389+
mBuilder.put("CLOB", new TypeLiteralInfo("'", "'"));
1390+
mBuilder.put("LONGTEXT", new TypeLiteralInfo("'", "'"));
1391+
mBuilder.put("MEDIUMTEXT", new TypeLiteralInfo("'", "'"));
1392+
mBuilder.put("TINYTEXT", new TypeLiteralInfo("'", "'"));
1393+
mBuilder.put("NVARCHAR", new TypeLiteralInfo("'", "'"));
1394+
mBuilder.put("TINYBLOB", new TypeLiteralInfo("'", "'"));
1395+
mBuilder.put("CHARACTER", new TypeLiteralInfo("'", "'"));
1396+
mBuilder.put("CHAR", new TypeLiteralInfo("'", "'"));
1397+
mBuilder.put("BLOB", new TypeLiteralInfo("'", "'"));
1398+
mBuilder.put("VARCHAR2", new TypeLiteralInfo("'", "'"));
1399+
mBuilder.put("TEXT", new TypeLiteralInfo("'", "'"));
1400+
mBuilder.put("NCHAR", new TypeLiteralInfo("'", "'"));
1401+
mBuilder.put("NCHAR VARYING", new TypeLiteralInfo("'", "'"));
1402+
mBuilder.put("BINARY LARGE OBJECT", new TypeLiteralInfo("'", "'"));
1403+
mBuilder.put("VARBINARY", new TypeLiteralInfo("'", "'"));
1404+
mBuilder.put("BYTEA", new TypeLiteralInfo("'", "'"));
1405+
mBuilder.put("VARCHAR", new TypeLiteralInfo("'", "'"));
1406+
1407+
// ---------------------------------------------------------
1408+
// Date & Time Types (including aliases)
1409+
// ---------------------------------------------------------
1410+
mBuilder.put("DateTime", new TypeLiteralInfo("'", "'"));
1411+
mBuilder.put("DateTime32", new TypeLiteralInfo("'", "'"));
1412+
mBuilder.put("DateTime64", new TypeLiteralInfo("'", "'"));
1413+
mBuilder.put("Date", new TypeLiteralInfo("'", "'"));
1414+
mBuilder.put("Date32", new TypeLiteralInfo("'", "'"));
1415+
mBuilder.put("Time", new TypeLiteralInfo("'", "'"));
1416+
mBuilder.put("Time64", new TypeLiteralInfo("'", "'"));
1417+
mBuilder.put("TIMESTAMP", new TypeLiteralInfo("'", "'"));
1418+
1419+
// ---------------------------------------------------------
1420+
// Network Types (including aliases)
1421+
// ---------------------------------------------------------
1422+
mBuilder.put("IPv4", new TypeLiteralInfo("'", "'"));
1423+
mBuilder.put("IPv6", new TypeLiteralInfo("'", "'"));
1424+
mBuilder.put("INET4", new TypeLiteralInfo("'", "'"));
1425+
mBuilder.put("INET6", new TypeLiteralInfo("'", "'"));
1426+
1427+
// ---------------------------------------------------------
1428+
// Enum Types (including aliases)
1429+
// ---------------------------------------------------------
1430+
mBuilder.put("Enum", new TypeLiteralInfo("'", "'"));
1431+
mBuilder.put("Enum8", new TypeLiteralInfo("'", "'"));
1432+
mBuilder.put("Enum16", new TypeLiteralInfo("'", "'"));
1433+
mBuilder.put("ENUM", new TypeLiteralInfo("'", "'"));
1434+
1435+
// ---------------------------------------------------------
1436+
// Complex / Collection Types
1437+
// ---------------------------------------------------------
1438+
mBuilder.put("Array", new TypeLiteralInfo("[", "]"));
1439+
mBuilder.put("Map", new TypeLiteralInfo("{", "}"));
1440+
mBuilder.put("Tuple", new TypeLiteralInfo("(", ")"));
1441+
mBuilder.put("AggregateFunction", new TypeLiteralInfo("(", ")"));
1442+
mBuilder.put("SimpleAggregateFunction", new TypeLiteralInfo("(", ")"));
1443+
1444+
// ---------------------------------------------------------
1445+
// Geo Types
1446+
// ---------------------------------------------------------
1447+
mBuilder.put("Point", new TypeLiteralInfo("(", ")"));
1448+
mBuilder.put("Ring", new TypeLiteralInfo("[", "]"));
1449+
mBuilder.put("Polygon", new TypeLiteralInfo("[", "]"));
1450+
mBuilder.put("MultiPolygon", new TypeLiteralInfo("[", "]"));
1451+
mBuilder.put("LineString", new TypeLiteralInfo("[", "]"));
1452+
mBuilder.put("MultiLineString", new TypeLiteralInfo("[", "]"));
1453+
1454+
TYPE_LITERAL_INFO_MAP = mBuilder.buildOrThrow();
1455+
}
1456+
1457+
private static final Consumer<Map<String, Object>> TYPE_INFO_LITERAL_FUNCTION = row -> {
1458+
String typeName = (String) row.get("TYPE_NAME");
1459+
1460+
TypeLiteralInfo literalInfo = TYPE_LITERAL_INFO_MAP.get(typeName);
1461+
if (literalInfo != null) {
1462+
row.put("LITERAL_PREFIX", literalInfo.getPrefix());
1463+
row.put("LITERAL_SUFFIX", literalInfo.getSuffix());
1464+
}
1465+
};
1466+
13471467
private static final List<Consumer<Map<String, Object>>> GET_TYPE_INFO_MUTATORS = Arrays.asList(
13481468
TYPE_INFO_VALUE_FUNCTION,
1349-
NULLABILITY_VALUE_FUNCTION
1469+
NULLABILITY_VALUE_FUNCTION,
1470+
TYPE_INFO_LITERAL_FUNCTION
13501471
);
13511472

13521473
private static final String DATA_TYPE_INFO_SQL = getDataTypeInfoSql();

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,47 @@ public void testGetServerVersions() throws Exception {
567567
}
568568
}
569569

570+
private static final java.util.Map<ClickHouseDataType, String[]> TYPE_LITERAL_EXPECTATIONS;
571+
static {
572+
java.util.Map<ClickHouseDataType, String[]> map = new java.util.EnumMap<>(ClickHouseDataType.class);
573+
574+
String[] quote = new String[]{"'", "'"};
575+
map.put(ClickHouseDataType.String, quote);
576+
map.put(ClickHouseDataType.FixedString, quote);
577+
map.put(ClickHouseDataType.UUID, quote);
578+
map.put(ClickHouseDataType.Date, quote);
579+
map.put(ClickHouseDataType.Date32, quote);
580+
map.put(ClickHouseDataType.DateTime, quote);
581+
map.put(ClickHouseDataType.DateTime32, quote);
582+
map.put(ClickHouseDataType.DateTime64, quote);
583+
map.put(ClickHouseDataType.Time, quote);
584+
map.put(ClickHouseDataType.Time64, quote);
585+
map.put(ClickHouseDataType.Enum, quote);
586+
map.put(ClickHouseDataType.Enum8, quote);
587+
map.put(ClickHouseDataType.Enum16, quote);
588+
map.put(ClickHouseDataType.IPv4, quote);
589+
map.put(ClickHouseDataType.IPv6, quote);
590+
591+
String[] bracket = new String[]{"[", "]"};
592+
map.put(ClickHouseDataType.Array, bracket);
593+
map.put(ClickHouseDataType.Ring, bracket);
594+
map.put(ClickHouseDataType.Polygon, bracket);
595+
map.put(ClickHouseDataType.MultiPolygon, bracket);
596+
map.put(ClickHouseDataType.LineString, bracket);
597+
map.put(ClickHouseDataType.MultiLineString, bracket);
598+
599+
String[] brace = new String[]{"{", "}"};
600+
map.put(ClickHouseDataType.Map, brace);
601+
602+
String[] parenthesis = new String[]{"(", ")"};
603+
map.put(ClickHouseDataType.Tuple, parenthesis);
604+
map.put(ClickHouseDataType.Point, parenthesis);
605+
map.put(ClickHouseDataType.AggregateFunction, parenthesis);
606+
map.put(ClickHouseDataType.SimpleAggregateFunction, parenthesis);
607+
608+
TYPE_LITERAL_EXPECTATIONS = java.util.Collections.unmodifiableMap(map);
609+
}
610+
570611
@Test(groups = { "integration" })
571612
public void testGetTypeInfo() throws Exception {
572613
try (Connection conn = getJdbcConnection()) {
@@ -638,8 +679,16 @@ public void testGetTypeInfo() throws Exception {
638679
assertEquals(rs.getInt("DATA_TYPE"), rs.getObject("DATA_TYPE"));
639680

640681
assertEquals(rs.getInt("PRECISION"), dataType.getMaxPrecision());
641-
assertNull(rs.getString("LITERAL_PREFIX"));
642-
assertNull(rs.getString("LITERAL_SUFFIX"));
682+
683+
String[] expectedLiterals = TYPE_LITERAL_EXPECTATIONS.get(dataType);
684+
if (expectedLiterals != null) {
685+
assertEquals(rs.getString("LITERAL_PREFIX"), expectedLiterals[0]);
686+
assertEquals(rs.getString("LITERAL_SUFFIX"), expectedLiterals[1]);
687+
} else {
688+
assertNull(rs.getString("LITERAL_PREFIX"), "Expected null prefix for " + dataType);
689+
assertNull(rs.getString("LITERAL_SUFFIX"), "Expected null suffix for " + dataType);
690+
}
691+
643692
assertEquals(rs.getInt("MINIMUM_SCALE"), dataType.getMinScale());
644693
assertEquals(rs.getInt("MAXIMUM_SCALE"), dataType.getMaxScale());
645694
assertNull(rs.getString("CREATE_PARAMS"));

0 commit comments

Comments
 (0)