Search in sources :

Example 1 with ArrayReader

use of org.apache.drill.exec.vector.accessor.ArrayReader 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)

Example 2 with ArrayReader

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

the class TestResultSetLoaderMapArray method testOverwriteRow.

/**
 * Version of the {#link TestResultSetLoaderProtocol#testOverwriteRow()} test
 * that uses nested columns inside an array of maps. Here we must call
 * <tt>start()</tt> to reset the array back to the initial start position after
 * each "discard."
 */
@Test
public void testOverwriteRow() {
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("b", MinorType.INT).add("c", MinorType.VARCHAR).resumeSchema().buildSchema();
    ResultSetLoaderImpl.ResultSetOptions options = new OptionBuilder().setSchema(schema).setRowCountLimit(ValueVector.MAX_ROW_COUNT).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    RowSetLoader rootWriter = rsLoader.writer();
    // Can't use the shortcut to populate rows when doing overwrites.
    ScalarWriter aWriter = rootWriter.scalar("a");
    ArrayWriter maWriter = rootWriter.array("m");
    TupleWriter mWriter = maWriter.tuple();
    ScalarWriter bWriter = mWriter.scalar("b");
    ScalarWriter cWriter = mWriter.scalar("c");
    // Write 100,000 rows, overwriting 99% of them. This will cause vector
    // overflow and data corruption if overwrite does not work; but will happily
    // produce the correct result if everything works as it should.
    byte[] value = new byte[512];
    Arrays.fill(value, (byte) 'X');
    int count = 0;
    rsLoader.startBatch();
    while (count < 10_000) {
        rootWriter.start();
        count++;
        aWriter.setInt(count);
        for (int i = 0; i < 10; i++) {
            bWriter.setInt(count * 10 + i);
            cWriter.setBytes(value, value.length);
            maWriter.save();
        }
        if (count % 100 == 0) {
            rootWriter.save();
        }
    }
    // Verify using a reader.
    RowSet result = fixture.wrap(rsLoader.harvest());
    assertEquals(count / 100, result.rowCount());
    RowSetReader reader = result.reader();
    ArrayReader maReader = reader.array("m");
    TupleReader mReader = maReader.tuple();
    int rowId = 1;
    while (reader.next()) {
        assertEquals(rowId * 100, reader.scalar("a").getInt());
        assertEquals(10, maReader.size());
        for (int i = 0; i < 10; i++) {
            maReader.setPosn(i);
            assertEquals(rowId * 1000 + i, mReader.scalar("b").getInt());
            assertTrue(Arrays.equals(value, mReader.scalar("c").getBytes()));
        }
        rowId++;
    }
    result.clear();
    rsLoader.close();
}
Also used : TupleReader(org.apache.drill.exec.vector.accessor.TupleReader) SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.test.rowSet.RowSet) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) ResultSetLoader(org.apache.drill.exec.physical.rowSet.ResultSetLoader) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.rowSet.RowSetLoader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 3 with ArrayReader

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

the class RowSetTest method testRepeatedMapStructure.

