Skip to content
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
@@ -0,0 +1,97 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.parquet.benchmarks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.apache.parquet.example.data.Group;
import org.apache.parquet.example.data.simple.SimpleGroupFactory;
import org.apache.parquet.hadoop.ParquetFileWriter;
import org.apache.parquet.hadoop.ParquetWriter;
import org.apache.parquet.hadoop.example.ExampleParquetWriter;
import org.apache.parquet.hadoop.metadata.CompressionCodecName;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.MessageTypeParser;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/** The same rows written with and without content defined chunking, to {@link BlackHoleOutputFile}. */
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
public class ContentDefinedChunkingWriteBenchmarks {

private static final MessageType SCHEMA = MessageTypeParser.parseMessageType("message m {"
+ " required int64 l; optional double d; optional binary s (STRING);"
+ " optional group list (LIST) { repeated group list { optional int64 element; } } }");

@Param({"false", "true"})
public boolean chunking;

private final List<Group> rows = new ArrayList<>();

@Setup
public void setup() {
Random random = new Random(TestDataFactory.DEFAULT_SEED);
SimpleGroupFactory factory = new SimpleGroupFactory(SCHEMA);
for (int i = 0; i < 100_000; i++) {
Group row = factory.newGroup().append("l", random.nextLong());
if (random.nextInt(10) > 0) {
row.append("d", random.nextDouble()).append("s", Long.toString(random.nextLong(), 36));
}
Group list = row.addGroup("list");
for (int n = random.nextInt(9); n > 0; n--) {
list.addGroup("list").append("element", random.nextLong());
}
rows.add(row);
}
}

/** Dictionary encoding is off and the page row count limit lifted, so that the hashing dominates. */
@Benchmark
public void write() throws IOException {
try (ParquetWriter<Group> writer = ExampleParquetWriter.builder(BlackHoleOutputFile.INSTANCE)
.withWriteMode(ParquetFileWriter.Mode.OVERWRITE)
.withType(SCHEMA)
.withCompressionCodec(CompressionCodecName.UNCOMPRESSED)
.withDictionaryEncoding(false)
.withPageRowCountLimit(Integer.MAX_VALUE)
.withContentDefinedChunking(chunking)
.build()) {
for (Group row : rows) {
writer.write(row);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.apache.parquet.bytes.BytesUtils.getWidthFromMaxInt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalDouble;
Expand All @@ -33,6 +34,7 @@
import org.apache.parquet.bytes.HeapByteBufferAllocator;
import org.apache.parquet.column.impl.ColumnWriteStoreV1;
import org.apache.parquet.column.impl.ColumnWriteStoreV2;
import org.apache.parquet.column.impl.ContentDefinedChunker;
import org.apache.parquet.column.page.PageWriteStore;
import org.apache.parquet.column.values.ValuesWriter;
import org.apache.parquet.column.values.alp.AlpConfig;
Expand Down Expand Up @@ -72,6 +74,9 @@ public class ParquetProperties {
public static final int DEFAULT_BLOOM_FILTER_CANDIDATES_NUMBER = 5;
public static final boolean DEFAULT_STATISTICS_ENABLED = true;
public static final boolean DEFAULT_SIZE_STATISTICS_ENABLED = true;
public static final long DEFAULT_CONTENT_DEFINED_CHUNKING_MIN_SIZE = 256 * 1024;
public static final long DEFAULT_CONTENT_DEFINED_CHUNKING_MAX_SIZE = 1024 * 1024;
public static final int DEFAULT_CONTENT_DEFINED_CHUNKING_NORM_LEVEL = 0;

public static final boolean DEFAULT_PAGE_WRITE_CHECKSUM_ENABLED = true;

Expand Down Expand Up @@ -144,6 +149,10 @@ public static WriterVersion fromString(String name) {
private final ColumnProperty<Boolean> sizeStatistics;
private final ColumnProperty<CompressionCodecName> columnCodecs;
private final ColumnProperty<Integer> columnCompressionLevels;
private final boolean contentDefinedChunkingEnabled;
private final long contentDefinedChunkingMinSize;
private final long contentDefinedChunkingMaxSize;
private final int contentDefinedChunkingNormLevel;

private ParquetProperties(Builder builder) {
this.pageSizeThreshold = builder.pageSize;
Expand Down Expand Up @@ -179,6 +188,15 @@ private ParquetProperties(Builder builder) {
this.sizeStatistics = builder.sizeStatistics.build();
this.columnCodecs = builder.columnCodecs.build();
this.columnCompressionLevels = builder.columnCompressionLevels.build();
this.contentDefinedChunkingEnabled = builder.contentDefinedChunkingEnabled;
this.contentDefinedChunkingMinSize = builder.contentDefinedChunkingMinSize;
this.contentDefinedChunkingMaxSize = builder.contentDefinedChunkingMaxSize;
this.contentDefinedChunkingNormLevel = builder.contentDefinedChunkingNormLevel;
if (contentDefinedChunkingEnabled) {
// Rejected here rather than once a writer has opened its file
ContentDefinedChunker.calculateMask(
contentDefinedChunkingMinSize, contentDefinedChunkingMaxSize, contentDefinedChunkingNormLevel);
}
}

public static Builder builder() {
Expand Down Expand Up @@ -366,6 +384,30 @@ public ColumnWriteStore newColumnWriteStore(
};
}

/**
* EXPERIMENTAL: The column write store of a row group, continuing the chunking of the file's earlier
* row groups. Required with content defined chunking.
*
* @param schema the schema of the row group
* @param pageStore the page store of the row group
* @param bloomFilterWriteStore the bloom filter store of the row group
* @param chunkers the file's {@link ContentDefinedChunker#forColumns chunkers}, or {@code null}
* without content defined chunking
* @return the column write store
*/
public ColumnWriteStore newColumnWriteStore(
MessageType schema,
PageWriteStore pageStore,
BloomFilterWriteStore bloomFilterWriteStore,
List<ContentDefinedChunker> chunkers) {
validateAlp(schema);
return switch (writerVersion) {
case PARQUET_1_0 -> new ColumnWriteStoreV1(schema, pageStore, bloomFilterWriteStore, this, chunkers);
case PARQUET_2_0 -> new ColumnWriteStoreV2(schema, pageStore, bloomFilterWriteStore, this, chunkers);
default -> throw new IllegalArgumentException("unknown version " + writerVersion);
};
}

public int getMinRowCountForPageSizeCheck() {
return minRowCountForPageSizeCheck;
}
Expand Down Expand Up @@ -394,6 +436,42 @@ public int getRowGroupRowCountLimit() {
return rowGroupRowCountLimit;
}

/**
* EXPERIMENTAL
*
* @return {@code true} if content defined chunking is enabled
*/
public boolean isContentDefinedChunkingEnabled() {
return contentDefinedChunkingEnabled;
}

/**
* EXPERIMENTAL
*
* @return the minimum content defined chunk size in bytes
*/
public long getContentDefinedChunkingMinSize() {
return contentDefinedChunkingMinSize;
}

/**
* EXPERIMENTAL
*
* @return the maximum content defined chunk size in bytes
*/
public long getContentDefinedChunkingMaxSize() {
return contentDefinedChunkingMaxSize;
}

/**
* EXPERIMENTAL
*
* @return the normalization level of the content defined chunking's rolling hash mask
*/
public int getContentDefinedChunkingNormLevel() {
return contentDefinedChunkingNormLevel;
}

public int getPageRowCountLimit() {
return pageRowCountLimit;
}
Expand Down Expand Up @@ -488,6 +566,12 @@ public String toString() {
+ "Bloom filter expected number of distinct values are: " + bloomFilterNDVs + '\n'
+ "Bloom filter false positive probabilities are: " + bloomFilterFPPs + '\n'
+ "Page row count limit to " + getPageRowCountLimit() + '\n'
+ "Content defined chunking is: "
+ (contentDefinedChunkingEnabled
? contentDefinedChunkingMinSize + " to " + contentDefinedChunkingMaxSize
+ " bytes, normalization level " + contentDefinedChunkingNormLevel
: "off")
+ '\n'
+ "Writing page checksums is: " + (getPageWriteChecksumEnabled() ? "on" : "off") + '\n'
+ "Statistics enabled: " + statisticsEnabled + '\n'
+ "Size statistics enabled: " + sizeStatisticsEnabled + '\n'
Expand Down Expand Up @@ -537,6 +621,10 @@ public static class Builder {
private final ColumnProperty.Builder<Boolean> sizeStatistics;
private final ColumnProperty.Builder<CompressionCodecName> columnCodecs;
private final ColumnProperty.Builder<Integer> columnCompressionLevels;
private boolean contentDefinedChunkingEnabled;
private long contentDefinedChunkingMinSize = DEFAULT_CONTENT_DEFINED_CHUNKING_MIN_SIZE;
private long contentDefinedChunkingMaxSize = DEFAULT_CONTENT_DEFINED_CHUNKING_MAX_SIZE;
private int contentDefinedChunkingNormLevel = DEFAULT_CONTENT_DEFINED_CHUNKING_NORM_LEVEL;

private Builder() {
enableDict = ColumnProperty.<Boolean>builder().withDefaultValue(DEFAULT_IS_DICTIONARY_ENABLED);
Expand Down Expand Up @@ -590,6 +678,10 @@ private Builder(ParquetProperties toCopy) {
this.sizeStatisticsEnabled = toCopy.sizeStatisticsEnabled;
this.columnCodecs = ColumnProperty.builder(toCopy.columnCodecs);
this.columnCompressionLevels = ColumnProperty.builder(toCopy.columnCompressionLevels);
this.contentDefinedChunkingEnabled = toCopy.contentDefinedChunkingEnabled;
this.contentDefinedChunkingMinSize = toCopy.contentDefinedChunkingMinSize;
this.contentDefinedChunkingMaxSize = toCopy.contentDefinedChunkingMaxSize;
this.contentDefinedChunkingNormLevel = toCopy.contentDefinedChunkingNormLevel;
}

/**
Expand Down Expand Up @@ -899,6 +991,61 @@ public Builder withPageRowCountLimit(int rowCount) {
return this;
}

/**
* EXPERIMENTAL: Enable or disable content defined chunking, which also ends data pages where a
* rolling hash of the values matches, as Arrow C++ and arrow-rs do, so that files sharing a run of
* values share pages. Disabled by default. The page size and row count limits still apply. Column
* write stores must then come from
* {@link ParquetProperties#newColumnWriteStore(MessageType, PageWriteStore, BloomFilterWriteStore,
* List)}, and {@link #build()} rejects an invalid chunk size range.
*
* <p>With dictionary encoding, an edit that adds values renumbers the indices of values first seen
* after it. As parquet-java chooses between dictionary and plain encoding on the first page of a
* column chunk, which chunking can cut short, some column chunks fall back to plain encoding.
*
* @param enabled whether to enable content defined chunking
* @return this builder for method chaining.
*/
public Builder withContentDefinedChunking(boolean enabled) {
this.contentDefinedChunkingEnabled = enabled;
return this;
}

/**
* EXPERIMENTAL: Set the minimum content defined chunk size, 256 KiB by default. Sizes count values
* and levels before encoding, at the width Arrow holds them in: an INT(16) value counts 2 bytes.
*
* @param minSize the minimum chunk size in bytes
* @return this builder for method chaining.
*/
public Builder withContentDefinedChunkingMinSize(long minSize) {
this.contentDefinedChunkingMinSize = minSize;
return this;
}

/**
* EXPERIMENTAL: Set the maximum content defined chunk size, 1 MiB by default.
*
* @param maxSize the maximum chunk size in bytes
* @return this builder for method chaining.
*/
public Builder withContentDefinedChunkingMaxSize(long maxSize) {
this.contentDefinedChunkingMaxSize = maxSize;
return this;
}

/**
* EXPERIMENTAL: Set the normalization level of the content defined chunking's rolling hash mask, 0
* by default. Raising it makes chunk sizes more uniform at the cost of more small pages.
*
* @param normLevel the normalization level
* @return this builder for method chaining.
*/
public Builder withContentDefinedChunkingNormLevel(int normLevel) {
this.contentDefinedChunkingNormLevel = normLevel;
return this;
}

public Builder withPageWriteChecksumEnabled(boolean val) {
this.pageWriteChecksumEnabled = val;
return this;
Expand Down
Loading
Loading