Skip to content

Commit

Permalink
[hive] Make HiveCatalog alterTable works for Hive 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
JingsongLi committed Nov 24, 2024
1 parent 989a433 commit 8688206
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8688206

Please sign in to comment.