Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.column.ContentDefinedChunkingOptions;
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 ? ContentDefinedChunkingOptions.DEFAULT : null)
.build()) {
for (Group row : rows) {
writer.write(row);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.column;

import org.apache.parquet.column.impl.ContentDefinedChunker;

/**
* EXPERIMENTAL: The settings of content defined chunking, as Arrow C++ and arrow-rs {@code CdcOptions},
* with the same defaults. Sizes count values and levels before encoding, at the width Arrow holds them
* in: an INT(16) value counts 2 bytes, although Parquet stores 4.
*
* @see ParquetProperties.Builder#withContentDefinedChunking(ContentDefinedChunkingOptions)
*/
public final class ContentDefinedChunkingOptions {

/** 256 KiB minimum, 1 MiB maximum and normalization level 0. */
public static final ContentDefinedChunkingOptions DEFAULT =
new ContentDefinedChunkingOptions(256 * 1024, 1024 * 1024, 0);

private final long minChunkSize;
private final long maxChunkSize;
private final int normLevel;

/**
* @param minChunkSize the minimum chunk size in bytes. The rolling hash will not be updated until this
* size is reached for each chunk. Note that all data sent through the hash function is counted
* towards the chunk size, including definition and repetition levels if present.
* @param maxChunkSize the maximum chunk size in bytes. The chunker creates a new chunk whenever the
* chunk size exceeds this value. The chunk size distribution approximates a normal distribution
* between minChunkSize and maxChunkSize. The page size and row count limits of the writer still
* apply within a chunk.
* @param normLevel the normalization level to center the chunk size around the average size more
* aggressively. Increasing the normalization level increases the probability of finding a chunk
* boundary, improving the deduplication ratio, but also increases the number of small chunks
* resulting in many small data pages. The default 0 provides a good balance between deduplication
* ratio and fragmentation. Use 1 or 2 to reach a higher deduplication ratio at the expense of
* fragmentation.
* @throws IllegalArgumentException if the minimum is negative, the maximum not above it, or the range
* too narrow for the normalization level
*/
public ContentDefinedChunkingOptions(long minChunkSize, long maxChunkSize, int normLevel) {
ContentDefinedChunker.calculateMask(minChunkSize, maxChunkSize, normLevel);
this.minChunkSize = minChunkSize;
this.maxChunkSize = maxChunkSize;
this.normLevel = normLevel;
}

public long getMinChunkSize() {
return minChunkSize;
}

public long getMaxChunkSize() {
return maxChunkSize;
}

public int getNormLevel() {
return normLevel;
}
}
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 @@ -144,6 +146,7 @@ public static WriterVersion fromString(String name) {
private final ColumnProperty<Boolean> sizeStatistics;
private final ColumnProperty<CompressionCodecName> columnCodecs;
private final ColumnProperty<Integer> columnCompressionLevels;
private final ContentDefinedChunkingOptions contentDefinedChunking;

private ParquetProperties(Builder builder) {
this.pageSizeThreshold = builder.pageSize;
Expand Down Expand Up @@ -179,6 +182,7 @@ private ParquetProperties(Builder builder) {
this.sizeStatistics = builder.sizeStatistics.build();
this.columnCodecs = builder.columnCodecs.build();
this.columnCompressionLevels = builder.columnCompressionLevels.build();
this.contentDefinedChunking = builder.contentDefinedChunking;
}

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

/**
* 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 +422,14 @@ public int getRowGroupRowCountLimit() {
return rowGroupRowCountLimit;
}

public boolean isContentDefinedChunkingEnabled() {
return contentDefinedChunking != null;
}

public ContentDefinedChunkingOptions getContentDefinedChunkingOptions() {
return contentDefinedChunking;
}

public int getPageRowCountLimit() {
return pageRowCountLimit;
}
Expand Down Expand Up @@ -488,6 +524,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: "
+ (contentDefinedChunking != null
? contentDefinedChunking.getMinChunkSize() + " to " + contentDefinedChunking.getMaxChunkSize()
+ " bytes, normalization level " + contentDefinedChunking.getNormLevel()
: "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 +579,7 @@ public static class Builder {
private final ColumnProperty.Builder<Boolean> sizeStatistics;
private final ColumnProperty.Builder<CompressionCodecName> columnCodecs;
private final ColumnProperty.Builder<Integer> columnCompressionLevels;
private ContentDefinedChunkingOptions contentDefinedChunking;

private Builder() {
enableDict = ColumnProperty.<Boolean>builder().withDefaultValue(DEFAULT_IS_DICTIONARY_ENABLED);
Expand Down Expand Up @@ -590,6 +633,7 @@ private Builder(ParquetProperties toCopy) {
this.sizeStatisticsEnabled = toCopy.sizeStatisticsEnabled;
this.columnCodecs = ColumnProperty.builder(toCopy.columnCodecs);
this.columnCompressionLevels = ColumnProperty.builder(toCopy.columnCompressionLevels);
this.contentDefinedChunking = toCopy.contentDefinedChunking;
}

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

public Builder withContentDefinedChunking() {
return withContentDefinedChunking(ContentDefinedChunkingOptions.DEFAULT);
}

/**
* EXPERIMENTAL: Enable or disable content defined chunking, which also ends data pages at boundaries
* derived from the values, 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)}.
*
* @param options the content defined chunking settings, or {@code null} to disable it
* @return this builder for method chaining.
*/
public Builder withContentDefinedChunking(ContentDefinedChunkingOptions options) {
this.contentDefinedChunking = options;
return this;
}

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