Search in sources :

Example 81 with ResultSetLoader

use of org.apache.drill.exec.physical.resultSet.ResultSetLoader in project drill by apache.

the class TestResultSetLoaderEmptyProject method testEmptyTopSchema.

/**
 * Verify that empty projection works: allows skipping rows and
 * reporting those rows as a batch with no vectors but with the
 * desired row count.
 */
@Test
public void testEmptyTopSchema() {
    List<SchemaPath> selection = Lists.newArrayList();
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.INT).buildSchema();
    ResultSetOptions options = new ResultSetOptionBuilder().projection(Projections.parse(selection)).readerSchema(schema).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    assertTrue(rsLoader.isProjectionEmpty());
    // Can't skip rows if batch not started.
    int rowCount = 100_000;
    try {
        rsLoader.skipRows(10);
        fail();
    } catch (IllegalStateException e) {
    // Expected
    }
    // Loop to skip 100,000 rows. Should occur in two batches.
    rsLoader.startBatch();
    int skipped = rsLoader.skipRows(rowCount);
    assertEquals(skipped, ValueVector.MAX_ROW_COUNT);
    VectorContainer output = rsLoader.harvest();
    assertEquals(skipped, output.getRecordCount());
    assertEquals(0, output.getNumberOfColumns());
    output.zeroVectors();
    // Second batch
    rowCount -= skipped;
    rsLoader.startBatch();
    skipped = rsLoader.skipRows(rowCount);
    assertEquals(skipped, rowCount);
    output = rsLoader.harvest();
    assertEquals(skipped, output.getRecordCount());
    assertEquals(0, output.getNumberOfColumns());
    output.zeroVectors();
    rsLoader.close();
}
Also used : ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) SchemaPath(org.apache.drill.common.expression.SchemaPath) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ResultSetOptions(org.apache.drill.exec.physical.resultSet.impl.ResultSetLoaderImpl.ResultSetOptions) VectorContainer(org.apache.drill.exec.record.VectorContainer) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 82 with ResultSetLoader

use of org.apache.drill.exec.physical.resultSet.ResultSetLoader in project drill by apache.

the class TestResultSetLoaderMapArray method testCloseWithoutHarvest.

/**
 * Test that memory is released if the loader is closed with an active
 * batch (that is, before the batch is harvested.)
 */
@Test
public void testCloseWithoutHarvest() {
    TupleMetadata schema = new SchemaBuilder().addMapArray("m").add("a", MinorType.INT).add("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();
    ArrayWriter maWriter = rootWriter.array("m");
    TupleWriter mWriter = maWriter.tuple();
    rsLoader.startBatch();
    for (int i = 0; i < 40; i++) {
        rootWriter.start();
        for (int j = 0; j < 3; j++) {
            mWriter.scalar("a").setInt(i);
            mWriter.scalar("b").setString("b-" + i);
            maWriter.save();
        }
        rootWriter.save();
    }
    // Don't harvest the batch. Allocator will complain if the
    // loader does not release memory.
    rsLoader.close();
}
Also used : 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) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 83 with ResultSetLoader

use of org.apache.drill.exec.physical.resultSet.ResultSetLoader in project drill by apache.

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
 * {@code start()} 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 ResultSetOptionBuilder().readerSchema(schema).rowCountLimit(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++) {
            assert (maReader.next());
            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.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) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 84 with ResultSetLoader

use of org.apache.drill.exec.physical.resultSet.ResultSetLoader in project drill by apache.

the class TestResultSetLoaderOmittedValues method testOmittedValuesAtEnd.

/**
 * Test "holes" in the middle of a batch, and unset columns at
 * the end. Ending the batch should fill in missing values.
 */