@Test
public void testRepeatedMapStructure() {
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).addMapArray("m").add("b", MinorType.INT).add("c", MinorType.INT).resumeSchema().buildSchema();
    ExtendableRowSet rowSet = fixture.rowSet(schema);
    RowSetWriter writer = rowSet.writer();
    // Map and Int
    // Pick out components and lightly test. (Assumes structure
    // tested earlier is still valid, so no need to exhaustively
    // test again.)
    assertEquals(ObjectType.SCALAR, writer.column("a").type());
    assertEquals(ObjectType.ARRAY, writer.column("m").type());
    ArrayWriter maWriter = writer.column(1).array();
    assertEquals(ObjectType.TUPLE, maWriter.entryType());
    TupleWriter mapWriter = maWriter.tuple();
    assertEquals(ObjectType.SCALAR, mapWriter.column("b").type());
    assertEquals(ObjectType.SCALAR, mapWriter.column("c").type());
    ScalarWriter aWriter = writer.column("a").scalar();
    ScalarWriter bWriter = mapWriter.scalar("b");
    ScalarWriter cWriter = mapWriter.scalar("c");
    assertEquals(ValueType.INTEGER, aWriter.valueType());
    assertEquals(ValueType.INTEGER, bWriter.valueType());
    assertEquals(ValueType.INTEGER, cWriter.valueType());
    // Write data
    aWriter.setInt(10);
    bWriter.setInt(101);
    cWriter.setInt(102);
    // Advance to next array position
    maWriter.save();
    bWriter.setInt(111);
    cWriter.setInt(112);
    maWriter.save();
    writer.save();
    aWriter.setInt(20);
    bWriter.setInt(201);
    cWriter.setInt(202);
    maWriter.save();
    bWriter.setInt(211);
    cWriter.setInt(212);
    maWriter.save();
    writer.save();
    aWriter.setInt(30);
    bWriter.setInt(301);
    cWriter.setInt(302);
    maWriter.save();
    bWriter.setInt(311);
    cWriter.setInt(312);
    maWriter.save();
    writer.save();
    // Finish the row set and get a reader.
    SingleRowSet actual = writer.done();
    RowSetReader reader = actual.reader();
    // Verify reader structure
    assertEquals(ObjectType.SCALAR, reader.column("a").type());
    assertEquals(ObjectType.ARRAY, reader.column("m").type());
    ArrayReader maReader = reader.column(1).array();
    assertEquals(ObjectType.TUPLE, maReader.entryType());
    TupleReader mapReader = maReader.tuple();
    assertEquals(ObjectType.SCALAR, mapReader.column("b").type());
    assertEquals(ObjectType.SCALAR, mapReader.column("c").type());
    ScalarReader aReader = reader.column("a").scalar();
    ScalarReader bReader = mapReader.scalar("b");
    ScalarReader cReader = mapReader.scalar("c");
    assertEquals(ValueType.INTEGER, aReader.valueType());
    assertEquals(ValueType.INTEGER, bReader.valueType());
    assertEquals(ValueType.INTEGER, cReader.valueType());
    // Row 1: use index accessors
    assertTrue(reader.next());
    assertEquals(10, aReader.getInt());
    TupleReader ixReader = maReader.tuple(0);
    assertEquals(101, ixReader.scalar(0).getInt());
    assertEquals(102, ixReader.scalar(1).getInt());
    ixReader = maReader.tuple(1);
    assertEquals(111, ixReader.scalar(0).getInt());
    assertEquals(112, ixReader.scalar(1).getInt());
    // Row 2: use common accessor with explicit positioning,
    // but access scalars through the map reader.
    assertTrue(reader.next());
    assertEquals(20, aReader.getInt());
    maReader.setPosn(0);
    assertEquals(201, mapReader.scalar(0).getInt());
    assertEquals(202, mapReader.scalar(1).getInt());
    maReader.setPosn(1);
    assertEquals(211, mapReader.scalar(0).getInt());
    assertEquals(212, mapReader.scalar(1).getInt());
    // Row 3: use common accessor for scalars
    assertTrue(reader.next());
    assertEquals(30, aReader.getInt());
    maReader.setPosn(0);
    assertEquals(301, bReader.getInt());
    assertEquals(302, cReader.getInt());
    maReader.setPosn(1);
    assertEquals(311, bReader.getInt());
    assertEquals(312, cReader.getInt());
    assertFalse(reader.next());
    // Verify that the map accessor's value count was set.
    @SuppressWarnings("resource") RepeatedMapVector mapVector = (RepeatedMapVector) actual.container().getValueVector(1).getValueVector();
    assertEquals(3, mapVector.getAccessor().getValueCount());
    // Verify the readers and writers again using the testing tools.
    SingleRowSet expected = fixture.rowSetBuilder(schema).addRow(10, objArray(objArray(101, 102), objArray(111, 112))).addRow(20, objArray(objArray(201, 202), objArray(211, 212))).addRow(30, objArray(objArray(301, 302), objArray(311, 312))).build();
    new RowSetComparison(expected).verifyAndClearAll(actual);
}
Also used : RowSetWriter(org.apache.drill.test.rowSet.RowSetWriter) TupleReader(org.apache.drill.exec.vector.accessor.TupleReader) SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) RepeatedMapVector(org.apache.drill.exec.vector.complex.RepeatedMapVector) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) RowSetComparison(org.apache.drill.test.rowSet.RowSetComparison) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) ExtendableRowSet(org.apache.drill.test.rowSet.RowSet.ExtendableRowSet) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 4 with ArrayReader

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

