vvysotskyi commented on a change in pull request #1829: DRILL-7096: Develop vector for canonical Map<K,V> URL: https://github.com/apache/drill/pull/1829#discussion_r317193686
########## File path: exec/vector/src/main/java/org/apache/drill/exec/vector/complex/AbstractRepeatedMapVector.java ########## @@ -0,0 +1,524 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.drill.exec.vector.complex; + +import io.netty.buffer.DrillBuf; + +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.commons.lang3.ArrayUtils; +import org.apache.drill.common.types.TypeProtos; +import org.apache.drill.common.types.Types; +import org.apache.drill.exec.exception.OutOfMemoryException; +import org.apache.drill.exec.expr.BasicTypeHelper; +import org.apache.drill.exec.expr.holders.RepeatedValueHolder; +import org.apache.drill.exec.memory.BufferAllocator; +import org.apache.drill.exec.memory.AllocationManager.BufferLedger; +import org.apache.drill.exec.proto.UserBitShared.SerializedField; +import org.apache.drill.exec.record.MaterializedField; +import org.apache.drill.exec.record.TransferPair; +import org.apache.drill.exec.util.CallBack; +import org.apache.drill.exec.vector.AddOrGetResult; +import org.apache.drill.exec.vector.AllocationHelper; +import org.apache.drill.exec.vector.SchemaChangeCallBack; +import org.apache.drill.exec.vector.UInt4Vector; +import org.apache.drill.exec.vector.ValueVector; +import org.apache.drill.exec.vector.VectorDescriptor; + +public abstract class AbstractRepeatedMapVector extends AbstractMapVector implements RepeatedValueVector { + + protected final UInt4Vector offsets; // offsets to start of each record (considering record indices are 0-indexed) + protected final EmptyValuePopulator emptyPopulator; + + protected AbstractRepeatedMapVector(MaterializedField field, BufferAllocator allocator, CallBack callBack) { + this(field, new UInt4Vector(BaseRepeatedValueVector.OFFSETS_FIELD, allocator), callBack); + } + + protected AbstractRepeatedMapVector(MaterializedField field, UInt4Vector offsets, CallBack callBack) { + super(field, offsets.getAllocator(), callBack); + this.offsets = offsets; + this.emptyPopulator = new EmptyValuePopulator(offsets); + } + + @Override + public UInt4Vector getOffsetVector() { return offsets; } + + @Override + public ValueVector getDataVector() { + throw new UnsupportedOperationException(); + } + + @Override + public <T extends ValueVector> AddOrGetResult<T> addOrGetVector(VectorDescriptor descriptor) { + throw new UnsupportedOperationException(); + } + + @Override + public void setInitialCapacity(int numRecords) { + offsets.setInitialCapacity(numRecords + 1); + for (final ValueVector v : this) { + v.setInitialCapacity(numRecords * RepeatedValueVector.DEFAULT_REPEAT_PER_RECORD); + } + } + + public void allocateNew(int groupCount, int innerValueCount) { + clear(); + try { + allocateOffsetsNew(groupCount); + for (ValueVector v : getChildren()) { + AllocationHelper.allocatePrecomputedChildCount(v, groupCount, 50, innerValueCount); + } + } catch (OutOfMemoryException e){ + clear(); + throw e; + } + reset(); + } + + abstract void reset(); + + public void allocateOffsetsNew(int groupCount) { + offsets.allocateNew(groupCount + 1); + offsets.zeroVector(); + } + + public Iterator<String> fieldNameIterator() { + return getChildFieldNames().iterator(); + } + + @Override + public List<ValueVector> getPrimitiveVectors() { + final List<ValueVector> primitiveVectors = super.getPrimitiveVectors(); + primitiveVectors.add(offsets); + return primitiveVectors; + } + + @Override + public int getBufferSize() { + if (getValueCount() == 0) { + return 0; + } + return offsets.getBufferSize() + super.getBufferSize(); + } + + @Override + public int getAllocatedSize() { + return offsets.getAllocatedSize() + super.getAllocatedSize(); + } + + @Override + public int getBufferSizeFor(final int valueCount) { + if (valueCount == 0) { + return 0; + } + + long bufferSize = offsets.getBufferSizeFor(valueCount); + for (final ValueVector v : this) { + bufferSize += v.getBufferSizeFor(valueCount); + } + + return (int) bufferSize; + } + + @Override + public void close() { + offsets.close(); + super.close(); + } + + public TransferPair getTransferPairToSingleMap(String reference, BufferAllocator allocator) { + return new SingleMapTransferPair(this, reference, allocator); + } + + @Override + public abstract TransferPair getTransferPair(BufferAllocator allocator); + + @Override + public abstract TransferPair makeTransferPair(ValueVector to); + + @Override + public abstract TransferPair getTransferPair(String ref, BufferAllocator allocator); + + @Override + public boolean allocateNewSafe() { + /* boolean to keep track if all the memory allocation were successful + * Used in the case of composite vectors when we need to allocate multiple + * buffers for multiple vectors. If one of the allocations failed we need to + * clear all the memory that we allocated + */ + boolean success = false; + try { + if (!offsets.allocateNewSafe()) { + return false; + } + success = super.allocateNewSafe(); + } finally { + if (!success) { + clear(); + } + } + offsets.zeroVector(); + return success; + } + + abstract class AbstractRepeatedMapTransferPair<T extends AbstractRepeatedMapVector> implements TransferPair { + + protected final T to; + protected final T from; + private final TransferPair[] pairs; + + public AbstractRepeatedMapTransferPair(T to) { + this(to, true); + } + + public AbstractRepeatedMapTransferPair(T to, boolean allocate) { + this.from = (T) AbstractRepeatedMapVector.this; + this.to = to; + this.pairs = new TransferPair[from.size()]; + // todo: this.to.ephPair Review comment: Please either address this `TODO` or remove it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
