Skip to content

Commit 650e3c4

Browse files
julienledemkou
authored andcommitted
ARROW-271: Update Field structure to be more explicit
This is a proposal. I have not updated the code depending on this yet. Author: Julien Le Dem <julien@dremio.com> Closes apache#124 from julienledem/record_batch and squashes the following commits: 8e42d74 [Julien Le Dem] ARROW-271: Update Field structure to be more explicit add bit_width to vector layout
1 parent 45f4b45 commit 650e3c4

File tree

6 files changed

+95
-34
lines changed

6 files changed

+95
-34
lines changed

vector/src/main/codegen/templates/NullableValueVectors.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
<#include "/@includes/vv_imports.ftl" />
3636

37+
import org.apache.arrow.flatbuf.Precision;
38+
3739
/**
3840
* Nullable${minor.class} implements a vector of values which could be null. Elements in the vector
3941
* are first checked against a fixed length vector of boolean values. Then the element is retrieved
@@ -97,9 +99,9 @@ public final class ${className} extends BaseDataValueVector implements <#if type
9799
<#elseif minor.class == "Time">
98100
field = new Field(name, true, new org.apache.arrow.vector.types.pojo.ArrowType.Time(), null);
99101
<#elseif minor.class == "Float4">
100-
field = new Field(name, true, new FloatingPoint(org.apache.arrow.flatbuf.Precision.SINGLE), null);
102+
field = new Field(name, true, new FloatingPoint(Precision.SINGLE), null);
101103
<#elseif minor.class == "Float8">
102-
field = new Field(name, true, new FloatingPoint(org.apache.arrow.flatbuf.Precision.DOUBLE), null);
104+
field = new Field(name, true, new FloatingPoint(Precision.DOUBLE), null);
103105
<#elseif minor.class == "TimeStamp">
104106
field = new Field(name, true, new org.apache.arrow.vector.types.pojo.ArrowType.Timestamp(""), null);
105107
<#elseif minor.class == "IntervalDay">

vector/src/main/java/org/apache/arrow/vector/schema/ArrowVectorType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
public class ArrowVectorType {
2323

24-
public static final ArrowVectorType VALUES = new ArrowVectorType(VectorType.VALUES);
24+
public static final ArrowVectorType DATA = new ArrowVectorType(VectorType.DATA);
2525
public static final ArrowVectorType OFFSET = new ArrowVectorType(VectorType.OFFSET);
2626
public static final ArrowVectorType VALIDITY = new ArrowVectorType(VectorType.VALIDITY);
2727
public static final ArrowVectorType TYPE = new ArrowVectorType(VectorType.TYPE);

vector/src/main/java/org/apache/arrow/vector/schema/TypeLayout.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import org.apache.arrow.vector.types.pojo.ArrowType.Union;
5050
import org.apache.arrow.vector.types.pojo.ArrowType.Utf8;
5151

52+
import com.google.common.base.Preconditions;
53+
5254
/**
5355
* The layout of vectors for a given type
5456
* It defines its own vectors followed by the vectors for the children
@@ -182,7 +184,7 @@ public TypeLayout visit(IntervalYear type) { // TODO: check size
182184

183185
public TypeLayout(List<VectorLayout> vectors) {
184186
super();
185-
this.vectors = vectors;
187+
this.vectors = Preconditions.checkNotNull(vectors);
186188
}
187189

188190
public TypeLayout(VectorLayout... vectors) {
@@ -205,4 +207,22 @@ public List<ArrowVectorType> getVectorTypes() {
205207
public String toString() {
206208
return "TypeLayout{" + vectors + "}";
207209
}
210+
211+
@Override
212+
public int hashCode() {
213+
return vectors.hashCode();
214+
}
215+
216+
@Override
217+
public boolean equals(Object obj) {
218+
if (this == obj)
219+
return true;
220+
if (obj == null)
221+
return false;
222+
if (getClass() != obj.getClass())
223+
return false;
224+
TypeLayout other = (TypeLayout) obj;
225+
return vectors.equals(other.vectors);
226+
}
227+
208228
}

vector/src/main/java/org/apache/arrow/vector/schema/VectorLayout.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@
1717
*/
1818
package org.apache.arrow.vector.schema;
1919

20+
import static org.apache.arrow.vector.schema.ArrowVectorType.DATA;
2021
import static org.apache.arrow.vector.schema.ArrowVectorType.OFFSET;
2122
import static org.apache.arrow.vector.schema.ArrowVectorType.TYPE;
2223
import static org.apache.arrow.vector.schema.ArrowVectorType.VALIDITY;
23-
import static org.apache.arrow.vector.schema.ArrowVectorType.VALUES;
2424

