LuciferYang commented on code in PR #14658:
URL: https://github.com/apache/gravitino/pull/14658#discussion_r4478402074


##########
flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java:
##########
@@ -546,6 +550,220 @@ 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 testCreateTableRejectsComputedColumnsAndWatermarks() {
+    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));
+
+    // 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 column must be rejected like 
createTable does, while adding a
+   * physical column still goes through.
+   */
+  @Test
+  void testAlterTableRejectsComputedColumns() 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.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))))));
+    UnsupportedOperationException plainWatermarkError =
+        Assertions.assertThrows(
+            UnsupportedOperationException.class,
+            () -> plain.createTable(new ObjectPath("db", "t"), watermarked, 
false));
+    Assertions.assertFalse(
+        plainWatermarkError.getMessage().contains("generic table"),
+        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(DataType dataType) {
+    ResolvedExpression expression = Mockito.mock(ResolvedExpression.class);
+    Mockito.when(expression.getOutputDataType()).thenReturn(dataType);
+    Mockito.when(expression.asSerializableString()).thenReturn("`a`");

Review Comment:
   Changed the helper to build the watermark from the rowtime column in 
3d1279215.



##########
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.

Review Comment:
   The Generic Tables section writes it as `connector=hive` everywhere else, so 
I kept the new line consistent with it.



##########
flink-connector/flink-common/src/test/java/org/apache/gravitino/flink/connector/catalog/TestBaseCatalog.java:
##########
@@ -546,6 +550,220 @@ 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 testCreateTableRejectsComputedColumnsAndWatermarks() {

Review Comment:
   Renamed in 3d1279215.



-- 
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]

Reply via email to