Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#12272] feat(clickhouse): map ClickHouse Date32 to Gravitino DateType and verify Enum round-trip (#12275)

StormSpirit4091d5c56b21
diff --git a/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseTypeConverter.java b/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseTypeConverter.java
index 03f184b1f7..cc4d9c8adf 100644
--- a/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseTypeConverter.java
+++ b/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseTypeConverter.java
@@ -162,7 +162,7 @@ public class ClickHouseTypeConverter extends JdbcTypeConverter {
case DATE:
return Types.DateType.get();
case DATE32:
- // Date32 supports 1900-2299 vs Date's 1970-2149. Use ExternalType to preserve round-trip.
+ // Date32 has a wider range than Date; preserve its catalog type for round-trip.
return Types.ExternalType.of(DATE32);
case DATETIME:
// Default is 0 precision
diff --git a/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/integration/test/CatalogClickHouseIT.java b/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/integration/test/CatalogClickHouseIT.java
index 5ffa5344c4..71d9bc9203 100644
--- a/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/integration/test/CatalogClickHouseIT.java
+++ b/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/integration/test/CatalogClickHouseIT.java
@@ -78,6 +78,7 @@ import org.apache.gravitino.rel.expressions.transforms.Transforms;
import org.apache.gravitino.rel.indexes.Index;
import org.apache.gravitino.rel.indexes.Indexes;
import org.apache.gravitino.rel.types.Decimal;
+import org.apache.gravitino.rel.types.Type;
import org.apache.gravitino.rel.types.Types;
import org.apache.gravitino.utils.RandomNameUtils;
import org.junit.jupiter.api.AfterAll;
@@ -271,6 +272,50 @@ public class CatalogClickHouseIT extends BaseIT {
return properties;
}
+ private static String normalizeEnumFormatting(String value) {
+ StringBuilder normalized = new StringBuilder(value.length());
+ boolean inSingleQuote = false;
+ boolean pendingWhitespace = false;
+ for (int i = 0; i < value.length(); i++) {
+ char current = value.charAt(i);
+ boolean escapedQuote =
+ current == '\''
+ && i > 0
+ && value.charAt(i - 1) == '\\'
+ && (i < 2 || value.charAt(i - 2) != '\\');
+ if (current == '\'' && !escapedQuote) {
+ if (pendingWhitespace) {
+ appendWhitespaceUnlessSeparator(normalized);
+ pendingWhitespace = false;
+ }
+ normalized.append(current);
+ inSingleQuote = !inSingleQuote;
+ } else if (!inSingleQuote && Character.isWhitespace(current)) {
+ pendingWhitespace = true;
+ } else if (!inSingleQuote && (current == '=' || current == ',')) {
+ pendingWhitespace = false;
+ normalized.append(current);
+ } else {
+ if (pendingWhitespace) {
+ appendWhitespaceUnlessSeparator(normalized);
+ pendingWhitespace = false;
+ }
+ normalized.append(current);
+ }
+ }
+ return normalized.toString().trim();
+ }
+
+ private static void appendWhitespaceUnlessSeparator(StringBuilder builder) {
+ if (builder.length() == 0) {
+ return;
+ }
+ char previous = builder.charAt(builder.length() - 1);
+ if (previous != '=' && previous != ',') {
+ builder.append(' ');
+ }
+ }
+
@Test
void testOperationClickhouseSchema() {
SupportsSchemas schemas = catalog.asSchemas();
@@ -2836,6 +2881,7 @@ public class CatalogClickHouseIT extends BaseIT {
// may normalize the enum definition format (e.g., spacing around '=' and ',').
Assertions.assertTrue(loadedTable.columns()[4].dataType() instanceof Types.ExternalType);
Assertions.assertTrue(loadedTable.columns()[5].dataType() instanceof Types.ExternalType);
+ // Date32 remains ExternalType so its wider range and catalog type survive round-trip.
Assertions.assertEquals(Types.ExternalType.of("Date32"), loadedTable.columns()[6].dataType());
}
@@ -2872,4 +2918,83 @@ public class CatalogClickHouseIT extends BaseIT {
getSortOrders("id"),
Indexes.EMPTY_INDEXES));
}
+
+ @Test
+ void testEnumRoundTrip() {
+ // Create a table in ClickHouse with Enum8 and Enum16 columns
+ String tableName = GravitinoITUtils.genRandomName("enum_test");
+ clickhouseService.executeQuery(
+ String.format(
+ "CREATE TABLE `%s`.`%s` ("
+ + "id Int32, "
+ + "status Enum8('active' = 1, 'inactive' = 2), "
+ + "priority Enum16('low' = 100, 'medium' = 200, 'high' = 300)"
+ + ") ENGINE = MergeTree ORDER BY id",
+ schemaName, tableName));
+
+ // Load through Gravitino and verify column types are ExternalType
+ Table loaded = catalog.asTableCatalog().loadTable(NameIdentifier.of(schemaName, tableName));
+ Column[] columns = loaded.columns();
+ Assertions.assertEquals(3, columns.length);
+
+ // Enum8 type verification
+ Type enum8Type = columns[1].dataType();
+ Assertions.assertTrue(
+ enum8Type instanceof Types.ExternalType,
+ "Enum8 should map to ExternalType, but got: " + enum8Type.simpleString());
+ Assertions.assertEquals(
+ normalizeEnumFormatting("Enum8('active' = 1, 'inactive' = 2)"),
+ normalizeEnumFormatting(((Types.ExternalType) enum8Type).catalogString()));
+
+ // Enum16 type verification
+ Type enum16Type = columns[2].dataType();
+ Assertions.assertTrue(
+ enum16Type instanceof Types.ExternalType,
+ "Enum16 should map to ExternalType, but got: " + enum16Type.simpleString());
+ Assertions.assertEquals(
+ normalizeEnumFormatting("Enum16('low' = 100, 'medium' = 200, 'high' = 300)"),
+ normalizeEnumFormatting(((Types.ExternalType) enum16Type).catalogString()));
+
+ // Round-trip: recreate table through Gravitino with the loaded schema
+ String rtTableName = GravitinoITUtils.genRandomName("enum_rt");
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(schemaName, rtTableName),
+ columns,
+ "enum round-trip test",
+ createProperties(),
+ Transforms.EMPTY_TRANSFORM,
+ Distributions.NONE,
+ new SortOrder[] {SortOrders.of(NamedReference.field("id"), SortDirection.ASCENDING)});
+
+ // Verify types survived round-trip
+ Table rtLoaded = catalog.asTableCatalog().loadTable(NameIdentifier.of(schemaName, rtTableName));
+ Column[] rtColumns = rtLoaded.columns();
+ Type rtEnum8 = rtColumns[1].dataType();
+ Type rtEnum16 = rtColumns[2].dataType();
+ Assertions.assertTrue(
+ rtEnum8 instanceof Types.ExternalType, "Enum8 should survive round-trip as ExternalType");
+ Assertions.assertTrue(
+ rtEnum16 instanceof Types.ExternalType, "Enum16 should survive round-trip as ExternalType");
+ Assertions.assertEquals(
+ normalizeEnumFormatting(((Types.ExternalType) enum8Type).catalogString()),
+ normalizeEnumFormatting(((Types.ExternalType) rtEnum8).catalogString()));
+ Assertions.assertEquals(
+ normalizeEnumFormatting(((Types.ExternalType) enum16Type).catalogString()),
+ normalizeEnumFormatting(((Types.ExternalType) rtEnum16).catalogString()));
+
+ // Verify DDL on ClickHouse side contains full enum definitions
+ String createSql =
+ clickhouseService.executeQueryForResult(
+ String.format("SHOW CREATE TABLE `%s`.`%s`", schemaName, rtTableName));
+ Assertions.assertNotNull(createSql, "SHOW CREATE TABLE should return a result");
+ String normalizedCreateSql = normalizeEnumFormatting(createSql);
+ Assertions.assertTrue(
+ normalizedCreateSql.contains("Enum8('active'=1,'inactive'=2)"),
+ "SHOW CREATE TABLE should contain Enum8 definition: " + createSql);
+ Assertions.assertTrue(
+ normalizedCreateSql.contains("Enum16('low'=100,'medium'=200,'high'=300)"),
+ "SHOW CREATE TABLE should contain Enum16 definition: " + createSql);
+ }
}