Search in sources :

Example 1 with ScalarElementReader

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

the class TestResultSetLoaderOverflow method testSizeLimitOnArray.

/**
 * Test a row with a single array column which overflows. Verifies
 * that all the fiddly bits about offset vectors and so on works
 * correctly. Run this test (the simplest case) if you change anything
 * about the array handling code.
 */
@Test
public void testSizeLimitOnArray() {
    TupleMetadata schema = new SchemaBuilder().addArray("s", MinorType.VARCHAR).buildSchema();
    ResultSetOptions options = new OptionBuilder().setRowCountLimit(ValueVector.MAX_ROW_COUNT).setSchema(schema).build();
    ResultSetLoader rsLoader = new ResultSetLoaderImpl(fixture.allocator(), options);
    RowSetLoader rootWriter = rsLoader.writer();
    // Fill batch with rows of with a single array, three values each. Tack on
    // a suffix to each so we can be sure the proper data is written and moved
    // to the overflow batch.
    rsLoader.startBatch();
    byte[] value = new byte[473];
    Arrays.fill(value, (byte) 'X');
    String strValue = new String(value, Charsets.UTF_8);
    int count = 0;
    int rowSize = 0;
    int totalSize = 0;
    int valuesPerArray = 13;
    while (rootWriter.start()) {
        totalSize += rowSize;
        rowSize = 0;
        ScalarWriter array = rootWriter.array(0).scalar();
        for (int i = 0; i < valuesPerArray; i++) {
            String cellValue = strValue + (count + 1) + "." + i;
            array.setString(cellValue);
            rowSize += cellValue.length();
        }
        rootWriter.save();
        count++;
    }
    // Row count should include the overflow row.
    int expectedCount = count - 1;
    // Size without overflow row should fit in the vector, size
    // with overflow should not.
    assertTrue(totalSize <= ValueVector.MAX_BUFFER_SIZE);
    assertTrue(totalSize + rowSize > ValueVector.MAX_BUFFER_SIZE);
    // Result should exclude the overflow row. Last row
    // should hold the last full array.
    RowSet result = fixture.wrap(rsLoader.harvest());
    assertEquals(expectedCount, result.rowCount());
    RowSetReader reader = result.reader();
    reader.set(expectedCount - 1);
    ScalarElementReader arrayReader = reader.column(0).elements();
    assertEquals(valuesPerArray, arrayReader.size());
    for (int i = 0; i < valuesPerArray; i++) {
        String cellValue = strValue + (count - 1) + "." + i;
        assertEquals(cellValue, arrayReader.getString(i));
    }
    result.clear();
    // Next batch should start with the overflow row.
    // The only row in this next batch should be the whole
    // array being written at the time of overflow.
    rsLoader.startBatch();
    // VectorPrinter.printStrings((VarCharVector) ((VarCharColumnWriter) rootWriter.array(0).scalar()).vector(), 0, 5);
    // ((ResultSetLoaderImpl) rsLoader).dump(new HierarchicalPrinter());
    assertEquals(1, rootWriter.rowCount());
    assertEquals(expectedCount + 1, rsLoader.totalRowCount());
    result = fixture.wrap(rsLoader.harvest());
    // VectorPrinter.printStrings((VarCharVector) ((VarCharColumnWriter) rootWriter.array(0).scalar()).vector(), 0, 5);
    assertEquals(1, result.rowCount());
    reader = result.reader();
    reader.next();
    arrayReader = reader.column(0).elements();
    assertEquals(valuesPerArray, arrayReader.size());
    for (int i = 0; i < valuesPerArray; i++) {
        String cellValue = strValue + (count) + "." + i;
        assertEquals(cellValue, arrayReader.getString(i));
    }
    result.clear();
    rsLoader.close();
}
Also used : ScalarElementReader(org.apache.drill.exec.vector.accessor.ScalarElementReader) RowSet(org.apache.drill.test.rowSet.RowSet) ResultSetOptions(org.apache.drill.exec.physical.rowSet.impl.ResultSetLoaderImpl.ResultSetOptions) ResultSetLoader(org.apache.drill.exec.physical.rowSet.ResultSetLoader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) RowSetLoader(org.apache.drill.exec.physical.rowSet.RowSetLoader) 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 2 with ScalarElementReader

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

