liyafan82 commented on a change in pull request #7248: URL: https://github.com/apache/arrow/pull/7248#discussion_r467740638
########## File path: java/vector/src/main/java/org/apache/arrow/vector/validate/ValidateVectorBufferVisitor.java ########## @@ -0,0 +1,224 @@ +/* + * 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.arrow.vector.validate; + +import static org.apache.arrow.vector.validate.ValidateUtility.validateOrThrow; + +import org.apache.arrow.memory.ArrowBuf; +import org.apache.arrow.vector.BaseFixedWidthVector; +import org.apache.arrow.vector.BaseLargeVariableWidthVector; +import org.apache.arrow.vector.BaseVariableWidthVector; +import org.apache.arrow.vector.BitVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.arrow.vector.NullVector; +import org.apache.arrow.vector.TypeLayout; +import org.apache.arrow.vector.ValueVector; +import org.apache.arrow.vector.compare.VectorVisitor; +import org.apache.arrow.vector.complex.DenseUnionVector; +import org.apache.arrow.vector.complex.FixedSizeListVector; +import org.apache.arrow.vector.complex.LargeListVector; +import org.apache.arrow.vector.complex.ListVector; +import org.apache.arrow.vector.complex.NonNullableStructVector; +import org.apache.arrow.vector.complex.UnionVector; +import org.apache.arrow.vector.types.pojo.ArrowType; + +/** + * Visitor to validate vector buffers. + */ +public class ValidateVectorBufferVisitor implements VectorVisitor<Void, Void> { + + private void validateVectorCommon(ValueVector vector) { + ArrowType arrowType = vector.getField().getType(); + validateOrThrow(vector.getValueCount() >= 0, "vector valueCount is negative"); + + if (vector instanceof FieldVector) { + FieldVector fieldVector = (FieldVector) vector; + int typeBufferCount = TypeLayout.getTypeBufferCount(arrowType); + validateOrThrow(fieldVector.getFieldBuffers().size() == typeBufferCount, + String.format("Expected %s buffers in vector of type %s, got %s", + typeBufferCount, vector.getField().getType().toString(), fieldVector.getFieldBuffers().size())); + } + } + + private void validateValidityBuffer(ValueVector vector, int valueCount) { + ArrowBuf validityBuffer = vector.getValidityBuffer(); + validateOrThrow(validityBuffer != null, "The validity buffer is null."); + validateOrThrow(validityBuffer.capacity() * 8 >= valueCount, "No enough capacity for the validity buffer."); + } + + private void validateOffsetBuffer(ValueVector vector, long minCapacity) { + ArrowBuf offsetBuffer = vector.getOffsetBuffer(); + validateOrThrow(offsetBuffer != null, "The offset buffer is null."); + validateOrThrow(offsetBuffer.capacity() >= minCapacity, "No enough capacity for the offset buffer."); + } + + private void validateFixedWidthDataBuffer(ValueVector vector, int valueCount, int bitWidth) { + ArrowBuf dataBuffer = vector.getDataBuffer(); + validateOrThrow(dataBuffer != null, "The fixed width data buffer is null."); + validateOrThrow((long) bitWidth * valueCount <= dataBuffer.capacity() * 8L, + "No enough capacity for fixed width data buffer"); Review comment: @emkornfield Thanks for the good suggestion. I have revised the PR to provide more details to the error messages. ---------------------------------------------------------------- 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]
