danny0405 commented on code in PR #19393:
URL: https://github.com/apache/hudi/pull/19393#discussion_r3670796230


##########
hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/catalog/TestHoodieHiveCatalog.java:
##########
@@ -149,6 +164,259 @@ public static void closeCatalog() {
     }
   }
 
+  @Test

Review Comment:
   Addressed in `bda47c6539a9`. The combined test is now split into 
`testDatabaseLifecycle`, `testTableLifecycleAgainstMetastore`, and 
`testHiveSyncOptionsInjected`, with small shared setup/cleanup helpers.



##########
hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/catalog/TestHoodieHiveCatalog.java:
##########
@@ -149,6 +164,259 @@ public static void closeCatalog() {
     }
   }
 
+  @Test
+  void testDatabaseAndTableApiAgainstMetastore() throws Exception {
+    String databaseName = "catalog_api_db";
+    ObjectPath databaseTablePath = new ObjectPath(databaseName, 
"catalog_api_table");
+    hoodieCatalog.dropDatabase(databaseName, true, true);
+
+    try {
+      CatalogDatabase database =
+          new CatalogDatabaseImpl(new HashMap<>(), "catalog api database");
+      hoodieCatalog.createDatabase(databaseName, database, false);
+
+      assertTrue(hoodieCatalog.databaseExists(databaseName));
+      assertTrue(hoodieCatalog.listDatabases().contains(databaseName));
+      CatalogDatabase storedDatabase = hoodieCatalog.getDatabase(databaseName);
+      assertEquals("catalog api database", storedDatabase.getComment());
+      assertNotNull(storedDatabase.getProperties().get(DATABASE_LOCATION_URI));
+      assertThrows(
+          DatabaseAlreadyExistException.class,
+          () -> hoodieCatalog.createDatabase(databaseName, database, false));
+      hoodieCatalog.createDatabase(databaseName, database, true);
+
+      Map<String, String> changedProperties = new HashMap<>();
+      changedProperties.put("purpose", "coverage");
+      changedProperties.put("is_generic", "true");
+      hoodieCatalog.alterDatabase(
+          databaseName,
+          new CatalogDatabaseImpl(changedProperties, null),
+          false);
+      assertEquals(
+          "coverage",
+          
hoodieCatalog.getDatabase(databaseName).getProperties().get("purpose"));
+      assertFalse(
+          
hoodieCatalog.getDatabase(databaseName).getProperties().containsKey("is_generic"));
+
+      String newLocation = new Path(
+          hoodieCatalog.getHiveConf().getVar(
+              
org.apache.hadoop.hive.conf.HiveConf.ConfVars.METASTOREWAREHOUSE),
+          databaseName + "_relocated").toString();
+      Map<String, String> locationProperties = new HashMap<>();
+      locationProperties.put(ALTER_DATABASE_OP, 
AlterHiveDatabaseOp.CHANGE_LOCATION.name());
+      locationProperties.put(DATABASE_LOCATION_URI, newLocation);
+      hoodieCatalog.alterDatabase(
+          databaseName,
+          new CatalogDatabaseImpl(locationProperties, null),
+          false);
+      assertNotNull(

Review Comment:
   Partially addressed in `bda47c6539a9`: both owner transitions now assert the 
exact stored owner name as well as `PrincipalType`. I also tried the exact 
location assertion locally, but the embedded fixture uses Hive 2.x 
`ObjectStore`, whose `alterDatabase` only persists parameters and owners; the 
stored location remains unchanged. I removed the misleading non-null location 
assertion and documented that limitation inline rather than claiming the 
embedded metastore applied it.



##########
hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/catalog/TestHoodieHiveCatalog.java:
##########
@@ -149,6 +164,259 @@ public static void closeCatalog() {
     }
   }
 
+  @Test
+  void testDatabaseAndTableApiAgainstMetastore() throws Exception {
+    String databaseName = "catalog_api_db";
+    ObjectPath databaseTablePath = new ObjectPath(databaseName, 
"catalog_api_table");
+    hoodieCatalog.dropDatabase(databaseName, true, true);
+
+    try {
+      CatalogDatabase database =
+          new CatalogDatabaseImpl(new HashMap<>(), "catalog api database");
+      hoodieCatalog.createDatabase(databaseName, database, false);
+
+      assertTrue(hoodieCatalog.databaseExists(databaseName));
+      assertTrue(hoodieCatalog.listDatabases().contains(databaseName));
+      CatalogDatabase storedDatabase = hoodieCatalog.getDatabase(databaseName);
+      assertEquals("catalog api database", storedDatabase.getComment());
+      assertNotNull(storedDatabase.getProperties().get(DATABASE_LOCATION_URI));
+      assertThrows(
+          DatabaseAlreadyExistException.class,
+          () -> hoodieCatalog.createDatabase(databaseName, database, false));
+      hoodieCatalog.createDatabase(databaseName, database, true);
+
+      Map<String, String> changedProperties = new HashMap<>();
+      changedProperties.put("purpose", "coverage");
+      changedProperties.put("is_generic", "true");
+      hoodieCatalog.alterDatabase(
+          databaseName,
+          new CatalogDatabaseImpl(changedProperties, null),
+          false);
+      assertEquals(
+          "coverage",
+          
hoodieCatalog.getDatabase(databaseName).getProperties().get("purpose"));
+      assertFalse(
+          
hoodieCatalog.getDatabase(databaseName).getProperties().containsKey("is_generic"));
+
+      String newLocation = new Path(
+          hoodieCatalog.getHiveConf().getVar(
+              
org.apache.hadoop.hive.conf.HiveConf.ConfVars.METASTOREWAREHOUSE),
+          databaseName + "_relocated").toString();
+      Map<String, String> locationProperties = new HashMap<>();
+      locationProperties.put(ALTER_DATABASE_OP, 
AlterHiveDatabaseOp.CHANGE_LOCATION.name());
+      locationProperties.put(DATABASE_LOCATION_URI, newLocation);
+      hoodieCatalog.alterDatabase(
+          databaseName,
+          new CatalogDatabaseImpl(locationProperties, null),
+          false);
+      assertNotNull(
+          
hoodieCatalog.getDatabase(databaseName).getProperties().get(DATABASE_LOCATION_URI));
+
+      Map<String, String> userOwner = new HashMap<>();
+      userOwner.put(ALTER_DATABASE_OP, 
AlterHiveDatabaseOp.CHANGE_OWNER.name());
+      userOwner.put(DATABASE_OWNER_NAME, "catalog-user");
+      userOwner.put(DATABASE_OWNER_TYPE, USER_OWNER);
+      hoodieCatalog.alterDatabase(
+          databaseName,
+          new CatalogDatabaseImpl(userOwner, null),
+          false);
+      assertEquals(
+          PrincipalType.USER,
+          hoodieCatalog.getClient().getDatabase(databaseName).getOwnerType());
+
+      Map<String, String> roleOwner = new HashMap<>();
+      roleOwner.put(ALTER_DATABASE_OP, 
AlterHiveDatabaseOp.CHANGE_OWNER.name());
+      roleOwner.put(DATABASE_OWNER_NAME, "catalog-role");
+      roleOwner.put(DATABASE_OWNER_TYPE, ROLE_OWNER);
+      hoodieCatalog.alterDatabase(
+          databaseName,
+          new CatalogDatabaseImpl(roleOwner, null),
+          false);
+      assertEquals(
+          PrincipalType.ROLE,
+          hoodieCatalog.getClient().getDatabase(databaseName).getOwnerType());
+
+      Map<String, String> invalidOwner = new HashMap<>();
+      invalidOwner.put(ALTER_DATABASE_OP, 
AlterHiveDatabaseOp.CHANGE_OWNER.name());
+      invalidOwner.put(DATABASE_OWNER_NAME, "catalog-group");
+      invalidOwner.put(DATABASE_OWNER_TYPE, "GROUP");
+      assertThrows(
+          org.apache.flink.table.catalog.exceptions.CatalogException.class,
+          () -> hoodieCatalog.alterDatabase(
+              databaseName,
+              new CatalogDatabaseImpl(invalidOwner, null),
+              false));
+
+      hoodieCatalog.alterDatabase(
+          "missing_catalog_api_db",
+          new CatalogDatabaseImpl(Collections.emptyMap(), null),
+          true);
+      assertThrows(
+          DatabaseNotExistException.class,
+          () -> hoodieCatalog.alterDatabase(
+              "missing_catalog_api_db",
+              new CatalogDatabaseImpl(Collections.emptyMap(), null),
+              false));
+
+      Map<String, String> tableOptions = new HashMap<>();
+      tableOptions.put(CONNECTOR.key(), "hudi");
+      CatalogTable catalogTable =
+          CatalogUtils.createCatalogTable(schema, partitions, tableOptions, 
"stored in hms");
+      hoodieCatalog.createTable(databaseTablePath, catalogTable, false);
+      assertThrows(
+          TableAlreadyExistException.class,
+          () -> hoodieCatalog.createTable(databaseTablePath, catalogTable, 
false));
+      hoodieCatalog.createTable(databaseTablePath, catalogTable, true);
+
+      
assertTrue(hoodieCatalog.listTables(databaseName).contains(databaseTablePath.getObjectName()));
+      assertTrue(hoodieCatalog.tableExists(databaseTablePath));
+      Table hiveTable = hoodieCatalog.getHiveTable(databaseTablePath);
+      assertEquals("hudi", hiveTable.getParameters().get(CONNECTOR.key()));
+      assertEquals("stored in hms", 
hiveTable.getParameters().get(TableOptionProperties.COMMENT));
+      assertEquals(
+          
"uuid:int,name:string,age:int,infos:array<string>,ts_3:timestamp,ts_6:timestamp",
+          hiveTable.getSd().getCols().stream()
+              .filter(field -> !field.getName().startsWith("_hoodie_"))
+              .map(field -> field.getName() + ":" + field.getType())
+              .collect(Collectors.joining(",")));
+      assertEquals(
+          schema.getColumns().stream()
+              .map(Schema.UnresolvedColumn::getName)
+              .collect(Collectors.toList()),
+          
hoodieCatalog.getTable(databaseTablePath).getUnresolvedSchema().getColumns().stream()
+              .map(Schema.UnresolvedColumn::getName)
+              .collect(Collectors.toList()));
+
+      String metastoreUris = hoodieCatalog.getHiveConf().getVar(
+          org.apache.hadoop.hive.conf.HiveConf.ConfVars.METASTOREURIS);
+      try {
+        hoodieCatalog.getHiveConf().setVar(
+            org.apache.hadoop.hive.conf.HiveConf.ConfVars.METASTOREURIS,
+            "thrift://localhost:9083");
+        Map<String, String> supplementedOptions =
+            hoodieCatalog.getTable(databaseTablePath).getOptions();
+        assertEquals("true", 
supplementedOptions.get(FlinkOptions.HIVE_SYNC_ENABLED.key()));

Review Comment:
   Addressed in `bda47c6539a9`: `testHiveSyncOptionsInjected` now asserts 
`HIVE_SYNC_METASTORE_URIS` equals the simulated remote endpoint 
`thrift://localhost:9083`.



##########
hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/table/catalog/TestHoodieCatalogUtil.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.hudi.table.catalog;
+
+import org.apache.hudi.configuration.FlinkOptions;
+import org.apache.hudi.utils.CatalogUtils;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.catalog.CatalogPartitionSpec;
+import org.apache.flink.table.catalog.CatalogTable;
+import org.apache.flink.table.catalog.ObjectPath;
+import org.apache.flink.table.catalog.exceptions.CatalogException;
+import org.apache.flink.table.catalog.exceptions.PartitionSpecInvalidException;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests for {@link HoodieCatalogUtil}.
+ */
+class TestHoodieCatalogUtil {
+
+  @Test
+  void testPartitionKeyAndPathHelpers() {
+    Schema schema = Schema.newBuilder()
+        .column("id", DataTypes.INT())

Review Comment:
   Addressed in `bda47c6539a9`: renamed to 
`testPartitionKeyExtractionAndPathInference`.



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