the class TestScalarAccessors method decimalArrayTester.

private void decimalArrayTester(MinorType type, int precision) {
    MajorType majorType = MajorType.newBuilder().setMinorType(type).setScale(3).setPrecision(precision).setMode(DataMode.REPEATED).build();
    BatchSchema batchSchema = new SchemaBuilder().add("col", majorType).build();
    BigDecimal v1 = BigDecimal.ZERO;
    BigDecimal v2 = BigDecimal.valueOf(123_456_789, 3);
    BigDecimal v3 = BigDecimal.TEN;
    SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new BigDecimal[] {}).addSingleCol(new BigDecimal[] { v1, v2, v3 }).build();
    assertEquals(2, rs.rowCount());
    RowSetReader reader = rs.reader();
    ScalarElementReader colReader = reader.elements(0);
    assertEquals(ValueType.DECIMAL, colReader.valueType());
    assertTrue(reader.next());
    assertEquals(0, colReader.size());
    assertTrue(reader.next());
    assertEquals(3, colReader.size());
    assertEquals(0, v1.compareTo(colReader.getDecimal(0)));
    assertEquals(0, v2.compareTo(colReader.getDecimal(1)));
    assertEquals(0, v3.compareTo(colReader.getDecimal(2)));
    assertEquals(0, v2.compareTo((BigDecimal) colReader.getObject(1)));
    assertEquals(v2.toString(), colReader.getAsString(1));
    assertFalse(reader.next());
    rs.clear();
}
Also used : SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) BatchSchema(org.apache.drill.exec.record.BatchSchema) ScalarElementReader(org.apache.drill.exec.vector.accessor.ScalarElementReader) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) BigDecimal(java.math.BigDecimal)

Example 3 with ScalarElementReader

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

the class TestScalarAccessors method longArrayTester.

private void longArrayTester(MinorType type) {
    BatchSchema batchSchema = new SchemaBuilder().addArray("col", type).build();
    SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new long[] {}).addSingleCol(new long[] { 0, 20, 30 }).build();
    assertEquals(2, rs.rowCount());
    RowSetReader reader = rs.reader();
    ScalarElementReader colReader = reader.elements(0);
    assertEquals(ValueType.LONG, colReader.valueType());
    assertTrue(reader.next());
    assertEquals(0, colReader.size());
    assertTrue(reader.next());
    assertEquals(3, colReader.size());
    assertEquals(0, colReader.getLong(0));
    assertEquals(20, colReader.getLong(1));
    assertEquals(30, colReader.getLong(2));
    assertEquals(0L, colReader.getObject(0));
    assertEquals(20L, colReader.getObject(1));
    assertEquals(30L, colReader.getObject(2));
    assertEquals("0", colReader.getAsString(0));
    assertEquals("20", colReader.getAsString(1));
    assertEquals("30", colReader.getAsString(2));
    assertFalse(reader.next());
    rs.clear();
}
Also used : SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) BatchSchema(org.apache.drill.exec.record.BatchSchema) ScalarElementReader(org.apache.drill.exec.vector.accessor.ScalarElementReader) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) RowSetReader(org.apache.drill.test.rowSet.RowSetReader)

Example 4 with ScalarElementReader

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

the class TestScalarAccessors method testIntervalArray.

