-
Notifications
You must be signed in to change notification settings - Fork 1.5k
GH-3242: Emit and Read min/max statistics for int96 timestamp columns #3243
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
0267c33
9a6a205
91e47db
beb7ad3
e1e2e8d
26c3ddd
d65b7b8
21bf8ae
43028d7
db9231c
85f3c3c
5eef533
efacf01
2df49cb
90656f1
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 |
|---|---|---|
| @@ -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; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.apache.parquet.VersionParser.ParsedVersion; | ||
| import org.apache.parquet.VersionParser.VersionParseException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Not all parquet writers populate the int96 statistics correctly. For example: arrow-rs | ||
| * https://github.com/apache/arrow-rs/blob/3ed9aedabc9e5a90170e43ff818f24a29eafb35b/parquet/src/file/statistics.rs#L212-L215 | ||
| * This class is used to detect whether a file was written with a version that has correct int96 statistics. | ||
| */ | ||
| public class ValidInt96Stats { | ||
| private static final AtomicBoolean alreadyLogged = new AtomicBoolean(false); | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ValidInt96Stats.class); | ||
|
|
||
| /** | ||
| * Decides if the statistics from a file created by createdBy (the created_by field from parquet format) | ||
| * should be trusted for INT96 columns. | ||
| * | ||
| * @param createdBy the created-by string from a file footer | ||
| * @return true if the statistics are valid and can be trusted, false otherwise | ||
| */ | ||
| public static boolean hasValidInt96Stats(String createdBy) { | ||
| if (Strings.isNullOrEmpty(createdBy)) { | ||
| warnOnce("Cannot verify INT96 statistics because created_by is null or empty"); | ||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| ParsedVersion version = VersionParser.parse(createdBy); | ||
| if ("parquet-mr".equals(version.application)) { | ||
|
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 recall that @rdblue has an opinion in maintaining an allow-list here. |
||
| return version.version != null && version.version.compareTo("1.16.0") > 0; | ||
| } | ||
| if ("parquet-mr compatible Photon".equals(version.application)) { | ||
| return true; | ||
| } | ||
| } catch (RuntimeException | VersionParseException e) { | ||
| warnParseErrorOnce(createdBy, e); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static void warnParseErrorOnce(String createdBy, Throwable e) { | ||
| if (!alreadyLogged.getAndSet(true)) { | ||
rahulketch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| LOG.warn("Cannot verify INT96 statistics because created_by could not be parsed: " + createdBy, e); | ||
| } | ||
| } | ||
|
|
||
| private static void warnOnce(String message) { | ||
| if (!alreadyLogged.getAndSet(true)) { | ||
| LOG.warn(message); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,34 @@ public String toString() { | |
| } | ||
| }; | ||
|
|
||
| /* | ||
| * This comparator is for comparing two timestamps represented as int96 binary. | ||
| * It is a two level comparison. | ||
| * Days (last 4 bytes compared as unsigned little endian int32), | ||
| * Nanoseconds (first 8 bytes compared as unsigned little endian int64) | ||
| */ | ||
| static final PrimitiveComparator<Binary> BINARY_AS_INT96_TIMESTAMP_COMPARATOR = new BinaryComparator() { | ||
| @Override | ||
| int compareBinary(Binary b1, Binary b2) { | ||
| ByteBuffer bb1 = b1.toByteBuffer().slice(); | ||
|
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 check if their lengths are exactly 12 before anything? I recall that |
||
| ByteBuffer bb2 = b2.toByteBuffer().slice(); | ||
| bb1.order(java.nio.ByteOrder.LITTLE_ENDIAN); | ||
| bb2.order(java.nio.ByteOrder.LITTLE_ENDIAN); | ||
rahulketch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| int jd1 = bb1.getInt(8); | ||
| int jd2 = bb2.getInt(8); | ||
| if (jd1 != jd2) return Integer.compareUnsigned(jd1, jd2) < 0 ? -1 : 1; | ||
rahulketch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| long s1 = bb1.getLong(0); | ||
| long s2 = bb2.getLong(0); | ||
| if (s1 != s2) return Long.compareUnsigned(s1, s2) < 0 ? -1 : 1; | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BINARY_AS_INT96_TIMESTAMP_COMPARATOR"; | ||
| } | ||
| }; | ||
|
|
||
| /* | ||
| * This comparator is for comparing two signed decimal values represented in twos-complement binary. In case of the | ||
| * binary length of one value is shorter than the other it will be padded by the corresponding prefix (0xFF for | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class ValidInt96StatsTest { | ||
|
|
||
| @Test | ||
| public void testNullAndEmpty() { | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats(null)); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParquetMrValid() { | ||
| // Versions > 1.15.0 should be valid | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.16.0")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.15.1")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 2.0.0")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.16.0 (build abcd)")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.15.1-SNAPSHOT")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParquetMrInvalid() { | ||
| // Versions <= 1.15.0 should be invalid | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.15.0")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.12.3")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.14.0")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.12.3 (build abcd)")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.12.3-SNAPSHOT")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.12.3rc1")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("parquet-mr version 1.12.3rc1-SNAPSHOT")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParquetMrCompatiblePhotonValid() { | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr compatible Photon version 1.0.0")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr compatible Photon version 1.0.0 (build abcd)")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr compatible Photon version 1.0.0-SNAPSHOT")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr compatible Photon version 1.0.0rc1")); | ||
| assertTrue(ValidInt96Stats.hasValidInt96Stats("parquet-mr compatible Photon version 1.0.0rc1-SNAPSHOT")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInvalidApplications() { | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("arrow-rs version 0.1.0")); | ||
| assertFalse(ValidInt96Stats.hasValidInt96Stats("impala version 1.6.0")); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.