the class TestResultSetLoaderRepeatedList method test2DOverflow.

@Test
public void test2DOverflow() {
    final TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addRepeatedList("list2").addArray(MinorType.VARCHAR).resumeSchema().buildSchema();
    final ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(schema).build();
    final ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    final RowSetLoader writer = rsLoader.writer();
    // Fill the batch with enough data to cause overflow.
    // Data must be large enough to cause overflow before 64K rows
    // Make a bit bigger to overflow early.
    final int outerSize = 7;
    final int innerSize = 5;
    final int strLength = ValueVector.MAX_BUFFER_SIZE / ValueVector.MAX_ROW_COUNT / outerSize / innerSize + 20;
    final byte[] value = new byte[strLength - 6];
    Arrays.fill(value, (byte) 'X');
    final String strValue = new String(value, Charsets.UTF_8);
    int rowCount = 0;
    int elementCount = 0;
    final ArrayWriter outerWriter = writer.array(1);
    final ArrayWriter innerWriter = outerWriter.array();
    final ScalarWriter elementWriter = innerWriter.scalar();
    rsLoader.startBatch();
    while (!writer.isFull()) {
        writer.start();
        writer.scalar(0).setInt(rowCount);
        for (int j = 0; j < outerSize; j++) {
            for (int k = 0; k < innerSize; k++) {
                elementWriter.setString(String.format("%s%06d", strValue, elementCount));
                elementCount++;
            }
            outerWriter.save();
        }
        writer.save();
        rowCount++;
    }
    // Number of rows should be driven by vector size.
    // Our row count should include the overflow row
    final int expectedCount = ValueVector.MAX_BUFFER_SIZE / (strLength * innerSize * outerSize);
    assertEquals(expectedCount + 1, rowCount);
    // Loader's row count should include only "visible" rows
    assertEquals(expectedCount, writer.rowCount());
    // Total count should include invisible and look-ahead rows.
    assertEquals(expectedCount + 1, rsLoader.totalRowCount());
    // Result should exclude the overflow row
    RowSet result = fixture.wrap(rsLoader.harvest());
    assertEquals(expectedCount, result.rowCount());
    // Verify the data.
    RowSetReader reader = result.reader();
    ArrayReader outerReader = reader.array(1);
    ArrayReader innerReader = outerReader.array();
    ScalarReader strReader = innerReader.scalar();
    int readRowCount = 0;
    int readElementCount = 0;
    while (reader.next()) {
        assertEquals(readRowCount, reader.scalar(0).getInt());
        for (int i = 0; i < outerSize; i++) {
            assertTrue(outerReader.next());
            for (int j = 0; j < innerSize; j++) {
                assertTrue(innerReader.next());
                assertEquals(String.format("%s%06d", strValue, readElementCount), strReader.getString());
                readElementCount++;
            }
            assertFalse(innerReader.next());
        }
        assertFalse(outerReader.next());
        readRowCount++;
    }
    assertEquals(readRowCount, result.rowCount());
    result.clear();
    // Write a few more rows to verify the overflow row.
    rsLoader.startBatch();
    for (int i = 0; i < 1000; i++) {
        writer.start();
        writer.scalar(0).setInt(rowCount);
        for (int j = 0; j < outerSize; j++) {
            for (int k = 0; k < innerSize; k++) {
                elementWriter.setString(String.format("%s%06d", strValue, elementCount));
                elementCount++;
            }
            outerWriter.save();
        }
        writer.save();
        rowCount++;
    }
    result = fixture.wrap(rsLoader.harvest());
    assertEquals(1001, result.rowCount());
    final int startCount = readRowCount;
    reader = result.reader();
    outerReader = reader.array(1);
    innerReader = outerReader.array();
    strReader = innerReader.scalar();
    while (reader.next()) {
        assertEquals(readRowCount, reader.scalar(0).getInt());
        for (int i = 0; i < outerSize; i++) {
            assertTrue(outerReader.next());
            for (int j = 0; j < innerSize; j++) {
                assertTrue(innerReader.next());
                elementWriter.setString(String.format("%s%06d", strValue, readElementCount));
                assertEquals(String.format("%s%06d", strValue, readElementCount), strReader.getString());
                readElementCount++;
            }
            assertFalse(innerReader.next());
        }
        assertFalse(outerReader.next());
        readRowCount++;
    }
    assertEquals(readRowCount - startCount, result.rowCount());
    result.clear();
    rsLoader.close();
}
Also used : SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) RowSetReader(org.apache.drill.exec.physical.rowSet.RowSetReader) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 5 with ArrayReader

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

