LuciferYang commented on code in PR #14658:
URL: https://github.com/apache/gravitino/pull/14658#discussion_r4585619797
##########
flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java:
##########
@@ -546,6 +550,235 @@ public void
testAlterTableWithCommentChangeForwardsAlterCall() throws Exception
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(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(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(
+ org.apache.flink.table.catalog.TableChange.add(
+ Column.computed("b",
resolvedExpression(DataTypes.INT())))),
+ false));
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ catalog.alterTable(
+ new ObjectPath("db", "t"),
+ existing,
+ Collections.singletonList(
+ org.apache.flink.table.catalog.TableChange.add(
+ Column.metadata("m", DataTypes.INT(), null, false))),
+ false));
+
+ Assertions.assertDoesNotThrow(
+ () ->
+ catalog.alterTable(
+ new ObjectPath("db", "t"),
+ existing,
+ Collections.singletonList(
+ org.apache.flink.table.catalog.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(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(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(DataTypes.TIMESTAMP_LTZ(3))))));
Review Comment:
Fixed in ae367de01. `resolvedExpression` now takes the column name, and
`resolvedCatalogTable` builds the unresolved watermark from the spec's
expression, so both schemas use `ts`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]