Search in sources :

Example 1 with ListWriter

use of org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter in project drill by apache.

the class JsonReader method ensureAtLeastOneField.

@SuppressWarnings("resource")
@Override
public void ensureAtLeastOneField(ComplexWriter writer) {
    List<BaseWriter.MapWriter> writerList = Lists.newArrayList();
    List<PathSegment> fieldPathList = Lists.newArrayList();
    BitSet emptyStatus = new BitSet(columns.size());
    // first pass: collect which fields are empty
    for (int i = 0; i < columns.size(); i++) {
        SchemaPath sp = columns.get(i);
        PathSegment fieldPath = sp.getRootSegment();
        BaseWriter.MapWriter fieldWriter = writer.rootAsMap();
        while (fieldPath.getChild() != null && !fieldPath.getChild().isArray()) {
            fieldWriter = fieldWriter.map(fieldPath.getNameSegment().getPath());
            fieldPath = fieldPath.getChild();
        }
        writerList.add(fieldWriter);
        fieldPathList.add(fieldPath);
        if (fieldWriter.isEmptyMap()) {
            emptyStatus.set(i, true);
        }
        if (i == 0 && !allTextMode) {
            // avoid schema change exceptions by downstream operators.
            break;
        }
    }
    // so we rely on the emptyStatus.
    for (int j = 0; j < fieldPathList.size(); j++) {
        BaseWriter.MapWriter fieldWriter = writerList.get(j);
        PathSegment fieldPath = fieldPathList.get(j);
        if (emptyStatus.get(j)) {
            if (allTextMode) {
                fieldWriter.varChar(fieldPath.getNameSegment().getPath());
            } else {
                fieldWriter.integer(fieldPath.getNameSegment().getPath());
            }
        }
    }
    for (ListWriter field : emptyArrayWriters) {
        // checks that array has not been initialized
        if (field.getValueCapacity() == 0) {
            if (allTextMode) {
                field.varChar();
            } else {
                field.integer();
            }
        }
    }
}
Also used : MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) SchemaPath(org.apache.drill.common.expression.SchemaPath) MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) BitSet(java.util.BitSet) PathSegment(org.apache.drill.common.expression.PathSegment)

Example 2 with ListWriter

use of org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter in project drill by apache.

the class TestScanBatchWriters method sanityTest.

@Test
public void sanityTest() throws Exception {
    Scan scanConfig = new AbstractSubScan("bob") {

        @Override
        public String getOperatorType() {
            return "";
        }
    };
    OperatorContext opContext = fixture.newOperatorContext(scanConfig);
    // Setup: normally done by ScanBatch
    VectorContainer container = new VectorContainer(fixture.allocator());
    OutputMutator output = new ScanBatch.Mutator(opContext, fixture.allocator(), container);
    DrillBuf buffer = opContext.getManagedBuffer();
    try (VectorContainerWriter writer = new VectorContainerWriter(output)) {
        // Per-batch
        writer.allocate();
        writer.reset();
        BaseWriter.MapWriter map = writer.rootAsMap();
        // Write one record (10, "Fred", [100, 110, 120] )
        map.integer("a").writeInt(10);
        byte[] bytes = "Fred".getBytes("UTF-8");
        buffer.setBytes(0, bytes, 0, bytes.length);
        map.varChar("b").writeVarChar(0, bytes.length, buffer);
        try (ListWriter list = map.list("c")) {
            list.startList();
            list.integer().writeInt(100);
            list.integer().writeInt(110);
            list.integer().writeInt(120);
            list.endList();
            // Write another record: (20, "Wilma", [])
            writer.setPosition(1);
            map.integer("a").writeInt(20);
            bytes = "Wilma".getBytes("UTF-8");
            buffer.setBytes(0, bytes, 0, bytes.length);
            map.varChar("b").writeVarChar(0, bytes.length, buffer);
            writer.setValueCount(2);
            // Wrap-up done by ScanBatch
            container.setRecordCount(2);
            container.buildSchema(SelectionVectorMode.NONE);
            RowSet rowSet = fixture.wrap(container);
            // Expected
            TupleMetadata schema = new SchemaBuilder().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).addArray("c", MinorType.INT).buildSchema();
            RowSet expected = fixture.rowSetBuilder(schema).addRow(10, "Fred", new int[] { 100, 110, 120 }).addRow(20, "Wilma", null).build();
            new RowSetComparison(expected).verifyAndClearAll(rowSet);
        }
    } finally {
        opContext.close();
    }
}
Also used : AbstractSubScan(org.apache.drill.exec.physical.base.AbstractSubScan) VectorContainerWriter(org.apache.drill.exec.vector.complex.impl.VectorContainerWriter) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) OutputMutator(org.apache.drill.exec.physical.impl.OutputMutator) VectorContainer(org.apache.drill.exec.record.VectorContainer) RowSetComparison(org.apache.drill.test.rowSet.RowSetComparison) OutputMutator(org.apache.drill.exec.physical.impl.OutputMutator) OperatorContext(org.apache.drill.exec.ops.OperatorContext) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) TupleMetadata(org.apache.drill.exec.record.metadata.TupleMetadata) SchemaBuilder(org.apache.drill.exec.record.metadata.SchemaBuilder) Scan(org.apache.drill.exec.physical.base.Scan) AbstractSubScan(org.apache.drill.exec.physical.base.AbstractSubScan) DrillBuf(io.netty.buffer.DrillBuf) SubOperatorTest(org.apache.drill.test.SubOperatorTest) Test(org.junit.Test)

