-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Support native parquet writer in hive module and Misc fixes #3400
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4e48c0f
Expose written bytes and buffered bytes in ParquetWriter
qqibrow fb5053a
Support getRetainedBytes in ParquetWriter
qqibrow 330ec3f
Add parquet writer in hive module
qqibrow 971ad00
Add test for optimized parquet writer in hive module
qqibrow a5f0ec5
Add compression in ParquetFileWriterFactory
qqibrow b9ce66a
Set statistics in RowGroup metadata
qqibrow 17546f0
resetDictionary right after get dictionary page
qqibrow bb27f1c
Add encoding should be called after getBytes() and before reset()
qqibrow 4559143
Support setting page size and row group size in parquet writer
qqibrow 8c82541
Set statistics in row group metadata
qqibrow d8dcc5d
Set parquet writer page size and row group size in ParquetTester
qqibrow a5f7df3
Set ParquetWriterOptions based on session parameters
qqibrow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
presto-hive/src/main/java/io/prestosql/plugin/hive/parquet/ParquetFileWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * 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 io.prestosql.plugin.hive.parquet; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.prestosql.parquet.writer.ParquetWriter; | ||
| import io.prestosql.parquet.writer.ParquetWriterOptions; | ||
| import io.prestosql.plugin.hive.FileWriter; | ||
| import io.prestosql.spi.Page; | ||
| import io.prestosql.spi.PrestoException; | ||
| import io.prestosql.spi.block.Block; | ||
| import io.prestosql.spi.block.BlockBuilder; | ||
| import io.prestosql.spi.block.RunLengthEncodedBlock; | ||
| import io.prestosql.spi.type.Type; | ||
| import org.apache.parquet.hadoop.metadata.CompressionCodecName; | ||
| import org.openjdk.jol.info.ClassLayout; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
|
|
||
| import static com.google.common.base.MoreObjects.toStringHelper; | ||
| import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_WRITER_CLOSE_ERROR; | ||
| import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_WRITER_DATA_ERROR; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class ParquetFileWriter | ||
| implements FileWriter | ||
| { | ||
| private static final int INSTANCE_SIZE = ClassLayout.parseClass(ParquetFileWriter.class).instanceSize(); | ||
|
|
||
| private final ParquetWriter parquetWriter; | ||
| private final Callable<Void> rollbackAction; | ||
| private final int[] fileInputColumnIndexes; | ||
| private final List<Block> nullBlocks; | ||
|
|
||
| public ParquetFileWriter( | ||
| OutputStream outputStream, | ||
| Callable<Void> rollbackAction, | ||
| List<String> columnNames, | ||
| List<Type> fileColumnTypes, | ||
| ParquetWriterOptions parquetWriterOptions, | ||
| int[] fileInputColumnIndexes, | ||
| CompressionCodecName compressionCodecName) | ||
| { | ||
| requireNonNull(outputStream, "outputStream is null"); | ||
|
|
||
| this.parquetWriter = new ParquetWriter( | ||
| outputStream, | ||
| columnNames, | ||
| fileColumnTypes, | ||
| parquetWriterOptions, | ||
| compressionCodecName); | ||
|
|
||
| this.rollbackAction = requireNonNull(rollbackAction, "rollbackAction is null"); | ||
| this.fileInputColumnIndexes = requireNonNull(fileInputColumnIndexes, "fileInputColumnIndexes is null"); | ||
|
|
||
| ImmutableList.Builder<Block> nullBlocks = ImmutableList.builder(); | ||
| for (Type fileColumnType : fileColumnTypes) { | ||
| BlockBuilder blockBuilder = fileColumnType.createBlockBuilder(null, 1, 0); | ||
| blockBuilder.appendNull(); | ||
| nullBlocks.add(blockBuilder.build()); | ||
| } | ||
| this.nullBlocks = nullBlocks.build(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getWrittenBytes() | ||
| { | ||
| return parquetWriter.getWrittenBytes(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getSystemMemoryUsage() | ||
| { | ||
| return INSTANCE_SIZE + parquetWriter.getRetainedBytes(); | ||
| } | ||
|
|
||
| @Override | ||
| public void appendRows(Page dataPage) | ||
| { | ||
| Block[] blocks = new Block[fileInputColumnIndexes.length]; | ||
| for (int i = 0; i < fileInputColumnIndexes.length; i++) { | ||
| int inputColumnIndex = fileInputColumnIndexes[i]; | ||
| if (inputColumnIndex < 0) { | ||
| blocks[i] = new RunLengthEncodedBlock(nullBlocks.get(i), dataPage.getPositionCount()); | ||
| } | ||
| else { | ||
| blocks[i] = dataPage.getBlock(inputColumnIndex); | ||
| } | ||
| } | ||
| Page page = new Page(dataPage.getPositionCount(), blocks); | ||
| try { | ||
| parquetWriter.write(page); | ||
| } | ||
| catch (IOException | UncheckedIOException e) { | ||
| throw new PrestoException(HIVE_WRITER_DATA_ERROR, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void commit() | ||
| { | ||
| try { | ||
| parquetWriter.close(); | ||
| } | ||
| catch (IOException | UncheckedIOException e) { | ||
| try { | ||
| rollbackAction.call(); | ||
| } | ||
| catch (Exception ignored) { | ||
| // ignore | ||
| } | ||
| throw new PrestoException(HIVE_WRITER_CLOSE_ERROR, "Error committing write parquet to Hive", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void rollback() | ||
| { | ||
| try { | ||
| try { | ||
| parquetWriter.close(); | ||
| } | ||
| finally { | ||
| rollbackAction.call(); | ||
| } | ||
| } | ||
| catch (Exception e) { | ||
| throw new PrestoException(HIVE_WRITER_CLOSE_ERROR, "Error rolling back write parquet to Hive", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public long getValidationCpuNanos() | ||
| { | ||
| return 0; | ||
| } | ||
|
qqibrow marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return toStringHelper(this) | ||
| .add("writer", parquetWriter) | ||
| .toString(); | ||
| } | ||
| } | ||
147 changes: 147 additions & 0 deletions
147
presto-hive/src/main/java/io/prestosql/plugin/hive/parquet/ParquetFileWriterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * 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 io.prestosql.plugin.hive.parquet; | ||
|
|
||
| import io.prestosql.parquet.writer.ParquetWriterOptions; | ||
| import io.prestosql.plugin.hive.FileWriter; | ||
| import io.prestosql.plugin.hive.HdfsEnvironment; | ||
| import io.prestosql.plugin.hive.HiveConfig; | ||
| import io.prestosql.plugin.hive.HiveFileWriterFactory; | ||
| import io.prestosql.plugin.hive.HiveSessionProperties; | ||
|
qqibrow marked this conversation as resolved.
Outdated
|
||
| import io.prestosql.plugin.hive.NodeVersion; | ||
| import io.prestosql.plugin.hive.metastore.StorageFormat; | ||
| import io.prestosql.spi.PrestoException; | ||
| import io.prestosql.spi.connector.ConnectorSession; | ||
| import io.prestosql.spi.type.Type; | ||
| import io.prestosql.spi.type.TypeManager; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat; | ||
| import org.apache.hadoop.mapred.JobConf; | ||
| import org.apache.parquet.hadoop.ParquetOutputFormat; | ||
| import org.apache.parquet.hadoop.metadata.CompressionCodecName; | ||
| import org.joda.time.DateTimeZone; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.Callable; | ||
|
|
||
| import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_WRITER_OPEN_ERROR; | ||
| import static io.prestosql.plugin.hive.util.HiveUtil.getColumnNames; | ||
| import static io.prestosql.plugin.hive.util.HiveUtil.getColumnTypes; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| public class ParquetFileWriterFactory | ||
| implements HiveFileWriterFactory | ||
| { | ||
| private final DateTimeZone hiveStorageTimeZone; | ||
| private final HdfsEnvironment hdfsEnvironment; | ||
| private final TypeManager typeManager; | ||
| private final NodeVersion nodeVersion; | ||
|
|
||
| @Inject | ||
| public ParquetFileWriterFactory( | ||
| HdfsEnvironment hdfsEnvironment, | ||
| TypeManager typeManager, | ||
| NodeVersion nodeVersion, | ||
| HiveConfig hiveConfig) | ||
| { | ||
| this( | ||
| hdfsEnvironment, | ||
| typeManager, | ||
| nodeVersion, | ||
| requireNonNull(hiveConfig, "hiveConfig is null").getDateTimeZone()); | ||
| } | ||
|
|
||
| public ParquetFileWriterFactory( | ||
| HdfsEnvironment hdfsEnvironment, | ||
| TypeManager typeManager, | ||
| NodeVersion nodeVersion, | ||
| DateTimeZone hiveStorageTimeZone) | ||
| { | ||
| this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null"); | ||
| this.typeManager = requireNonNull(typeManager, "typeManager is null"); | ||
| this.nodeVersion = requireNonNull(nodeVersion, "nodeVersion is null"); | ||
| this.hiveStorageTimeZone = requireNonNull(hiveStorageTimeZone, "hiveStorageTimeZone is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<FileWriter> createFileWriter( | ||
| Path path, | ||
| List<String> inputColumnNames, | ||
| StorageFormat storageFormat, | ||
| Properties schema, | ||
| JobConf conf, | ||
| ConnectorSession session) | ||
| { | ||
| if (!HiveSessionProperties.isParquetOptimizedWriterEnabled(session)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| if (!MapredParquetOutputFormat.class.getName().equals(storageFormat.getOutputFormat())) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| ParquetWriterOptions parquetWriterOptions = ParquetWriterOptions.builder() | ||
| .setMaxPageSize(HiveSessionProperties.getParquetWriterPageSize(session)) | ||
| .setMaxBlockSize(HiveSessionProperties.getParquetWriterBlockSize(session)) | ||
| .build(); | ||
|
|
||
| CompressionCodecName compressionCodecName = getCompression(conf); | ||
|
|
||
| List<String> fileColumnNames = getColumnNames(schema); | ||
| List<Type> fileColumnTypes = getColumnTypes(schema).stream() | ||
| .map(hiveType -> hiveType.getType(typeManager)) | ||
| .collect(toList()); | ||
|
|
||
| int[] fileInputColumnIndexes = fileColumnNames.stream() | ||
| .mapToInt(inputColumnNames::indexOf) | ||
| .toArray(); | ||
|
|
||
| try { | ||
| FileSystem fileSystem = hdfsEnvironment.getFileSystem(session.getUser(), path, conf); | ||
|
|
||
| Callable<Void> rollbackAction = () -> { | ||
| fileSystem.delete(path, false); | ||
| return null; | ||
| }; | ||
|
|
||
| return Optional.of(new ParquetFileWriter( | ||
| fileSystem.create(path), | ||
| rollbackAction, | ||
| fileColumnNames, | ||
| fileColumnTypes, | ||
| parquetWriterOptions, | ||
| fileInputColumnIndexes, | ||
| compressionCodecName)); | ||
| } | ||
| catch (IOException e) { | ||
| throw new PrestoException(HIVE_WRITER_OPEN_ERROR, "Error creating Parquet file", e); | ||
| } | ||
| } | ||
|
|
||
| private static CompressionCodecName getCompression(JobConf configuration) | ||
| { | ||
| String compressionName = configuration.get(ParquetOutputFormat.COMPRESSION); | ||
| if (compressionName == null) { | ||
| return CompressionCodecName.GZIP; | ||
| } | ||
| return CompressionCodecName.valueOf(compressionName); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.