diff --git a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveAlterTableUtils.java b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveAlterTableUtils.java new file mode 100644 index 000000000000..8f77499486fd --- /dev/null +++ b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveAlterTableUtils.java @@ -0,0 +1,54 @@ +/* + * 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.paimon.hive; + +import org.apache.paimon.catalog.Identifier; + +import org.apache.hadoop.hive.common.StatsSetupConst; +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.EnvironmentContext; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.thrift.TException; + +/** Utils for hive alter table. */ +public class HiveAlterTableUtils { + + public static void alterTable(IMetaStoreClient client, Identifier identifier, Table table) + throws TException { + try { + alterTableWithEnv(client, identifier, table); + } catch (NoClassDefFoundError | NoSuchMethodError e) { + alterTableWithoutEnv(client, identifier, table); + } + } + + private static void alterTableWithEnv( + IMetaStoreClient client, Identifier identifier, Table table) throws TException { + EnvironmentContext environmentContext = new EnvironmentContext(); + environmentContext.putToProperties(StatsSetupConst.CASCADE, "true"); + environmentContext.putToProperties(StatsSetupConst.DO_NOT_UPDATE_STATS, "false"); + client.alter_table_with_environmentContext( + identifier.getDatabaseName(), identifier.getTableName(), table, environmentContext); + } + + private static void alterTableWithoutEnv( + IMetaStoreClient client, Identifier identifier, Table table) throws TException { + client.alter_table(identifier.getDatabaseName(), identifier.getTableName(), table, true); + } +} diff --git a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java index 0f2fb6fa9d1f..b92c3b59d925 100644 --- a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java +++ b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java @@ -56,12 +56,10 @@ import org.apache.flink.table.hive.LegacyHiveClasses; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; -import org.apache.hadoop.hive.common.StatsSetupConst; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.IMetaStoreClient; import org.apache.hadoop.hive.metastore.TableType; import org.apache.hadoop.hive.metastore.api.Database; -import org.apache.hadoop.hive.metastore.api.EnvironmentContext; import org.apache.hadoop.hive.metastore.api.FieldSchema; import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; import org.apache.hadoop.hive.metastore.api.SerDeInfo; @@ -87,7 +85,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; @@ -876,25 +873,7 @@ private void alterTableToHms(Table table, Identifier identifier, TableSchema new updateHmsTablePars(table, newSchema); Path location = getTableLocation(identifier, table); updateHmsTable(table, identifier, newSchema, newSchema.options().get("provider"), location); - clients.execute( - client -> - client.alter_table_with_environmentContext( - identifier.getDatabaseName(), - identifier.getTableName(), - table, - createHiveEnvironmentContext())); - } - - private EnvironmentContext createHiveEnvironmentContext() { - EnvironmentContext environmentContext = new EnvironmentContext(); - environmentContext.putToProperties(StatsSetupConst.CASCADE, "true"); - if (Objects.isNull(options)) { - return environmentContext; - } - environmentContext.putToProperties( - StatsSetupConst.DO_NOT_UPDATE_STATS, - options.getString(StatsSetupConst.DO_NOT_UPDATE_STATS, "false")); - return environmentContext; + clients.execute(client -> HiveAlterTableUtils.alterTable(client, identifier, table)); } @Override