-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PARQUET-3479: Add configuration to disable early dictionary compression check #3556
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,7 +30,12 @@ public class FallbackValuesWriter<I extends ValuesWriter & RequiresFallback, F e | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public static <I extends ValuesWriter & RequiresFallback, F extends ValuesWriter> FallbackValuesWriter<I, F> of( | ||||||||||||||||||||||||||
| I initialWriter, F fallBackWriter) { | ||||||||||||||||||||||||||
| return new FallbackValuesWriter<>(initialWriter, fallBackWriter); | ||||||||||||||||||||||||||
| return new FallbackValuesWriter<>(initialWriter, fallBackWriter, 0); | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public static <I extends ValuesWriter & RequiresFallback, F extends ValuesWriter> FallbackValuesWriter<I, F> of( | ||||||||||||||||||||||||||
| I initialWriter, F fallBackWriter, long checkAfterBytes) { | ||||||||||||||||||||||||||
| return new FallbackValuesWriter<>(initialWriter, fallBackWriter, checkAfterBytes); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
|
|
@@ -44,6 +49,14 @@ public static <I extends ValuesWriter & RequiresFallback, F extends ValuesWriter | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private boolean fellBackAlready = false; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private boolean compressionChecked = false; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private final long checkAfterBytes; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /* Accumulates raw bytes across pages (only reset in resetDictionary) so the | ||||||||||||||||||||||||||
| * threshold check works even when individual pages are smaller than checkAfterBytes. */ | ||||||||||||||||||||||||||
| private long cumulativeRawBytes = 0; | ||||||||||||||||||||||||||
|
Comment on lines
+52
to
+58
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
How about removing these blank lines? |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * writer currently written to | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
|
|
@@ -57,16 +70,16 @@ public static <I extends ValuesWriter & RequiresFallback, F extends ValuesWriter | |||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| private long rawDataByteSize = 0; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * indicates if this is the first page being processed | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| private boolean firstPage = true; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public FallbackValuesWriter(I initialWriter, F fallBackWriter) { | ||||||||||||||||||||||||||
| this(initialWriter, fallBackWriter, 0); | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public FallbackValuesWriter(I initialWriter, F fallBackWriter, long checkAfterBytes) { | ||||||||||||||||||||||||||
| super(); | ||||||||||||||||||||||||||
| this.initialWriter = initialWriter; | ||||||||||||||||||||||||||
| this.fallBackWriter = fallBackWriter; | ||||||||||||||||||||||||||
| this.currentWriter = initialWriter; | ||||||||||||||||||||||||||
| this.checkAfterBytes = checkAfterBytes; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
|
|
@@ -79,8 +92,8 @@ public long getBufferedSize() { | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public BytesInput getBytes() { | ||||||||||||||||||||||||||
| if (!fellBackAlready && firstPage) { | ||||||||||||||||||||||||||
| // we use the first page to decide if we're going to use this encoding | ||||||||||||||||||||||||||
| if (!fellBackAlready && !compressionChecked && cumulativeRawBytes >= checkAfterBytes) { | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a test where the threshold is crossed only after a |
||||||||||||||||||||||||||
| compressionChecked = true; | ||||||||||||||||||||||||||
| BytesInput bytes = initialWriter.getBytes(); | ||||||||||||||||||||||||||
| if (!initialWriter.isCompressionSatisfying(rawDataByteSize, bytes.size())) { | ||||||||||||||||||||||||||
| fallBack(); | ||||||||||||||||||||||||||
|
|
@@ -103,7 +116,6 @@ public Encoding getEncoding() { | |||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void reset() { | ||||||||||||||||||||||||||
| rawDataByteSize = 0; | ||||||||||||||||||||||||||
| firstPage = false; | ||||||||||||||||||||||||||
| currentWriter.reset(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -131,8 +143,9 @@ public void resetDictionary() { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| currentWriter = initialWriter; | ||||||||||||||||||||||||||
| fellBackAlready = false; | ||||||||||||||||||||||||||
| compressionChecked = false; | ||||||||||||||||||||||||||
| cumulativeRawBytes = 0; | ||||||||||||||||||||||||||
| initialUsedAndHadDictionary = false; | ||||||||||||||||||||||||||
| firstPage = true; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
|
|
@@ -167,6 +180,7 @@ private void fallBack() { | |||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void writeByte(int value) { | ||||||||||||||||||||||||||
| rawDataByteSize += 1; | ||||||||||||||||||||||||||
| cumulativeRawBytes += 1; | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use |
||||||||||||||||||||||||||
| currentWriter.writeByte(value); | ||||||||||||||||||||||||||
| checkFallback(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
@@ -175,34 +189,39 @@ public void writeByte(int value) { | |||||||||||||||||||||||||
| public void writeBytes(Binary v) { | ||||||||||||||||||||||||||
| // for rawdata, length(4 bytes int) is stored, followed by the binary content itself | ||||||||||||||||||||||||||
| rawDataByteSize += v.length() + 4; | ||||||||||||||||||||||||||
| cumulativeRawBytes += v.length() + 4; | ||||||||||||||||||||||||||
| currentWriter.writeBytes(v); | ||||||||||||||||||||||||||
| checkFallback(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void writeInteger(int v) { | ||||||||||||||||||||||||||
| rawDataByteSize += 4; | ||||||||||||||||||||||||||
| cumulativeRawBytes += 4; | ||||||||||||||||||||||||||
| currentWriter.writeInteger(v); | ||||||||||||||||||||||||||
| checkFallback(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void writeLong(long v) { | ||||||||||||||||||||||||||
| rawDataByteSize += 8; | ||||||||||||||||||||||||||
| cumulativeRawBytes += 8; | ||||||||||||||||||||||||||
| currentWriter.writeLong(v); | ||||||||||||||||||||||||||
| checkFallback(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void writeFloat(float v) { | ||||||||||||||||||||||||||
| rawDataByteSize += 4; | ||||||||||||||||||||||||||
| cumulativeRawBytes += 4; | ||||||||||||||||||||||||||
| currentWriter.writeFloat(v); | ||||||||||||||||||||||||||
| checkFallback(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||
| public void writeDouble(double v) { | ||||||||||||||||||||||||||
| rawDataByteSize += 8; | ||||||||||||||||||||||||||
| cumulativeRawBytes += 8; | ||||||||||||||||||||||||||
| currentWriter.writeDouble(v); | ||||||||||||||||||||||||||
| checkFallback(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * 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.values.fallback; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.apache.parquet.bytes.DirectByteBufferAllocator; | ||
| import org.apache.parquet.bytes.TrackingByteBufferAllocator; | ||
| import org.apache.parquet.column.Encoding; | ||
| import org.apache.parquet.column.values.dictionary.DictionaryValuesWriter.PlainIntegerDictionaryValuesWriter; | ||
| import org.apache.parquet.column.values.plain.PlainValuesWriter; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class TestFallbackValuesWriter { | ||
|
|
||
| private TrackingByteBufferAllocator allocator; | ||
|
|
||
| @Before | ||
| public void initAllocator() { | ||
| allocator = TrackingByteBufferAllocator.wrap(new DirectByteBufferAllocator()); | ||
| } | ||
|
|
||
| @After | ||
| public void closeAllocator() { | ||
| allocator.close(); | ||
| } | ||
|
|
||
| /** | ||
| * With threshold=0, the check fires on the first page and falls back for high-cardinality data. | ||
| */ | ||
| @Test | ||
| public void testThresholdZeroFallsBackImmediately() throws Exception { | ||
| int dictPageSize = 1024 * 1024; | ||
|
|
||
| PlainIntegerDictionaryValuesWriter dictWriter = new PlainIntegerDictionaryValuesWriter( | ||
| dictPageSize, Encoding.PLAIN_DICTIONARY, Encoding.PLAIN_DICTIONARY, allocator); | ||
| PlainValuesWriter plainWriter = new PlainValuesWriter(1024, 1024 * 1024, allocator); | ||
| FallbackValuesWriter<PlainIntegerDictionaryValuesWriter, PlainValuesWriter> writer = | ||
| FallbackValuesWriter.of(dictWriter, plainWriter, 0); | ||
|
|
||
| try { | ||
| for (int i = 0; i < 1000; i++) { | ||
| writer.writeInteger(i); | ||
| } | ||
| writer.getBytes(); | ||
|
|
||
| assertFalse( | ||
| "Should fall back to plain encoding with threshold=0 and high cardinality", | ||
| writer.getEncoding().usesDictionary()); | ||
| } finally { | ||
| writer.close(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * With a large threshold, the check never fires and dictionary encoding is preserved. | ||
| */ | ||
| @Test | ||
| public void testLargeThresholdPreservesDictionary() throws Exception { | ||
| int dictPageSize = 1024 * 1024; | ||
|
|
||
| PlainIntegerDictionaryValuesWriter dictWriter = new PlainIntegerDictionaryValuesWriter( | ||
| dictPageSize, Encoding.PLAIN_DICTIONARY, Encoding.PLAIN_DICTIONARY, allocator); | ||
| PlainValuesWriter plainWriter = new PlainValuesWriter(1024, 1024 * 1024, allocator); | ||
| FallbackValuesWriter<PlainIntegerDictionaryValuesWriter, PlainValuesWriter> writer = | ||
| FallbackValuesWriter.of(dictWriter, plainWriter, Long.MAX_VALUE); | ||
|
|
||
| try { | ||
| for (int i = 0; i < 1000; i++) { | ||
| writer.writeInteger(i); | ||
| } | ||
| writer.getBytes(); | ||
|
|
||
| assertTrue( | ||
| "Dictionary encoding should be preserved with large threshold", | ||
| writer.getEncoding().usesDictionary()); | ||
| } finally { | ||
| writer.close(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,7 @@ public static enum JobSummaryLevel { | |
| public static final String BLOCK_ROW_COUNT_LIMIT = "parquet.block.row.count.limit"; | ||
| public static final String PAGE_ROW_COUNT_LIMIT = "parquet.page.row.count.limit"; | ||
| public static final String PAGE_WRITE_CHECKSUM_ENABLED = "parquet.page.write-checksum.enabled"; | ||
| public static final String DICTIONARY_CHECK_AFTER_BYTES = "parquet.dictionary.check.after.raw.bytes"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also document
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH, the flag name still looks a little bit unclear to me. How about renaming it to |
||
| public static final String STATISTICS_ENABLED = "parquet.column.statistics.enabled"; | ||
| public static final String SIZE_STATISTICS_ENABLED = "parquet.size.statistics.enabled"; | ||
|
|
||
|
|
@@ -412,6 +413,14 @@ public static boolean getPageWriteChecksumEnabled(Configuration conf) { | |
| return conf.getBoolean(PAGE_WRITE_CHECKSUM_ENABLED, ParquetProperties.DEFAULT_PAGE_WRITE_CHECKSUM_ENABLED); | ||
| } | ||
|
|
||
| public static void setDictionaryCheckAfterBytes(Configuration conf, long val) { | ||
| conf.setLong(DICTIONARY_CHECK_AFTER_BYTES, val); | ||
| } | ||
|
|
||
| public static long getDictionaryCheckAfterBytes(Configuration conf) { | ||
| return conf.getLong(DICTIONARY_CHECK_AFTER_BYTES, ParquetProperties.DEFAULT_DICTIONARY_CHECK_AFTER_BYTES); | ||
| } | ||
|
|
||
| public static void setStatisticsEnabled(JobContext jobContext, boolean enabled) { | ||
| getConfiguration(jobContext).setBoolean(STATISTICS_ENABLED, enabled); | ||
| } | ||
|
|
@@ -526,6 +535,7 @@ public RecordWriter<Void, T> getRecordWriter(Configuration conf, Path file, Comp | |
| .withRowGroupRowCountLimit(getBlockRowCountLimit(conf)) | ||
| .withPageRowCountLimit(getPageRowCountLimit(conf)) | ||
| .withPageWriteChecksumEnabled(getPageWriteChecksumEnabled(conf)) | ||
| .withDictionaryCheckAfterBytes(getDictionaryCheckAfterBytes(conf)) | ||
| .withStatisticsEnabled(getStatisticsEnabled(conf)); | ||
| new ColumnConfigParser() | ||
| .withColumnConfig( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -771,6 +771,17 @@ public SELF withPageWriteChecksumEnabled(boolean enablePageWriteChecksum) { | |
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * Set the raw data byte threshold after which the dictionary compression check is performed. | ||
| * | ||
| * @param val byte threshold (0 means check on every page) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this says |
||
| * @return this builder for method chaining. | ||
| */ | ||
| public SELF withDictionaryCheckAfterBytes(long val) { | ||
| encodingPropsBuilder.withDictionaryCheckAfterBytes(val); | ||
| return self(); | ||
| } | ||
|
|
||
| /** | ||
| * Set max Bloom filter bytes for related columns. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we reject negative values here? A negative threshold effectively behaves like
0, but accepting it silently seems a bit confusing for a size-like config. Most nearby size/count options validate the input, soval >= 0would be clearer.