Search in sources :

Example 1 with FieldReader

use of org.apache.drill.exec.vector.complex.reader.FieldReader in project drill by apache.

the class MappifyUtility method mappify.

public static DrillBuf mappify(FieldReader reader, BaseWriter.ComplexWriter writer, DrillBuf buffer) {
    // Currently we expect single map as input
    if (DataMode.REPEATED == reader.getType().getMode() || !(reader.getType().getMinorType() == TypeProtos.MinorType.MAP)) {
        throw new DrillRuntimeException("kvgen function only supports Simple maps as input");
    }
    BaseWriter.ListWriter listWriter = writer.rootAsList();
    listWriter.startList();
    BaseWriter.MapWriter mapWriter = listWriter.map();
    // Iterate over the fields in the map
    Iterator<String> fieldIterator = reader.iterator();
    while (fieldIterator.hasNext()) {
        String str = fieldIterator.next();
        FieldReader fieldReader = reader.reader(str);
        // Skip the field if its null
        if (fieldReader.isSet() == false) {
            mapWriter.end();
            continue;
        }
        // writing a new field, start a new map
        mapWriter.start();
        // write "key":"columnname" into the map
        VarCharHolder vh = new VarCharHolder();
        byte[] b = str.getBytes(Charsets.UTF_8);
        buffer = buffer.reallocIfNeeded(b.length);
        buffer.setBytes(0, b);
        vh.start = 0;
        vh.end = b.length;
        vh.buffer = buffer;
        mapWriter.varChar(fieldKey).write(vh);
        // Write the value to the map
        MapUtility.writeToMapFromReader(fieldReader, mapWriter);
        mapWriter.end();
    }
    listWriter.endList();
    return buffer;
}
Also used : BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader)

Example 2 with FieldReader

use of org.apache.drill.exec.vector.complex.reader.FieldReader in project drill by apache.

the class RepeatedMapReaderImpl method reset.

@Override
public void reset() {
    super.reset();
    currentOffset = 0;
    maxOffset = 0;
    for (FieldReader reader : fields.values()) {
        reader.reset();
    }
    fields.clear();
}
Also used : FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader)

Example 3 with FieldReader

use of org.apache.drill.exec.vector.complex.reader.FieldReader in project drill by axbaretto.

the class MappifyUtility method mappify.

public static DrillBuf mappify(FieldReader reader, BaseWriter.ComplexWriter writer, DrillBuf buffer) {
    // Currently we expect single map as input
    if (DataMode.REPEATED == reader.getType().getMode() || !(reader.getType().getMinorType() == TypeProtos.MinorType.MAP)) {
        throw new DrillRuntimeException("kvgen function only supports Simple maps as input");
    }
    BaseWriter.ListWriter listWriter = writer.rootAsList();
    listWriter.startList();
    BaseWriter.MapWriter mapWriter = listWriter.map();
    // Iterate over the fields in the map
    Iterator<String> fieldIterator = reader.iterator();
    while (fieldIterator.hasNext()) {
        String str = fieldIterator.next();
        FieldReader fieldReader = reader.reader(str);
        // Skip the field if its null
        if (fieldReader.isSet() == false) {
            mapWriter.end();
            continue;
        }
        // writing a new field, start a new map
        mapWriter.start();
        // write "key":"columnname" into the map
        VarCharHolder vh = new VarCharHolder();
        byte[] b = str.getBytes(Charsets.UTF_8);
        buffer = buffer.reallocIfNeeded(b.length);
        buffer.setBytes(0, b);
        vh.start = 0;
        vh.end = b.length;
        vh.buffer = buffer;
        mapWriter.varChar(fieldKey).write(vh);
        // Write the value to the map
        MapUtility.writeToMapFromReader(fieldReader, mapWriter);
        mapWriter.end();
    }
    listWriter.endList();
    return buffer;
}
Also used : BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader)

Example 4 with FieldReader

use of org.apache.drill.exec.vector.complex.reader.FieldReader in project drill by axbaretto.

the class JsonWriter method writeValue.

