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

Fix correctness issue with double slash and add test for S3 and Glue metastore #17804

Closed
wants to merge 1 commit into from
Closed
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 @@ -17,6 +17,8 @@
import com.amazonaws.services.glue.AWSGlueAsync;
import com.amazonaws.services.glue.model.ConcurrentModificationException;
import io.trino.Session;
import io.trino.filesystem.hdfs.HdfsFileSystemFactory;
import io.trino.hdfs.TrinoHdfsFileSystemStats;
import io.trino.plugin.deltalake.TestingDeltaLakePlugin;
import io.trino.plugin.deltalake.metastore.TestingDeltaLakeMetastoreModule;
import io.trino.plugin.hive.metastore.glue.DefaultGlueColumnStatisticsProviderFactory;
Expand Down Expand Up @@ -92,6 +94,7 @@ protected QueryRunner createQueryRunner()
});

metastore = new GlueHiveMetastore(
new HdfsFileSystemFactory(HDFS_ENVIRONMENT, new TrinoHdfsFileSystemStats()),
HDFS_ENVIRONMENT,
glueConfig,
directExecutor(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static io.airlift.concurrent.MoreFutures.addExceptionCallback;
import static io.airlift.concurrent.MoreFutures.toListenableFuture;
import static io.trino.filesystem.hdfs.HadoopPaths.hadoopPath;
import static io.trino.hdfs.ConfigurationUtils.toJobConf;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_BAD_DATA;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_EXCEEDED_PARTITION_LIMIT;
Expand Down Expand Up @@ -429,7 +430,8 @@ private ListenableFuture<Void> loadPartition(HivePartitionMetadata partition)
return COMPLETED_FUTURE;
}

Path path = new Path(getPartitionLocation(table, partition.getPartition()));
Location location = Location.of(getPartitionLocation(table, partition.getPartition()));
Path path = hadoopPath(location);
Configuration configuration = hdfsEnvironment.getConfiguration(hdfsContext, path);
InputFormat<?, ?> inputFormat = getInputFormat(configuration, schema, false);
FileSystem fs = hdfsEnvironment.getFileSystem(hdfsContext, path);
Expand Down Expand Up @@ -548,7 +550,6 @@ private ListenableFuture<Void> loadPartition(HivePartitionMetadata partition)
}

