sachouche commented on a change in pull request #1330: DRILL-6147: Adding Columnar Parquet Batch Sizing functionality URL: https://github.com/apache/drill/pull/1330#discussion_r198941031
########## File path: exec/java-exec/src/main/java/org/apache/drill/exec/store/parquet/columnreaders/batchsizing/BatchSizingMemoryUtil.java ########## @@ -0,0 +1,329 @@ +/* + * 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.store.parquet.columnreaders.batchsizing; + +import org.apache.drill.common.types.TypeProtos.MajorType; +import org.apache.drill.exec.expr.TypeHelper; +import org.apache.drill.exec.memory.BaseAllocator; +import org.apache.drill.exec.store.parquet.columnreaders.ParquetColumnMetadata; +import org.apache.drill.exec.store.parquet.columnreaders.batchsizing.RecordBatchSizerManager.ColumnMemoryQuota; +import org.apache.drill.exec.vector.NullableVarBinaryVector; +import org.apache.drill.exec.vector.NullableVarCharVector; +import org.apache.drill.exec.vector.NullableVarDecimalVector; +import org.apache.drill.exec.vector.UInt1Vector; +import org.apache.drill.exec.vector.UInt4Vector; +import org.apache.drill.exec.vector.ValueVector; +import org.apache.drill.exec.vector.VarBinaryVector; +import org.apache.drill.exec.vector.VarCharVector; +import org.apache.drill.exec.vector.VarDecimalVector; +import org.apache.drill.exec.vector.VariableWidthVector; + +/** Helper class to assist the Flat Parquet reader build batches which adhere to memory sizing constraints */ +public final class BatchSizingMemoryUtil { + + /** BYTE in-memory width */ + public static final int BYTE_VALUE_WIDTH = UInt1Vector.VALUE_WIDTH; + /** INT in-memory width */ + public static final int INT_VALUE_WIDTH = UInt4Vector.VALUE_WIDTH; + /** Default variable length column average precision; + * computed in such a way that 64k values will fit within one MB to minimize internal fragmentation + */ + public static final int DEFAULT_VL_COLUMN_AVG_PRECISION = 16; + + /** + * This method will also load detailed information about this column's current memory usage (with regard + * to the value vectors). + * + * @param columnMemoryUsage container which contains column's memory usage information (usage information will + * be automatically updated by this method) + * @param newBitsMemory New nullable data which might be inserted when processing a new input chunk + * @param newOffsetsMemory New offsets data which might be inserted when processing a new input chunk + * @param newDataMemory New data which might be inserted when processing a new input chunk + * + * @return true if adding the new data will not lead this column's Value Vector go beyond the allowed + * limit; false otherwise + */ + public static boolean canAddNewData(ColumnMemoryUsageInfo columnMemoryUsage, + int newBitsMemory, + int newOffsetsMemory, + int newDataMemory) { + + // First we need to update the vector memory usage + final VectorMemoryUsageInfo vectorMemoryUsage = columnMemoryUsage.vectorMemoryUsage; + getMemoryUsage(columnMemoryUsage.vector, columnMemoryUsage.currValueCount, vectorMemoryUsage); + + // We need to compute the new ValueVector memory usage if we attempt to add the new payload + // usedCapacity, int newPayload, int currentCapacity + int totalBitsMemory = computeNewVectorCapacity(vectorMemoryUsage.bitsBytesUsed, + newBitsMemory, + vectorMemoryUsage.bitsBytesCapacity); + + int totalOffsetsMemory = computeNewVectorCapacity(vectorMemoryUsage.offsetsBytesUsed, + newOffsetsMemory, + vectorMemoryUsage.offsetsByteCapacity); + + int totalDataMemory = computeNewVectorCapacity(vectorMemoryUsage.dataBytesUsed, + newDataMemory, + vectorMemoryUsage.dataByteCapacity); + + // Alright now we can figure out whether the new payload will take us over the maximum memory threshold + int totalMemory = totalBitsMemory + totalOffsetsMemory + totalDataMemory; + assert totalMemory >= 0; + + return totalMemory <= columnMemoryUsage.memoryQuota.getMaxMemoryUsage(); + } + + /** + * Load memory usage information for a variable length value vector + * + * @param vector source value vector + * @param currValueCount current value count + * @param vectorMemory result object which contains source vector memory usage information + */ + public static void getMemoryUsage(ValueVector sourceVector, + int currValueCount, + VectorMemoryUsageInfo vectorMemoryUsage) { + + assert sourceVector instanceof VariableWidthVector; + + vectorMemoryUsage.reset(); // reset result container + + final MajorType type = sourceVector.getField().getType(); + + switch (type.getMinorType()) { Review comment: At this time, Flat Parquet reader can only support non-nested/non-repeatable columns. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
