1717
1818package org .apache .arrow .vector .util ;
1919
20- import org .apache .arrow .memory .ArrowBuf ;
20+ import static org .apache .arrow .vector .validate .ValidateUtil .validateOrThrow ;
21+
2122import org .apache .arrow .util .Preconditions ;
2223import org .apache .arrow .vector .BaseFixedWidthVector ;
23- import org .apache .arrow .vector .BufferLayout ;
24- import org .apache .arrow .vector .FieldVector ;
25- import org .apache .arrow .vector .TypeLayout ;
2624import org .apache .arrow .vector .ValueVector ;
2725import org .apache .arrow .vector .VectorSchemaRoot ;
28- import org .apache .arrow .vector .types .pojo .ArrowType ;
29- import org .apache .arrow .vector .validate .ValidateVectorVisitor ;
26+ import org .apache .arrow .vector .validate .ValidateVectorBufferVisitor ;
27+ import org .apache .arrow .vector .validate .ValidateVectorDataVisitor ;
28+ import org .apache .arrow .vector .validate .ValidateVectorTypeVisitor ;
3029
3130/**
3231 * Utility methods for {@link ValueVector}.
3332 */
3433public class ValueVectorUtility {
3534
35+ private ValueVectorUtility () {
36+ }
37+
3638 /**
3739 * Get the toString() representation of vector suitable for debugging.
3840 * Note since vectors may have millions of values, this method only show max 20 values.
@@ -92,44 +94,57 @@ public static String getToString(ValueVector vector, int start, int end) {
9294 }
9395
9496 /**
95- * Validate field vector.
97+ * Utility to validate vector in O(1) time .
9698 */
97- public static void validate (FieldVector vector ) {
99+ public static void validate (ValueVector vector ) {
98100 Preconditions .checkNotNull (vector );
99101
100- ArrowType arrowType = vector .getField ().getType ();
101- int typeBufferCount = TypeLayout .getTypeBufferCount (arrowType );
102- TypeLayout typeLayout = TypeLayout .getTypeLayout (arrowType );
102+ ValidateVectorTypeVisitor typeVisitor = new ValidateVectorTypeVisitor ();
103+ vector .accept (typeVisitor , null );
103104
104- if ( vector . getValueCount () < 0 ) {
105- throw new IllegalArgumentException ( " vector valueCount is negative" );
106- }
105+ ValidateVectorBufferVisitor bufferVisitor = new ValidateVectorBufferVisitor ();
106+ vector . accept ( bufferVisitor , null );
107+ }
107108
108- if (vector .getFieldBuffers ().size () != typeBufferCount ) {
109- throw new IllegalArgumentException (String .format ("Expected %s buffers in vector of type %s, got %s" ,
110- typeBufferCount , vector .getField ().getType ().toString (), vector .getBufferSize ()));
111- }
109+ /**
110+ * Utility to validate vector in O(n) time, where n is the value count.
111+ */
112+ public static void validateFull (ValueVector vector ) {
113+ validate (vector );
112114
113- for (int i = 0 ; i < typeBufferCount ; i ++) {
114- ArrowBuf buffer = vector .getFieldBuffers ().get (i );
115- BufferLayout bufferLayout = typeLayout .getBufferLayouts ().get (i );
116- if (buffer == null ) {
117- continue ;
118- }
119- int minBufferSize = vector .getValueCount () * bufferLayout .getTypeBitWidth ();
115+ ValidateVectorDataVisitor dataVisitor = new ValidateVectorDataVisitor ();
116+ vector .accept (dataVisitor , null );
117+ }
120118
121- if (buffer .capacity () < minBufferSize / 8 ) {
122- throw new IllegalArgumentException (String .format ("Buffer #%s too small in vector of type %s" +
123- "and valueCount %s : expected at least %s byte(s), got %s" ,
124- i , vector .getField ().getType ().toString (),
125- vector .getValueCount (), minBufferSize , buffer .capacity ()));
126- }
119+ /**
120+ * Utility to validate vector schema root in O(1) time.
121+ */
122+ public static void validate (VectorSchemaRoot root ) {
123+ Preconditions .checkNotNull (root );
124+ int valueCount = root .getRowCount ();
125+ validateOrThrow (valueCount >= 0 , "The row count of vector schema root %s is negative." , valueCount );
126+ for (ValueVector childVec : root .getFieldVectors ()) {
127+ validateOrThrow (valueCount == childVec .getValueCount (),
128+ "Child vector and vector schema root have different value counts. " +
129+ "Child vector value count %s, vector schema root value count %s" , childVec .getValueCount (), valueCount );
130+ validate (childVec );
127131 }
128-
129- ValidateVectorVisitor visitor = new ValidateVectorVisitor ();
130- vector .accept (visitor , null );
131132 }
132133
134+ /**
135+ * Utility to validate vector in O(n) time, where n is the value count.
136+ */
137+ public static void validateFull (VectorSchemaRoot root ) {
138+ Preconditions .checkNotNull (root );
139+ int valueCount = root .getRowCount ();
140+ validateOrThrow (valueCount >= 0 , "The row count of vector schema root %s is negative." , valueCount );
141+ for (ValueVector childVec : root .getFieldVectors ()) {
142+ validateOrThrow (valueCount == childVec .getValueCount (),
143+ "Child vector and vector schema root have different value counts. " +
144+ "Child vector value count %s, vector schema root value count %s" , childVec .getValueCount (), valueCount );
145+ validateFull (childVec );
146+ }
147+ }
133148
134149 /**
135150 * Pre allocate memory for BaseFixedWidthVector.
@@ -154,5 +169,4 @@ public static void ensureCapacity(VectorSchemaRoot root, int targetCapacity) {
154169 }
155170 }
156171 }
157-
158172}
0 commit comments