Example 3 with ListWriter

use of org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter in project drill by apache.

the class DrillParquetGroupConverter method getConverterForType.

protected PrimitiveConverter getConverterForType(String name, PrimitiveType type) {
    switch(type.getPrimitiveTypeName()) {
        case INT32:
            {
                if (type.getOriginalType() == null) {
                    return getIntConverter(name, type);
                }
                switch(type.getOriginalType()) {
                    case UINT_8:
                    case UINT_16:
                    case UINT_32:
                    case INT_8:
                    case INT_16:
                    case INT_32:
                        {
                            return getIntConverter(name, type);
                        }
                    case DECIMAL:
                        {
                            ParquetReaderUtility.checkDecimalTypeEnabled(options);
                            return getVarDecimalConverter(name, type);
                        }
                    case DATE:
                        {
                            DateWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).date(), l -> l.list().date()) : getWriter(name, (m, f) -> m.date(f), l -> l.date());
                            switch(containsCorruptedDates) {
                                case META_SHOWS_CORRUPTION:
                                    return new DrillCorruptedDateConverter(writer);
                                case META_SHOWS_NO_CORRUPTION:
                                    return new DrillDateConverter(writer);
                                case META_UNCLEAR_TEST_VALUES:
                                    return new CorruptionDetectingDateConverter(writer);
                                default:
                                    throw new DrillRuntimeException(String.format("Issue setting up parquet reader for date type, " + "unrecognized date corruption status %s. See DRILL-4203 for more info.", containsCorruptedDates));
                            }
                        }
                    case TIME_MILLIS:
                        {
                            TimeWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).time(), l -> l.list().time()) : getWriter(name, (m, f) -> m.time(f), l -> l.time());
                            return new DrillTimeConverter(writer);
                        }
                    default:
                        {
                            throw new UnsupportedOperationException("Unsupported type: " + type.getOriginalType());
                        }
                }
            }
        case INT64:
            {
                if (type.getOriginalType() == null) {
                    return getBigIntConverter(name, type);
                }
                switch(type.getOriginalType()) {
                    case UINT_64:
                    case INT_64:
                        return getBigIntConverter(name, type);
                    case TIMESTAMP_MICROS:
                        {
                            TimeStampWriter writer = getTimeStampWriter(name, type);
                            return new DrillTimeStampMicrosConverter(writer);
                        }
                    case TIME_MICROS:
                        {
                            TimeWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).time(), l -> l.list().time()) : getWriter(name, MapWriter::time, ListWriter::time);
                            return new DrillTimeMicrosConverter(writer);
                        }
                    case DECIMAL:
                        {
                            ParquetReaderUtility.checkDecimalTypeEnabled(options);
                            return getVarDecimalConverter(name, type);
                        }
                    case TIMESTAMP_MILLIS:
                        {
                            TimeStampWriter writer = getTimeStampWriter(name, type);
                            return new DrillTimeStampConverter(writer);
                        }
                    default:
                        {
                            throw new UnsupportedOperationException("Unsupported type " + type.getOriginalType());
                        }
                }
            }
        case INT96:
            {
                // TODO: replace null with TIMESTAMP_NANOS once parquet support such type annotation.
                if (type.getOriginalType() == null) {
                    if (options.getOption(ExecConstants.PARQUET_READER_INT96_AS_TIMESTAMP).bool_val) {
                        TimeStampWriter writer = getTimeStampWriter(name, type);
                        return new DrillFixedBinaryToTimeStampConverter(writer);
                    } else {
                        VarBinaryWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).varBinary(), l -> l.list().varBinary()) : getWriter(name, (m, f) -> m.varBinary(f), listWriter -> listWriter.varBinary());
                        return new DrillFixedBinaryToVarbinaryConverter(writer, ParquetColumnMetadata.getTypeLengthInBits(type.getPrimitiveTypeName()) / 8, mutator.getManagedBuffer());
                    }
                }
            }
        case FLOAT:
            {
                Float4Writer writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).float4(), l -> l.list().float4()) : getWriter(name, (m, f) -> m.float4(f), l -> l.float4());
                return new DrillFloat4Converter(writer);
            }
        case DOUBLE:
            {
                Float8Writer writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).float8(), l -> l.list().float8()) : getWriter(name, (m, f) -> m.float8(f), l -> l.float8());
                return new DrillFloat8Converter(writer);
            }
        case BOOLEAN:
            {
                BitWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).bit(), l -> l.list().bit()) : getWriter(name, (m, f) -> m.bit(f), l -> l.bit());
                return new DrillBoolConverter(writer);
            }
        case BINARY:
            {
                LogicalTypeAnnotation.LogicalTypeAnnotationVisitor<PrimitiveConverter> typeAnnotationVisitor = new LogicalTypeAnnotation.LogicalTypeAnnotationVisitor<PrimitiveConverter>() {

                    @Override
                    public Optional<PrimitiveConverter> visit(LogicalTypeAnnotation.DecimalLogicalTypeAnnotation decimalLogicalType) {
                        ParquetReaderUtility.checkDecimalTypeEnabled(options);
                        return Optional.of(getVarDecimalConverter(name, type));
                    }

                    @Override
                    public Optional<PrimitiveConverter> visit(LogicalTypeAnnotation.StringLogicalTypeAnnotation stringLogicalType) {
                        return Optional.of(getVarCharConverter(name, type));
                    }

                    @Override
                    public Optional<PrimitiveConverter> visit(LogicalTypeAnnotation.EnumLogicalTypeAnnotation stringLogicalType) {
                        return Optional.of(getVarCharConverter(name, type));
                    }
                };
                Supplier<PrimitiveConverter> converterSupplier = () -> {
                    VarBinaryWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).varBinary(), l -> l.list().varBinary()) : getWriter(name, MapWriter::varBinary, ListWriter::varBinary);
                    return new DrillVarBinaryConverter(writer, mutator.getManagedBuffer());
                };
                return Optional.ofNullable(type.getLogicalTypeAnnotation()).map(typeAnnotation -> typeAnnotation.accept(typeAnnotationVisitor)).flatMap(Function.identity()).orElseGet(converterSupplier);
            }
        case FIXED_LEN_BYTE_ARRAY:
            LogicalTypeAnnotation.LogicalTypeAnnotationVisitor<PrimitiveConverter> typeAnnotationVisitor = new LogicalTypeAnnotation.LogicalTypeAnnotationVisitor<PrimitiveConverter>() {

                @Override
                public Optional<PrimitiveConverter> visit(LogicalTypeAnnotation.DecimalLogicalTypeAnnotation decimalLogicalType) {
                    ParquetReaderUtility.checkDecimalTypeEnabled(options);
                    return Optional.of(getVarDecimalConverter(name, type));
                }

                @Override
                public Optional<PrimitiveConverter> visit(LogicalTypeAnnotation.IntervalLogicalTypeAnnotation intervalLogicalType) {
                    IntervalWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).interval(), l -> l.list().interval()) : getWriter(name, MapWriter::interval, ListWriter::interval);
                    return Optional.of(new DrillFixedLengthByteArrayToInterval(writer));
                }
            };
            Supplier<PrimitiveConverter> converterSupplier = () -> {
                VarBinaryWriter writer = type.isRepetition(Repetition.REPEATED) ? getWriter(name, (m, f) -> m.list(f).varBinary(), l -> l.list().varBinary()) : getWriter(name, MapWriter::varBinary, ListWriter::varBinary);
                return new DrillFixedBinaryToVarbinaryConverter(writer, type.getTypeLength(), mutator.getManagedBuffer());
            };
            return Optional.ofNullable(type.getLogicalTypeAnnotation()).map(typeAnnotation -> typeAnnotation.accept(typeAnnotationVisitor)).flatMap(Function.identity()).orElseGet(converterSupplier);
        default:
            throw new UnsupportedOperationException("Unsupported type: " + type.getPrimitiveTypeName());
    }
}
Also used : IntervalHolder(org.apache.drill.exec.expr.holders.IntervalHolder) PrimitiveType(org.apache.parquet.schema.PrimitiveType) VarDecimalHolder(org.apache.drill.exec.expr.holders.VarDecimalHolder) SingleMapWriter(org.apache.drill.exec.vector.complex.impl.SingleMapWriter) BiFunction(java.util.function.BiFunction) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) ParquetReaderUtility(org.apache.drill.exec.store.parquet.ParquetReaderUtility) OutputMutator(org.apache.drill.exec.physical.impl.OutputMutator) VarCharWriter(org.apache.drill.exec.vector.complex.writer.VarCharWriter) PathSegment(org.apache.drill.common.expression.PathSegment) TimeStampWriter(org.apache.drill.exec.vector.complex.writer.TimeStampWriter) PrimitiveConverter(org.apache.parquet.io.api.PrimitiveConverter) DateHolder(org.apache.drill.exec.expr.holders.DateHolder) DrillBuf(io.netty.buffer.DrillBuf) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) VarBinaryWriter(org.apache.drill.exec.vector.complex.writer.VarBinaryWriter) BigIntWriter(org.apache.drill.exec.vector.complex.writer.BigIntWriter) AbstractRepeatedMapWriter(org.apache.drill.exec.vector.complex.impl.AbstractRepeatedMapWriter) IntWriter(org.apache.drill.exec.vector.complex.writer.IntWriter) GroupType(org.apache.parquet.schema.GroupType) GroupConverter(org.apache.parquet.io.api.GroupConverter) VarDecimalWriter(org.apache.drill.exec.vector.complex.writer.VarDecimalWriter) Collection(java.util.Collection) SchemaPath(org.apache.drill.common.expression.SchemaPath) Float4Writer(org.apache.drill.exec.vector.complex.writer.Float4Writer) TimeWriter(org.apache.drill.exec.vector.complex.writer.TimeWriter) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) Binary(org.apache.parquet.io.api.Binary) Longs(org.apache.drill.shaded.guava.com.google.common.primitives.Longs) List(java.util.List) DYNAMIC_STAR(org.apache.drill.common.expression.SchemaPath.DYNAMIC_STAR) Optional(java.util.Optional) Type(org.apache.parquet.schema.Type) ExecConstants(org.apache.drill.exec.ExecConstants) DateWriter(org.apache.drill.exec.vector.complex.writer.DateWriter) BitHolder(org.apache.drill.exec.expr.holders.BitHolder) OptionManager(org.apache.drill.exec.server.options.OptionManager) Ints(org.apache.drill.shaded.guava.com.google.common.primitives.Ints) BitWriter(org.apache.drill.exec.vector.complex.writer.BitWriter) Float8Writer(org.apache.drill.exec.vector.complex.writer.Float8Writer) Converter(org.apache.parquet.io.api.Converter) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) Repetition(org.apache.parquet.schema.Type.Repetition) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) LogicalTypeAnnotation(org.apache.parquet.schema.LogicalTypeAnnotation) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Float8Holder(org.apache.drill.exec.expr.holders.Float8Holder) VarBinaryHolder(org.apache.drill.exec.expr.holders.VarBinaryHolder) ArrayList(java.util.ArrayList) Float4Holder(org.apache.drill.exec.expr.holders.Float4Holder) NanoTimeUtils.getDateTimeValueFromBinary(org.apache.drill.exec.store.parquet.ParquetReaderUtility.NanoTimeUtils.getDateTimeValueFromBinary) TimeHolder(org.apache.drill.exec.expr.holders.TimeHolder) IntHolder(org.apache.drill.exec.expr.holders.IntHolder) DictWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.DictWriter) Iterator(java.util.Iterator) IntervalWriter(org.apache.drill.exec.vector.complex.writer.IntervalWriter) ParquetColumnMetadata(org.apache.drill.exec.store.parquet.columnreaders.ParquetColumnMetadata) Collections(java.util.Collections) TimeStampHolder(org.apache.drill.exec.expr.holders.TimeStampHolder) DateTimeConstants(org.joda.time.DateTimeConstants) BitWriter(org.apache.drill.exec.vector.complex.writer.BitWriter) SingleMapWriter(org.apache.drill.exec.vector.complex.impl.SingleMapWriter) AbstractRepeatedMapWriter(org.apache.drill.exec.vector.complex.impl.AbstractRepeatedMapWriter) MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) Float4Writer(org.apache.drill.exec.vector.complex.writer.Float4Writer) TimeWriter(org.apache.drill.exec.vector.complex.writer.TimeWriter) VarBinaryWriter(org.apache.drill.exec.vector.complex.writer.VarBinaryWriter) TimeStampWriter(org.apache.drill.exec.vector.complex.writer.TimeStampWriter) LogicalTypeAnnotation(org.apache.parquet.schema.LogicalTypeAnnotation) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) DateWriter(org.apache.drill.exec.vector.complex.writer.DateWriter) Supplier(java.util.function.Supplier) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) IntervalWriter(org.apache.drill.exec.vector.complex.writer.IntervalWriter) Optional(java.util.Optional) Float8Writer(org.apache.drill.exec.vector.complex.writer.Float8Writer) PrimitiveConverter(org.apache.parquet.io.api.PrimitiveConverter)

