-
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 1 commit
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, /*checkAfterBytes=*/ 0); | ||
|
yadavay-amzn marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public static <I extends ValuesWriter & RequiresFallback, F extends ValuesWriter> FallbackValuesWriter<I, F> of( | ||
| I initialWriter, F fallBackWriter, long checkAfterBytes) { | ||
|
yadavay-amzn marked this conversation as resolved.
Outdated
|
||
| return new FallbackValuesWriter<>(initialWriter, fallBackWriter, checkAfterBytes); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -43,6 +48,11 @@ public static <I extends ValuesWriter & RequiresFallback, F extends ValuesWriter | |
| public final F fallBackWriter; | ||
|
|
||
| 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; | ||
|
yadavay-amzn marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * writer currently written to | ||
|
|
@@ -57,16 +67,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, /*checkAfterBytes=*/ 0); | ||
| } | ||
|
|
||
| public FallbackValuesWriter(I initialWriter, F fallBackWriter, long checkAfterBytes) { | ||
| super(); | ||
| this.initialWriter = initialWriter; | ||
| this.fallBackWriter = fallBackWriter; | ||
| this.currentWriter = initialWriter; | ||
| this.checkAfterBytes = checkAfterBytes; | ||
| } | ||
|
Comment on lines
+80
to
86
|
||
|
|
||
| @Override | ||
|
|
@@ -79,8 +89,9 @@ 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 | ||
| cumulativeRawBytes += rawDataByteSize; | ||
|
yadavay-amzn marked this conversation as resolved.
Outdated
|
||
| if (!fellBackAlready && !compressionChecked && cumulativeRawBytes >= checkAfterBytes) { | ||
|
wgtmac marked this conversation as resolved.
Outdated
|
||
| compressionChecked = true; | ||
| BytesInput bytes = initialWriter.getBytes(); | ||
| if (!initialWriter.isCompressionSatisfying(rawDataByteSize, bytes.size())) { | ||
|
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. I think the threshold gate now accumulates raw bytes across pages, but the compression check still compares the current page raw size against the current page encoded size plus the whole accumulated dictionary size. This seems biased toward fallback once the check is delayed beyond the first page. Should this comparison use cumulative raw/encoded sizes, or otherwise avoid charging the full column-chunk dictionary size to a single page? A finite-threshold test with repeated/moderate-cardinality values across pages would help verify the intended PARQUET-3479 case. |
||
| fallBack(); | ||
|
|
@@ -103,7 +114,6 @@ public Encoding getEncoding() { | |
| @Override | ||
| public void reset() { | ||
| rawDataByteSize = 0; | ||
| firstPage = false; | ||
| currentWriter.reset(); | ||
| } | ||
|
|
||
|
|
@@ -131,8 +141,9 @@ public void resetDictionary() { | |
| } | ||
| currentWriter = initialWriter; | ||
| fellBackAlready = false; | ||
| compressionChecked = false; | ||
| cumulativeRawBytes = 0; | ||
| initialUsedAndHadDictionary = false; | ||
| firstPage = true; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * 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(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Threshold is crossed only after a reset() (page flush). cumulativeRawBytes accumulates | ||
| * across pages while rawDataByteSize resets per page. | ||
| */ | ||
| @Test | ||
| public void testThresholdCrossedAfterReset() throws Exception { | ||
| int dictPageSize = 1024 * 1024; | ||
| long threshold = 500; | ||
|
|
||
| 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, threshold); | ||
|
|
||
| try { | ||
| // Write ~300 bytes (75 ints * 4 bytes = 300) — below threshold | ||
| for (int i = 0; i < 75; i++) { | ||
| writer.writeInteger(i); | ||
| } | ||
| // Simulate page flush — check should NOT fire (cumulative = 300 < 500) | ||
| writer.getBytes(); | ||
| assertTrue( | ||
| "Should still use dictionary before threshold is crossed", | ||
| writer.getEncoding().usesDictionary()); | ||
| writer.reset(); | ||
|
|
||
| // Write another ~300 bytes (75 ints * 4 = 300, cumulative now 600 > 500) | ||
| for (int i = 75; i < 150; i++) { | ||
| writer.writeInteger(i); | ||
| } | ||
| // Check SHOULD fire now and fall back (high cardinality, bad compression) | ||
| writer.getBytes(); | ||
| assertFalse( | ||
| "Should fall back after cumulative bytes cross threshold", | ||
| writer.getEncoding().usesDictionary()); | ||
| } finally { | ||
| writer.close(); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.