private void writeValue(FieldReader reader) throws JsonGenerationException, IOException {
    final DataMode m = reader.getType().getMode();
    final MinorType mt = reader.getType().getMinorType();
    switch(m) {
        case OPTIONAL:
        case REQUIRED:
            switch(mt) {
                case FLOAT4:
                    gen.writeFloat(reader);
                    break;
                case FLOAT8:
                    gen.writeDouble(reader);
                    break;
                case INT:
                    gen.writeInt(reader);
                    break;
                case SMALLINT:
                    gen.writeSmallInt(reader);
                    break;
                case TINYINT:
                    gen.writeTinyInt(reader);
                    break;
                case BIGINT:
                    gen.writeBigInt(reader);
                    break;
                case BIT:
                    gen.writeBoolean(reader);
                    break;
                case DATE:
                    gen.writeDate(reader);
                    break;
                case TIME:
                    gen.writeTime(reader);
                    break;
                case TIMESTAMP:
                    gen.writeTimestamp(reader);
                    break;
                case INTERVALYEAR:
                case INTERVALDAY:
                case INTERVAL:
                    gen.writeInterval(reader);
                    break;
                case DECIMAL28DENSE:
                case DECIMAL28SPARSE:
                case DECIMAL38DENSE:
                case DECIMAL38SPARSE:
                case DECIMAL9:
                case DECIMAL18:
                    gen.writeDecimal(reader);
                    break;
                case LIST:
                    // this is a pseudo class, doesn't actually contain the real reader so we have to drop down.
                    gen.writeStartArray();
                    while (reader.next()) {
                        writeValue(reader.reader());
                    }
                    gen.writeEndArray();
                    break;
                case MAP:
                    gen.writeStartObject();
                    if (reader.isSet()) {
                        for (String name : reader) {
                            FieldReader childReader = reader.reader(name);
                            if (childReader.isSet()) {
                                gen.writeFieldName(name);
                                writeValue(childReader);
                            }
                        }
                    }
                    gen.writeEndObject();
                    break;
                case NULL:
                case LATE:
                    gen.writeUntypedNull();
                    break;
                case VAR16CHAR:
                    gen.writeVar16Char(reader);
                    break;
                case VARBINARY:
                    gen.writeBinary(reader);
                    break;
                case VARCHAR:
                    gen.writeVarChar(reader);
                    break;
            }
            break;
        case REPEATED:
            gen.writeStartArray();
            switch(mt) {
                case FLOAT4:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeFloat(i, reader);
                    }
                    break;
                case FLOAT8:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeDouble(i, reader);
                    }
                    break;
                case INT:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeInt(i, reader);
                    }
                    break;
                case SMALLINT:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeSmallInt(i, reader);
                    }
                    break;
                case TINYINT:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeTinyInt(i, reader);
                    }
                    break;
                case BIGINT:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeBigInt(i, reader);
                    }
                    break;
                case BIT:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeBoolean(i, reader);
                    }
                    break;
                case DATE:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeDate(i, reader);
                    }
                    break;
                case TIME:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeTime(i, reader);
                    }
                    break;
                case TIMESTAMP:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeTimestamp(i, reader);
                    }
                    break;
                case INTERVALYEAR:
                case INTERVALDAY:
                case INTERVAL:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeInterval(i, reader);
                    }
                    break;
                case DECIMAL28DENSE:
                case DECIMAL28SPARSE:
                case DECIMAL38DENSE:
                case DECIMAL38SPARSE:
                case DECIMAL9:
                case DECIMAL18:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeDecimal(i, reader);
                    }
                    break;
                case LIST:
                    for (int i = 0; i < reader.size(); i++) {
                        while (reader.next()) {
                            writeValue(reader.reader());
                        }
                    }
                    break;
                case MAP:
                    while (reader.next()) {
                        gen.writeStartObject();
                        for (String name : reader) {
                            FieldReader mapField = reader.reader(name);
                            if (mapField.isSet()) {
                                gen.writeFieldName(name);
                                writeValue(mapField);
                            }
                        }
                        gen.writeEndObject();
                    }
                    break;
                case NULL:
                    break;
                case VAR16CHAR:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeVar16Char(i, reader);
                    }
                    break;
                case VARBINARY:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeBinary(i, reader);
                    }
                    break;
                case VARCHAR:
                    for (int i = 0; i < reader.size(); i++) {
                        gen.writeVarChar(i, reader);
                    }
                    break;
                default:
                    throw new IllegalStateException(String.format("Unable to handle type %s.", mt));
            }
            gen.writeEndArray();
            break;
    }
}
Also used : DataMode(org.apache.drill.common.types.TypeProtos.DataMode) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader)

Example 5 with FieldReader

use of org.apache.drill.exec.vector.complex.reader.FieldReader in project drill by axbaretto.

the class TestBsonRecordReader method testArrayType.

@Test
public void testArrayType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("arrayKey");
    bw.writeStartArray();
    bw.writeInt32(1);
    bw.writeInt32(2);
    bw.writeInt32(3);
    bw.writeEndArray();
    bw.writeEndDocument();
    bw.flush();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    FieldReader reader = mapReader.reader("arrayKey");
    assertEquals(3, reader.size());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) Test(org.junit.Test)

Aggregations

FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)30 Test (org.junit.Test)8 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)6 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)6 BaseWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter)6 BsonDocument (org.bson.BsonDocument)6 BsonDocumentReader (org.bson.BsonDocumentReader)6 ValueVector (org.apache.drill.exec.vector.ValueVector)5 VarCharHolder (org.apache.drill.exec.expr.holders.VarCharHolder)4 BaseTest (org.apache.drill.test.BaseTest)4 BsonDocumentWriter (org.bson.BsonDocumentWriter)4 BsonWriter (org.bson.BsonWriter)4 DataMode (org.apache.drill.common.types.TypeProtos.DataMode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Annotation (java.lang.annotation.Annotation)2 Inject (javax.inject.Inject)2 FieldDescriptor (org.apache.drill.common.scanner.persistence.FieldDescriptor)2 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)2