Skip to content

HIVE-28961: Respect partition limit in alter_table_req for partitioned tables #5823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -240,6 +240,8 @@ public void testListener() throws Exception {


driver.run(String.format("alter table %s rename to %s", tblName, renamed));
// remove the last auth call, which is for the get_partitions_req
authCalls.remove(authCalls.size() - 1);
listSize = authCalls.size();

Table renamedTableFromEvent = (
Original file line number Diff line number Diff line change
@@ -54,8 +54,10 @@
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.PartitionsRequest;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
import org.apache.thrift.TException;

import java.io.IOException;
import java.net.URI;
@@ -351,26 +353,30 @@ public void alterTable(RawStore msdb, Warehouse wh, String catName, String dbnam
String oldTblLocPath = srcPath.toUri().getPath();
String newTblLocPath = dataWasMoved ? destPath.toUri().getPath() : null;

// also the location field in partition
parts = msdb.getPartitions(catalogName, databaseName, tableName, -1);
for (Partition part : parts) {
String oldPartLoc = part.getSd().getLocation();
if (dataWasMoved && oldPartLoc.contains(oldTblLocPath)) {
URI oldUri = new Path(oldPartLoc).toUri();
String newPath = oldUri.getPath().replace(oldTblLocPath, newTblLocPath);
Path newPartLocPath = new Path(oldUri.getScheme(), oldUri.getAuthority(), newPath);
part.getSd().setLocation(newPartLocPath.toString());
}
part.setDbName(newDbName);
part.setTableName(newTblName);
}
// Do not verify stats parameters on a partitioned table.
msdb.alterTable(catalogName, databaseName, tableName, newt, null);
int partitionBatchSize = MetastoreConf.getIntVar(handler.getConf(),
MetastoreConf.ConfVars.BATCH_RETRIEVE_MAX);

// alterPartition is only for changing the partition location in the table rename
if (dataWasMoved) {
PartitionsRequest req = new PartitionsRequest(newDbName, newTblName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid calls get_partitions_req if not dataWasMoved

Please give more context about this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When dataWasMoved is false, we do not need get all partitions and update the location of partitions.

req.setCatName(catName);
req.setMaxParts((short) -1);
Copy link
Preview

Copilot AI May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a brief comment explaining the rationale behind using -1 to indicate fetching all partitions to aid future maintainers.

Copilot uses AI. Check for mistakes.

parts = handler.get_partitions_req(req).getPartitions();

for (Partition part : parts) {
String oldPartLoc = part.getSd().getLocation();
if (oldPartLoc.contains(oldTblLocPath)) {
URI oldUri = new Path(oldPartLoc).toUri();
String newPath = oldUri.getPath().replace(oldTblLocPath, newTblLocPath);
Path newPartLocPath = new Path(oldUri.getScheme(), oldUri.getAuthority(), newPath);
part.getSd().setLocation(newPartLocPath.toString());
}
part.setDbName(newDbName);
part.setTableName(newTblName);
}

Batchable.runBatched(partitionBatchSize, parts, new Batchable<Partition, Void>() {
@Override
public List<Void> run(List<Partition> input) throws Exception {
@@ -410,7 +416,10 @@ public List<Void> run(List<Partition> input) throws Exception {
msdb.alterTable(catalogName, databaseName, tableName, newt, null);

if (cascade || retainOnColRemoval) {
parts = msdb.getPartitions(catalogName, databaseName, tableName, -1);
PartitionsRequest req = new PartitionsRequest(dbname, name);
req.setCatName(catName);
req.setMaxParts((short) -1);
parts = handler.get_partitions_req(req).getPartitions();
Table finalOldt = oldt;
int partitionBatchSize = MetastoreConf.getIntVar(handler.getConf(),
MetastoreConf.ConfVars.BATCH_RETRIEVE_MAX);
@@ -469,17 +478,13 @@ public List<Void> run(List<Partition> input) throws Exception {
}
// commit the changes
success = msdb.commitTransaction();
} catch (InvalidObjectException e) {
} catch (InvalidOperationException | MetaException e) {
throw e;
} catch (TException e) {
LOG.debug("Failed to get object from Metastore ", e);
throw new InvalidOperationException(
"Unable to change partition or table."
+ " Check metastore logs for detailed stack." + e.getMessage());
}
catch (NoSuchObjectException e) {
LOG.debug("Object not found in metastore ", e);
throw new InvalidOperationException(
"Unable to change partition or table. Object " + e.getMessage() + " does not exist."
+ " Check metastore logs for detailed stack.");
} finally {
if (success) {
// Txn was committed successfully.
Original file line number Diff line number Diff line change
@@ -3780,6 +3780,30 @@ public void testAlterTableRenameBucketedColumnNegative() throws Exception {
silentDropDatabase(dbName);
}

@Test(expected = MetaException.class)
public void testAlterTableCascadeExceedsPartitionLimits() throws Throwable {
String dbName = "alterTblDb";
String tblName = "altertbl";
String ds = "2025-05-21 23:47:12";

cleanUp(dbName, tblName, null);

// Create too many partitions, just enough to validate over limit requests
List<List<String>> values = new ArrayList<>();
for (int i = 0; i < DEFAULT_LIMIT_PARTITION_REQUEST + 1; i++) {
values.add(makeVals(ds, Integer.toString(i)));
}

createMultiPartitionTableSchema(dbName, tblName, null, values);

Table tbl = client.getTable(dbName, tblName);
List<FieldSchema> cols = tbl.getSd().getCols();
cols.add(new FieldSchema("new_col", ColumnType.STRING_TYPE_NAME, ""));
tbl.getSd().setCols(cols);
//add new column with cascade option
client.alter_table(dbName, tblName, tbl, true);
}

@Test
public void testDataConnector() throws Throwable {
final String connector_name1 = "test_connector1";
Original file line number Diff line number Diff line change
@@ -325,7 +325,7 @@ public void testListener() throws Exception {
msc.alter_table(dbName, tblName, renamedTable);
listSize++;
assertEquals(notifyList.size(), listSize);
PreAlterTableEvent preAlterTableE = (PreAlterTableEvent) preNotifyList.get(preNotifyList.size() - 1);
PreAlterTableEvent preAlterTableE = (PreAlterTableEvent) preNotifyList.get(preNotifyList.size() - 2);

renamedTable = msc.getTable(dbName, renamed);

Loading
Oops, something went wrong.