Search in sources :

Example 6 with SingleDictWriter

use of org.apache.drill.exec.vector.complex.impl.SingleDictWriter in project drill by apache.

the class TestDictVector method testSplitAndTransfer.

@Test
public void testSplitAndTransfer() {
    MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
    try (DictVector mapVector = new DictVector(field, allocator, null)) {
        mapVector.allocateNew();
        List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, 12L, 0.03f, 13L));
        BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
        int index = 0;
        for (Map<Object, Object> map : maps) {
            mapWriter.setPosition(index++);
            mapWriter.start();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapWriter.startKeyValuePair();
                mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
                mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
                mapWriter.endKeyValuePair();
            }
            mapWriter.end();
        }
        int start = 1;
        int length = 2;
        DictVector newMapVector = new DictVector(field, allocator, null);
        TransferPair transferPair = mapVector.makeTransferPair(newMapVector);
        transferPair.splitAndTransfer(start, length);
        BaseReader.DictReader mapReader = newMapVector.getReader();
        index = 0;
        for (Map<Object, Object> map : maps.subList(start, start + length)) {
            mapReader.setPosition(index++);
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapReader.next();
                Float actualKey = mapReader.reader(DictVector.FIELD_KEY_NAME).readFloat();
                Long actualValue = mapReader.reader(DictVector.FIELD_VALUE_NAME).readLong();
                assertEquals(entry.getKey(), actualKey);
                assertEquals(entry.getValue(), actualValue);
            }
        }
        newMapVector.clear();
    }
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) TransferPair(org.apache.drill.exec.record.TransferPair) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) BaseReader(org.apache.drill.exec.vector.complex.reader.BaseReader) SingleDictWriter(org.apache.drill.exec.vector.complex.impl.SingleDictWriter) Map(java.util.Map) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test) VectorTest(org.apache.drill.categories.VectorTest)

Example 7 with SingleDictWriter

use of org.apache.drill.exec.vector.complex.impl.SingleDictWriter in project drill by apache.

the class TestDictVector method testGetByKey.

@Test
public void testGetByKey() {
    MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
    try (DictVector mapVector = new DictVector(field, allocator, null)) {
        mapVector.allocateNew();
        List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 4f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(-2.5f, 11L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 12L, 9.73f, 13L, 4f, 14L));
        BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
        int index = 0;
        for (Map<Object, Object> map : maps) {
            mapWriter.setPosition(index++);
            mapWriter.start();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapWriter.startKeyValuePair();
                mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
                mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
                mapWriter.endKeyValuePair();
            }
            mapWriter.end();
        }
        BaseReader.DictReader mapReader = mapVector.getReader();
        float key = 4.0f;
        // Due to limitations of Calcite, we can pass NameSegment and ArraySegment
        // only as String and int respectively hence we need to transform float key
        // to String to be able to use it with DictReader which then
        // will derive appropriate type internally.
        String stringKey = String.valueOf(key);
        NullableBigIntHolder valueHolder = new NullableBigIntHolder();
        index = 0;
        for (Map<Object, Object> map : maps) {
            mapReader.setPosition(index++);
            mapReader.next();
            mapReader.read(stringKey, valueHolder);
            assertEquals(map.get(key), valueHolder.isSet == 1 ? valueHolder.value : null);
            // reset value holder to reuse it for the next row
            valueHolder.isSet = 0;
        }
    }
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NullableBigIntHolder(org.apache.drill.exec.expr.holders.NullableBigIntHolder) BaseReader(org.apache.drill.exec.vector.complex.reader.BaseReader) SingleDictWriter(org.apache.drill.exec.vector.complex.impl.SingleDictWriter) Map(java.util.Map) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test) VectorTest(org.apache.drill.categories.VectorTest)

Example 8 with SingleDictWriter

use of org.apache.drill.exec.vector.complex.impl.SingleDictWriter in project drill by apache.

the class TestDictVector method testLoadValueVector.