@Test
public void testIntervalArray() {
    BatchSchema batchSchema = new SchemaBuilder().addArray("col", MinorType.INTERVAL).build();
    Period p1 = Period.days(0);
    Period p2 = Period.years(7).plusMonths(8).plusDays(3).plusHours(4).plusMinutes(5).plusSeconds(23);
    Period p3 = Period.years(9999).plusMonths(11).plusDays(365).plusHours(23).plusMinutes(59).plusSeconds(59);
    SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new Period[] {}).addSingleCol(new Period[] { p1, p2, p3 }).build();
    assertEquals(2, rs.rowCount());
    RowSetReader reader = rs.reader();
    ScalarElementReader colReader = reader.elements(0);
    assertEquals(ValueType.PERIOD, colReader.valueType());
    assertTrue(reader.next());
    assertEquals(0, colReader.size());
    assertTrue(reader.next());
    assertEquals(3, colReader.size());
    assertEquals(p1, colReader.getPeriod(0).normalizedStandard());
    assertEquals(p2, colReader.getPeriod(1).normalizedStandard());
    assertEquals(p3.normalizedStandard(), colReader.getPeriod(2).normalizedStandard());
    assertEquals(p2, ((Period) colReader.getObject(1)).normalizedStandard());
    assertEquals(p2.toString(), colReader.getAsString(1));
    assertFalse(reader.next());
    rs.clear();
}
Also used : SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) BatchSchema(org.apache.drill.exec.record.BatchSchema) ScalarElementReader(org.apache.drill.exec.vector.accessor.ScalarElementReader) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) Period(org.joda.time.Period) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 5 with ScalarElementReader

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

the class TestScalarAccessors method testStringArray.

@Test
public void testStringArray() {
    BatchSchema batchSchema = new SchemaBuilder().addArray("col", MinorType.VARCHAR).build();
    SingleRowSet rs = fixture.rowSetBuilder(batchSchema).addSingleCol(new String[] {}).addSingleCol(new String[] { "fred", "", "wilma" }).build();
    assertEquals(2, rs.rowCount());
    RowSetReader reader = rs.reader();
    ScalarElementReader colReader = reader.elements(0);
    assertEquals(ValueType.STRING, colReader.valueType());
    assertTrue(reader.next());
    assertEquals(0, colReader.size());
    assertTrue(reader.next());
    assertEquals(3, colReader.size());
    assertEquals("fred", colReader.getString(0));
    assertEquals("", colReader.getString(1));
    assertEquals("wilma", colReader.getString(2));
    assertEquals("fred", colReader.getObject(0));
    assertEquals("", colReader.getObject(1));
    assertEquals("wilma", colReader.getObject(2));
    assertEquals("\"fred\"", colReader.getAsString(0));
    assertEquals("\"\"", colReader.getAsString(1));
    assertEquals("\"wilma\"", colReader.getAsString(2));
    assertFalse(reader.next());
    rs.clear();
}
Also used : SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) BatchSchema(org.apache.drill.exec.record.BatchSchema) ScalarElementReader(org.apache.drill.exec.vector.accessor.ScalarElementReader) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Aggregations

ScalarElementReader (org.apache.drill.exec.vector.accessor.ScalarElementReader)17 RowSetReader (org.apache.drill.test.rowSet.RowSetReader)17 SchemaBuilder (org.apache.drill.test.rowSet.schema.SchemaBuilder)17 SingleRowSet (org.apache.drill.test.rowSet.RowSet.SingleRowSet)14 SubOperatorTest (org.apache.drill.test.SubOperatorTest)12 Test (org.junit.Test)12 BatchSchema (org.apache.drill.exec.record.BatchSchema)10 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)8 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)7 ResultSetLoader (org.apache.drill.exec.physical.rowSet.ResultSetLoader)4 RowSetLoader (org.apache.drill.exec.physical.rowSet.RowSetLoader)4 RowSet (org.apache.drill.test.rowSet.RowSet)4 ExtendableRowSet (org.apache.drill.test.rowSet.RowSet.ExtendableRowSet)4 RowSetWriter (org.apache.drill.test.rowSet.RowSetWriter)4 ResultSetOptions (org.apache.drill.exec.physical.rowSet.impl.ResultSetLoaderImpl.ResultSetOptions)3 RowSetComparison (org.apache.drill.test.rowSet.RowSetComparison)3 Period (org.joda.time.Period)3 ScalarReader (org.apache.drill.exec.vector.accessor.ScalarReader)2 TupleReader (org.apache.drill.exec.vector.accessor.TupleReader)2 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)2