Search in sources :

Example 1 with ValueType

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

the class TestFillEmpties method doFillEmptiesScalar.

private void doFillEmptiesScalar(MajorType majorType) {
    TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
    ExtendableRowSet rs = fixture.rowSet(schema);
    RowSetWriter writer = rs.writer();
    ScalarWriter colWriter = writer.scalar(0);
    ValueType valueType = colWriter.valueType();
    boolean nullable = majorType.getMode() == DataMode.OPTIONAL;
    for (int i = 0; i < ROW_COUNT; i++) {
        if (i % 5 == 0) {
            colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
        }
        writer.save();
    }
    SingleRowSet result = writer.done();
    RowSetReader reader = result.reader();
    ScalarReader colReader = reader.scalar(0);
    MinorType type = majorType.getMinorType();
    boolean isVariable = (type == MinorType.VARCHAR || type == MinorType.VAR16CHAR || type == MinorType.VARBINARY);
    for (int i = 0; i < ROW_COUNT; i++) {
        assertTrue(reader.next());
        if (i % 5 != 0) {
            if (nullable) {
                // Nullable types fill with nulls.
                assertTrue(colReader.isNull());
                continue;
            }
            if (isVariable) {
                // Variable width types fill with a zero-length value.
                assertEquals(0, colReader.getBytes().length);
                continue;
            }
        }
        // All other types fill with zero-bytes, interpreted as some form
        // of zero for each type.
        Object actual = colReader.getObject();
        Object expected = RowSetUtilities.testDataFromInt(valueType, majorType, i % 5 == 0 ? i : 0);
        RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "]", valueType, expected, actual);
    }
    result.clear();
}
Also used : RowSetWriter(org.apache.drill.test.rowSet.RowSetWriter) SingleRowSet(org.apache.drill.test.rowSet.RowSet.SingleRowSet) ValueType(org.apache.drill.exec.vector.accessor.ValueType) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.test.rowSet.schema.SchemaBuilder) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) ExtendableRowSet(org.apache.drill.test.rowSet.RowSet.ExtendableRowSet)

Example 2 with ValueType

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

the class JsonFileBuilder method build.

public void build(File tableFile) throws IOException {
    tableFile.getParentFile().mkdirs();
    try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(tableFile))) {
        final RowSetReader reader = rowSet.reader();
        final int numCols = rowSet.batchSchema().getFieldCount();
        final Iterator<MaterializedField> fieldIterator = rowSet.batchSchema().iterator();
        final List<String> columnNames = Lists.newArrayList();
        final List<String> columnFormatters = Lists.newArrayList();
        // Build formatters from first row.
        while (fieldIterator.hasNext()) {
            final String columnName = fieldIterator.next().getName();
            final ScalarReader columnReader = reader.scalar(columnName);
            final ValueType valueType = columnReader.valueType();
            final String columnFormatter;
            if (customFormatters.containsKey(columnName)) {
                columnFormatter = customFormatters.get(columnName);
            } else if (DEFAULT_FORMATTERS.containsKey(valueType)) {
                columnFormatter = DEFAULT_FORMATTERS.get(valueType);
            } else {
                final String message = String.format("Unsupported column type %s", valueType);
                throw new UnsupportedOperationException(message);
            }
            columnNames.add(columnName);
            columnFormatters.add(columnFormatter);
        }
        final StringBuilder sb = new StringBuilder();
        String lineSeparator = "";
        for (int index = 0; index < rowSet.rowCount(); index++) {
            reader.next();
            sb.append(lineSeparator);
            sb.append('{');
            String separator = "";
            for (int columnIndex = 0; columnIndex < numCols; columnIndex++) {
                sb.append(separator);
                final String columnName = columnNames.get(columnIndex);
                final ScalarReader columnReader = reader.scalar(columnIndex);
                final String columnFormatter = columnFormatters.get(columnIndex);
                final Object columnObject = columnReader.getObject();
                final String columnString = String.format(columnFormatter, columnObject);
                sb.append('"').append(columnName).append('"').append(':').append(columnString);
                separator = ",";
            }
            sb.append('}');
            lineSeparator = "\n";
            os.write(sb.toString().getBytes());
            sb.delete(0, sb.length());
        }
    }
}
Also used : ValueType(org.apache.drill.exec.vector.accessor.ValueType) MaterializedField(org.apache.drill.exec.record.MaterializedField) ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) FileOutputStream(java.io.FileOutputStream) RowSetReader(org.apache.drill.test.rowSet.RowSetReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with ValueType

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

the class TestFillEmpties method testDefaultInSchema.

/**
 * Test the more typical case in which the default value is set in the
 * column metadata. The reader mechanism will automatically set the default
 * for the column writer from the (properly formed) default value in the
 * column metadata.
 */
@Test
public void testDefaultInSchema() {
    TupleMetadata schema = new SchemaBuilder().add("a", MinorType.INT).buildSchema();
    schema.metadata("a").setDefaultValue("11");
    ExtendableRowSet rs = fixture.rowSet(schema);
    RowSetWriter writer = rs.writer();
    ScalarWriter colWriter = writer.scalar(0);
    ValueType valueType = colWriter.extendedType();
    for (int i = 0; i < ROW_COUNT; i++) {
        if (i % 5 == 0) {
            colWriter.setInt(i);
        }
        writer.save();
    }
    SingleRowSet result = writer.done();
    RowSetReader reader = result.reader();
    ScalarReader colReader = reader.scalar(0);
    Object defaultValue = schema.metadata("a").decodeDefaultValue();
    assertNotNull(defaultValue);
    for (int i = 0; i < ROW_COUNT; i++) {
        assertTrue(reader.next());
        Object actual = colReader.getValue();
        Object expected = i % 5 == 0 ? i : defaultValue;
        RowSetUtilities.assertEqualValues(MinorType.INT.toString() + "[" + i + "]", valueType, expected, actual);
    }
    result.clear();
}
Also used : ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ValueType(org.apache.drill.exec.vector.accessor.ValueType) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 4 with ValueType

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

the class TestFillEmpties method dofillEmptiesRepeated.

private void dofillEmptiesRepeated(MajorType majorType) {
    TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
    ExtendableRowSet rs = fixture.rowSet(schema);
    RowSetWriter writer = rs.writer();
    ScalarWriter colWriter = writer.array(0).scalar();
    ValueType valueType = colWriter.valueType();
    for (int i = 0; i < ROW_COUNT; i++) {
        if (i % 5 == 0) {
            // Write two values so we can exercise a bit of the array logic.
            colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
            colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i + 1));
        }
        writer.save();
    }
    SingleRowSet result = writer.done();
    RowSetReader reader = result.reader();
    ArrayReader aReader = reader.array(0);
    ScalarReader colReader = aReader.scalar();
    for (int i = 0; i < ROW_COUNT; i++) {
        assertTrue(reader.next());
        if (i % 5 != 0) {
            // Empty arrays are defined to be the same as a zero-length array.
            assertEquals(0, aReader.size());
        } else {
            for (int j = 0; j < 2; j++) {
                assertTrue(aReader.next());
                Object actual = colReader.getObject();
                Object expected = RowSetUtilities.testDataFromInt(valueType, majorType, i + j);
                RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "][" + j + "]", valueType, expected, actual);
            }
        }
    }
    result.clear();
}
Also used : ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) ArrayReader(org.apache.drill.exec.vector.accessor.ArrayReader) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ValueType(org.apache.drill.exec.vector.accessor.ValueType) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)

