use of org.apache.arrow.vector.IntVector in project beam by apache.
the class ArrowConversionTest method rowIterator.
@Test
public void rowIterator() {
org.apache.arrow.vector.types.pojo.Schema schema = new org.apache.arrow.vector.types.pojo.Schema(asList(field("int32", new ArrowType.Int(32, true)), field("float64", new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), field("string", new ArrowType.Utf8()), field("timestampMicroUTC", new ArrowType.Timestamp(TimeUnit.MICROSECOND, "UTC")), field("timestampMilliUTC", new ArrowType.Timestamp(TimeUnit.MILLISECOND, "UTC")), field("int32_list", new ArrowType.List(), field("int32s", new ArrowType.Int(32, true))), field("boolean", new ArrowType.Bool()), field("fixed_size_binary", new ArrowType.FixedSizeBinary(3))));
Schema beamSchema = ArrowConversion.ArrowSchemaTranslator.toBeamSchema(schema);
VectorSchemaRoot expectedSchemaRoot = VectorSchemaRoot.create(schema, allocator);
expectedSchemaRoot.allocateNew();
expectedSchemaRoot.setRowCount(16);
IntVector intVector = (IntVector) expectedSchemaRoot.getFieldVectors().get(0);
Float8Vector floatVector = (Float8Vector) expectedSchemaRoot.getFieldVectors().get(1);
VarCharVector strVector = (VarCharVector) expectedSchemaRoot.getFieldVectors().get(2);
TimeStampMicroTZVector timestampMicroUtcVector = (TimeStampMicroTZVector) expectedSchemaRoot.getFieldVectors().get(3);
TimeStampMilliTZVector timeStampMilliTZVector = (TimeStampMilliTZVector) expectedSchemaRoot.getFieldVectors().get(4);
ListVector int32ListVector = (ListVector) expectedSchemaRoot.getFieldVectors().get(5);
IntVector int32ListElementVector = int32ListVector.<IntVector>addOrGetVector(new org.apache.arrow.vector.types.pojo.FieldType(false, new ArrowType.Int(32, true), null)).getVector();
BitVector boolVector = (BitVector) expectedSchemaRoot.getFieldVectors().get(6);
FixedSizeBinaryVector fixedSizeBinaryVector = (FixedSizeBinaryVector) expectedSchemaRoot.getFieldVectors().get(7);
ArrayList<Row> expectedRows = new ArrayList<>();
for (int i = 0; i < 16; i++) {
DateTime dt = new DateTime(2019, 1, i + 1, i, i, i, DateTimeZone.UTC);
expectedRows.add(Row.withSchema(beamSchema).addValues(i, i + .1 * i, "" + i, dt, dt, ImmutableList.of(i), (i % 2) != 0, new byte[] { (byte) i, (byte) (i + 1), (byte) (i + 2) }).build());
intVector.set(i, i);
floatVector.set(i, i + .1 * i);
strVector.set(i, new Text("" + i));
timestampMicroUtcVector.set(i, dt.getMillis() * 1000);
timeStampMilliTZVector.set(i, dt.getMillis());
int32ListVector.startNewValue(i);
int32ListElementVector.set(i, i);
int32ListVector.endValue(i, 1);
boolVector.set(i, i % 2);
fixedSizeBinaryVector.set(i, new byte[] { (byte) i, (byte) (i + 1), (byte) (i + 2) });
}
assertThat(ImmutableList.copyOf(ArrowConversion.rowsFromRecordBatch(beamSchema, expectedSchemaRoot)), IsIterableContainingInOrder.contains(expectedRows.stream().map((row) -> equalTo(row)).collect(ImmutableList.toImmutableList())));
expectedSchemaRoot.close();
}
use of org.apache.arrow.vector.IntVector in project twister2 by DSC-SPIDAL.
the class ArrowAllToAll method onReceive.
@Override
public void onReceive(int source, ChannelBuffer buffer, int length) {
PendingReceiveTable table = receives.get(source);
receivedBuffers++;
ArrowBuf buf = ((ArrowChannelBuffer) buffer).getArrowBuf();
table.buffers.add(buf);
if (table.bufferIndex == 0) {
table.fieldNodes.add(new ArrowFieldNode(table.noArray, 0));
}
VectorSchemaRoot schemaRoot = table.root;
List<FieldVector> fieldVectors = schemaRoot.getFieldVectors();
// we received everything for this array
if (table.noBuffers == table.bufferIndex + 1) {
FieldVector fieldVector = fieldVectors.get(table.columnIndex);
loadBuffers(fieldVector, fieldVector.getField(), table.buffers.iterator(), table.fieldNodes.iterator());
table.arrays.add(fieldVector);
table.buffers.clear();
if (table.arrays.size() == schemaRoot.getFieldVectors().size()) {
List<ArrowColumn> columns = new ArrayList<>();
// create the table
for (FieldVector v : fieldVectors) {
ArrowColumn c;
if (v instanceof BaseFixedWidthVector) {
if (v instanceof IntVector) {
c = new Int4Column((IntVector) v);
} else if (v instanceof Float4Vector) {
c = new Float4Column((Float4Vector) v);
} else if (v instanceof Float8Vector) {
c = new Float8Column((Float8Vector) v);
} else if (v instanceof UInt8Vector) {
c = new Int8Column((UInt8Vector) v);
} else if (v instanceof UInt2Vector) {
c = new UInt2Column((UInt2Vector) v);
} else {
throw new RuntimeException("Un-supported type : " + v.getClass().getName());
}
} else if (v instanceof BaseVariableWidthVector) {
if (v instanceof VarCharVector) {
c = new StringColumn((VarCharVector) v);
} else if (v instanceof VarBinaryVector) {
c = new BinaryColumn((VarBinaryVector) v);
} else {
throw new RuntimeException("Un-supported type : " + v.getClass().getName());
}
} else {
throw new RuntimeException("Un-supported type : " + v.getClass().getName());
}
columns.add(c);
}
Table t = new ArrowTable(schemaRoot.getSchema(), table.noArray, columns);
LOG.info("Received table from source " + source + " to " + table.target + " count" + t.rowCount());
recvCallback.onReceive(source, table.target, t);
table.clear();
}
}
}
use of org.apache.arrow.vector.IntVector in project carbondata by apache.
the class ArrowWriter method createFieldWriter.
private static ArrowFieldWriter createFieldWriter(ValueVector valueVector) {
if (valueVector instanceof BitVector) {
return new BooleanWriter((BitVector) valueVector);
} else if (valueVector instanceof TinyIntVector) {
return new ByteWriter((TinyIntVector) valueVector);
} else if (valueVector instanceof SmallIntVector) {
return new ShortWriter((SmallIntVector) valueVector);
} else if (valueVector instanceof IntVector) {
return new IntWriter((IntVector) valueVector);
} else if (valueVector instanceof BigIntVector) {
return new LongWriter((BigIntVector) valueVector);
} else if (valueVector instanceof DecimalVector) {
DecimalVector decimalVector = (DecimalVector) valueVector;
final Field field = decimalVector.getField();
ArrowType.Decimal c = (ArrowType.Decimal) field.getType();
return new DecimalWriter((DecimalVector) valueVector, c.getPrecision(), c.getScale());
} else if (valueVector instanceof VarCharVector) {
return new StringWriter((VarCharVector) valueVector);
} else if (valueVector instanceof Float4Vector) {
return new FloatWriter((Float4Vector) valueVector);
} else if (valueVector instanceof Float8Vector) {
return new DoubleWriter((Float8Vector) valueVector);
} else if (valueVector instanceof ListVector) {
ArrowFieldWriter elementVector = createFieldWriter(((ListVector) valueVector).getDataVector());
return new ArrayWriter((ListVector) valueVector, elementVector);
} else if (valueVector instanceof StructVector) {
StructVector s = (StructVector) valueVector;
List<ArrowFieldWriter> arrowFieldWriters = new ArrayList<>();
for (int i = 0; i < s.size(); i++) {
arrowFieldWriters.add(createFieldWriter(s.getChildByOrdinal(i)));
}
return new StructWriter(s, arrowFieldWriters.toArray(new ArrowFieldWriter[arrowFieldWriters.size()]));
} else if (valueVector instanceof VarBinaryVector) {
return new BinaryWriter((VarBinaryVector) valueVector);
} else if (valueVector instanceof DateDayVector) {
return new DateWriter((DateDayVector) valueVector);
} else if (valueVector instanceof TimeStampMicroTZVector) {
return new TimeStampWriter((TimeStampMicroTZVector) valueVector);
} else {
throw new UnsupportedOperationException("Invalid data type");
}
}
Aggregations