Search in sources :

Example 1 with ArrayWriter

use of org.apache.drill.exec.vector.accessor.ArrayWriter in project drill by apache.

the class TupleWriterImpl method setArray.

public void setArray(int colIndex, Object value) {
    if (value == null) {
        return;
    }
    String objClass = value.getClass().getName();
    if (!objClass.startsWith("[")) {
        throw new IllegalArgumentException("Argument is not an array");
    }
    ColumnWriter colWriter = column(colIndex);
    if (colWriter.valueType() != ValueType.ARRAY) {
        throw new IllegalArgumentException("Column is not an array");
    }
    ArrayWriter arrayWriter = colWriter.array();
    // Figure out type
    char second = objClass.charAt(1);
    switch(second) {
        case 'B':
            AccessorUtilities.setByteArray(arrayWriter, (byte[]) value);
            break;
        case 'S':
            AccessorUtilities.setShortArray(arrayWriter, (short[]) value);
            break;
        case 'I':
            AccessorUtilities.setIntArray(arrayWriter, (int[]) value);
            break;
        case 'J':
            AccessorUtilities.setLongArray(arrayWriter, (long[]) value);
            break;
        case 'F':
            AccessorUtilities.setFloatArray(arrayWriter, (float[]) value);
            break;
        case 'D':
            AccessorUtilities.setDoubleArray(arrayWriter, (double[]) value);
            break;
        case 'Z':
            AccessorUtilities.setBooleanArray(arrayWriter, (boolean[]) value);
            break;
        case 'L':
            int posn = objClass.indexOf(';');
            // If the array is of type Object, then we have no type info.
            String memberClassName = objClass.substring(2, posn);
            if (memberClassName.equals(String.class.getName())) {
                AccessorUtilities.setStringArray(arrayWriter, (String[]) value);
            } else if (memberClassName.equals(Period.class.getName())) {
                AccessorUtilities.setPeriodArray(arrayWriter, (Period[]) value);
            } else if (memberClassName.equals(BigDecimal.class.getName())) {
                AccessorUtilities.setBigDecimalArray(arrayWriter, (BigDecimal[]) value);
            } else {
                throw new IllegalArgumentException("Unknown Java array type: " + memberClassName);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown Java array type: " + second);
    }
}
Also used : ColumnWriter(org.apache.drill.exec.vector.accessor.ColumnWriter) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) BigDecimal(java.math.BigDecimal)

Example 2 with ArrayWriter

use of org.apache.drill.exec.vector.accessor.ArrayWriter in project drill by apache.

the class RowSetTest method TestTopScalarArray.

@Test
public void TestTopScalarArray() {
    BatchSchema batchSchema = new SchemaBuilder().add("c", MinorType.INT).addArray("a", MinorType.INT).build();
    ExtendableRowSet rs1 = fixture.rowSet(batchSchema);
    RowSetWriter writer = rs1.writer();
    writer.column(0).setInt(10);
    ArrayWriter array = writer.column(1).array();
    array.setInt(100);
    array.setInt(110);
    writer.save();
    writer.column(0).setInt(20);
    array = writer.column(1).array();
    array.setInt(200);
    array.setInt(120);
    array.setInt(220);
    writer.save();
    writer.column(0).setInt(30);
    writer.save();
    writer.done();
    RowSetReader reader = rs1.reader();
    assertTrue(reader.next());
    assertEquals(10, reader.column(0).getInt());
    ArrayReader arrayReader = reader.column(1).array();
    assertEquals(2, arrayReader.size());
    assertEquals(100, arrayReader.getInt(0));
    assertEquals(110, arrayReader.getInt(1));
    assertTrue(reader.next());
    assertEquals(20, reader.column(0).getInt());
    arrayReader = reader.column(1).array();
    assertEquals(3, arrayReader.size());
    assertEquals(200, arrayReader.getInt(0));
    assertEquals(120, arrayReader.getInt(1));
    assertEquals(220, arrayReader.getInt(2));
    assertTrue(reader.next());
    assertEquals(30, reader.column(0).getInt());
    arrayReader = reader.column(1).array();
    assertEquals(0, arrayReader.size());
    assertFalse(reader.next());
    SingleRowSet rs2 = fixture.rowSetBuilder(batchSchema).add(10, new int[] { 100, 110 }).add(20, new int[] { 200, 120, 220 }).add(30, null).build();
    new RowSetComparison(rs1).verifyAndClear(rs2);
}
Also used : RowSetWriter(org.apache.drill.test.rowSet.RowSet.RowSetWriter) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) RowSetComparison(org.apache.drill.test.rowSet.RowSetComparison) BatchSchema(org.apache.drill.exec.record.BatchSchema) SchemaBuilder(org.apache.drill.test.rowSet.SchemaBuilder) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) RowSetReader(org.apache.drill.test.rowSet.RowSet.RowSetReader) ExtendableRowSet(org.apache.drill.test.rowSet.RowSet.ExtendableRowSet) Test(org.junit.Test)

Aggregations

ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)2 BigDecimal (java.math.BigDecimal)1 BatchSchema (org.apache.drill.exec.record.BatchSchema)1 ArrayReader (org.apache.drill.exec.vector.accessor.ArrayReader)1 ColumnWriter (org.apache.drill.exec.vector.accessor.ColumnWriter)1 ExtendableRowSet (org.apache.drill.test.rowSet.RowSet.ExtendableRowSet)1 RowSetReader (org.apache.drill.test.rowSet.RowSet.RowSetReader)1 RowSetWriter (org.apache.drill.test.rowSet.RowSet.RowSetWriter)1 SingleRowSet (org.apache.drill.test.rowSet.RowSet.SingleRowSet)1 RowSetComparison (org.apache.drill.test.rowSet.RowSetComparison)1 SchemaBuilder (org.apache.drill.test.rowSet.SchemaBuilder)1 Test (org.junit.Test)1