-
Notifications
You must be signed in to change notification settings - Fork 110
GH-946: Add Variant extension type support #947
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
Open
tmater
wants to merge
5
commits into
apache:main
Choose a base branch
from
tmater:variant_logical
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
479ea37
GH-946: Add Variant extension type support
tmater 27ee846
rebase on Add ExtensionTypeWriterFactory to TransferPair
tmater 3a0d927
rebase, introduce arrow-variant module, add complex tests
tmater b76a0ea
Move Variant classes from arrow-vector to arrow-variant module
tmater 23549e6
Spotless + Checkstyle
tmater 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,51 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| 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"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.apache.arrow</groupId> | ||
| <artifactId>arrow-java-root</artifactId> | ||
| <version>19.0.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>arrow-variant</artifactId> | ||
| <name>Arrow Variant</name> | ||
| <description>Arrow Variant type support.</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.arrow</groupId> | ||
| <artifactId>arrow-memory-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.arrow</groupId> | ||
| <artifactId>arrow-vector</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.parquet</groupId> | ||
| <artifactId>parquet-variant</artifactId> | ||
| <version>${dep.parquet.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.arrow</groupId> | ||
| <artifactId>arrow-memory-unsafe</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
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,28 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| @SuppressWarnings("requires-automatic") | ||
| module org.apache.arrow.variant { | ||
| exports org.apache.arrow.variant; | ||
| exports org.apache.arrow.variant.extension; | ||
| exports org.apache.arrow.variant.impl; | ||
| exports org.apache.arrow.variant.holders; | ||
|
|
||
| requires org.apache.arrow.memory.core; | ||
| requires org.apache.arrow.vector; | ||
| requires parquet.variant; | ||
| } |
217 changes: 217 additions & 0 deletions
217
arrow-variant/src/main/java/org/apache/arrow/variant/Variant.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,217 @@ | ||
| /* | ||
| * 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.arrow.variant; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Objects; | ||
| import java.util.UUID; | ||
| import org.apache.arrow.memory.ArrowBuf; | ||
| import org.apache.arrow.variant.holders.NullableVariantHolder; | ||
|
|
||
| /** | ||
| * Wrapper around parquet-variant's Variant implementation. | ||
| * | ||
| * <p>This wrapper exists to isolate the parquet-variant dependency from Arrow's public API, | ||
| * allowing the vector module to expose variant functionality without requiring users to depend on | ||
| * parquet-variant directly. It also ensures that nested variant values (from arrays and objects) | ||
| * are consistently wrapped. | ||
| */ | ||
| public class Variant { | ||
|
|
||
| private final org.apache.parquet.variant.Variant delegate; | ||
|
|
||
| /** Creates a Variant from raw metadata and value byte arrays. */ | ||
| public Variant(byte[] metadata, byte[] value) { | ||
| this.delegate = new org.apache.parquet.variant.Variant(value, metadata); | ||
| } | ||
|
|
||
| /** Creates a Variant by copying data from ArrowBuf instances. */ | ||
| public Variant( | ||
| ArrowBuf metadataBuffer, | ||
| int metadataStart, | ||
| int metadataEnd, | ||
| ArrowBuf valueBuffer, | ||
| int valueStart, | ||
| int valueEnd) { | ||
| byte[] metadata = new byte[metadataEnd - metadataStart]; | ||
| byte[] value = new byte[valueEnd - valueStart]; | ||
| metadataBuffer.getBytes(metadataStart, metadata); | ||
| valueBuffer.getBytes(valueStart, value); | ||
| this.delegate = new org.apache.parquet.variant.Variant(value, metadata); | ||
| } | ||
|
|
||
| private Variant(org.apache.parquet.variant.Variant delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| /** Constructs a Variant from a NullableVariantHolder. */ | ||
| public Variant(NullableVariantHolder holder) { | ||
| this( | ||
| holder.metadataBuffer, | ||
| holder.metadataStart, | ||
| holder.metadataEnd, | ||
| holder.valueBuffer, | ||
| holder.valueStart, | ||
| holder.valueEnd); | ||
| } | ||
|
|
||
| public ByteBuffer getValueBuffer() { | ||
| return delegate.getValueBuffer(); | ||
| } | ||
|
|
||
| public ByteBuffer getMetadataBuffer() { | ||
| return delegate.getMetadataBuffer(); | ||
| } | ||
|
|
||
| public boolean getBoolean() { | ||
| return delegate.getBoolean(); | ||
| } | ||
|
|
||
| public byte getByte() { | ||
| return delegate.getByte(); | ||
| } | ||
|
|
||
| public short getShort() { | ||
| return delegate.getShort(); | ||
| } | ||
|
|
||
| public int getInt() { | ||
| return delegate.getInt(); | ||
| } | ||
|
|
||
| public long getLong() { | ||
| return delegate.getLong(); | ||
| } | ||
|
|
||
| public double getDouble() { | ||
| return delegate.getDouble(); | ||
| } | ||
|
|
||
| public BigDecimal getDecimal() { | ||
| return delegate.getDecimal(); | ||
| } | ||
|
|
||
| public float getFloat() { | ||
| return delegate.getFloat(); | ||
| } | ||
|
|
||
| public ByteBuffer getBinary() { | ||
| return delegate.getBinary(); | ||
| } | ||
|
|
||
| public UUID getUUID() { | ||
| return delegate.getUUID(); | ||
| } | ||
|
|
||
| public String getString() { | ||
| return delegate.getString(); | ||
| } | ||
|
|
||
| public Type getType() { | ||
| return Type.fromParquet(delegate.getType()); | ||
| } | ||
|
|
||
| public int numObjectElements() { | ||
| return delegate.numObjectElements(); | ||
| } | ||
|
|
||
| public Variant getFieldByKey(String key) { | ||
| org.apache.parquet.variant.Variant result = delegate.getFieldByKey(key); | ||
| return result != null ? wrap(result) : null; | ||
| } | ||
|
|
||
| public ObjectField getFieldAtIndex(int idx) { | ||
| org.apache.parquet.variant.Variant.ObjectField field = delegate.getFieldAtIndex(idx); | ||
| return new ObjectField(field.key, wrap(field.value)); | ||
| } | ||
|
|
||
| public int numArrayElements() { | ||
| return delegate.numArrayElements(); | ||
| } | ||
|
|
||
| public Variant getElementAtIndex(int index) { | ||
| org.apache.parquet.variant.Variant result = delegate.getElementAtIndex(index); | ||
| return result != null ? wrap(result) : null; | ||
| } | ||
|
|
||
| private static Variant wrap(org.apache.parquet.variant.Variant parquetVariant) { | ||
| return new Variant(parquetVariant); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| Variant variant = (Variant) o; | ||
| return delegate.getMetadataBuffer().equals(variant.delegate.getMetadataBuffer()) | ||
| && delegate.getValueBuffer().equals(variant.delegate.getValueBuffer()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(delegate.getMetadataBuffer(), delegate.getValueBuffer()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Variant{type=" + getType() + '}'; | ||
| } | ||
|
|
||
| public enum Type { | ||
| OBJECT, | ||
| ARRAY, | ||
| NULL, | ||
| BOOLEAN, | ||
| BYTE, | ||
| SHORT, | ||
| INT, | ||
| LONG, | ||
| STRING, | ||
| DOUBLE, | ||
| DECIMAL4, | ||
| DECIMAL8, | ||
| DECIMAL16, | ||
| DATE, | ||
| TIMESTAMP_TZ, | ||
| TIMESTAMP_NTZ, | ||
| FLOAT, | ||
| BINARY, | ||
| TIME, | ||
| TIMESTAMP_NANOS_TZ, | ||
| TIMESTAMP_NANOS_NTZ, | ||
| UUID; | ||
|
|
||
| static Type fromParquet(org.apache.parquet.variant.Variant.Type parquetType) { | ||
| return Type.valueOf(parquetType.name()); | ||
| } | ||
| } | ||
|
|
||
| public static final class ObjectField { | ||
| public final String key; | ||
| public final Variant value; | ||
|
|
||
| public ObjectField(String key, Variant value) { | ||
| this.key = key; | ||
| this.value = value; | ||
| } | ||
| } | ||
| } | ||
93 changes: 93 additions & 0 deletions
93
arrow-variant/src/main/java/org/apache/arrow/variant/extension/VariantType.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,93 @@ | ||
| /* | ||
| * 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.arrow.variant.extension; | ||
|
|
||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.variant.impl.VariantWriterImpl; | ||
| import org.apache.arrow.vector.FieldVector; | ||
| import org.apache.arrow.vector.ValueVector; | ||
| import org.apache.arrow.vector.complex.writer.FieldWriter; | ||
| import org.apache.arrow.vector.types.pojo.ArrowType; | ||
| import org.apache.arrow.vector.types.pojo.ArrowType.ExtensionType; | ||
| import org.apache.arrow.vector.types.pojo.ExtensionTypeRegistry; | ||
| import org.apache.arrow.vector.types.pojo.FieldType; | ||
|
|
||
| /** | ||
| * Arrow extension type for <a | ||
| * href="https://github.com/apache/parquet-format/blob/master/VariantEncoding.md">Parquet | ||
| * Variant</a> binary encoding. The type itself does not support shredded variant data. | ||
| */ | ||
| public final class VariantType extends ExtensionType { | ||
tmater marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static final VariantType INSTANCE = new VariantType(); | ||
|
|
||
| public static final String EXTENSION_NAME = "parquet.variant"; | ||
|
|
||
| static { | ||
| ExtensionTypeRegistry.register(INSTANCE); | ||
| } | ||
|
|
||
| private VariantType() {} | ||
|
|
||
| @Override | ||
| public ArrowType storageType() { | ||
| return ArrowType.Struct.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public String extensionName() { | ||
| return EXTENSION_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean extensionEquals(ExtensionType other) { | ||
| return other instanceof VariantType; | ||
| } | ||
|
|
||
| @Override | ||
| public String serialize() { | ||
| return ""; | ||
| } | ||
|
|
||
| @Override | ||
| public ArrowType deserialize(ArrowType storageType, String serializedData) { | ||
| if (!storageType.equals(this.storageType())) { | ||
| throw new UnsupportedOperationException( | ||
| "Cannot construct VariantType from underlying type " + storageType); | ||
| } | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public FieldVector getNewVector(String name, FieldType fieldType, BufferAllocator allocator) { | ||
| return new VariantVector(name, allocator); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isComplex() { | ||
| // The type itself is not complex meaning we need separate functions to convert/extract | ||
| // different types. | ||
| // Meanwhile, the containing vector is complex in terms of containing multiple values (metadata | ||
| // and value) | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public FieldWriter getNewFieldWriter(ValueVector vector) { | ||
| return new VariantWriterImpl((VariantVector) vector); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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 we should expose these to the user.
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 follows how
parquet-variantdefines it, the ByteBuffer is read-only.I’m happy to change it; I just wanted to call it out.
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'm not sure it is useful for the users, but do not have a strong opinion to remove it.