cshuo commented on code in PR #19539: URL: https://github.com/apache/hudi/pull/19539#discussion_r3733065823
########## hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHoodieHiveSyncClientOperations.java: ########## @@ -0,0 +1,211 @@ +/* + * 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.hive; + +import org.apache.hudi.common.util.Option; +import org.apache.hudi.hive.ddl.DDLExecutor; +import org.apache.hudi.hive.ddl.JDBCBasedMetadataOperator; +import org.apache.hudi.sync.common.model.FieldSchema; +import org.apache.hudi.sync.common.model.Partition; + +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.SerDeInfo; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.thrift.TApplicationException; +import org.apache.thrift.TException; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.apache.hudi.hadoop.utils.HoodieHiveUtils.GLOBALLY_CONSISTENT_READ_TIMESTAMP; +import static org.apache.hudi.sync.common.HoodieMetaSyncOperations.HOODIE_LAST_COMMIT_COMPLETION_TIME_SYNC; +import static org.apache.hudi.sync.common.HoodieMetaSyncOperations.HOODIE_LAST_COMMIT_TIME_SYNC; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_BASE_FILE_FORMAT; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_BASE_PATH; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class TestHoodieHiveSyncClientOperations { + + @Test + void thriftMismatchActivatesJdbcMetadataFallback() throws Exception { + TestFixture fixture = newFixture(); + TApplicationException incompatible = new TApplicationException("unknown get_table method"); + when(fixture.metaStoreClient.tableExists("test_db", "table")) + .thenThrow(new TException(incompatible)); + when(fixture.jdbcMetadataOperator.tableExists("table")).thenReturn(true); + when(fixture.jdbcMetadataOperator.databaseExists("test_db")).thenReturn(true); + when(fixture.jdbcMetadataOperator.getTableProperty("table", HOODIE_LAST_COMMIT_TIME_SYNC)) + .thenReturn(Option.of("100")); + when(fixture.jdbcMetadataOperator.getTableProperty("table", HOODIE_LAST_COMMIT_COMPLETION_TIME_SYNC)) + .thenReturn(Option.of("101")); + when(fixture.jdbcMetadataOperator.getTableProperty("table", GLOBALLY_CONSISTENT_READ_TIMESTAMP)) + .thenReturn(Option.of("099")); + when(fixture.jdbcMetadataOperator.getTableLocation("table")).thenReturn("/warehouse/table"); + List<FieldSchema> fields = Collections.singletonList(new FieldSchema("id", "string", Option.empty())); + when(fixture.jdbcMetadataOperator.getFieldSchemas("table")).thenReturn(fields); + List<Partition> partitions = Collections.singletonList( + new Partition(Collections.singletonList("2026-08-06"), "/base/datestr=2026-08-06")); + when(fixture.jdbcMetadataOperator.getAllPartitions("table", "/base")).thenReturn(partitions); + + assertTrue(fixture.syncClient.tableExists("table")); + assertTrue(fixture.syncClient.updateTableProperties("table", Collections.singletonMap("owner", "hudi"))); + assertTrue(fixture.syncClient.updateSerdeProperties("table", + new HashMap<>(Collections.singletonMap("compression", "none")), false)); + assertEquals(partitions, fixture.syncClient.getAllPartitions("table")); + assertEquals(partitions, + fixture.syncClient.getPartitionsFromList("table", Collections.singletonList("datestr=2026-08-06"))); + assertTrue(fixture.syncClient.databaseExists("test_db")); + assertEquals("100", fixture.syncClient.getLastCommitTimeSynced("table").get()); + assertEquals("101", fixture.syncClient.getLastCommitCompletionTimeSynced("table").get()); + assertEquals("099", fixture.syncClient.getLastReplicatedTime("table").get()); + assertEquals(fields, fixture.syncClient.getMetastoreFieldSchemas("table")); + assertEquals("/warehouse/table", fixture.syncClient.getTableLocation("table")); + + fixture.syncClient.deleteLastReplicatedTimeStamp("table"); + fixture.syncClient.createOrReplaceTable("table", null, "input", "output", "serde", Review Comment: [P2] Both `createOrReplaceTable` tests verify the destructive drop and rename/alter operations, but never verify that `ddlExecutor.createTable` created the temporary table first. A regression that removes or reorders temporary-table creation could therefore pass these tests while leaving the live Hive table absent after rename fails. Capture the generated temporary name and use Mockito `InOrder` to verify create → drop → rename, including that the same temporary name is renamed. ########## hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/TestHoodieHiveSyncClientOperations.java: ########## @@ -0,0 +1,211 @@ +/* + * 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.hive; + +import org.apache.hudi.common.util.Option; +import org.apache.hudi.hive.ddl.DDLExecutor; +import org.apache.hudi.hive.ddl.JDBCBasedMetadataOperator; +import org.apache.hudi.sync.common.model.FieldSchema; +import org.apache.hudi.sync.common.model.Partition; + +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; +import org.apache.hadoop.hive.metastore.api.SerDeInfo; +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.thrift.TApplicationException; +import org.apache.thrift.TException; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.apache.hudi.hadoop.utils.HoodieHiveUtils.GLOBALLY_CONSISTENT_READ_TIMESTAMP; +import static org.apache.hudi.sync.common.HoodieMetaSyncOperations.HOODIE_LAST_COMMIT_COMPLETION_TIME_SYNC; +import static org.apache.hudi.sync.common.HoodieMetaSyncOperations.HOODIE_LAST_COMMIT_TIME_SYNC; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_BASE_FILE_FORMAT; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_BASE_PATH; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class TestHoodieHiveSyncClientOperations { + + @Test + void thriftMismatchActivatesJdbcMetadataFallback() throws Exception { + TestFixture fixture = newFixture(); + TApplicationException incompatible = new TApplicationException("unknown get_table method"); + when(fixture.metaStoreClient.tableExists("test_db", "table")) + .thenThrow(new TException(incompatible)); + when(fixture.jdbcMetadataOperator.tableExists("table")).thenReturn(true); + when(fixture.jdbcMetadataOperator.databaseExists("test_db")).thenReturn(true); + when(fixture.jdbcMetadataOperator.getTableProperty("table", HOODIE_LAST_COMMIT_TIME_SYNC)) + .thenReturn(Option.of("100")); + when(fixture.jdbcMetadataOperator.getTableProperty("table", HOODIE_LAST_COMMIT_COMPLETION_TIME_SYNC)) + .thenReturn(Option.of("101")); + when(fixture.jdbcMetadataOperator.getTableProperty("table", GLOBALLY_CONSISTENT_READ_TIMESTAMP)) + .thenReturn(Option.of("099")); + when(fixture.jdbcMetadataOperator.getTableLocation("table")).thenReturn("/warehouse/table"); + List<FieldSchema> fields = Collections.singletonList(new FieldSchema("id", "string", Option.empty())); + when(fixture.jdbcMetadataOperator.getFieldSchemas("table")).thenReturn(fields); + List<Partition> partitions = Collections.singletonList( + new Partition(Collections.singletonList("2026-08-06"), "/base/datestr=2026-08-06")); + when(fixture.jdbcMetadataOperator.getAllPartitions("table", "/base")).thenReturn(partitions); + + assertTrue(fixture.syncClient.tableExists("table")); + assertTrue(fixture.syncClient.updateTableProperties("table", Collections.singletonMap("owner", "hudi"))); + assertTrue(fixture.syncClient.updateSerdeProperties("table", + new HashMap<>(Collections.singletonMap("compression", "none")), false)); + assertEquals(partitions, fixture.syncClient.getAllPartitions("table")); + assertEquals(partitions, + fixture.syncClient.getPartitionsFromList("table", Collections.singletonList("datestr=2026-08-06"))); + assertTrue(fixture.syncClient.databaseExists("test_db")); + assertEquals("100", fixture.syncClient.getLastCommitTimeSynced("table").get()); + assertEquals("101", fixture.syncClient.getLastCommitCompletionTimeSynced("table").get()); + assertEquals("099", fixture.syncClient.getLastReplicatedTime("table").get()); + assertEquals(fields, fixture.syncClient.getMetastoreFieldSchemas("table")); + assertEquals("/warehouse/table", fixture.syncClient.getTableLocation("table")); + + fixture.syncClient.deleteLastReplicatedTimeStamp("table"); + fixture.syncClient.createOrReplaceTable("table", null, "input", "output", "serde", Review Comment: Both `createOrReplaceTable` tests verify the destructive drop and rename/alter operations, but never verify that `ddlExecutor.createTable` created the temporary table first. A regression that removes or reorders temporary-table creation could therefore pass these tests while leaving the live Hive table absent after rename fails. Capture the generated temporary name and use Mockito `InOrder` to verify create → drop → rename, including that the same temporary name is renamed. -- 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]
