2828
2929import org .apache .arrow .memory .BufferAllocator ;
3030import org .apache .arrow .memory .RootAllocator ;
31- import org .apache .arrow .vector .FieldVector ;
3231import org .apache .arrow .vector .VectorLoader ;
3332import org .apache .arrow .vector .VectorSchemaRoot ;
3433import org .apache .arrow .vector .VectorUnloader ;
3938import org .apache .arrow .vector .file .json .JsonFileReader ;
4039import org .apache .arrow .vector .file .json .JsonFileWriter ;
4140import org .apache .arrow .vector .schema .ArrowRecordBatch ;
42- import org .apache .arrow .vector .types .pojo .ArrowType ;
43- import org .apache .arrow .vector .types .pojo .ArrowType .FloatingPoint ;
44- import org .apache .arrow .vector .types .pojo .Field ;
4541import org .apache .arrow .vector .types .pojo .Schema ;
42+ import org .apache .arrow .vector .util .Validator ;
4643import org .apache .commons .cli .CommandLine ;
4744import org .apache .commons .cli .CommandLineParser ;
4845import org .apache .commons .cli .Options ;
5148import org .slf4j .Logger ;
5249import org .slf4j .LoggerFactory ;
5350
54- import com .google .common .base .Objects ;
55-
5651public class Integration {
5752 private static final Logger LOGGER = LoggerFactory .getLogger (Integration .class );
5853
@@ -143,7 +138,7 @@ public void execute(File arrowFile, File jsonFile) throws IOException {
143138 LOGGER .debug ("ARROW schema: " + arrowSchema );
144139 LOGGER .debug ("JSON Input file size: " + jsonFile .length ());
145140 LOGGER .debug ("JSON schema: " + jsonSchema );
146- compareSchemas (jsonSchema , arrowSchema );
141+ Validator . compareSchemas (jsonSchema , arrowSchema );
147142
148143 List <ArrowBlock > recordBatches = footer .getRecordBatches ();
149144 Iterator <ArrowBlock > iterator = recordBatches .iterator ();
@@ -154,8 +149,7 @@ public void execute(File arrowFile, File jsonFile) throws IOException {
154149 VectorSchemaRoot arrowRoot = new VectorSchemaRoot (arrowSchema , allocator );) {
155150 VectorLoader vectorLoader = new VectorLoader (arrowRoot );
156151 vectorLoader .load (inRecordBatch );
157- // TODO: compare
158- compare (arrowRoot , jsonRoot );
152+ Validator .compareVectorSchemaRoot (arrowRoot , jsonRoot );
159153 }
160154 jsonRoot .close ();
161155 }
@@ -227,86 +221,4 @@ private static void fatalError(String message, Throwable e) {
227221 System .exit (1 );
228222 }
229223
230-
231- private static void compare (VectorSchemaRoot arrowRoot , VectorSchemaRoot jsonRoot ) {
232- compareSchemas (jsonRoot .getSchema (), arrowRoot .getSchema ());
233- if (arrowRoot .getRowCount () != jsonRoot .getRowCount ()) {
234- throw new IllegalArgumentException ("Different row count:\n " + arrowRoot .getRowCount () + "\n " + jsonRoot .getRowCount ());
235- }
236- List <FieldVector > arrowVectors = arrowRoot .getFieldVectors ();
237- List <FieldVector > jsonVectors = jsonRoot .getFieldVectors ();
238- if (arrowVectors .size () != jsonVectors .size ()) {
239- throw new IllegalArgumentException ("Different column count:\n " + arrowVectors .size () + "\n " + jsonVectors .size ());
240- }
241- for (int i = 0 ; i < arrowVectors .size (); i ++) {
242- Field field = arrowRoot .getSchema ().getFields ().get (i );
243- FieldVector arrowVector = arrowVectors .get (i );
244- FieldVector jsonVector = jsonVectors .get (i );
245- int valueCount = arrowVector .getAccessor ().getValueCount ();
246- if (valueCount != jsonVector .getAccessor ().getValueCount ()) {
247- throw new IllegalArgumentException ("Different value count for field " + field + " : " + valueCount + " != " + jsonVector .getAccessor ().getValueCount ());
248- }
249- for (int j = 0 ; j < valueCount ; j ++) {
250- Object arrow = arrowVector .getAccessor ().getObject (j );
251- Object json = jsonVector .getAccessor ().getObject (j );
252- if (!equals (field .getType (), arrow , json )) {
253- throw new IllegalArgumentException (
254- "Different values in column:\n " + field + " at index " + j + ": " + arrow + " != " + json );
255- }
256- }
257- }
258- }
259-
260- private static boolean equals (ArrowType type , final Object arrow , final Object json ) {
261- if (type instanceof ArrowType .FloatingPoint ) {
262- FloatingPoint fpType = (FloatingPoint ) type ;
263- switch (fpType .getPrecision ()) {
264- case DOUBLE :
265- return equalEnough ((Double )arrow , (Double )json );
266- case SINGLE :
267- return equalEnough ((Float )arrow , (Float )json );
268- case HALF :
269- default :
270- throw new UnsupportedOperationException ("unsupported precision: " + fpType );
271- }
272- }
273- return Objects .equal (arrow , json );
274- }
275-
276- static boolean equalEnough (Float f1 , Float f2 ) {
277- if (f1 == null || f2 == null ) {
278- return f1 == null && f2 == null ;
279- }
280- if (f1 .isNaN ()) {
281- return f2 .isNaN ();
282- }
283- if (f1 .isInfinite ()) {
284- return f2 .isInfinite () && Math .signum (f1 ) == Math .signum (f2 );
285- }
286- float average = Math .abs ((f1 + f2 ) / 2 );
287- float differenceScaled = Math .abs (f1 - f2 ) / (average == 0.0f ? 1f : average );
288- return differenceScaled < 1.0E-6f ;
289- }
290-
291- static boolean equalEnough (Double f1 , Double f2 ) {
292- if (f1 == null || f2 == null ) {
293- return f1 == null && f2 == null ;
294- }
295- if (f1 .isNaN ()) {
296- return f2 .isNaN ();
297- }
298- if (f1 .isInfinite ()) {
299- return f2 .isInfinite () && Math .signum (f1 ) == Math .signum (f2 );
300- }
301- double average = Math .abs ((f1 + f2 ) / 2 );
302- double differenceScaled = Math .abs (f1 - f2 ) / (average == 0.0d ? 1d : average );
303- return differenceScaled < 1.0E-12d ;
304- }
305-
306-
307- private static void compareSchemas (Schema jsonSchema , Schema arrowSchema ) {
308- if (!arrowSchema .equals (jsonSchema )) {
309- throw new IllegalArgumentException ("Different schemas:\n " + arrowSchema + "\n " + jsonSchema );
310- }
311- }
312224}
0 commit comments