@Test
public void testLoadValueVector() {
    MaterializedField field = MaterializedField.create("map", DictVector.TYPE);
    try (DictVector mapVector = new DictVector(field, allocator, null)) {
        mapVector.allocateNew();
        List<Map<Object, Object>> maps = Arrays.asList(TestBuilder.mapOfObject(4f, 1L, 5.3f, 2L, 0.3f, 3L, -0.2f, 4L, 102.07f, 5L), TestBuilder.mapOfObject(45f, 6L, 9.2f, 7L), TestBuilder.mapOfObject(4.01f, 8L, 9.2f, 9L, -2.3f, 10L), TestBuilder.mapOfObject(), TestBuilder.mapOfObject(11f, 11L, 9.73f, 12L, 0.03f, 13L));
        BaseWriter.DictWriter mapWriter = new SingleDictWriter(mapVector, null);
        int index = 0;
        for (Map<Object, Object> map : maps) {
            mapWriter.setPosition(index++);
            mapWriter.start();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapWriter.startKeyValuePair();
                mapWriter.float4(DictVector.FIELD_KEY_NAME).writeFloat4((float) entry.getKey());
                mapWriter.bigInt(DictVector.FIELD_VALUE_NAME).writeBigInt((long) entry.getValue());
                mapWriter.endKeyValuePair();
            }
            mapWriter.end();
        }
        UserBitShared.SerializedField oldField = mapVector.getMetadata();
        WritableBatch writableBatch = WritableBatch.getBatchNoHV(oldField.getValueCount(), Collections.singletonList(mapVector), false);
        DrillBuf byteBuf = TestLoad.serializeBatch(allocator, writableBatch);
        DictVector newMapVector = new DictVector(field.clone(), allocator, null);
        newMapVector.load(oldField, byteBuf);
        BaseReader.DictReader mapReader = newMapVector.getReader();
        index = 0;
        for (Map<Object, Object> map : maps) {
            mapReader.setPosition(index++);
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                mapReader.next();
                Float actualKey = mapReader.reader(DictVector.FIELD_KEY_NAME).readFloat();
                Long actualValue = mapReader.reader(DictVector.FIELD_VALUE_NAME).readLong();
                assertEquals(entry.getKey(), actualKey);
                assertEquals(entry.getValue(), actualValue);
            }
        }
        newMapVector.clear();
        byteBuf.release();
        writableBatch.clear();
    }
}
Also used : DictVector(org.apache.drill.exec.vector.complex.DictVector) BaseWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter) MaterializedField(org.apache.drill.exec.record.MaterializedField) BaseReader(org.apache.drill.exec.vector.complex.reader.BaseReader) SingleDictWriter(org.apache.drill.exec.vector.complex.impl.SingleDictWriter) WritableBatch(org.apache.drill.exec.record.WritableBatch) Map(java.util.Map) UserBitShared(org.apache.drill.exec.proto.UserBitShared) DrillBuf(io.netty.buffer.DrillBuf) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test) VectorTest(org.apache.drill.categories.VectorTest)

Aggregations

Map (java.util.Map)8 VectorTest (org.apache.drill.categories.VectorTest)8 ExecTest (org.apache.drill.exec.ExecTest)8 MaterializedField (org.apache.drill.exec.record.MaterializedField)8 DictVector (org.apache.drill.exec.vector.complex.DictVector)8 SingleDictWriter (org.apache.drill.exec.vector.complex.impl.SingleDictWriter)8 BaseWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter)8 Test (org.junit.Test)8 BaseReader (org.apache.drill.exec.vector.complex.reader.BaseReader)6 DrillBuf (io.netty.buffer.DrillBuf)2 WritableBatch (org.apache.drill.exec.record.WritableBatch)2 List (java.util.List)1 NullableBigIntHolder (org.apache.drill.exec.expr.holders.NullableBigIntHolder)1 UserBitShared (org.apache.drill.exec.proto.UserBitShared)1 RecordBatchLoader (org.apache.drill.exec.record.RecordBatchLoader)1 TransferPair (org.apache.drill.exec.record.TransferPair)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1