jbonofre commented on code in PR #947:
URL: https://github.com/apache/arrow-java/pull/947#discussion_r2715757658


##########
vector/src/main/java/org/apache/arrow/vector/extension/VariantVector.java:
##########
@@ -0,0 +1,306 @@
+/*
+ * 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.vector.extension;
+
+import java.util.List;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.util.hash.ArrowBufHasher;
+import org.apache.arrow.vector.BitVectorHelper;
+import org.apache.arrow.vector.ExtensionTypeVector;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.ValueVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.complex.AbstractStructVector;
+import org.apache.arrow.vector.complex.StructVector;
+import org.apache.arrow.vector.complex.reader.FieldReader;
+import org.apache.arrow.vector.holders.NullableVariantHolder;
+import org.apache.arrow.vector.holders.VariantHolder;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.ArrowType.Binary;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.FieldType;
+import org.apache.arrow.vector.util.CallBack;
+import org.apache.arrow.vector.util.TransferPair;
+
+public class VariantVector extends ExtensionTypeVector<StructVector> {
+
+  public static final String METADATA_VECTOR_NAME = "metadata";
+  public static final String VALUE_VECTOR_NAME = "value";
+
+  private final Field rootField;
+
+  /**
+   * Constructs a new VariantVector with the given name and allocator.
+   *
+   * @param name the name of the vector
+   * @param allocator the buffer allocator for memory management
+   */
+  public VariantVector(String name, BufferAllocator allocator) {
+    super(
+        name,
+        allocator,
+        new StructVector(
+            name,
+            allocator,
+            FieldType.nullable(ArrowType.Struct.INSTANCE),
+            null,
+            AbstractStructVector.ConflictPolicy.CONFLICT_ERROR,
+            false));
+    rootField = createVariantField(name);
+    ((FieldVector) this.getUnderlyingVector())
+        .initializeChildrenFromFields(rootField.getChildren());
+  }
+
+  /**
+   * Creates a new VariantVector with the given name. The Variant Field schema 
has to be the same
+   * everywhere, otherwise ArrowBuffer loading might fail during 
serialization/deserialization and
+   * schema mismatches can occur. This includes CompleteType's VARIANT and 
VARIANT_REQUIRED types.
+   */
+  public static Field createVariantField(String name) {
+    return new Field(
+        name, new FieldType(true, VariantType.INSTANCE, null), 
createVariantChildFields());
+  }
+
+  /**
+   * Creates the child fields for the VariantVector. Metadata vector will be 
index 0 and value
+   * vector will be index 1.
+   */
+  public static List<Field> createVariantChildFields() {
+    return List.of(
+        new Field(METADATA_VECTOR_NAME, new FieldType(false, Binary.INSTANCE, 
null), null),
+        new Field(VALUE_VECTOR_NAME, new FieldType(false, Binary.INSTANCE, 
null), null));
+  }
+
+  @Override
+  public void initializeChildrenFromFields(List<Field> children) {
+    // No-op, as children are initialized in the constructor
+  }
+
+  @Override
+  public Field getField() {
+    return rootField;
+  }
+
+  public VarBinaryVector getMetadataVector() {
+    return getUnderlyingVector().getChild(METADATA_VECTOR_NAME, 
VarBinaryVector.class);
+  }
+
+  public VarBinaryVector getValueVector() {
+    return getUnderlyingVector().getChild(VALUE_VECTOR_NAME, 
VarBinaryVector.class);
+  }
+
+  @Override
+  public TransferPair makeTransferPair(ValueVector target) {
+    return new VariantTransferPair(this, (VariantVector) target);
+  }
+
+  @Override
+  public TransferPair getTransferPair(Field field, BufferAllocator allocator) {
+    return new VariantTransferPair(this, new VariantVector(field.getName(), 
allocator));
+  }
+
+  @Override
+  public TransferPair getTransferPair(Field field, BufferAllocator allocator, 
CallBack callBack) {
+    return getTransferPair(field, allocator);
+  }
+
+  @Override
+  public TransferPair getTransferPair(String ref, BufferAllocator allocator) {
+    return new VariantTransferPair(this, new VariantVector(ref, allocator));
+  }
+
+  @Override
+  public TransferPair getTransferPair(String ref, BufferAllocator allocator, 
CallBack callBack) {
+    return getTransferPair(ref, allocator);
+  }
+
+  @Override
+  public TransferPair getTransferPair(BufferAllocator allocator) {
+    return getTransferPair(this.getField().getName(), allocator);
+  }
+
+  @Override
+  public void copyFrom(int fromIndex, int thisIndex, ValueVector from) {
+    getUnderlyingVector()
+        .copyFrom(fromIndex, thisIndex, ((VariantVector) 
from).getUnderlyingVector());
+  }
+
+  @Override
+  public void copyFromSafe(int fromIndex, int thisIndex, ValueVector from) {
+    getUnderlyingVector()
+        .copyFromSafe(fromIndex, thisIndex, ((VariantVector) 
from).getUnderlyingVector());
+  }
+
+  @Override
+  public Object getObject(int index) {
+    return getUnderlyingVector().getObject(index);
+  }

Review Comment:
   Generally speaking, I'm not super fan of introducing a "large" dependency 
for a single feature. However, for the case of variant, imho, it could be 
acceptable if we "narrow" it to a specific module (`arrow-variant`), using 
Parqet classes as "util classes", not exposed to Arrow users.
   I would like to check how big is Variant in Parquet, because we can also 
duplicate the code (it's totally fine if mentioned in the Arrow `LICENSE`).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to