mchades commented on code in PR #14318:
URL: https://github.com/apache/gravitino/pull/14318#discussion_r4408367866


##########
core/src/test/java/org/apache/gravitino/catalog/TestWritableDataTypeDispatchers.java:
##########
@@ -0,0 +1,337 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.catalog;
+
+import static org.mockito.Mockito.verifyNoInteractions;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.TestCatalog;
+import org.apache.gravitino.connector.TestCatalogOperations;
+import org.apache.gravitino.function.Function;
+import org.apache.gravitino.function.FunctionChange;
+import org.apache.gravitino.function.FunctionColumn;
+import org.apache.gravitino.function.FunctionDefinition;
+import org.apache.gravitino.function.FunctionDefinitions;
+import org.apache.gravitino.function.FunctionImpl;
+import org.apache.gravitino.function.FunctionImpls;
+import org.apache.gravitino.function.FunctionParam;
+import org.apache.gravitino.function.FunctionParams;
+import org.apache.gravitino.function.FunctionType;
+import org.apache.gravitino.rel.Column;
+import org.apache.gravitino.rel.Table;
+import org.apache.gravitino.rel.TableChange;
+import org.apache.gravitino.rel.expressions.Expression;
+import org.apache.gravitino.rel.expressions.FunctionExpression;
+import org.apache.gravitino.rel.expressions.distributions.Distributions;
+import org.apache.gravitino.rel.expressions.distributions.Strategy;
+import org.apache.gravitino.rel.expressions.literals.Literal;
+import org.apache.gravitino.rel.expressions.literals.Literals;
+import org.apache.gravitino.rel.expressions.sorts.SortDirection;
+import org.apache.gravitino.rel.expressions.sorts.SortOrder;
+import org.apache.gravitino.rel.expressions.sorts.SortOrders;
+import org.apache.gravitino.rel.expressions.transforms.Transform;
+import org.apache.gravitino.rel.expressions.transforms.Transforms;
+import org.apache.gravitino.rel.indexes.Indexes;
+import org.apache.gravitino.rel.partitions.Partition;
+import org.apache.gravitino.rel.partitions.Partitions;
+import org.apache.gravitino.rel.partitions.RangePartition;
+import org.apache.gravitino.rel.types.Type;
+import org.apache.gravitino.rel.types.Types;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
+
+public class TestWritableDataTypeDispatchers extends TestOperationDispatcher {
+  private static final NameIdentifier SCHEMA =
+      NameIdentifier.of(metalake, catalog, "writable_types");
+  private static final Map<String, String> PROPERTIES = ImmutableMap.of("k1", 
"v1", "k2", "v2");
+  private static final Type UNPARSED = Types.UnparsedType.of("future_type");
+  private static final Type EXTERNAL = Types.ExternalType.of("jsonb");
+  private static final FunctionImpl[] IMPLS = {
+    FunctionImpls.ofJava(FunctionImpl.RuntimeType.SPARK, "com.example.UDF")
+  };
+
+  private static TableOperationDispatcher tables;
+  private static PartitionOperationDispatcher partitions;
+  private static FunctionOperationDispatcher functions;
+
+  @BeforeAll
+  public static void initialize() throws IllegalAccessException {
+    SchemaOperationDispatcher schemas =
+        new SchemaOperationDispatcher(catalogManager, entityStore, 
idGenerator, secretManager);
+    FieldUtils.writeField(GravitinoEnv.getInstance(), 
"internalSchemaDispatcher", schemas, true);
+    schemas.createSchema(SCHEMA, "comment", PROPERTIES);
+    tables =
+        new TableOperationDispatcher(
+            catalogManager, entityStore, idGenerator, () -> schemas, 
secretManager);
+    partitions =
+        new PartitionOperationDispatcher(catalogManager, entityStore, 
idGenerator, secretManager);
+    functions =
+        new FunctionOperationDispatcher(
+            catalogManager, schemas, entityStore, idGenerator, secretManager);
+  }
+
+  @Test
+  public void testCreateTableRejectsTypesBeforeCatalogAccess() {
+    for (Type type :
+        new Type[] {UNPARSED, Types.ListType.of(UNPARSED, true), 
Types.ExternalType.of(" ")}) {
+      assertRejected(
+          () ->
+              tables.createTable(
+                  ident("invalid"), new Column[] {Column.of("c", type)}, null, 
PROPERTIES),
+          "columns[0].type");
+    }
+    Literal<?> literal = Literals.of(null, UNPARSED);
+    assertRejected(
+        () ->
+            tables.createTable(
+                ident("invalid"),
+                new Column[] {Column.of("c", Types.StringType.get(), null, 
literal)},
+                null,
+                PROPERTIES),
+        "columns[0].defaultValue.dataType");
+    Column[] columns = {Column.of("c", Types.StringType.get())};
+    RangePartition range = Partitions.range("p", literal, 
Literals.stringLiteral("a"), null);
+    assertRejected(
+        () ->
+            tables.createTable(
+                ident("invalid"),
+                columns,
+                null,
+                PROPERTIES,
+                new Transform[] {
+                  Transforms.range(new String[] {"c"}, new RangePartition[] 
{range})
+                },
+                Distributions.NONE,
+                new SortOrder[0],
+                Indexes.EMPTY_INDEXES),
+        "partitioning[0].assignments[0].upper.dataType");
+    assertRejected(
+        () ->
+            tables.createTable(
+                ident("invalid"),
+                columns,
+                null,
+                PROPERTIES,
+                new Transform[] {Transforms.apply("f", new Expression[] 
{literal})},
+                Distributions.NONE,
+                new SortOrder[0],
+                Indexes.EMPTY_INDEXES),
+        "partitioning[0].funcArgs[0].dataType");
+    assertRejected(
+        () ->
+            tables.createTable(
+                ident("invalid"),
+                columns,
+                null,
+                PROPERTIES,
+                new Transform[0],
+                Distributions.of(Strategy.HASH, 2, literal),
+                new SortOrder[0],
+                Indexes.EMPTY_INDEXES),
+        "distribution.funcArgs[0].dataType");
+    assertRejected(
+        () ->
+            tables.createTable(
+                ident("invalid"),
+                columns,
+                null,
+                PROPERTIES,
+                new Transform[0],
+                Distributions.NONE,
+                new SortOrder[] {SortOrders.of(literal, 
SortDirection.ASCENDING)},
+                Indexes.EMPTY_INDEXES),
+        "sortOrders[0].sortTerm.dataType");
+    verifyNoInteractions(entityStore);
+  }
+
+  @Test
+  public void testTableChangesValidateEntireBatchBeforeMutation() {
+    NameIdentifier ident = ident("alter_types");
+    tables.createTable(ident, new Column[] {Column.of("c", EXTERNAL)}, 
"original", PROPERTIES);
+    for (TableChange invalid :
+        new TableChange[] {
+          TableChange.addColumn(new String[] {"new"}, 
Types.ListType.of(UNPARSED, true)),
+          TableChange.updateColumnType(new String[] {"c"}, 
Types.ExternalType.of(" ")),
+          TableChange.addColumn(
+              new String[] {"new"},
+              EXTERNAL,
+              null,
+              TableChange.ColumnPosition.defaultPos(),
+              true,
+              false,
+              Literals.of("x", UNPARSED)),
+          TableChange.updateColumnDefaultValue(
+              new String[] {"c"}, FunctionExpression.of("f", Literals.of(null, 
UNPARSED)))
+        }) {
+      assertRejected(
+          () -> tables.alterTable(ident, TableChange.setProperty("k1", 
"changed"), invalid),
+          "updates[1]");
+      Table unchanged = tables.loadTable(ident);
+      Assertions.assertEquals("v1", unchanged.properties().get("k1"));
+      Assertions.assertEquals(1, unchanged.columns().length);
+      Assertions.assertEquals(EXTERNAL, unchanged.columns()[0].dataType());
+    }
+    Table updated =
+        tables.alterTable(
+            ident,
+            TableChange.addColumn(new String[] {"new"}, 
Types.ListType.of(EXTERNAL, true)),
+            TableChange.updateColumnType(new String[] {"c"}, 
Types.StringType.get()));
+    Assertions.assertEquals(Types.StringType.get(), 
updated.columns()[0].dataType());
+    Assertions.assertEquals(Types.ListType.of(EXTERNAL, true), 
updated.columns()[1].dataType());
+  }
+
+  @Test
+  public void testLegacyTableCanBeImportedAndManaged() {
+    NameIdentifier ident = ident("legacy_table");
+    TestCatalog testCatalog =
+        (TestCatalog)
+            catalogManager.loadCatalogAndWrap(NameIdentifier.of(metalake, 
catalog)).catalog();
+    ((TestCatalogOperations) testCatalog.ops())
+        .createTable(
+            ident,
+            new Column[] {Column.of("legacy", UNPARSED)},
+            "original",
+            PROPERTIES,
+            new Transform[0],
+            Distributions.NONE,
+            new SortOrder[0],
+            Indexes.EMPTY_INDEXES);
+    Assertions.assertEquals(UNPARSED, 
tables.loadTable(ident).columns()[0].dataType());
+    Table updated =

Review Comment:
   Non-type alters submit only `TableChange` deltas, and validation runs before 
the connector call. Columns returned by the connector are synchronized into the 
store without write-type validation. The existing import/rename/comment test 
covers this compatibility boundary; I’ve kept that representative coverage here.



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