Skip to content
Merged
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 @@ -69,6 +69,7 @@ public enum HiveErrorCode
// To be used for metadata inconsistencies and not for incorrect input from users
HIVE_INVALID_ENCRYPTION_METADATA(42, EXTERNAL),
HIVE_UNSUPPORTED_ENCRYPTION_OPERATION(43, USER_ERROR),
MALFORMED_HIVE_FILE_STATISTICS(44, INTERNAL_ERROR),
/**/;

private final ErrorCode errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public void configure(Binder binder)
binder.bind(HivePartitionManager.class).in(Scopes.SINGLETON);
binder.bind(LocationService.class).to(HiveLocationService.class).in(Scopes.SINGLETON);
binder.bind(TableParameterCodec.class).in(Scopes.SINGLETON);
binder.bind(HivePartitionStats.class).in(Scopes.SINGLETON);
Comment thread
NikhilCollooru marked this conversation as resolved.
Outdated
newExporter(binder).export(HivePartitionStats.class).as(generatedNameOf(HivePartitionStats.class, connectorId));
binder.bind(HiveMetadataFactory.class).in(Scopes.SINGLETON);
binder.bind(new TypeLiteral<Supplier<TransactionalMetadata>>() {}).to(HiveMetadataFactory.class).in(Scopes.SINGLETON);
binder.bind(StagingFileCommitter.class).to(HiveStagingFileCommitter.class).in(Scopes.SINGLETON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public interface HiveFileWriter

void appendRows(Page dataPage);

void commit();
// Page returned by commit should have fileSize as first channel
Optional<Page> commit();

void rollback();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed 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 com.facebook.presto.hive;

import com.facebook.presto.common.Page;
import com.facebook.presto.common.PageBuilder;
import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.spi.PrestoException;
import com.google.common.collect.ImmutableList;

import java.util.Optional;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.hive.HiveErrorCode.MALFORMED_HIVE_FILE_STATISTICS;
import static com.facebook.presto.hive.PartitionUpdate.FileWriteInfo;
import static io.airlift.slice.Slices.utf8Slice;
import static java.lang.String.format;

public class HiveManifestUtils
{
private static final int FILE_SIZE_CHANNEL = 0;
private static final int ROW_COUNT_CHANNEL = 1;

private HiveManifestUtils()
{
}

public static Page createFileStatisticsPage(long fileSize, long rowCount)
{
// FileStatistics page layout:
//
// fileSize rowCount
// X X
PageBuilder statsPageBuilder = new PageBuilder(ImmutableList.of(BIGINT, BIGINT));
statsPageBuilder.declarePosition();
BIGINT.writeLong(statsPageBuilder.getBlockBuilder(FILE_SIZE_CHANNEL), fileSize);
BIGINT.writeLong(statsPageBuilder.getBlockBuilder(ROW_COUNT_CHANNEL), rowCount);

return statsPageBuilder.build();
}

public static long getFileSize(Page statisticsPage, int position)
{
// FileStatistics page layout:
//
// fileSize rowCount
// X X

if (position < 0 || position >= statisticsPage.getPositionCount()) {
throw new PrestoException(MALFORMED_HIVE_FILE_STATISTICS, format("Invalid position: %d specified for FileStatistics page", position));
}
return BIGINT.getLong(statisticsPage.getBlock(FILE_SIZE_CHANNEL), position);
}

public static Optional<Page> createPartitionManifest(PartitionUpdate partitionUpdate)
{
// Manifest Page layout:
// fileName fileSize
// X X
// X X
// X X
// ....
PageBuilder manifestBuilder = new PageBuilder(ImmutableList.of(VARCHAR, BIGINT));
BlockBuilder fileNameBuilder = manifestBuilder.getBlockBuilder(0);
BlockBuilder fileSizeBuilder = manifestBuilder.getBlockBuilder(1);
for (FileWriteInfo fileWriteInfo : partitionUpdate.getFileWriteInfos()) {
if (!fileWriteInfo.getFileSize().isPresent()) {
return Optional.empty();
}
manifestBuilder.declarePosition();
VARCHAR.writeSlice(fileNameBuilder, utf8Slice(fileWriteInfo.getWriteFileName()));
BIGINT.writeLong(fileSizeBuilder, fileWriteInfo.getFileSize().get());
}
return Optional.of(manifestBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.hive;

import com.facebook.airlift.json.JsonCodec;
import com.facebook.presto.common.Page;
import com.facebook.presto.common.Subfield;
import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.predicate.Domain;
Expand All @@ -24,7 +25,6 @@
import com.facebook.presto.common.type.RowType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.TypeManager;
import com.facebook.presto.common.type.VarcharType;
import com.facebook.presto.hive.LocationService.WriteInfo;
import com.facebook.presto.hive.PartitionUpdate.FileWriteInfo;
import com.facebook.presto.hive.metastore.Column;
Expand Down Expand Up @@ -148,6 +148,7 @@
import static com.facebook.presto.common.type.DateType.DATE;
import static com.facebook.presto.common.type.TimestampType.TIMESTAMP;
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.common.type.Varchars.isVarcharType;
import static com.facebook.presto.expressions.LogicalRowExpressions.TRUE_CONSTANT;
import static com.facebook.presto.expressions.LogicalRowExpressions.binaryExpression;
Expand Down Expand Up @@ -177,6 +178,7 @@
import static com.facebook.presto.hive.HiveErrorCode.HIVE_UNKNOWN_ERROR;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_UNSUPPORTED_ENCRYPTION_OPERATION;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_UNSUPPORTED_FORMAT;
import static com.facebook.presto.hive.HiveManifestUtils.createPartitionManifest;
import static com.facebook.presto.hive.HivePartition.UNPARTITIONED_ID;
import static com.facebook.presto.hive.HivePartitioningHandle.createHiveCompatiblePartitioningHandle;
import static com.facebook.presto.hive.HivePartitioningHandle.createPrestoNativePartitioningHandle;
Expand Down Expand Up @@ -351,6 +353,7 @@ public class HiveMetadata
private final ZeroRowFileCreator zeroRowFileCreator;
private final PartitionObjectBuilder partitionObjectBuilder;
private final HiveEncryptionInformationProvider encryptionInformationProvider;
private final HivePartitionStats hivePartitionStats;

public HiveMetadata(
SemiTransactionalHiveMetastore metastore,
Expand All @@ -374,7 +377,8 @@ public HiveMetadata(
StagingFileCommitter stagingFileCommitter,
ZeroRowFileCreator zeroRowFileCreator,
PartitionObjectBuilder partitionObjectBuilder,
HiveEncryptionInformationProvider encryptionInformationProvider)
HiveEncryptionInformationProvider encryptionInformationProvider,
HivePartitionStats hivePartitionStats)
{
this.allowCorruptWritesForTesting = allowCorruptWritesForTesting;

Expand All @@ -399,6 +403,7 @@ public HiveMetadata(
this.zeroRowFileCreator = requireNonNull(zeroRowFileCreator, "zeroRowFileCreator is null");
this.partitionObjectBuilder = requireNonNull(partitionObjectBuilder, "partitionObjectBuilder is null");
this.encryptionInformationProvider = requireNonNull(encryptionInformationProvider, "encryptionInformationProvider is null");
this.hivePartitionStats = requireNonNull(hivePartitionStats, "hivePartitionStats is null");
}

public SemiTransactionalHiveMetastore getMetastore()
Expand Down Expand Up @@ -472,7 +477,7 @@ private Optional<SystemTable> getPropertiesSystemTable(SchemaTableName tableName
}
Map<String, String> sortedTableParameters = ImmutableSortedMap.copyOf(table.get().getParameters());
List<ColumnMetadata> columns = sortedTableParameters.keySet().stream()
.map(key -> new ColumnMetadata(key, VarcharType.VARCHAR))
.map(key -> new ColumnMetadata(key, VARCHAR))
.collect(toImmutableList());
List<Type> types = columns.stream()
.map(ColumnMetadata::getType)
Expand Down Expand Up @@ -1590,7 +1595,7 @@ private ImmutableList<PartitionUpdate> computePartitionUpdatesForMissingBuckets(
locationHandle.getWritePath(),
locationHandle.getTargetPath(),
fileNamesForMissingBuckets.stream()
.map(fileName -> new FileWriteInfo(fileName, fileName))
.map(fileName -> new FileWriteInfo(fileName, fileName, Optional.empty()))
.collect(toImmutableList()),
0,
0,
Expand All @@ -1613,7 +1618,7 @@ private ImmutableList<PartitionUpdate> computePartitionUpdatesForMissingBuckets(
partitionUpdate.getWritePath(),
partitionUpdate.getTargetPath(),
fileNamesForMissingBuckets.stream()
.map(fileName -> new FileWriteInfo(fileName, fileName))
.map(fileName -> new FileWriteInfo(fileName, fileName, Optional.empty()))
.collect(toImmutableList()),
0,
0,
Expand Down Expand Up @@ -1836,6 +1841,12 @@ else if (partitionUpdate.getUpdateMode() == NEW || partitionUpdate.getUpdateMode
Map<String, String> extraPartitionMetadata = handle.getEncryptionInformation()
.map(encryptionInfo -> encryptionInfo.getDwrfEncryptionMetadata().map(DwrfEncryptionMetadata::getExtraMetadata).orElseGet(ImmutableMap::of))
.orElseGet(ImmutableMap::of);

// TODO: Put the manifest blob in partition parameters
// Track the manifest blob size
Optional<Page> manifestBlob = createPartitionManifest(partitionUpdate);
manifestBlob.ifPresent(manifest -> hivePartitionStats.addManifestSizeInBytes(manifest.getRetainedSizeInBytes()));

Comment thread
NikhilCollooru marked this conversation as resolved.
Outdated
// insert into new partition or overwrite existing partition
Partition partition = partitionObjectBuilder.buildPartitionObject(
session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class HiveMetadataFactory
private final String prestoVersion;
private final PartitionObjectBuilder partitionObjectBuilder;
private final HiveEncryptionInformationProvider encryptionInformationProvider;
private final HivePartitionStats hivePartitionStats;

@Inject
@SuppressWarnings("deprecation")
Expand All @@ -84,7 +85,8 @@ public HiveMetadataFactory(
ZeroRowFileCreator zeroRowFileCreator,
NodeVersion nodeVersion,
PartitionObjectBuilder partitionObjectBuilder,
HiveEncryptionInformationProvider encryptionInformationProvider)
HiveEncryptionInformationProvider encryptionInformationProvider,
HivePartitionStats hivePartitionStats)
{
this(
metastore,
Expand All @@ -111,7 +113,8 @@ public HiveMetadataFactory(
zeroRowFileCreator,
nodeVersion.toString(),
partitionObjectBuilder,
encryptionInformationProvider);
encryptionInformationProvider,
hivePartitionStats);
}

public HiveMetadataFactory(
Expand Down Expand Up @@ -139,7 +142,8 @@ public HiveMetadataFactory(
ZeroRowFileCreator zeroRowFileCreator,
String prestoVersion,
PartitionObjectBuilder partitionObjectBuilder,
HiveEncryptionInformationProvider encryptionInformationProvider)
HiveEncryptionInformationProvider encryptionInformationProvider,
HivePartitionStats hivePartitionStats)
{
this.allowCorruptWritesForTesting = allowCorruptWritesForTesting;
this.skipDeletionForAlter = skipDeletionForAlter;
Expand Down Expand Up @@ -167,6 +171,7 @@ public HiveMetadataFactory(
this.prestoVersion = requireNonNull(prestoVersion, "prestoVersion is null");
this.partitionObjectBuilder = requireNonNull(partitionObjectBuilder, "partitionObjectBuilder is null");
this.encryptionInformationProvider = requireNonNull(encryptionInformationProvider, "encryptionInformationProvider is null");
this.hivePartitionStats = requireNonNull(hivePartitionStats, "hivePartitionStats is null");

if (!allowCorruptWritesForTesting && !timeZone.equals(DateTimeZone.getDefault())) {
log.warn("Hive writes are disabled. " +
Expand Down Expand Up @@ -208,6 +213,7 @@ public HiveMetadata get()
stagingFileCommitter,
zeroRowFileCreator,
partitionObjectBuilder,
encryptionInformationProvider);
encryptionInformationProvider,
hivePartitionStats);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed 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 com.facebook.presto.hive;

import com.facebook.airlift.stats.DistributionStat;
import org.weakref.jmx.Managed;
import org.weakref.jmx.Nested;

public class HivePartitionStats
{
private final DistributionStat manifestSizeInBytes = new DistributionStat();

public void addManifestSizeInBytes(long bytes)
{
manifestSizeInBytes.add(bytes);
}

@Managed
@Nested
public DistributionStat getManifestSizeInBytes()
{
return manifestSizeInBytes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Optional;
import java.util.function.Consumer;

import static com.facebook.presto.hive.HiveManifestUtils.getFileSize;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

Expand All @@ -37,6 +38,7 @@ public class HiveWriter

private long rowCount;
private long inputSizeInBytes;
private Optional<Page> fileStatistics = Optional.empty();

public HiveWriter(
HiveFileWriter fileWriter,
Expand Down Expand Up @@ -84,7 +86,7 @@ public void append(Page dataPage)

public void commit()
{
fileWriter.commit();
fileStatistics = fileWriter.commit();
onCommit.accept(this);
}

Expand All @@ -110,7 +112,7 @@ public PartitionUpdate getPartitionUpdate()
updateMode,
writePath,
targetPath,
ImmutableList.of(fileWriteInfo),
ImmutableList.of(new FileWriteInfo(fileWriteInfo.getWriteFileName(), fileWriteInfo.getTargetFileName(), fileStatistics.map(statisticsPage -> getFileSize(statisticsPage, 0)))),
rowCount,
inputSizeInBytes,
fileWriter.getWrittenBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public HiveWriter createWriter(Page partitionColumns, int position, OptionalInt
hiveFileWriter,
partitionName,
writerParameters.getUpdateMode(),
new FileWriteInfo(writeFileName, targetFileName),
new FileWriteInfo(writeFileName, targetFileName, Optional.empty()),
writerParameters.getWriteInfo().getWritePath().toString(),
writerParameters.getWriteInfo().getTargetPath().toString(),
createCommitEventListener(path, partitionName, hiveFileWriter, writerParameters),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static com.facebook.presto.hive.HiveErrorCode.HIVE_WRITER_CLOSE_ERROR;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_WRITER_DATA_ERROR;
import static com.facebook.presto.hive.HiveErrorCode.HIVE_WRITE_VALIDATION_FAILED;
import static com.facebook.presto.hive.HiveManifestUtils.createFileStatisticsPage;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;
Expand All @@ -64,6 +65,7 @@ public class OrcFileWriter
private final Optional<Supplier<OrcDataSource>> validationInputFactory;

private long validationCpuNanos;
private long rowCount;

public OrcFileWriter(
DataSink dataSink,
Expand Down Expand Up @@ -146,14 +148,15 @@ public void appendRows(Page dataPage)
Page page = new Page(dataPage.getPositionCount(), blocks);
try {
orcWriter.write(page);
rowCount += page.getPositionCount();
}
catch (IOException | UncheckedIOException e) {
throw new PrestoException(HIVE_WRITER_DATA_ERROR, e);
}
}

@Override
public void commit()
public Optional<Page> commit()
Comment thread
NikhilCollooru marked this conversation as resolved.
Outdated
{
try {
orcWriter.close();
Expand All @@ -180,6 +183,8 @@ public void commit()
throw new PrestoException(HIVE_WRITE_VALIDATION_FAILED, e);
}
}

return Optional.of(createFileStatisticsPage(getWrittenBytes(), rowCount));
}

@Override
Expand Down
Loading