@Test
public void testOmittedValuesAtEnd() {
    // Create columns up front
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addNullable("c", MinorType.VARCHAR).add("d", MinorType.INT).addNullable("e", MinorType.INT).addArray("f", MinorType.VARCHAR).buildSchema();
    ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().readerSchema(schema).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    RowSetLoader rootWriter = rsLoader.writer();
    rsLoader.startBatch();
    int rowCount = 0;
    ScalarWriter arrayWriter;
    for (int i = 0; i < 2; i++) {
        // Row 0, 1
        rootWriter.start();
        rowCount++;
        rootWriter.scalar(0).setInt(rowCount);
        rootWriter.scalar(1).setString("b_" + rowCount);
        rootWriter.scalar(2).setString("c_" + rowCount);
        rootWriter.scalar(3).setInt(rowCount * 10);
        rootWriter.scalar(4).setInt(rowCount * 100);
        arrayWriter = rootWriter.column(5).array().scalar();
        arrayWriter.setString("f_" + rowCount + "-1");
        arrayWriter.setString("f_" + rowCount + "-2");
        rootWriter.save();
    }
    for (int i = 0; i < 2; i++) {
        // Rows 2, 3
        rootWriter.start();
        rowCount++;
        rootWriter.scalar(0).setInt(rowCount);
        rootWriter.scalar(1).setString("b_" + rowCount);
        rootWriter.scalar(3).setInt(rowCount * 10);
        arrayWriter = rootWriter.column(5).array().scalar();
        arrayWriter.setString("f_" + rowCount + "-1");
        arrayWriter.setString("f_" + rowCount + "-2");
        rootWriter.save();
    }
    for (int i = 0; i < 2; i++) {
        // Rows 4, 5
        rootWriter.start();
        rowCount++;
        rootWriter.scalar(0).setInt(rowCount);
        rootWriter.scalar(2).setString("c_" + rowCount);
        rootWriter.scalar(4).setInt(rowCount * 100);
        rootWriter.save();
    }
    for (int i = 0; i < 2; i++) {
        // Rows 6, 7
        rootWriter.start();
        rowCount++;
        rootWriter.scalar(0).setInt(rowCount);
        rootWriter.scalar(1).setString("b_" + rowCount);
        rootWriter.scalar(2).setString("c_" + rowCount);
        rootWriter.scalar(3).setInt(rowCount * 10);
        rootWriter.scalar(4).setInt(rowCount * 100);
        arrayWriter = rootWriter.column(5).array().scalar();
        arrayWriter.setString("f_" + rowCount + "-1");
        arrayWriter.setString("f_" + rowCount + "-2");
        rootWriter.save();
    }
    for (int i = 0; i < 2; i++) {
        // Rows 8, 9
        rootWriter.start();
        rowCount++;
        rootWriter.scalar(0).setInt(rowCount);
        rootWriter.save();
    }
    // Harvest the row and verify.
    RowSet actual = fixture.wrap(rsLoader.harvest());
    // actual.print();
    TupleMetadata expectedSchema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).addNullable("c", MinorType.VARCHAR).add("d", MinorType.INT).addNullable("e", MinorType.INT).addArray("f", MinorType.VARCHAR).buildSchema();
    SingleRowSet expected = fixture.rowSetBuilder(expectedSchema).addRow(1, "b_1", "c_1", 10, 100, strArray("f_1-1", "f_1-2")).addRow(2, "b_2", "c_2", 20, 200, strArray("f_2-1", "f_2-2")).addRow(3, "b_3", null, 30, null, strArray("f_3-1", "f_3-2")).addRow(4, "b_4", null, 40, null, strArray("f_4-1", "f_4-2")).addRow(5, "", "c_5", 0, 500, strArray()).addRow(6, "", "c_6", 0, 600, strArray()).addRow(7, "b_7", "c_7", 70, 700, strArray("f_7-1", "f_7-2")).addRow(8, "b_8", "c_8", 80, 800, strArray("f_8-1", "f_8-2")).addRow(9, "", null, 0, null, strArray()).addRow(10, "", null, 0, null, strArray()).build();
    RowSetUtilities.verify(expected, actual);
    rsLoader.close();
}
Also used : SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 85 with ResultSetLoader

use of org.apache.drill.exec.physical.resultSet.ResultSetLoader in project drill by apache.

the class TestResultSetLoaderOmittedValues method testDefaultValues.

/**
 * Verify that a default value set on the schema is used to fill missing
 * required columns.
 */
@Test
public void testDefaultValues() {
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).add("b", MinorType.VARCHAR).buildSchema();
    schema.metadata("b").setDefaultValue("Foo");
    ResultSetLoaderImpl.ResultSetOptions options = new ResultSetOptionBuilder().rowCountLimit(ValueVector.MAX_ROW_COUNT).readerSchema(schema).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    RowSetLoader rootWriter = rsLoader.writer();
    rsLoader.startBatch();
    for (int i = 0; i < 7; i++) {
        rootWriter.start();
        rootWriter.scalar(0).setInt(i + 1);
        if (i % 3 != 0) {
            rootWriter.scalar(1).setString("b-" + (i + 1));
        }
        rootWriter.save();
    }
    RowSet result = fixture.wrap(rsLoader.harvest());
    SingleRowSet expected = fixture.rowSetBuilder(result.batchSchema()).addRow(1, "Foo").addRow(2, "b-2").addRow(3, "b-3").addRow(4, "Foo").addRow(5, "b-5").addRow(6, "b-6").addRow(7, "Foo").build();
    RowSetUtilities.verify(expected, result);
    rsLoader.close();
}
Also used : SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ResultSetLoader(org.apache.drill.exec.physical.resultSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) RowSetLoader(org.apache.drill.exec.physical.resultSet.RowSetLoader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Aggregations

ResultSetLoader (org.apache.drill.exec.physical.resultSet.ResultSetLoader)129 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)110 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)110 Test (org.junit.Test)110 SubOperatorTest (org.apache.drill.test.SubOperatorTest)109 RowSetLoader (org.apache.drill.exec.physical.resultSet.RowSetLoader)90 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)81 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)61 ResultSetOptions (org.apache.drill.exec.physical.resultSet.impl.ResultSetLoaderImpl.ResultSetOptions)29 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)25 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)24 SchemaPath (org.apache.drill.common.expression.SchemaPath)23 MockScanBuilder (org.apache.drill.exec.physical.impl.scan.ScanTestUtils.MockScanBuilder)18 ReaderSchemaOrchestrator (org.apache.drill.exec.physical.impl.scan.project.ReaderSchemaOrchestrator)18 ScanSchemaOrchestrator (org.apache.drill.exec.physical.impl.scan.project.ScanSchemaOrchestrator)18 ScanOrchestratorBuilder (org.apache.drill.exec.physical.impl.scan.project.ScanSchemaOrchestrator.ScanOrchestratorBuilder)18 VectorContainer (org.apache.drill.exec.record.VectorContainer)18 RowSetReader (org.apache.drill.exec.physical.rowSet.RowSetReader)17 ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)15 BatchSchemaBuilder (org.apache.drill.exec.record.BatchSchemaBuilder)13