Example 5 with ValueType

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

the class TestFillEmpties method doTestDefaultValue.

private void doTestDefaultValue(MajorType majorType) {
    TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
    ExtendableRowSet rs = fixture.rowSet(schema);
    RowSetWriter writer = rs.writer();
    ScalarWriter colWriter = writer.scalar(0);
    ValueType valueType = colWriter.extendedType();
    Object defaultValue = RowSetUtilities.testDataFromInt(valueType, majorType, 100);
    colWriter.setDefaultValue(defaultValue);
    for (int i = 0; i < ROW_COUNT; i++) {
        if (i % 5 == 0) {
            colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
        }
        writer.save();
    }
    SingleRowSet result = writer.done();
    RowSetReader reader = result.reader();
    ScalarReader colReader = reader.scalar(0);
    for (int i = 0; i < ROW_COUNT; i++) {
        assertTrue(reader.next());
        Object actual = colReader.getValue();
        Object expected = i % 5 == 0 ? RowSetUtilities.testDataFromInt(valueType, majorType, i) : defaultValue;
        RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "]", valueType, expected, actual);
    }
    result.clear();
}
Also used : ScalarReader(org.apache.drill.exec.vector.accessor.ScalarReader) SingleRowSet(org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet) ValueType(org.apache.drill.exec.vector.accessor.ValueType) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) ScalarWriter(org.apache.drill.exec.vector.accessor.ScalarWriter) ExtendableRowSet(org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)

Aggregations

ValueType (org.apache.drill.exec.vector.accessor.ValueType)7 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)6 ScalarReader (org.apache.drill.exec.vector.accessor.ScalarReader)6 ScalarWriter (org.apache.drill.exec.vector.accessor.ScalarWriter)6 ExtendableRowSet (org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)4 SingleRowSet (org.apache.drill.exec.physical.rowSet.RowSet.SingleRowSet)4 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)4 RowSetReader (org.apache.drill.test.rowSet.RowSetReader)3 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)2 ExtendableRowSet (org.apache.drill.test.rowSet.RowSet.ExtendableRowSet)2 SingleRowSet (org.apache.drill.test.rowSet.RowSet.SingleRowSet)2 RowSetWriter (org.apache.drill.test.rowSet.RowSetWriter)2 SchemaBuilder (org.apache.drill.test.rowSet.schema.SchemaBuilder)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 MaterializedField (org.apache.drill.exec.record.MaterializedField)1 ArrayReader (org.apache.drill.exec.vector.accessor.ArrayReader)1 ScalarElementReader (org.apache.drill.exec.vector.accessor.ScalarElementReader)1 SubOperatorTest (org.apache.drill.test.SubOperatorTest)1 Test (org.junit.Test)1