Example 4 with ListWriter

use of org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter in project drill by apache.

the class DrillParquetGroupConverter method createFieldConverter.

private Converter createFieldConverter(boolean skipRepeated, Type fieldType, String name, PathSegment colNextChild) {
    Converter converter;
    if (fieldType.isPrimitive()) {
        converter = getConverterForType(name, fieldType.asPrimitiveType());
    } else {
        while (colNextChild != null && !colNextChild.isNamed()) {
            colNextChild = colNextChild.getChild();
        }
        Collection<SchemaPath> columns = colNextChild == null ? Collections.emptyList() : Collections.singletonList(new SchemaPath(colNextChild.getNameSegment()));
        BaseWriter writer;
        GroupType fieldGroupType = fieldType.asGroupType();
        if (ParquetReaderUtility.isLogicalListType(fieldGroupType)) {
            writer = getWriter(name, MapWriter::list, ListWriter::list);
            converter = new DrillParquetGroupConverter(mutator, writer, fieldGroupType, columns, options, containsCorruptedDates, true, converterName);
        } else if (options.getOption(ExecConstants.PARQUET_READER_ENABLE_MAP_SUPPORT_VALIDATOR) && ParquetReaderUtility.isLogicalMapType(fieldGroupType)) {
            writer = getWriter(name, MapWriter::dict, ListWriter::dict);
            converter = new DrillParquetMapGroupConverter(mutator, (DictWriter) writer, fieldGroupType, options, containsCorruptedDates);
        } else if (fieldType.isRepetition(Repetition.REPEATED)) {
            if (skipRepeated) {
                converter = new DrillIntermediateParquetGroupConverter(mutator, baseWriter, fieldGroupType, columns, options, containsCorruptedDates, false, converterName);
            } else {
                writer = getWriter(name, (m, s) -> m.list(s).map(), l -> l.list().map());
                converter = new DrillParquetGroupConverter(mutator, writer, fieldGroupType, columns, options, containsCorruptedDates, false, converterName);
            }
        } else {
            writer = getWriter(name, MapWriter::map, ListWriter::map);
            converter = new DrillParquetGroupConverter(mutator, writer, fieldGroupType, columns, options, containsCorruptedDates, false, converterName);
        }
    }
    return converter;
}
Also used : IntervalHolder(org.apache.drill.exec.expr.holders.IntervalHolder) PrimitiveType(org.apache.parquet.schema.PrimitiveType) VarDecimalHolder(org.apache.drill.exec.expr.holders.VarDecimalHolder) SingleMapWriter(org.apache.drill.exec.vector.complex.impl.SingleMapWriter) BiFunction(java.util.function.BiFunction) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) ParquetReaderUtility(org.apache.drill.exec.store.parquet.ParquetReaderUtility) OutputMutator(org.apache.drill.exec.physical.impl.OutputMutator) VarCharWriter(org.apache.drill.exec.vector.complex.writer.VarCharWriter) PathSegment(org.apache.drill.common.expression.PathSegment) TimeStampWriter(org.apache.drill.exec.vector.complex.writer.TimeStampWriter) PrimitiveConverter(org.apache.parquet.io.api.PrimitiveConverter) DateHolder(org.apache.drill.exec.expr.holders.DateHolder) DrillBuf(io.netty.buffer.DrillBuf) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) VarBinaryWriter(org.apache.drill.exec.vector.complex.writer.VarBinaryWriter) BigIntWriter(org.apache.drill.exec.vector.complex.writer.BigIntWriter) AbstractRepeatedMapWriter(org.apache.drill.exec.vector.complex.impl.AbstractRepeatedMapWriter) IntWriter(org.apache.drill.exec.vector.complex.writer.IntWriter) GroupType(org.apache.parquet.schema.GroupType) GroupConverter(org.apache.parquet.io.api.GroupConverter) VarDecimalWriter(org.apache.drill.exec.vector.complex.writer.VarDecimalWriter) Collection(java.util.Collection) SchemaPath(org.apache.drill.common.expression.SchemaPath) Float4Writer(org.apache.drill.exec.vector.complex.writer.Float4Writer) TimeWriter(org.apache.drill.exec.vector.complex.writer.TimeWriter) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) Binary(org.apache.parquet.io.api.Binary) Longs(org.apache.drill.shaded.guava.com.google.common.primitives.Longs) List(java.util.List) DYNAMIC_STAR(org.apache.drill.common.expression.SchemaPath.DYNAMIC_STAR) Optional(java.util.Optional) Type(org.apache.parquet.schema.Type) ExecConstants(org.apache.drill.exec.ExecConstants) DateWriter(org.apache.drill.exec.vector.complex.writer.DateWriter) BitHolder(org.apache.drill.exec.expr.holders.BitHolder) OptionManager(org.apache.drill.exec.server.options.OptionManager) Ints(org.apache.drill.shaded.guava.com.google.common.primitives.Ints) BitWriter(org.apache.drill.exec.vector.complex.writer.BitWriter) Float8Writer(org.apache.drill.exec.vector.complex.writer.Float8Writer) Converter(org.apache.parquet.io.api.Converter) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) Repetition(org.apache.parquet.schema.Type.Repetition) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) LogicalTypeAnnotation(org.apache.parquet.schema.LogicalTypeAnnotation) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Float8Holder(org.apache.drill.exec.expr.holders.Float8Holder) VarBinaryHolder(org.apache.drill.exec.expr.holders.VarBinaryHolder) ArrayList(java.util.ArrayList) Float4Holder(org.apache.drill.exec.expr.holders.Float4Holder) NanoTimeUtils.getDateTimeValueFromBinary(org.apache.drill.exec.store.parquet.ParquetReaderUtility.NanoTimeUtils.getDateTimeValueFromBinary) TimeHolder(org.apache.drill.exec.expr.holders.TimeHolder) IntHolder(org.apache.drill.exec.expr.holders.IntHolder) DictWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.DictWriter) Iterator(java.util.Iterator) IntervalWriter(org.apache.drill.exec.vector.complex.writer.IntervalWriter) ParquetColumnMetadata(org.apache.drill.exec.store.parquet.columnreaders.ParquetColumnMetadata) Collections(java.util.Collections) TimeStampHolder(org.apache.drill.exec.expr.holders.TimeStampHolder) DateTimeConstants(org.joda.time.DateTimeConstants) SingleMapWriter(org.apache.drill.exec.vector.complex.impl.SingleMapWriter) AbstractRepeatedMapWriter(org.apache.drill.exec.vector.complex.impl.AbstractRepeatedMapWriter) MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) GroupType(org.apache.parquet.schema.GroupType) SchemaPath(org.apache.drill.common.expression.SchemaPath) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) PrimitiveConverter(org.apache.parquet.io.api.PrimitiveConverter) GroupConverter(org.apache.parquet.io.api.GroupConverter) Converter(org.apache.parquet.io.api.Converter)