TrinoFileSystem trinoFileSystem = fileSystemFactory.create(session);
Location location = Location.of(path.toString());
// Bucketed partitions are fully loaded immediately since all files must be loaded to determine the file to bucket mapping
if (tableBucketInfo.isPresent()) {
List<TrinoFileStatus> files = listBucketFiles(trinoFileSystem, location, splitFactory.getPartitionName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.util.Optional;

import static io.trino.filesystem.hdfs.HadoopPaths.hadoopPath;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_PATH_ALREADY_EXISTS;
import static io.trino.plugin.hive.LocationHandle.WriteMode.DIRECT_TO_TARGET_EXISTING_DIRECTORY;
import static io.trino.plugin.hive.LocationHandle.WriteMode.DIRECT_TO_TARGET_NEW_DIRECTORY;
Expand Down Expand Up @@ -63,7 +64,7 @@ public Location forNewTable(SemiTransactionalHiveMetastore metastore, ConnectorS
Location targetPath = getTableDefaultLocation(context, metastore, hdfsEnvironment, schemaName, tableName);

// verify the target directory for table
if (pathExists(context, hdfsEnvironment, new Path(targetPath.toString()))) {
if (pathExists(context, hdfsEnvironment, hadoopPath(targetPath))) {
throw new TrinoException(HIVE_PATH_ALREADY_EXISTS, format("Target directory for table '%s.%s' already exists: %s", schemaName, tableName, targetPath));
}
return targetPath;
Expand All @@ -76,13 +77,13 @@ public LocationHandle forNewTableAsSelect(SemiTransactionalHiveMetastore metasto
Location targetPath = externalLocation.orElseGet(() -> getTableDefaultLocation(context, metastore, hdfsEnvironment, schemaName, tableName));

// verify the target directory for the table
if (pathExists(context, hdfsEnvironment, new Path(targetPath.toString()))) {
if (pathExists(context, hdfsEnvironment, hadoopPath(targetPath))) {
throw new TrinoException(HIVE_PATH_ALREADY_EXISTS, format("Target directory for table '%s.%s' already exists: %s", schemaName, tableName, targetPath));
}

// TODO detect when existing table's location is a on a different file system than the temporary directory
if (shouldUseTemporaryDirectory(context, new Path(targetPath.toString()), externalLocation.isPresent())) {
Location writePath = createTemporaryPath(context, hdfsEnvironment, new Path(targetPath.toString()), temporaryStagingDirectoryPath);
if (shouldUseTemporaryDirectory(context, hadoopPath(targetPath), externalLocation.isPresent())) {
Location writePath = createTemporaryPath(context, hdfsEnvironment, hadoopPath(targetPath), temporaryStagingDirectoryPath);
return new LocationHandle(targetPath, writePath, STAGE_AND_MOVE_TO_TARGET_DIRECTORY);
}
return new LocationHandle(targetPath, targetPath, DIRECT_TO_TARGET_NEW_DIRECTORY);
Expand All @@ -94,8 +95,8 @@ public LocationHandle forExistingTable(SemiTransactionalHiveMetastore metastore,
HdfsContext context = new HdfsContext(session);
Location targetPath = Location.of(table.getStorage().getLocation());

if (shouldUseTemporaryDirectory(context, new Path(targetPath.toString()), false) && !isTransactionalTable(table.getParameters())) {
Location writePath = createTemporaryPath(context, hdfsEnvironment, new Path(targetPath.toString()), temporaryStagingDirectoryPath);
if (shouldUseTemporaryDirectory(context, hadoopPath(targetPath), false) && !isTransactionalTable(table.getParameters())) {
Location writePath = createTemporaryPath(context, hdfsEnvironment, hadoopPath(targetPath), temporaryStagingDirectoryPath);
return new LocationHandle(targetPath, writePath, STAGE_AND_MOVE_TO_TARGET_DIRECTORY);
}
return new LocationHandle(targetPath, targetPath, DIRECT_TO_TARGET_EXISTING_DIRECTORY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.getOnlyElement;
import static io.trino.filesystem.hdfs.HadoopPaths.hadoopPath;
import static io.trino.hdfs.ConfigurationUtils.toJobConf;
import static io.trino.plugin.hive.HiveAnalyzeProperties.getColumnNames;
import static io.trino.plugin.hive.HiveAnalyzeProperties.getPartitionList;
Expand Down Expand Up @@ -1734,7 +1735,7 @@ public Optional<ConnectorOutputMetadata> finishCreateTable(ConnectorSession sess
partitionUpdates = PartitionUpdate.mergePartitionUpdates(concat(partitionUpdates, partitionUpdatesForMissingBuckets));
for (PartitionUpdate partitionUpdate : partitionUpdatesForMissingBuckets) {
Optional<Partition> partition = table.getPartitionColumns().isEmpty() ? Optional.empty() : Optional.of(buildPartitionObject(session, table, partitionUpdate));
createEmptyFiles(session, partitionUpdate.getWritePath(), table, partition, partitionUpdate.getFileNames());
createEmptyFiles(session, hadoopPath(partitionUpdate.getWritePath()), table, partition, partitionUpdate.getFileNames());
}
if (handle.isTransactional()) {
AcidTransaction transaction = handle.getTransaction();
Expand Down Expand Up @@ -1765,7 +1766,7 @@ public Optional<ConnectorOutputMetadata> finishCreateTable(ConnectorSession sess
tableStatistics = new PartitionStatistics(createEmptyStatistics(), ImmutableMap.of());
}

Optional<Path> writePath = Optional.of(new Path(writeInfo.writePath().toString()));
Optional<Location> writePath = Optional.of(Location.of(writeInfo.writePath().toString()));
if (handle.getPartitionedBy().isEmpty()) {
List<String> fileNames;
if (partitionUpdates.isEmpty()) {
Expand Down Expand Up @@ -1845,7 +1846,7 @@ private List<PartitionUpdate> computePartitionUpdatesForMissingBuckets(
private List<String> computeFileNamesForMissingBuckets(
ConnectorSession session,
HiveStorageFormat storageFormat,
Path targetPath,
Location targetPath,
int bucketCount,
boolean transactionalCreateTable,
PartitionUpdate partitionUpdate)
Expand All @@ -1855,7 +1856,7 @@ private List<String> computeFileNamesForMissingBuckets(
return ImmutableList.of();
}
HdfsContext hdfsContext = new HdfsContext(session);
JobConf conf = toJobConf(hdfsEnvironment.getConfiguration(hdfsContext, targetPath));
JobConf conf = toJobConf(hdfsEnvironment.getConfiguration(hdfsContext, hadoopPath(targetPath)));
configureCompression(conf, selectCompressionCodec(session, storageFormat));
String fileExtension = HiveWriterFactory.getFileExtension(conf, fromHiveStorageFormat(storageFormat));
Set<String> fileNames = ImmutableSet.copyOf(partitionUpdate.getFileNames());
Expand Down Expand Up @@ -2141,7 +2142,7 @@ private Table finishChangingTable(AcidOperation acidOperation, String changeDesc
statistics,
handle.isRetriesEnabled());
}
createEmptyFiles(session, partitionUpdate.getWritePath(), table, partition, partitionUpdate.getFileNames());
createEmptyFiles(session, hadoopPath(partitionUpdate.getWritePath()), table, partition, partitionUpdate.getFileNames());
}
}

Expand Down Expand Up @@ -2227,10 +2228,10 @@ else if (partitionUpdate.getUpdateMode() == NEW || partitionUpdate.getUpdateMode
getColumnStatistics(partitionComputedStatistics, partitionName, partitionValues, partitionTypes));
if (partitionUpdate.getUpdateMode() == OVERWRITE) {
if (handle.getLocationHandle().getWriteMode() == DIRECT_TO_TARGET_EXISTING_DIRECTORY) {
removeNonCurrentQueryFiles(session, partitionUpdate.getTargetPath());
removeNonCurrentQueryFiles(session, hadoopPath(partitionUpdate.getTargetPath()));
if (handle.isRetriesEnabled()) {
HdfsContext hdfsContext = new HdfsContext(session);
cleanExtraOutputFiles(hdfsEnvironment, hdfsContext, session.getQueryId(), partitionUpdate.getTargetPath(), ImmutableSet.copyOf(partitionUpdate.getFileNames()));
cleanExtraOutputFiles(hdfsEnvironment, hdfsContext, session.getQueryId(), hadoopPath(partitionUpdate.getTargetPath()), ImmutableSet.copyOf(partitionUpdate.getFileNames()));
}
}
else {
Expand Down Expand Up @@ -2555,30 +2556,30 @@ private void finishOptimize(ConnectorSession session, ConnectorTableExecuteHandl
}

// path to be deleted
Set<Path> scannedPaths = splitSourceInfo.stream()
.map(file -> new Path((String) file))
Set<Location> scannedPaths = splitSourceInfo.stream()
.map(file -> Location.of((String) file))
.collect(toImmutableSet());
// track remaining files to be delted for error reporting
Set<Path> remainingFilesToDelete = new HashSet<>(scannedPaths);
Set<Location> remainingFilesToDelete = new HashSet<>(scannedPaths);

// delete loop
boolean someDeleted = false;
Optional<Path> firstScannedPath = Optional.empty();
Optional<Location> firstScannedPath = Optional.empty();
try {
for (Path scannedPath : scannedPaths) {
for (Location scannedPath : scannedPaths) {
if (firstScannedPath.isEmpty()) {
firstScannedPath = Optional.of(scannedPath);
}
retry().run("delete " + scannedPath, () -> {
checkedDelete(fs, scannedPath, false);
checkedDelete(fs, hadoopPath(scannedPath), false);
return null;
});
someDeleted = true;
remainingFilesToDelete.remove(scannedPath);
}
}
catch (Exception e) {
if (!someDeleted && (firstScannedPath.isEmpty() || exists(fs, firstScannedPath.get()))) {
if (!someDeleted && (firstScannedPath.isEmpty() || exists(fs, hadoopPath(firstScannedPath.get())))) {
// we are good - we did not delete any source files so we can just throw error and allow rollback to happend
// if someDeleted flag is false we do extra checkig if first file we tried to delete is still there. There is a chance that
// fs.delete above could throw exception but file was actually deleted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.plugin.hive;

import com.google.common.collect.ImmutableList;
import io.trino.filesystem.Location;
import io.trino.plugin.hive.PartitionUpdate.UpdateMode;
import io.trino.spi.Page;

Expand Down Expand Up @@ -114,8 +115,8 @@ public PartitionUpdate getPartitionUpdate()
return new PartitionUpdate(
partitionName.orElse(""),
updateMode,
writePath,
targetPath,
Location.of(writePath),
Location.of(targetPath),
ImmutableList.of(fileName),
rowCount,
inputSizeInBytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps;
import io.trino.filesystem.Location;
import io.trino.spi.TrinoException;
import org.apache.hadoop.fs.Path;

import java.util.Collection;
import java.util.List;
Expand All @@ -33,8 +33,8 @@ public class PartitionUpdate
{
private final String name;
private final UpdateMode updateMode;
private final Path writePath;
private final Path targetPath;
private final Location writePath;
private final Location targetPath;
private final List<String> fileNames;
private final long rowCount;
private final long inMemoryDataSizeInBytes;
Expand All @@ -54,8 +54,8 @@ public PartitionUpdate(
this(
name,
updateMode,
new Path(requireNonNull(writePath, "writePath is null")),
new Path(requireNonNull(targetPath, "targetPath is null")),
Location.of(requireNonNull(writePath, "writePath is null")),
Location.of(requireNonNull(targetPath, "targetPath is null")),
fileNames,
rowCount,
inMemoryDataSizeInBytes,
Expand All @@ -65,8 +65,8 @@ public PartitionUpdate(
public PartitionUpdate(
String name,
UpdateMode updateMode,
Path writePath,
Path targetPath,
Location writePath,
Location targetPath,
List<String> fileNames,
long rowCount,
long inMemoryDataSizeInBytes,
Expand Down Expand Up @@ -101,12 +101,12 @@ public UpdateMode getUpdateMode()
return updateMode;
}

public Path getWritePath()
public Location getWritePath()
{
return writePath;
}

public Path getTargetPath()
public Location getTargetPath()
{
return targetPath;
}
Expand Down
Loading