Skip to content
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

[hive]Fix hive catalog create partitions using table location. #5029

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -350,7 +350,6 @@ public void createPartitions(Identifier identifier, List<Map<String, String>> pa
Identifier tableIdentifier =
Identifier.create(identifier.getDatabaseName(), identifier.getTableName());
Table hmsTable = getHmsTable(tableIdentifier);
Path location = getTableLocation(tableIdentifier, hmsTable);
TableSchema schema = loadTableSchema(tableIdentifier, hmsTable);

if (!metastorePartitioned(schema)) {
Expand All @@ -359,27 +358,19 @@ public void createPartitions(Identifier identifier, List<Map<String, String>> pa

int currentTime = (int) (System.currentTimeMillis() / 1000);
StorageDescriptor sd = hmsTable.getSd();
String dataFilePath =
hmsTable.getParameters().containsKey(DATA_FILE_PATH_DIRECTORY.key())
? sd.getLocation()
+ "/"
+ hmsTable.getParameters().get(DATA_FILE_PATH_DIRECTORY.key())
: sd.getLocation();
String dataFilePath = getDataFilePath(tableIdentifier, hmsTable);
List<Partition> hivePartitions = new ArrayList<>();
for (Map<String, String> partitionSpec : partitions) {
Partition hivePartition = new Partition();
StorageDescriptor newSd = new StorageDescriptor(sd);
newSd.setLocation(
dataFilePath
+ "/"
+ PartitionPathUtils.generatePartitionPath(
new LinkedHashMap<>(partitionSpec)));
hivePartition.setDbName(identifier.getDatabaseName());
hivePartition.setTableName(identifier.getTableName());
hivePartition.setValues(new ArrayList<>(partitionSpec.values()));
hivePartition.setSd(newSd);
hivePartition.setCreateTime(currentTime);
hivePartition.setLastAccessTime(currentTime);
String partitionLocation = getPartitionLocation(dataFilePath, partitionSpec);
locationHelper.specifyPartitionLocation(hivePartition, partitionLocation);
hivePartitions.add(hivePartition);
}
try {
Expand All @@ -400,6 +391,9 @@ public void dropPartitions(Identifier identifier, List<Map<String, String>> part
tagToPart
? partitions
: removePartitionsExistsInOtherBranches(identifier, partitions);
Table hmsTable = getHmsTable(identifier);
boolean externalTable = isExternalTable(hmsTable);
String dataFilePath = getDataFilePath(identifier, hmsTable);
for (Map<String, String> part : metaPartitions) {
List<String> partitionValues = new ArrayList<>(part.values());
try {
Expand All @@ -410,6 +404,11 @@ public void dropPartitions(Identifier identifier, List<Map<String, String>> part
identifier.getTableName(),
partitionValues,
false));

if (!externalTable) {
String partitionLocation = getPartitionLocation(dataFilePath, part);
locationHelper.dropPathIfRequired(new Path(partitionLocation), fileIO);
}
} catch (NoSuchObjectException e) {
// do nothing if the partition not exists
} catch (Exception e) {
Expand All @@ -422,6 +421,19 @@ public void dropPartitions(Identifier identifier, List<Map<String, String>> part
}
}

private String getDataFilePath(Identifier tableIdentifier, Table hmsTable) {
String tableLocation = getTableLocation(tableIdentifier, hmsTable).toUri().toString();
return hmsTable.getParameters().containsKey(DATA_FILE_PATH_DIRECTORY.key())
? tableLocation + "/" + hmsTable.getParameters().get(DATA_FILE_PATH_DIRECTORY.key())
: tableLocation;
}

private String getPartitionLocation(String dataFilePath, Map<String, String> partitionSpec) {
return dataFilePath
+ "/"
+ PartitionPathUtils.generatePartitionPath(new LinkedHashMap<>(partitionSpec));
}

@Override
public void alterPartitions(
Identifier identifier, List<org.apache.paimon.partition.Partition> partitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.paimon.fs.Path;

import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.Table;

import java.io.IOException;
Expand All @@ -40,4 +41,8 @@ public interface LocationHelper {
void specifyDatabaseLocation(Path path, Database database);

String getDatabaseLocation(Database database);

void specifyPartitionLocation(Partition partition, String location);

String getPartitionLocation(Partition partition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.paimon.fs.Path;

import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.Table;

/** Helper for Setting Location in Hive Table Storage. */
Expand Down Expand Up @@ -58,4 +59,14 @@ public void specifyDatabaseLocation(Path path, Database database) {
public String getDatabaseLocation(Database database) {
return database.getLocationUri();
}

@Override
public void specifyPartitionLocation(Partition partition, String location) {
partition.getSd().setLocation(location);
}

@Override
public String getPartitionLocation(Partition partition) {
return partition.getSd().getLocation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.paimon.fs.Path;

import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.Partition;
import org.apache.hadoop.hive.metastore.api.Table;

import java.io.IOException;
Expand Down Expand Up @@ -82,4 +83,20 @@ public String getDatabaseLocation(Database database) {

return database.getLocationUri();
}

@Override
public void specifyPartitionLocation(Partition partition, String location) {
partition.putToParameters(LocationKeyExtractor.TBPROPERTIES_LOCATION_KEY, location);
}

@Override
public String getPartitionLocation(Partition partition) {
String location =
partition.getParameters().get(LocationKeyExtractor.TBPROPERTIES_LOCATION_KEY);
if (location != null) {
return location;
}

return partition.getSd().getLocation();
}
}