-
Notifications
You must be signed in to change notification settings - Fork 1.6k
GH-3696: Cache ParsedVersion in FileMetaData to eliminate redundant parsing #3700
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 |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ | |
| import java.io.Serializable; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import org.apache.parquet.VersionParser; | ||
| import org.apache.parquet.VersionParser.ParsedVersion; | ||
| import org.apache.parquet.crypto.InternalFileDecryptor; | ||
| import org.apache.parquet.schema.MessageType; | ||
|
|
||
|
|
@@ -42,6 +44,8 @@ public enum EncryptionType { | |
| private final MessageType schema; | ||
| private final Map<String, String> keyValueMetaData; | ||
| private final String createdBy; | ||
| private transient volatile ParsedVersion writerVersion; | ||
| private transient volatile boolean writerVersionParsed; | ||
|
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. Please replace |
||
| private final InternalFileDecryptor fileDecryptor; | ||
| private final EncryptionType encryptionType; | ||
|
|
||
|
|
@@ -118,4 +122,25 @@ public InternalFileDecryptor getFileDecryptor() { | |
| public EncryptionType getEncryptionType() { | ||
| return encryptionType; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the parsed writer version from the {@code createdBy} string, or {@code null} | ||
| * if {@code createdBy} is null, empty, or unparseable. The result is computed lazily | ||
| * and cached. Callers that need to distinguish missing vs. unparseable can check | ||
| * {@link #getCreatedBy()}. | ||
| */ | ||
| @JsonIgnore | ||
| public ParsedVersion getWriterVersion() { | ||
|
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. This adds the cached value, but no production call site uses it yet. Please migrate the callers listed in the issue, especially footer stats, page stats, reader init, rewrite, and lazy encrypted metadata paths. |
||
| if (!writerVersionParsed) { | ||
| if (createdBy != null && !createdBy.isEmpty()) { | ||
| try { | ||
| writerVersion = VersionParser.parse(createdBy); | ||
| } catch (RuntimeException | VersionParser.VersionParseException e) { | ||
| // leave writerVersion as null — parse failed | ||
| } | ||
| } | ||
| writerVersionParsed = true; | ||
| } | ||
| return writerVersion; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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.hadoop.metadata; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.Collections; | ||
| import org.apache.parquet.schema.MessageType; | ||
| import org.apache.parquet.schema.PrimitiveType; | ||
| import org.apache.parquet.schema.Type; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class FileMetaDataTest { | ||
|
|
||
| private static final MessageType SCHEMA = new MessageType( | ||
| "test", new PrimitiveType(Type.Repetition.REQUIRED, PrimitiveType.PrimitiveTypeName.INT32, "id")); | ||
|
|
||
| @Test | ||
| void validCreatedByIsParsed() { | ||
| FileMetaData meta = | ||
| new FileMetaData(SCHEMA, Collections.emptyMap(), "parquet-mr version 1.12.0 (build abc123)"); | ||
|
|
||
| assertThat(meta.getWriterVersion()).isNotNull(); | ||
|
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. These tests only cover the getter. They do not prove the repeated parsing problem is fixed. Please add coverage for at least one migrated production path using the cached |
||
| assertThat(meta.getWriterVersion().application).isEqualTo("parquet-mr"); | ||
| assertThat(meta.getWriterVersion().version).isEqualTo("1.12.0"); | ||
| assertThat(meta.getWriterVersion().appBuildHash).isEqualTo("abc123"); | ||
| } | ||
|
|
||
| @Test | ||
| void nullCreatedByReturnsNullWriterVersion() { | ||
| FileMetaData meta = new FileMetaData(SCHEMA, Collections.emptyMap(), null); | ||
|
|
||
| assertThat(meta.getWriterVersion()).isNull(); | ||
| assertThat(meta.getCreatedBy()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void emptyCreatedByReturnsNullWriterVersion() { | ||
| FileMetaData meta = new FileMetaData(SCHEMA, Collections.emptyMap(), ""); | ||
|
|
||
| assertThat(meta.getWriterVersion()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void unparseableCreatedByReturnsNullWriterVersion() { | ||
| FileMetaData meta = new FileMetaData(SCHEMA, Collections.emptyMap(), "no-version-here"); | ||
|
|
||
| assertThat(meta.getWriterVersion()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void versionWithoutBuildHash() { | ||
| FileMetaData meta = new FileMetaData(SCHEMA, Collections.emptyMap(), "parquet-mr version 1.8.0"); | ||
|
|
||
| assertThat(meta.getWriterVersion()).isNotNull(); | ||
| assertThat(meta.getWriterVersion().application).isEqualTo("parquet-mr"); | ||
| assertThat(meta.getWriterVersion().version).isEqualTo("1.8.0"); | ||
| assertThat(meta.getWriterVersion().appBuildHash).isNull(); | ||
| } | ||
|
|
||
| } | ||
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.
This cached field is
transient final. After Java deserialization it will staynulleven whencreatedByis valid. Recompute lazily ingetWriterVersion(), or add serialization handling and tests.