the class TestResultSetLoaderMapArray method testOmittedValues.

/**
 * Check that the "fill-empties" logic descends down into
 * a repeated map.
 */
@Test
public void testOmittedValues() {
    TupleMetadata schema = new SchemaBuilder().add("id", MinorType.INT).addMapArray("m").addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeSchema().buildSchema();
    ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).rowCountLimit(ValueVector.MAX_ROW_COUNT).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    RowSetLoader rootWriter = rsLoader.writer();
    int mapSkip = 5;
    int entrySkip = 3;
    int rowCount = 1000;
    int entryCount = 10;
    rsLoader.startBatch();
    ArrayWriter maWriter = rootWriter.array("m");
    TupleWriter mWriter = maWriter.tuple();
    for (int i = 0; i < rowCount; i++) {
        rootWriter.start();
        rootWriter.scalar(0).setInt(i);
        if (i % mapSkip != 0) {
            for (int j = 0; j < entryCount; j++) {
                if (j % entrySkip != 0) {
                    mWriter.scalar(0).setInt(i * entryCount + j);
                    mWriter.scalar(1).setString("b-" + i + "." + j);
                }
                maWriter.save();
            }
        }
        rootWriter.save();
    }
    RowSet result = fixture.wrap(rsLoader.harvest());
    assertEquals(rowCount, result.rowCount());
    RowSetReader reader = result.reader();
    ArrayReader maReader = reader.array("m");
    TupleReader mReader = maReader.tuple();
    for (int i = 0; i < rowCount; i++) {
        assertTrue(reader.next());
        assertEquals(i, reader.scalar(0).getInt());
        if (i % mapSkip == 0) {
            assertEquals(0, maReader.size());
            continue;
        }
        assertEquals(entryCount, maReader.size());
        for (int j = 0; j < entryCount; j++) {
            assertTrue(maReader.next());
            if (j % entrySkip == 0) {
                assertTrue(mReader.scalar(0).isNull());
                assertTrue(mReader.scalar(1).isNull());
            } else {
                assertFalse(mReader.scalar(0).isNull());
                assertFalse(mReader.scalar(1).isNull());
                assertEquals(i * entryCount + j, mReader.scalar(0).getInt());
                assertEquals("b-" + i + "." + j, mReader.scalar(1).getString());
            }
        }
    }
    result.clear();
    rsLoader.close();
}
Also used : TupleReader(org.apache.drill.exec.vector.accessor.TupleReader) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter) RowSetReader(org.apache.drill.exec.physical.rowSet.RowSetReader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Aggregations

ArrayReader (org.apache.drill.exec.vector.accessor.ArrayReader)40 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)36 Test (org.junit.Test)33 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)31 SubOperatorTest (org.apache.drill.test.SubOperatorTest)30 ScalarReader (org.apache.drill.exec.vector.accessor.ScalarReader)29 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)27 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)22 ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)19 TupleReader (org.apache.drill.exec.vector.accessor.TupleReader)12 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)12 ExtendableRowSet (org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)11 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)9 RowSetReader (org.apache.drill.exec.physical.rowSet.RowSetReader)9 ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)7 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)7 ObjectReader (org.apache.drill.exec.vector.accessor.ObjectReader)7 SingleRowSet (org.apache.drill.test.rowSet.RowSet.SingleRowSet)5 VectorContainer (org.apache.drill.exec.record.VectorContainer)4 ObjectWriter (org.apache.drill.exec.vector.accessor.ObjectWriter)4