Example 5 with ListWriter

use of org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter in project drill by apache.

the class TestRepeated method listOfList.

// 
// @Test
// public void repeatedMap() {
// 
// /**
// * We're going to try to create an object that looks like:
// *
// *  {
// *    a: [
// *      {x: 1, y: 2}
// *      {x: 2, y: 1}
// *    ]
// *  }
// *
// */
// MapVector v = new MapVector("", allocator);
// ComplexWriter writer = new ComplexWriterImpl("col", v);
// 
// MapWriter map = writer.rootAsMap();
// 
// map.start();
// ListWriter list = map.list("a");
// MapWriter inner = list.map();
// 
// IntHolder holder = new IntHolder();
// IntWriter xCol = inner.integer("x");
// IntWriter yCol = inner.integer("y");
// 
// inner.start();
// 
// holder.value = 1;
// xCol.write(holder);
// holder.value = 2;
// yCol.write(holder);
// 
// inner.end();
// 
// inner.start();
// 
// holder.value = 2;
// xCol.write(holder);
// holder.value = 1;
// yCol.write(holder);
// 
// inner.end();
// 
// IntWriter numCol = map.integer("nums");
// holder.value = 14;
// numCol.write(holder);
// 
// map.end();
// 
// 
// assertTrue(writer.ok());
// 
// }
@Test
public void listOfList() throws Exception {
    /**
     * We're going to try to create an object that looks like:
     *
     *  {
     *    a: [
     *      [1,2,3],
     *      [2,3,4]
     *    ],
     *    nums: 14,
     *    b: [
     *      { c: 1 },
     *      { c: 2 , x: 15}
     *    ]
     *  }
     */
    final MapVector mapVector = new MapVector("", allocator, null);
    final ComplexWriterImpl writer = new ComplexWriterImpl("col", mapVector);
    writer.allocate();
    {
        final MapWriter map = writer.rootAsMap();
        final ListWriter list = map.list("a");
        list.startList();
        final ListWriter innerList = list.list();
        final IntWriter innerInt = innerList.integer();
        innerList.startList();
        final IntHolder holder = new IntHolder();
        holder.value = 1;
        innerInt.write(holder);
        holder.value = 2;
        innerInt.write(holder);
        holder.value = 3;
        innerInt.write(holder);
        innerList.endList();
        innerList.startList();
        holder.value = 4;
        innerInt.write(holder);
        holder.value = 5;
        innerInt.write(holder);
        innerList.endList();
        list.endList();
        final IntWriter numCol = map.integer("nums");
        holder.value = 14;
        numCol.write(holder);
        final MapWriter repeatedMap = map.list("b").map();
        repeatedMap.start();
        holder.value = 1;
        repeatedMap.integer("c").write(holder);
        repeatedMap.end();
        repeatedMap.start();
        holder.value = 2;
        repeatedMap.integer("c").write(holder);
        final BigIntHolder h = new BigIntHolder();
        h.value = 15;
        repeatedMap.bigInt("x").write(h);
        repeatedMap.end();
        map.end();
    }
    {
        writer.setPosition(1);
        final MapWriter map = writer.rootAsMap();
        final ListWriter list = map.list("a");
        list.startList();
        final ListWriter innerList = list.list();
        final IntWriter innerInt = innerList.integer();
        innerList.startList();
        final IntHolder holder = new IntHolder();
        holder.value = -1;
        innerInt.write(holder);
        holder.value = -2;
        innerInt.write(holder);
        holder.value = -3;
        innerInt.write(holder);
        innerList.endList();
        innerList.startList();
        holder.value = -4;
        innerInt.write(holder);
        holder.value = -5;
        innerInt.write(holder);
        innerList.endList();
        list.endList();
        final IntWriter numCol = map.integer("nums");
        holder.value = -28;
        numCol.write(holder);
        final MapWriter repeatedMap = map.list("b").map();
        repeatedMap.start();
        holder.value = -1;
        repeatedMap.integer("c").write(holder);
        repeatedMap.end();
        repeatedMap.start();
        holder.value = -2;
        repeatedMap.integer("c").write(holder);
        final BigIntHolder h = new BigIntHolder();
        h.value = -30;
        repeatedMap.bigInt("x").write(h);
        repeatedMap.end();
        map.end();
    }
    final ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    final JsonWriter jsonWriter = new JsonWriter(stream, true, true);
    final FieldReader reader = mapVector.getChild("col", MapVector.class).getReader();
    reader.setPosition(0);
    jsonWriter.write(reader);
    reader.setPosition(1);
    jsonWriter.write(reader);
    writer.close();
}
Also used : MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) IntHolder(org.apache.drill.exec.expr.holders.IntHolder) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ComplexWriterImpl(org.apache.drill.exec.vector.complex.impl.ComplexWriterImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonWriter(org.apache.drill.exec.vector.complex.fn.JsonWriter) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MapVector(org.apache.drill.exec.vector.complex.MapVector) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest)

Aggregations

ListWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter)8 MapWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter)7 BigIntHolder (org.apache.drill.exec.expr.holders.BigIntHolder)4 IntHolder (org.apache.drill.exec.expr.holders.IntHolder)4 BaseWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter)4 DrillBuf (io.netty.buffer.DrillBuf)3 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)3 PathSegment (org.apache.drill.common.expression.PathSegment)3 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 OutputMutator (org.apache.drill.exec.physical.impl.OutputMutator)3 SingleMapWriter (org.apache.drill.exec.vector.complex.impl.SingleMapWriter)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Optional (java.util.Optional)2 BiFunction (java.util.function.BiFunction)2 Function (java.util.function.Function)2 Supplier (java.util.function.Supplier)2