This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 21c942a2ef [#14656] fix(flink-connector): Reject computed columns,
metadata columns and watermarks instead of dropping them (#14658)
21c942a2ef is described below
commit 21c942a2ef0a02410e775b3172f32b6d716e97b2
Author: YangJie <[email protected]>
AuthorDate: Mon Nov 16 08:58:13 2026 -0500
[#14656] fix(flink-connector): Reject computed columns, metadata columns
and watermarks instead of dropping them (#14658)
### What changes were proposed in this pull request?
- `BaseCatalog.createTable` throws `UnsupportedOperationException` when
the table has a computed column, a metadata column or a watermark. The
add-column path of `alterTable` does the same for computed and metadata
columns; watermark changes in `ALTER TABLE` were already rejected.
- In the Hive catalog, the error suggests creating the table as a Flink
generic table, which keeps them in `flink.*` properties. Generic tables
don't go through this path and are unchanged.
- The Flink connector docs list the limitation.
### Why are the changes needed?
Gravitino columns have no expression or metadata key, so these columns
were stored as plain physical columns and the watermark was dropped,
with no error. The table then had a different schema from the one
declared. For `t (a INT, b AS a + 1)`, `INSERT INTO t VALUES (1)` fails
with "Different number of columns", and after `INSERT INTO t (a) VALUES
(1)` the value of `b` is NULL instead of 2.
Fixes #14656
### Does this PR introduce _any_ user-facing change?
Yes. `CREATE TABLE` and `ALTER TABLE ... ADD` with these now fail in the
Paimon, Iceberg and JDBC catalogs and for Hive tables with
`'connector'='hive'`. `CREATE TABLE IF NOT EXISTS` with them also fails
when the table already exists. Paimon's own Flink catalog keeps these in
table options, so the Gravitino Paimon catalog is now stricter than it;
storing them the same way would be a separate change.
### How was this patch tested?
- New `TestBaseCatalog` tests, run in each Flink version module:
`createTable` rejects a computed column, a metadata column and a
watermark, and still creates a table with only physical columns;
`alterTable` rejects adding a computed or a metadata column and still
adds a physical one; the generic table advice appears only in catalogs
that support generic tables.
- Reproduced the bug on main with the Flink 1.20 integration test
environment for the Paimon, Iceberg and MySQL catalogs.
---
docs/flink-connector/flink-catalog-hive.md | 3 +
docs/flink-connector/flink-catalog-iceberg.md | 1 +
docs/flink-connector/flink-catalog-paimon.md | 1 +
docs/flink-connector/flink-connector.md | 4 +
.../flink/connector/catalog/BaseCatalog.java | 66 ++++++
.../flink/connector/hive/GravitinoHiveCatalog.java | 6 +
.../flink/connector/catalog/TestBaseCatalog.java | 232 +++++++++++++++++++++
7 files changed, 313 insertions(+)
diff --git a/docs/flink-connector/flink-catalog-hive.md
b/docs/flink-connector/flink-catalog-hive.md
index 4d3af0e773..1317401129 100644
--- a/docs/flink-connector/flink-catalog-hive.md
+++ b/docs/flink-connector/flink-catalog-hive.md
@@ -39,6 +39,9 @@ When loading or altering a table, Gravitino Flink connector
detects generic tabl
reconstructed from the serialized `flink.*` properties. Hive tables continue
to use the native
Hive schema.
+Only generic tables keep computed columns, metadata columns and watermarks.
Creating a Hive table
+(`connector=hive`) with them, or adding such a column to it, fails.
+
:::note
Set `connector=hive` explicitly when creating a raw Hive table. Otherwise, the
table is
created as a generic table by default in HiveCatalog. Starting from Apache
Flink 1.18,
diff --git a/docs/flink-connector/flink-catalog-iceberg.md
b/docs/flink-connector/flink-catalog-iceberg.md
index f11491abbb..45af4a2485 100644
--- a/docs/flink-connector/flink-catalog-iceberg.md
+++ b/docs/flink-connector/flink-catalog-iceberg.md
@@ -31,6 +31,7 @@ To enable the Flink connector, you must download the Iceberg
Flink runtime JAR a
- `UPDATE` clause
- `DELETE` clause
- `CREATE TABLE LIKE` clause
+- Computed columns, metadata columns and watermarks
## Getting Started
diff --git a/docs/flink-connector/flink-catalog-paimon.md
b/docs/flink-connector/flink-catalog-paimon.md
index e21a70a077..e0b5d791df 100644
--- a/docs/flink-connector/flink-catalog-paimon.md
+++ b/docs/flink-connector/flink-catalog-paimon.md
@@ -36,6 +36,7 @@ Supports most DDL and DML operations in Flink SQL, except
such operations:
- `UPDATE` clause
- `DELETE` clause
- `CALL` clause
+- Computed columns, metadata columns and watermarks
## Prerequisites
diff --git a/docs/flink-connector/flink-connector.md
b/docs/flink-connector/flink-connector.md
index fd229a0179..9cd5b1dd10 100644
--- a/docs/flink-connector/flink-connector.md
+++ b/docs/flink-connector/flink-connector.md
@@ -21,6 +21,10 @@ The connector is published as a version-specific runtime JAR
for each supported
4. Supports [Jdbc catalog](flink-catalog-jdbc.md)
5. Supports most DDL and DML SQLs.
+Gravitino tables can't store computed columns, metadata columns or watermarks,
so `CREATE TABLE`
+and `ALTER TABLE ... ADD` with them fail in every catalog. The only exception
is generic tables in
+the [Hive catalog](flink-catalog-hive.md#generic-tables).
+
## Prerequisites
* Scala 2.12 (Flink 1.19 and 1.20 only; Flink 2.x removed the Scala APIs)
diff --git
a/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/catalog/BaseCatalog.java
b/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/catalog/BaseCatalog.java
index 719c1e6ede..a0134523b5 100644
---
a/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/catalog/BaseCatalog.java
+++
b/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/catalog/BaseCatalog.java
@@ -46,6 +46,7 @@ import org.apache.flink.table.catalog.ObjectPath;
import org.apache.flink.table.catalog.ResolvedCatalogBaseTable;
import org.apache.flink.table.catalog.ResolvedCatalogView;
import org.apache.flink.table.catalog.UniqueConstraint;
+import org.apache.flink.table.catalog.WatermarkSpec;
import org.apache.flink.table.catalog.exceptions.CatalogException;
import org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException;
import org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException;
@@ -427,6 +428,7 @@ public abstract class BaseCatalog extends AbstractCatalog {
NameIdentifier.of(tablePath.getDatabaseName(),
tablePath.getObjectName());
ResolvedCatalogBaseTable<?> resolvedTable = (ResolvedCatalogBaseTable<?>)
table;
+ rejectUnsupportedSchemaElements(resolvedTable);
Column[] columns =
resolvedTable.getResolvedSchema().getColumns().stream()
.map(this::toGravitinoColumn)
@@ -890,6 +892,68 @@ public abstract class BaseCatalog extends AbstractCatalog {
return Optional.of(primaryKeyFieldList);
}
+ /**
+ * Returns whether this catalog can store a table as a Flink generic table,
which serializes the
+ * full Flink schema, including computed columns, metadata columns and
watermarks, into {@code
+ * flink.*} properties. Only the Hive catalog can.
+ *
+ * @return true if generic tables are supported, false by default
+ */
+ protected boolean supportsGenericTables() {
+ return false;
+ }
+
+ /**
+ * Rejects computed columns, metadata columns and watermarks, which the
Gravitino column model
+ * cannot represent. Storing them anyway would turn each such column into a
plain physical column
+ * and drop the watermark, without any error. This path serves Paimon,
Iceberg and JDBC tables,
+ * and Hive tables with {@code 'connector'='hive'}.
+ *
+ * @param resolvedTable the resolved create-table request
+ */
+ private void rejectUnsupportedSchemaElements(ResolvedCatalogBaseTable<?>
resolvedTable) {
+ for (org.apache.flink.table.catalog.Column column :
+ resolvedTable.getResolvedSchema().getColumns()) {
+ rejectUnsupportedColumn(column);
+ }
+ List<WatermarkSpec> watermarks =
resolvedTable.getResolvedSchema().getWatermarkSpecs();
+ if (!watermarks.isEmpty()) {
+ // Flink allows at most one watermark per table.
+ throw new UnsupportedOperationException(
+ String.format(
+ "The watermark on column '%s' cannot be stored in the Gravitino
catalog '%s'.%s",
+ watermarks.get(0).getRowtimeAttribute(), getName(),
genericTableAdvice()));
+ }
+ }
+
+ /**
+ * Rejects a computed or metadata column. Shared by createTable and the
add-column path of
+ * alterTable.
+ *
+ * @param column the column to validate
+ */
+ private void rejectUnsupportedColumn(org.apache.flink.table.catalog.Column
column) {
+ String kind;
+ if (column instanceof
org.apache.flink.table.catalog.Column.ComputedColumn) {
+ kind = "computed";
+ } else if (column instanceof
org.apache.flink.table.catalog.Column.MetadataColumn) {
+ kind = "metadata";
+ } else {
+ return;
+ }
+ throw new UnsupportedOperationException(
+ String.format(
+ "Column '%s' is a %s column, which cannot be stored in the
Gravitino catalog '%s'.%s",
+ column.getName(), kind, getName(), genericTableAdvice()));
+ }
+
+ private String genericTableAdvice() {
+ return supportsGenericTables()
+ ? " To keep it, create the table as a Flink generic table, with a
'connector' other than"
+ + " 'hive'."
+ : "";
+ }
+
private Column toGravitinoColumn(org.apache.flink.table.catalog.Column
column) {
return Column.of(
column.getName(),
@@ -917,6 +981,8 @@ public abstract class BaseCatalog extends AbstractCatalog {
private void addColumn(
org.apache.flink.table.catalog.TableChange.AddColumn change,
List<TableChange> changes) {
+ // ALTER TABLE t ADD b AS a + 1 (or a METADATA column) arrives here as a
non-physical column.
+ rejectUnsupportedColumn(change.getColumn());
changes.add(
TableChange.addColumn(
new String[] {change.getColumn().getName()},
diff --git
a/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/hive/GravitinoHiveCatalog.java
b/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/hive/GravitinoHiveCatalog.java
index 83ce99884d..d934c46965 100644
---
a/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/hive/GravitinoHiveCatalog.java
+++
b/flink-connector/flink-common/src/main/java/org/apache/gravitino/flink/connector/hive/GravitinoHiveCatalog.java
@@ -146,6 +146,12 @@ public abstract class GravitinoHiveCatalog extends
BaseCatalog {
return hiveCatalog;
}
+ /** {@inheritDoc} */
+ @Override
+ protected boolean supportsGenericTables() {
+ return true;
+ }
+
@Override
public void createTable(ObjectPath tablePath, CatalogBaseTable table,
boolean ignoreIfExists)
throws TableAlreadyExistException, DatabaseNotExistException,
CatalogException {
diff --git
a/flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java
b/flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java
index 19dc82b7b0..6d2cd35028 100644
---
a/flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java
+++
b/flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java
@@ -32,14 +32,18 @@ import org.apache.flink.table.catalog.AbstractCatalog;
import org.apache.flink.table.catalog.CatalogBaseTable;
import org.apache.flink.table.catalog.CatalogDatabase;
import org.apache.flink.table.catalog.CatalogDatabaseImpl;
+import org.apache.flink.table.catalog.CatalogTable;
import org.apache.flink.table.catalog.CatalogView;
import org.apache.flink.table.catalog.Column;
import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.ResolvedCatalogTable;
import org.apache.flink.table.catalog.ResolvedCatalogView;
import org.apache.flink.table.catalog.ResolvedSchema;
import org.apache.flink.table.catalog.TableChange;
+import org.apache.flink.table.catalog.WatermarkSpec;
import org.apache.flink.table.catalog.exceptions.CatalogException;
import org.apache.flink.table.catalog.exceptions.TableNotExistException;
+import org.apache.flink.table.expressions.ResolvedExpression;
import org.apache.flink.table.types.DataType;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.NameIdentifier;
@@ -549,6 +553,234 @@ public abstract class TestBaseCatalog {
Mockito.verify(tableCatalog, Mockito.times(1)).alterTable(Mockito.any(),
Mockito.any());
}
+ /**
+ * Computed columns, metadata columns and watermarks cannot be represented
by Gravitino columns.
+ * createTable must reject them instead of storing plain physical columns
without the watermark.
+ */
+ @Test
+ void testCreateTableRejectsComputedAndMetadataColumnsAndWatermarks() {
+ TestableBaseCatalog catalog =
+ new TestableBaseCatalog(Mockito.mock(AbstractCatalog.class),
mockedCatalogHandle());
+
+ // computed column: b AS a + 1
+ List<Column> computed =
+ Arrays.asList(
+ Column.physical("a", DataTypes.INT()),
+ Column.computed("b", resolvedExpression("a", DataTypes.INT())));
+ ResolvedCatalogTable computedTable =
+ resolvedCatalogTable(resolvedSchema(computed,
Collections.emptyList()));
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> catalog.createTable(new ObjectPath("db", "t1"), computedTable,
false));
+ // Flink calls createTable for CREATE TABLE IF NOT EXISTS even when the
table exists, and the
+ // check runs before the Gravitino call, so IF NOT EXISTS is rejected too.
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> catalog.createTable(new ObjectPath("db", "t1"), computedTable,
true));
+
+ // metadata column: m INT METADATA
+ List<Column> metadata =
+ Arrays.asList(
+ Column.physical("a", DataTypes.INT()),
+ Column.metadata("m", DataTypes.INT(), null, false));
+ ResolvedCatalogTable metadataTable =
+ resolvedCatalogTable(resolvedSchema(metadata,
Collections.emptyList()));
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> catalog.createTable(new ObjectPath("db", "t4"), metadataTable,
false));
+
+ // watermark spec on a timestamp column
+ List<WatermarkSpec> watermarkSpecs =
+ Collections.singletonList(
+ WatermarkSpec.of("a", resolvedExpression("a",
DataTypes.TIMESTAMP_LTZ(3))));
+ ResolvedCatalogTable watermarkedTable =
+ resolvedCatalogTable(
+ resolvedSchema(
+ Collections.singletonList(Column.physical("a",
DataTypes.TIMESTAMP_LTZ(3))),
+ watermarkSpecs));
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> catalog.createTable(new ObjectPath("db", "t2"),
watermarkedTable, false));
+
+ // plain physical column still creates
+ ResolvedCatalogTable plainTable =
+ resolvedCatalogTable(
+ resolvedSchema(
+ Collections.singletonList(Column.physical("a",
DataTypes.INT())),
+ Collections.emptyList()));
+ Assertions.assertDoesNotThrow(
+ () -> catalog.createTable(new ObjectPath("db", "t3"), plainTable,
false));
+ }
+
+ /**
+ * ALTER TABLE ... ADD of a computed or metadata column must be rejected
like createTable does,
+ * while adding a physical column still goes through.
+ */
+ @Test
+ void testAlterTableRejectsComputedAndMetadataColumns() throws Exception {
+ Catalog gravitinoCatalogHandle = Mockito.mock(Catalog.class);
+ TableCatalog tableCatalog = Mockito.mock(TableCatalog.class);
+
Mockito.when(gravitinoCatalogHandle.asTableCatalog()).thenReturn(tableCatalog);
+
+ Schema schema = Schema.newBuilder().column("a", DataTypes.INT()).build();
+ CatalogTable existing =
+ catalogCompat().createCatalogTable(schema, null, ImmutableList.of(),
ImmutableMap.of());
+ TestableBaseCatalog catalog =
+ new TestableBaseCatalog(
+ Mockito.mock(AbstractCatalog.class), gravitinoCatalogHandle,
existing);
+
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ catalog.alterTable(
+ new ObjectPath("db", "t"),
+ existing,
+ Collections.singletonList(
+ TableChange.add(
+ Column.computed("b", resolvedExpression("a",
DataTypes.INT())))),
+ false));
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ catalog.alterTable(
+ new ObjectPath("db", "t"),
+ existing,
+ Collections.singletonList(
+ TableChange.add(Column.metadata("m", DataTypes.INT(),
null, false))),
+ false));
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ catalog.alterTable(
+ new ObjectPath("db", "t"),
+ existing,
+ Collections.singletonList(TableChange.add(Column.physical("c",
DataTypes.INT()))),
+ false));
+ }
+
+ /**
+ * The generic table advice is only given by catalogs that support generic
tables, which is only
+ * the Hive catalog.
+ */
+ @Test
+ void testUnsupportedSchemaAdviceMentionsGenericTablesOnlyWhenSupported() {
+ TestableBaseCatalog plain =
+ new TestableBaseCatalog(Mockito.mock(AbstractCatalog.class),
mockedCatalogHandle());
+ TestableBaseCatalog genericLike =
+ new TestableBaseCatalog(Mockito.mock(AbstractCatalog.class),
mockedCatalogHandle()) {
+ @Override
+ protected boolean supportsGenericTables() {
+ return true;
+ }
+ };
+
+ UnsupportedOperationException plainError =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ plain.createTable(
+ new ObjectPath("db", "t"),
+ resolvedCatalogTable(
+ resolvedSchema(
+ Collections.singletonList(
+ Column.computed("b", resolvedExpression("a",
DataTypes.INT()))),
+ Collections.emptyList())),
+ false));
+ Assertions.assertFalse(
+ plainError.getMessage().contains("generic table"),
plainError.getMessage());
+
+ UnsupportedOperationException genericLikeError =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ genericLike.createTable(
+ new ObjectPath("db", "t"),
+ resolvedCatalogTable(
+ resolvedSchema(
+ Collections.singletonList(
+ Column.computed("b", resolvedExpression("a",
DataTypes.INT()))),
+ Collections.emptyList())),
+ false));
+ Assertions.assertTrue(
+ genericLikeError.getMessage().contains("generic table"),
genericLikeError.getMessage());
+
+ // the watermark message follows the same conditional advice
+ ResolvedCatalogTable watermarked =
+ resolvedCatalogTable(
+ resolvedSchema(
+ Collections.singletonList(Column.physical("ts",
DataTypes.TIMESTAMP_LTZ(3))),
+ Collections.singletonList(
+ WatermarkSpec.of("ts", resolvedExpression("ts",
DataTypes.TIMESTAMP_LTZ(3))))));
+ UnsupportedOperationException plainWatermarkError =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> plain.createTable(new ObjectPath("db", "t"), watermarked,
false));
+ Assertions.assertFalse(
+ plainWatermarkError.getMessage().contains("generic table"),
+ plainWatermarkError.getMessage());
+ Assertions.assertTrue(
+ plainWatermarkError.getMessage().contains("column 'ts'"),
plainWatermarkError.getMessage());
+ UnsupportedOperationException genericLikeWatermarkError =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> genericLike.createTable(new ObjectPath("db", "t"),
watermarked, false));
+ Assertions.assertTrue(
+ genericLikeWatermarkError.getMessage().contains("generic table"),
+ genericLikeWatermarkError.getMessage());
+ }
+
+ private static Catalog mockedCatalogHandle() {
+ Catalog handle = Mockito.mock(Catalog.class);
+
Mockito.doReturn(Mockito.mock(TableCatalog.class)).when(handle).asTableCatalog();
+ return handle;
+ }
+
+ private static ResolvedExpression resolvedExpression(String column, DataType
dataType) {
+ ResolvedExpression expression = Mockito.mock(ResolvedExpression.class);
+ Mockito.when(expression.getOutputDataType()).thenReturn(dataType);
+ Mockito.when(expression.asSerializableString()).thenReturn("`" + column +
"`");
+ return expression;
+ }
+
+ /**
+ * Builds a resolved schema. The 3-argument constructor is deprecated from
Flink 2.1 in favor of
+ * the 4-argument one taking indexes, but it is the only signature shared by
every supported Flink
+ * version, so the deprecation is suppressed rather than routed through
{@link CatalogCompat}.
+ *
+ * @param columns the schema columns
+ * @param watermarkSpecs the watermark specs
+ * @return a resolved schema
+ */
+ @SuppressWarnings("deprecation")
+ private static ResolvedSchema resolvedSchema(
+ List<Column> columns, List<WatermarkSpec> watermarkSpecs) {
+ return new ResolvedSchema(columns, watermarkSpecs, null);
+ }
+
+ /**
+ * Builds a resolved table from the given schema. The unresolved {@link
CatalogTable} is created
+ * through {@link #catalogCompat()}, because {@code CatalogTable.of(...)}
was removed in Flink 2.x
+ * while {@code CatalogTable.newBuilder()} only exists from Flink 1.20.
+ *
+ * @param schema the resolved schema to wrap
+ * @return a resolved catalog table carrying the schema
+ */
+ private ResolvedCatalogTable resolvedCatalogTable(ResolvedSchema schema) {
+ Schema.Builder builder = Schema.newBuilder();
+ schema.getColumns().forEach(c -> builder.column(c.getName(),
c.getDataType()));
+ schema
+ .getWatermarkSpecs()
+ .forEach(
+ w ->
+ builder.watermark(
+ w.getRowtimeAttribute(),
w.getWatermarkExpression().asSerializableString()));
+ CatalogTable unresolved =
+ catalogCompat()
+ .createCatalogTable(
+ builder.build(), null, Collections.emptyList(),
Collections.emptyMap());
+ return new ResolvedCatalogTable(unresolved, schema);
+ }
+
/**
* The Gravitino catalog handle must be fetched once per catalog instance:
the client has no
* catalog cache and Flink consults catalog() on every table resolution, so
a per-call fetch