25-
public class VectorLayout {
25+
import com.google.common.base.Preconditions;
26+
import com.google.flatbuffers.FlatBufferBuilder;
27+
28+
public class VectorLayout implements FBSerializable {
2629

2730
private static final VectorLayout VALIDITY_VECTOR = new VectorLayout(VALIDITY, 1);
2831
private static final VectorLayout OFFSET_VECTOR = new VectorLayout(OFFSET, 32);
2932
private static final VectorLayout TYPE_VECTOR = new VectorLayout(TYPE, 32);
30-
private static final VectorLayout BOOLEAN_VECTOR = new VectorLayout(VALUES, 1);
31-
private static final VectorLayout VALUES_64 = new VectorLayout(VALUES, 64);
32-
private static final VectorLayout VALUES_32 = new VectorLayout(VALUES, 32);
33-
private static final VectorLayout VALUES_16 = new VectorLayout(VALUES, 16);
34-
private static final VectorLayout VALUES_8 = new VectorLayout(VALUES, 8);
33+
private static final VectorLayout BOOLEAN_VECTOR = new VectorLayout(DATA, 1);
34+
private static final VectorLayout VALUES_64 = new VectorLayout(DATA, 64);
35+
private static final VectorLayout VALUES_32 = new VectorLayout(DATA, 32);
36+
private static final VectorLayout VALUES_16 = new VectorLayout(DATA, 16);
37+
private static final VectorLayout VALUES_8 = new VectorLayout(DATA, 8);
3538

3639
public static VectorLayout typeVector() {
3740
return TYPE_VECTOR;
@@ -68,14 +71,21 @@ public static VectorLayout byteVector() {
6871
return dataVector(8);
6972
}
7073

71-
private final int typeBitWidth;
74+
private final short typeBitWidth;
7275

7376
private final ArrowVectorType type;
7477

7578
private VectorLayout(ArrowVectorType type, int typeBitWidth) {
7679
super();
77-
this.type = type;
78-
this.typeBitWidth = typeBitWidth;
80+
this.type = Preconditions.checkNotNull(type);
81+
this.typeBitWidth = (short)typeBitWidth;
82+
if (typeBitWidth <= 0) {
83+
throw new IllegalArgumentException("bitWidth invalid: " + typeBitWidth);
84+
}
85+
}
86+
87+
public VectorLayout(org.apache.arrow.flatbuf.VectorLayout layout) {
88+
this(new ArrowVectorType(layout.type()), layout.bitWidth());
7989
}
8090

8191
public int getTypeBitWidth() {
@@ -90,4 +100,28 @@ public ArrowVectorType getType() {
90100
public String toString() {
91101
return String.format("{width=%s,type=%s}", typeBitWidth, type);
92102
}
103+
104+
@Override
105+
public int hashCode() {
106+
return 31 * (31 + type.hashCode()) + typeBitWidth;
107+
}
108+
109+
@Override
110+
public boolean equals(Object obj) {
111+
if (this == obj)
112+
return true;
113+
if (obj == null)
114+
return false;
115+
if (getClass() != obj.getClass())
116+
return false;
117+
VectorLayout other = (VectorLayout) obj;
118+
return type.equals(other.type) && (typeBitWidth == other.typeBitWidth);
119+
}
120+
121+
@Override
122+
public int writeTo(FlatBufferBuilder builder) {;
123+
return org.apache.arrow.flatbuf.VectorLayout.createVectorLayout(builder, typeBitWidth, type.getType());
124+
}
125+
126+
93127
}

vector/src/main/java/org/apache/arrow/vector/types/pojo/Field.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020

2121
import static org.apache.arrow.vector.types.pojo.ArrowType.getTypeForField;
2222

23-
import java.util.ArrayList;
2423
import java.util.List;
2524
import java.util.Objects;
2625

27-
import org.apache.arrow.vector.schema.ArrowVectorType;
2826
import org.apache.arrow.vector.schema.TypeLayout;
27+
import org.apache.arrow.vector.schema.VectorLayout;
2928

3029
import com.google.common.collect.ImmutableList;
3130
import com.google.flatbuffers.FlatBufferBuilder;
@@ -37,7 +36,7 @@ public class Field {
3736
private final List<Field> children;
3837
private final TypeLayout typeLayout;
3938

40-
public Field(String name, boolean nullable, ArrowType type, List<Field> children) {
39+
private Field(String name, boolean nullable, ArrowType type, List<Field> children, TypeLayout typeLayout) {
4140
this.name = name;
4241
this.nullable = nullable;
4342
this.type = type;
@@ -46,34 +45,37 @@ public Field(String name, boolean nullable, ArrowType type, List<Field> children
4645
} else {
4746
this.children = children;
4847
}
49-
this.typeLayout = TypeLayout.getTypeLayout(type);
48+
this.typeLayout = typeLayout;
49+
}
50+
51+
public Field(String name, boolean nullable, ArrowType type, List<Field> children) {
52+
this(name, nullable, type, children, TypeLayout.getTypeLayout(type));
5053
}
5154

5255
public static Field convertField(org.apache.arrow.flatbuf.Field field) {
5356
String name = field.name();
5457
boolean nullable = field.nullable();
5558
ArrowType type = getTypeForField(field);
56-
List<ArrowVectorType> buffers = new ArrayList<>();
57-
for (int i = 0; i < field.buffersLength(); ++i) {
58-
buffers.add(new ArrowVectorType(field.buffers(i)));
59+
ImmutableList.Builder<org.apache.arrow.vector.schema.VectorLayout> layout = ImmutableList.builder();
60+
for (int i = 0; i < field.layoutLength(); ++i) {
61+
layout.add(new org.apache.arrow.vector.schema.VectorLayout(field.layout(i)));
5962
}
6063
ImmutableList.Builder<Field> childrenBuilder = ImmutableList.builder();
6164
for (int i = 0; i < field.childrenLength(); i++) {
6265
childrenBuilder.add(convertField(field.children(i)));
6366
}
6467
List<Field> children = childrenBuilder.build();
65-
Field result = new Field(name, nullable, type, children);
66-
TypeLayout typeLayout = result.getTypeLayout();
67-
if (typeLayout.getVectors().size() != field.buffersLength()) {
68-
List<ArrowVectorType> types = new ArrayList<>();
69-
for (int i = 0; i < field.buffersLength(); i++) {
70-
types.add(new ArrowVectorType(field.buffers(i)));
71-
}
72-
throw new IllegalArgumentException("Deserialized field does not match expected vectors. expected: " + typeLayout.getVectorTypes() + " got " + types);
73-
}
68+
Field result = new Field(name, nullable, type, children, new TypeLayout(layout.build()));
7469
return result;
7570
}
7671

72+
public void validate() {
73+
TypeLayout expectedLayout = TypeLayout.getTypeLayout(type);
74+
if (!expectedLayout.equals(typeLayout)) {
75+
throw new IllegalArgumentException("Deserialized field does not match expected vectors. expected: " + expectedLayout + " got " + typeLayout);
76+
}
77+
}
78+
7779
public int getField(FlatBufferBuilder builder) {
7880
int nameOffset = builder.createString(name);
7981
int typeOffset = type.getType(builder);
@@ -82,18 +84,19 @@ public int getField(FlatBufferBuilder builder) {
8284
childrenData[i] = children.get(i).getField(builder);
8385
}
8486
int childrenOffset = org.apache.arrow.flatbuf.Field.createChildrenVector(builder, childrenData);
85-
short[] buffersData = new short[typeLayout.getVectors().size()];
87+
int[] buffersData = new int[typeLayout.getVectors().size()];
8688
for (int i = 0; i < buffersData.length; i++) {
87-
buffersData[i] = typeLayout.getVectors().get(i).getType().getType();
89+
VectorLayout vectorLayout = typeLayout.getVectors().get(i);
90+
buffersData[i] = vectorLayout.writeTo(builder);
8891
}
89-
int buffersOffset = org.apache.arrow.flatbuf.Field.createBuffersVector(builder, buffersData );
92+
int layoutOffset = org.apache.arrow.flatbuf.Field.createLayoutVector(builder, buffersData);
9093
org.apache.arrow.flatbuf.Field.startField(builder);
9194
org.apache.arrow.flatbuf.Field.addName(builder, nameOffset);
9295
org.apache.arrow.flatbuf.Field.addNullable(builder, nullable);
9396
org.apache.arrow.flatbuf.Field.addTypeType(builder, type.getTypeType());
9497
org.apache.arrow.flatbuf.Field.addType(builder, typeOffset);
9598
org.apache.arrow.flatbuf.Field.addChildren(builder, childrenOffset);
96-
org.apache.arrow.flatbuf.Field.addBuffers(builder, buffersOffset);
99+
org.apache.arrow.flatbuf.Field.addLayout(builder, layoutOffset);
97100
return org.apache.arrow.flatbuf.Field.endField(builder);
98101
}
99102

vector/src/test/java/org/apache/arrow/vector/pojo/TestConvert.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import static org.junit.Assert.assertEquals;
2323

2424
import org.apache.arrow.flatbuf.UnionMode;
25+
import static org.junit.Assert.assertEquals;
26+
2527
import org.apache.arrow.vector.types.pojo.ArrowType.FloatingPoint;
2628
import org.apache.arrow.vector.types.pojo.ArrowType.Int;
2729
import org.apache.arrow.vector.types.pojo.ArrowType.List;

0 commit comments

Comments
 (0)