Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions java/vector/src/main/codegen/templates/AbstractFieldWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ public MapWriter map(boolean keysSorted) {
return null;
}

@Override
public ExtensionWriter extension(String name, ArrowType arrowType) {
fail("Extension");
return null;
}

@Override
public ExtensionWriter extension(ArrowType arrowType) {
fail("Extension");
return null;
}

@Override
public MapWriter map(String name, boolean keysSorted) {
fail("Map");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ public MapWriter map() {
return getWriter(MinorType.LIST).map();
}

@Override
public ExtensionWriter extension(ArrowType arrowType) {
return getWriter(MinorType.EXTENSIONTYPE).extension(arrowType);
}

@Override
public MapWriter map(boolean keysSorted) {
return getWriter(MinorType.MAP, new ArrowType.Map(keysSorted));
Expand All @@ -292,6 +297,12 @@ public MapWriter map(String name) {
return getWriter(MinorType.STRUCT).map(name);
}

@Override
public ExtensionWriter extension(String name, ArrowType arrowType) {
return getWriter(MinorType.EXTENSIONTYPE).extension(name, arrowType);
}


@Override
public MapWriter map(String name, boolean keysSorted) {
return getWriter(MinorType.STRUCT).map(name, keysSorted);
Expand Down
5 changes: 5 additions & 0 deletions java/vector/src/main/codegen/templates/BaseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,25 @@ public interface StructWriter extends BaseWriter {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where some design decisions have to be made of how to actually expose the ExtensionWriter to the user and how the user should actually extend some ExtensionWriter.

As mentioned above I would make this one interface BaswWriter exposing all the possible writers and leaving them abstract in the concrete implementations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds reasonable to me.

void copyReaderToField(String name, FieldReader reader);
StructWriter struct(String name);
ExtensionWriter extension(String name, ArrowType arrowType);
ListWriter list(String name);
MapWriter map(String name);
MapWriter map(String name, boolean keysSorted);
void start();
void end();
}

public interface ExtensionWriter extends StructWriter {
}

public interface ListWriter extends BaseWriter {
void startList();
void endList();
StructWriter struct();
ListWriter list();
MapWriter map();
MapWriter map(boolean keysSorted);
ExtensionWriter extension(ArrowType arrowType);
void copyReader(FieldReader reader);

<#list vv.types as type><#list type.minor as minor>
Expand Down
26 changes: 26 additions & 0 deletions java/vector/src/main/codegen/templates/StructWriters.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public class ${mode}StructWriter extends AbstractFieldWriter {
map(child.getName(), arrowType.getKeysSorted());
break;
}
case EXTENSIONTYPE:
extension(child.getName(), child.getType());
break;
case DENSEUNION: {
FieldType fieldType = new FieldType(addVectorAsNullable, MinorType.DENSEUNION.getType(), null, null);
DenseUnionWriter writer = new DenseUnionWriter(container.addOrGet(child.getName(), fieldType, DenseUnionVector.class), getNullableStructWriterFactory());
Expand Down Expand Up @@ -132,6 +135,29 @@ public Field getField() {
return container.getField();
}

@Override
public ExtensionWriter extension(String name, ArrowType arrowType) {
String finalName = handleCase(name);
FieldWriter writer = fields.get(finalName);
if(writer == null){
int vectorCount=container.size();
FieldType fieldType = new FieldType(addVectorAsNullable, arrowType, null, null);
ExtensionTypeVector vector = container.addOrGet(name, fieldType, ExtensionTypeVector.class);
writer = new PromotableWriter(vector, container, getNullableStructWriterFactory());
if(vectorCount != container.size()) {
writer.allocate();
}
writer.setPosition(idx());
fields.put(finalName, writer);
} else {
if (writer instanceof PromotableWriter) {
// ensure writers are initialized
((PromotableWriter)writer).getWriter(MinorType.EXTENSIONTYPE, arrowType);
}
}
return (ExtensionWriter) writer;
}

@Override
public StructWriter struct(String name) {
String finalName = handleCase(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ public MapWriter map(String name, boolean keysSorted) {
return mapWriter;
}

@Override
public ExtensionWriter extension(ArrowType arrowType) {
writer.extension(arrowType);
return writer;
}

@Override
public ExtensionWriter extension(String name, ArrowType arrowType) {
ExtensionWriter extensionWriter = writer.extension(name, arrowType);
return extensionWriter;
}

@Override
public void startList() {
int start = vector.startNewValue(idx());
Expand Down
12 changes: 12 additions & 0 deletions java/vector/src/main/codegen/templates/UnionListWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public MapWriter map(String name, boolean keysSorted) {
return mapWriter;
}

@Override
public ExtensionWriter extension(ArrowType arrowType) {
writer.extension(arrowType);
return writer;
}

@Override
public ExtensionWriter extension(String name, ArrowType arrowType) {
ExtensionWriter extensionWriter = writer.extension(name, arrowType);
return extensionWriter;
}

<#if listName == "LargeList">
@Override
public void startList() {
Expand Down
20 changes: 20 additions & 0 deletions java/vector/src/main/codegen/templates/UnionWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ private MapWriter getMapWriter(ArrowType arrowType) {
}
return mapWriter;
}

private ExtensionWriter getExtensionWriter(ArrowType arrowType) {
throw new UnsupportedOperationException("ExtensionTypes are not supported yet.");
}

public MapWriter asMap(ArrowType arrowType) {
data.setType(idx(), MinorType.MAP);
Expand All @@ -183,6 +187,8 @@ BaseWriter getWriter(MinorType minorType, ArrowType arrowType) {
return getListWriter();
case MAP:
return getMapWriter(arrowType);
case EXTENSIONTYPE:
return getExtensionWriter(arrowType);
<#list vv.types as type>
<#list type.minor as minor>
<#assign name = minor.class?cap_first />
Expand Down Expand Up @@ -402,6 +408,20 @@ public MapWriter map(String name, boolean keysSorted) {
return getStructWriter().map(name, keysSorted);
}

@Override
public ExtensionWriter extension(ArrowType arrowType) {
data.setType(idx(), MinorType.EXTENSIONTYPE);
getListWriter().setPosition(idx());
return getListWriter().extension(arrowType);
}

@Override
public ExtensionWriter extension(String name, ArrowType arrowType) {
data.setType(idx(), MinorType.EXTENSIONTYPE);
getStructWriter().setPosition(idx());
return getStructWriter().extension(name, arrowType);
}

<#list vv.types as type><#list type.minor as minor>
<#assign lowerName = minor.class?uncap_first />
<#if lowerName == "int" ><#assign lowerName = "integer" /></#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Locale;

import org.apache.arrow.memory.ArrowBuf;
import org.apache.arrow.vector.ExtensionTypeVector;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.NullVector;
import org.apache.arrow.vector.ValueVector;
Expand Down Expand Up @@ -224,6 +225,9 @@ private void setWriter(ValueVector v) {
case UNION:
writer = new UnionWriter((UnionVector) vector, nullableStructWriterFactory);
break;
case EXTENSIONTYPE:
writer = new UnionExtensionWriter((ExtensionTypeVector) vector);
break;
default:
writer = type.getNewFieldWriter(vector);
break;
Expand Down Expand Up @@ -255,6 +259,7 @@ private boolean requiresArrowType(MinorType type) {
type == MinorType.MAP ||
type == MinorType.DURATION ||
type == MinorType.FIXEDSIZEBINARY ||
type == MinorType.EXTENSIONTYPE ||
(type.name().startsWith("TIMESTAMP") && type.name().endsWith("TZ"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.complex.impl;

import org.apache.arrow.vector.ExtensionTypeVector;
import org.apache.arrow.vector.types.pojo.Field;

public class UnionExtensionWriter extends AbstractFieldWriter {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is currently more skeleton like as the concrete details of the ExtensionWriter interface will need to be nailed down.

protected ExtensionTypeVector vector;

public UnionExtensionWriter(ExtensionTypeVector vector) {
this.vector = vector;
}

@Override
public void allocate() {
vector.allocateNew();
}

@Override
public void clear() {
vector.clear();
}

@Override
public int getValueCapacity() {
return vector.getValueCapacity();
}

@Override
public Field getField() {
return vector.getField();
}

@Override
public void close() throws Exception {
vector.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.arrow.vector.complex.writer;

import org.apache.arrow.vector.complex.writer.BaseWriter.ExtensionWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.ListWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.MapWriter;
import org.apache.arrow.vector.complex.writer.BaseWriter.ScalarWriter;
Expand All @@ -26,7 +27,7 @@
* Composite of all writer types. Writers are convenience classes for incrementally
* adding values to {@linkplain org.apache.arrow.vector.ValueVector}s.
*/
public interface FieldWriter extends StructWriter, ListWriter, MapWriter, ScalarWriter {
public interface FieldWriter extends StructWriter, ListWriter, MapWriter, ScalarWriter, ExtensionWriter {
void allocate();

void clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@

package org.apache.arrow.vector;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we use JUnit5?

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.complex.AbstractStructVector;
Expand All @@ -35,9 +40,11 @@
import org.apache.arrow.vector.holders.ComplexHolder;
import org.apache.arrow.vector.types.Types;
import org.apache.arrow.vector.types.Types.MinorType;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.arrow.vector.types.pojo.ArrowType.Struct;
import org.apache.arrow.vector.types.pojo.Field;
import org.apache.arrow.vector.types.pojo.FieldType;
import org.apache.arrow.vector.types.pojo.TestExtensionType;
import org.apache.arrow.vector.util.TransferPair;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -82,6 +89,44 @@ public void testMakeTransferPair() {
}
}

@Test
public void testStructVectorWithExtensionTypes() {
TestExtensionType.UuidType uuidType = new TestExtensionType.UuidType();
Field uuidField = new Field("struct_child", FieldType.nullable(uuidType), null);
Field structField = new Field("struct", FieldType.nullable(new ArrowType.Struct()), List.of(uuidField));
StructVector s1 = new StructVector(structField, allocator, null);
StructVector s2 = (StructVector) structField.createVector(allocator);
s1.close();
s2.close();
}

@Test
public void testStructVectorTransferPairWithExtensionType() {
TestExtensionType.UuidType uuidType = new TestExtensionType.UuidType();
Field uuidField = new Field("uuid_child", FieldType.nullable(uuidType), null);
Field structField = new Field("struct", FieldType.nullable(new ArrowType.Struct()), List.of(uuidField));

StructVector s1 = (StructVector) structField.createVector(allocator);
TestExtensionType.UuidVector uuidVector =
s1.addOrGet("uuid_child", FieldType.nullable(uuidType), TestExtensionType.UuidVector.class);
s1.setValueCount(1);
uuidVector.set(0, new UUID(1, 2));
s1.setIndexDefined(0);

TransferPair tp = s1.getTransferPair(structField, allocator);
final StructVector toVector = (StructVector) tp.getTo();
assertEquals(s1.getField(), toVector.getField());
assertEquals(s1.getField().getChildren().get(0), toVector.getField().getChildren().get(0));
// also fails but probably another issue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also debugged the code. It seems the copy didn't work properly. I guess you're still not finished with this work, right?

// assertEquals(s1.getValueCount(), toVector.getValueCount());
// assertEquals(s1, toVector);

s1.close();
toVector.close();
}



@Test
public void testAllocateAfterReAlloc() throws Exception {
Map<String, String> metadata = new HashMap<>();
Expand Down
Loading