-
Notifications
You must be signed in to change notification settings - Fork 1.5k
GH-3116: Implement the Variant binary encoding #3117
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
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c3c71b7
Implement Variant encoding
gene-db c5d19e6
remove optional
gene-db 0086b34
split test
gene-db 5af337f
cleanup
gene-db 5997732
cleanup comment
gene-db de96bac
Run mvn spotless:apply
gene-db 848ddcb
Fix dependencies
gene-db 1a448ea
Fix tests for older jdk versions
gene-db 2056297
Address PR comments
gene-db 1ea911c
Add new variant types
gene-db cb954a6
Fix tests for older JDK versions
gene-db db6b98e
Return UUID
gene-db c220c3c
Return java.util.UUID
gene-db 2318114
mvn spotless:apply
gene-db 553dbe9
review feedback
gene-db 7f2cd6e
Cleanup/improve apis
gene-db f310080
cleanup unused constructor/member
gene-db 1040ae8
Update api to use byte-array + offset
gene-db ba905c8
Use ScalarToJson interface
gene-db 968e9a1
Use ByteByffer instead
gene-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <!-- | ||
| ~ 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. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <groupId>org.apache.parquet</groupId> | ||
| <artifactId>parquet</artifactId> | ||
| <relativePath>../pom.xml</relativePath> | ||
| <version>1.16.0-SNAPSHOT</version> | ||
| </parent> | ||
|
|
||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>parquet-variant</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>Apache Parquet Variant</name> | ||
| <url>https://parquet.apache.org</url> | ||
|
|
||
| <properties> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.parquet</groupId> | ||
| <artifactId>parquet-cli</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.parquet</groupId> | ||
| <artifactId>parquet-jackson</artifactId> | ||
| <version>${project.version}</version> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>${jackson.groupId}</groupId> | ||
| <artifactId>jackson-core</artifactId> | ||
| <version>${jackson.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>${jackson.groupId}</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <version>${jackson-databind.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| <version>${slf4j.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-jar-plugin</artifactId> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| </project> | ||
157 changes: 157 additions & 0 deletions
157
parquet-variant/src/main/java/org/apache/parquet/variant/DefaultScalarToJson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * 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.variant; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.time.Instant; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalTime; | ||
| import java.time.ZoneOffset; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.format.DateTimeFormatterBuilder; | ||
| import java.time.temporal.ChronoField; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Base64; | ||
| import java.util.Locale; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * This converts Variant scalar values to JSON. | ||
| */ | ||
| public class DefaultScalarToJson implements Variant.ScalarToJson { | ||
| /** The format for a timestamp without time zone. */ | ||
| private static final DateTimeFormatter TIMESTAMP_NTZ_FORMATTER = new DateTimeFormatterBuilder() | ||
| .append(DateTimeFormatter.ISO_LOCAL_DATE) | ||
| .appendLiteral('T') | ||
| .appendPattern("HH:mm:ss") | ||
| .appendFraction(ChronoField.MICRO_OF_SECOND, 6, 6, true) | ||
| .toFormatter(Locale.US); | ||
|
|
||
| /** The format for a timestamp without time zone, with nanosecond precision. */ | ||
| private static final DateTimeFormatter TIMESTAMP_NANOS_NTZ_FORMATTER = new DateTimeFormatterBuilder() | ||
| .append(DateTimeFormatter.ISO_LOCAL_DATE) | ||
| .appendLiteral('T') | ||
| .appendPattern("HH:mm:ss") | ||
| .appendFraction(ChronoField.NANO_OF_SECOND, 9, 9, true) | ||
| .toFormatter(Locale.US); | ||
|
|
||
| /** The format for a timestamp with time zone. */ | ||
| private static final DateTimeFormatter TIMESTAMP_FORMATTER = new DateTimeFormatterBuilder() | ||
| .append(TIMESTAMP_NTZ_FORMATTER) | ||
| .appendOffset("+HH:MM", "+00:00") | ||
| .toFormatter(Locale.US); | ||
|
|
||
| /** The format for a timestamp with time zone, with nanosecond precision. */ | ||
| private static final DateTimeFormatter TIMESTAMP_NANOS_FORMATTER = new DateTimeFormatterBuilder() | ||
| .append(TIMESTAMP_NANOS_NTZ_FORMATTER) | ||
| .appendOffset("+HH:MM", "+00:00") | ||
| .toFormatter(Locale.US); | ||
|
|
||
| /** The format for a time. */ | ||
| private static final DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder() | ||
| .appendPattern("HH:mm:ss") | ||
| .appendFraction(ChronoField.MICRO_OF_SECOND, 6, 6, true) | ||
| .toFormatter(Locale.US); | ||
|
|
||
| public void writeNull(JsonGenerator gen) throws IOException { | ||
| gen.writeNull(); | ||
| } | ||
|
|
||
| public void writeBoolean(JsonGenerator gen, boolean value) throws IOException { | ||
| gen.writeBoolean(value); | ||
| } | ||
|
|
||
| public void writeByte(JsonGenerator gen, byte value) throws IOException { | ||
| gen.writeNumber(value); | ||
| } | ||
|
|
||
| public void writeShort(JsonGenerator gen, short value) throws IOException { | ||
| gen.writeNumber(value); | ||
| } | ||
|
|
||
| public void writeInt(JsonGenerator gen, int value) throws IOException { | ||
| gen.writeNumber(value); | ||
| } | ||
|
|
||
| public void writeLong(JsonGenerator gen, long value) throws IOException { | ||
| gen.writeNumber(value); | ||
| } | ||
|
|
||
| public void writeFloat(JsonGenerator gen, float value) throws IOException { | ||
| gen.writeNumber(value); | ||
| } | ||
|
|
||
| public void writeDouble(JsonGenerator gen, double value) throws IOException { | ||
| gen.writeNumber(value); | ||
| } | ||
|
|
||
| public void writeString(JsonGenerator gen, String value) throws IOException { | ||
| gen.writeString(value); | ||
| } | ||
|
|
||
| public void writeBinary(JsonGenerator gen, byte[] value) throws IOException { | ||
| gen.writeString(Base64.getEncoder().encodeToString(value)); | ||
| } | ||
|
|
||
| public void writeDecimal(JsonGenerator gen, BigDecimal value) throws IOException { | ||
| gen.writeNumber(value.toPlainString()); | ||
| } | ||
|
|
||
| public void writeUUID(JsonGenerator gen, UUID value) throws IOException { | ||
| gen.writeString(value.toString()); | ||
| } | ||
|
|
||
| public void writeDate(JsonGenerator gen, int value) throws IOException { | ||
| gen.writeString(LocalDate.ofEpochDay(value).toString()); | ||
| } | ||
|
|
||
| public void writeTime(JsonGenerator gen, long microsSinceMidnight) throws IOException { | ||
| gen.writeString(TIME_FORMATTER.format(LocalTime.ofNanoOfDay(microsSinceMidnight * 1_000))); | ||
| } | ||
|
|
||
| public void writeTimestamp(JsonGenerator gen, long microsSinceEpoch) throws IOException { | ||
| gen.writeString( | ||
| TIMESTAMP_FORMATTER.format(microsToInstant(microsSinceEpoch).atZone(ZoneOffset.UTC))); | ||
| } | ||
|
|
||
| public void writeTimestampNtz(JsonGenerator gen, long microsSinceEpoch) throws IOException { | ||
| gen.writeString( | ||
| TIMESTAMP_NTZ_FORMATTER.format(microsToInstant(microsSinceEpoch).atZone(ZoneOffset.UTC))); | ||
| } | ||
|
|
||
| public void writeTimestampNanos(JsonGenerator gen, long nanosSinceEpoch) throws IOException { | ||
| gen.writeString( | ||
| TIMESTAMP_NANOS_FORMATTER.format(nanosToInstant(nanosSinceEpoch).atZone(ZoneOffset.UTC))); | ||
| } | ||
|
|
||
| public void writeTimestampNanosNtz(JsonGenerator gen, long nanosSinceEpoch) throws IOException { | ||
| gen.writeString(TIMESTAMP_NANOS_NTZ_FORMATTER.format( | ||
| nanosToInstant(nanosSinceEpoch).atZone(ZoneOffset.UTC))); | ||
| } | ||
|
|
||
| protected Instant microsToInstant(long microsSinceEpoch) { | ||
| return Instant.EPOCH.plus(microsSinceEpoch, ChronoUnit.MICROS); | ||
| } | ||
|
|
||
| protected Instant nanosToInstant(long timestampNanos) { | ||
| return Instant.EPOCH.plus(timestampNanos, ChronoUnit.NANOS); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
parquet-variant/src/main/java/org/apache/parquet/variant/MalformedVariantException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * 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.variant; | ||
|
|
||
| /** | ||
| * An exception indicating that the Variant is malformed. | ||
| */ | ||
| public class MalformedVariantException extends RuntimeException { | ||
| public MalformedVariantException(String message) { | ||
| super(message); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
parquet-variant/src/main/java/org/apache/parquet/variant/UnknownVariantTypeException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.variant; | ||
|
|
||
| /** | ||
| * An exception indicating that the Variant contains an unknown type. | ||
| */ | ||
| public class UnknownVariantTypeException extends RuntimeException { | ||
| public final int typeId; | ||
|
|
||
| /** | ||
| * @param typeId the type id that was unknown | ||
| */ | ||
| public UnknownVariantTypeException(int typeId) { | ||
| super("Unknown type in Variant. id: " + typeId); | ||
| this.typeId = typeId; | ||
| } | ||
|
|
||
| /** | ||
| * @return the type id that was unknown | ||
| */ | ||
| public int typeId() { | ||
| return typeId; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks a lot @gene-db to drive the reference implementation.
I have a general question on the requirement: we implement mostly Parse_Json() in this PR. Are we required to construct variant with richer type - date, timestamp, etc.? May be out of scope for this PR. I have the implementation in Iceberg (apache/iceberg#11857 to add the full support. As I talked to @rdblue, that may not be required for Iceberg but I can include such implementation in Parquet after this PR if needed.
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.
I don't think
parse_jsonshould be trying to determine what type a particular JSON string is supposed to be. The JSON spec doesn't have the richer types, soparse_jsonwill not try to guess what the strings might be. It might be error-prone and would be costly in terms of performance. Therefore,parse_jsonwill only use a subset of the variant types.This PR also supports the variant builder, which supports creating variant values with all of the variant types.