forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare.cc
More file actions
1336 lines (1160 loc) · 45.5 KB
/
Copy pathcompare.cc
File metadata and controls
1336 lines (1160 loc) · 45.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
// Functions for comparing Arrow data structures
#include "arrow/compare.h"
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "arrow/array.h"
#include "arrow/array/diff.h"
#include "arrow/buffer.h"
#include "arrow/scalar.h"
#include "arrow/sparse_tensor.h"
#include "arrow/status.h"
#include "arrow/tensor.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/bit_run_reader.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/bitmap_ops.h"
#include "arrow/util/bitmap_reader.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/logging.h"
#include "arrow/util/macros.h"
#include "arrow/util/memory.h"
#include "arrow/visit_scalar_inline.h"
#include "arrow/visit_type_inline.h"
namespace arrow {
using internal::BitmapEquals;
using internal::BitmapReader;
using internal::BitmapUInt64Reader;
using internal::checked_cast;
using internal::OptionalBitmapEquals;
// ----------------------------------------------------------------------
// Public method implementations
namespace {
// TODO also handle HALF_FLOAT NaNs
template <bool Approximate, bool NansEqual, bool SignedZerosEqual>
struct FloatingEqualityFlags {
static constexpr bool approximate = Approximate;
static constexpr bool nans_equal = NansEqual;
static constexpr bool signed_zeros_equal = SignedZerosEqual;
};
template <typename T, typename Flags>
struct FloatingEquality {
explicit FloatingEquality(const EqualOptions& options)
: epsilon(static_cast<T>(options.atol())) {}
bool operator()(T x, T y) const {
if (x == y) {
return Flags::signed_zeros_equal || (std::signbit(x) == std::signbit(y));
}
if (Flags::nans_equal && std::isnan(x) && std::isnan(y)) {
return true;
}
if (Flags::approximate && (fabs(x - y) <= epsilon)) {
return true;
}
return false;
}
const T epsilon;
};
template <typename T, typename Visitor>
struct FloatingEqualityDispatcher {
const EqualOptions& options;
bool floating_approximate;
Visitor&& visit;
template <bool Approximate, bool NansEqual>
void DispatchL3() {
if (options.signed_zeros_equal()) {
visit(FloatingEquality<T, FloatingEqualityFlags<Approximate, NansEqual, true>>{
options});
} else {
visit(FloatingEquality<T, FloatingEqualityFlags<Approximate, NansEqual, false>>{
options});
}
}
template <bool Approximate>
void DispatchL2() {
if (options.nans_equal()) {
DispatchL3<Approximate, true>();
} else {
DispatchL3<Approximate, false>();
}
}
void Dispatch() {
if (floating_approximate) {
DispatchL2<true>();
} else {
DispatchL2<false>();
}
}
};
// Call `visit(equality_func)` where `equality_func` has the signature `bool(T, T)`
// and returns true if the two values compare equal.
template <typename T, typename Visitor>
void VisitFloatingEquality(const EqualOptions& options, bool floating_approximate,
Visitor&& visit) {
FloatingEqualityDispatcher<T, Visitor>{options, floating_approximate,
std::forward<Visitor>(visit)}
.Dispatch();
}
inline bool IdentityImpliesEqualityNansNotEqual(const DataType& type) {
if (type.id() == Type::FLOAT || type.id() == Type::DOUBLE) {
return false;
}
for (const auto& child : type.fields()) {
if (!IdentityImpliesEqualityNansNotEqual(*child->type())) {
return false;
}
}
return true;
}
inline bool IdentityImpliesEquality(const DataType& type, const EqualOptions& options) {
if (options.nans_equal()) {
return true;
}
return IdentityImpliesEqualityNansNotEqual(type);
}
bool CompareArrayRanges(const ArrayData& left, const ArrayData& right,
int64_t left_start_idx, int64_t left_end_idx,
int64_t right_start_idx, const EqualOptions& options,
bool floating_approximate);
class RangeDataEqualsImpl {
public:
// PRE-CONDITIONS:
// - the types are equal
// - the ranges are in bounds
RangeDataEqualsImpl(const EqualOptions& options, bool floating_approximate,
const ArrayData& left, const ArrayData& right,
int64_t left_start_idx, int64_t right_start_idx,
int64_t range_length)
: options_(options),
floating_approximate_(floating_approximate),
left_(left),
right_(right),
left_start_idx_(left_start_idx),
right_start_idx_(right_start_idx),
range_length_(range_length),
result_(false) {}
bool Compare() {
// Compare null bitmaps
if (left_start_idx_ == 0 && right_start_idx_ == 0 && range_length_ == left_.length &&
range_length_ == right_.length) {
// If we're comparing entire arrays, we can first compare the cached null counts
if (left_.GetNullCount() != right_.GetNullCount()) {
return false;
}
}
if (!OptionalBitmapEquals(left_.buffers[0], left_.offset + left_start_idx_,
right_.buffers[0], right_.offset + right_start_idx_,
range_length_)) {
return false;
}
// Compare values
return CompareWithType(*left_.type);
}
bool CompareWithType(const DataType& type) {
result_ = true;
if (range_length_ != 0) {
ARROW_CHECK_OK(VisitTypeInline(type, this));
}
return result_;
}
Status Visit(const NullType&) { return Status::OK(); }
template <typename TypeClass>
enable_if_primitive_ctype<TypeClass, Status> Visit(const TypeClass& type) {
return ComparePrimitive(type);
}
template <typename TypeClass>
enable_if_t<is_temporal_type<TypeClass>::value, Status> Visit(const TypeClass& type) {
return ComparePrimitive(type);
}
Status Visit(const BooleanType&) {
const uint8_t* left_bits = left_.GetValues<uint8_t>(1, 0);
const uint8_t* right_bits = right_.GetValues<uint8_t>(1, 0);
auto compare_runs = [&](int64_t i, int64_t length) -> bool {
if (length <= 8) {
// Avoid the BitmapUInt64Reader overhead for very small runs
for (int64_t j = i; j < i + length; ++j) {
if (bit_util::GetBit(left_bits, left_start_idx_ + left_.offset + j) !=
bit_util::GetBit(right_bits, right_start_idx_ + right_.offset + j)) {
return false;
}
}
return true;
} else if (length <= 1024) {
BitmapUInt64Reader left_reader(left_bits, left_start_idx_ + left_.offset + i,
length);
BitmapUInt64Reader right_reader(right_bits, right_start_idx_ + right_.offset + i,
length);
while (left_reader.position() < length) {
if (left_reader.NextWord() != right_reader.NextWord()) {
return false;
}
}
DCHECK_EQ(right_reader.position(), length);
} else {
// BitmapEquals is the fastest method on large runs
return BitmapEquals(left_bits, left_start_idx_ + left_.offset + i, right_bits,
right_start_idx_ + right_.offset + i, length);
}
return true;
};
VisitValidRuns(compare_runs);
return Status::OK();
}
Status Visit(const FloatType& type) { return CompareFloating(type); }
Status Visit(const DoubleType& type) { return CompareFloating(type); }
// Also matches StringType
Status Visit(const BinaryType& type) { return CompareBinary(type); }
// Also matches LargeStringType
Status Visit(const LargeBinaryType& type) { return CompareBinary(type); }
Status Visit(const FixedSizeBinaryType& type) {
const auto byte_width = type.byte_width();
const uint8_t* left_data = left_.GetValues<uint8_t>(1, 0);
const uint8_t* right_data = right_.GetValues<uint8_t>(1, 0);
if (left_data != nullptr && right_data != nullptr) {
auto compare_runs = [&](int64_t i, int64_t length) -> bool {
return memcmp(left_data + (left_start_idx_ + left_.offset + i) * byte_width,
right_data + (right_start_idx_ + right_.offset + i) * byte_width,
length * byte_width) == 0;
};
VisitValidRuns(compare_runs);
} else {
auto compare_runs = [&](int64_t i, int64_t length) -> bool { return true; };
VisitValidRuns(compare_runs);
}
return Status::OK();
}
// Also matches MapType
Status Visit(const ListType& type) { return CompareList(type); }
Status Visit(const LargeListType& type) { return CompareList(type); }
Status Visit(const FixedSizeListType& type) {
const auto list_size = type.list_size();
const ArrayData& left_data = *left_.child_data[0];
const ArrayData& right_data = *right_.child_data[0];
auto compare_runs = [&](int64_t i, int64_t length) -> bool {
RangeDataEqualsImpl impl(options_, floating_approximate_, left_data, right_data,
(left_start_idx_ + left_.offset + i) * list_size,
(right_start_idx_ + right_.offset + i) * list_size,
length * list_size);
return impl.Compare();
};
VisitValidRuns(compare_runs);
return Status::OK();
}
Status Visit(const StructType& type) {
const int32_t num_fields = type.num_fields();
auto compare_runs = [&](int64_t i, int64_t length) -> bool {
for (int32_t f = 0; f < num_fields; ++f) {
RangeDataEqualsImpl impl(options_, floating_approximate_, *left_.child_data[f],
*right_.child_data[f],
left_start_idx_ + left_.offset + i,
right_start_idx_ + right_.offset + i, length);
if (!impl.Compare()) {
return false;
}
}
return true;
};
VisitValidRuns(compare_runs);
return Status::OK();
}
Status Visit(const SparseUnionType& type) {
const auto& child_ids = type.child_ids();
const int8_t* left_codes = left_.GetValues<int8_t>(1);
const int8_t* right_codes = right_.GetValues<int8_t>(1);
// Unions don't have a null bitmap
for (int64_t i = 0; i < range_length_; ++i) {
const auto type_id = left_codes[left_start_idx_ + i];
if (type_id != right_codes[right_start_idx_ + i]) {
result_ = false;
break;
}
const auto child_num = child_ids[type_id];
// XXX can we instead detect runs of same-child union values?
RangeDataEqualsImpl impl(
options_, floating_approximate_, *left_.child_data[child_num],
*right_.child_data[child_num], left_start_idx_ + left_.offset + i,
right_start_idx_ + right_.offset + i, 1);
if (!impl.Compare()) {
result_ = false;
break;
}
}
return Status::OK();
}
Status Visit(const DenseUnionType& type) {
const auto& child_ids = type.child_ids();
const int8_t* left_codes = left_.GetValues<int8_t>(1);
const int8_t* right_codes = right_.GetValues<int8_t>(1);
const int32_t* left_offsets = left_.GetValues<int32_t>(2);
const int32_t* right_offsets = right_.GetValues<int32_t>(2);
for (int64_t i = 0; i < range_length_; ++i) {
const auto type_id = left_codes[left_start_idx_ + i];
if (type_id != right_codes[right_start_idx_ + i]) {
result_ = false;
break;
}
const auto child_num = child_ids[type_id];
RangeDataEqualsImpl impl(
options_, floating_approximate_, *left_.child_data[child_num],
*right_.child_data[child_num], left_offsets[left_start_idx_ + i],
right_offsets[right_start_idx_ + i], 1);
if (!impl.Compare()) {
result_ = false;
break;
}
}
return Status::OK();
}
Status Visit(const DictionaryType& type) {
// Compare dictionaries
result_ &= CompareArrayRanges(
*left_.dictionary, *right_.dictionary,
/*left_start_idx=*/0,
/*left_end_idx=*/std::max(left_.dictionary->length, right_.dictionary->length),
/*right_start_idx=*/0, options_, floating_approximate_);
if (result_) {
// Compare indices
result_ &= CompareWithType(*type.index_type());
}
return Status::OK();
}
Status Visit(const ExtensionType& type) {
// Compare storages
result_ &= CompareWithType(*type.storage_type());
return Status::OK();
}
protected:
template <typename TypeClass, typename CType = typename TypeClass::c_type>
Status ComparePrimitive(const TypeClass&) {
const CType* left_values = left_.GetValues<CType>(1);
const CType* right_values = right_.GetValues<CType>(1);
VisitValidRuns([&](int64_t i, int64_t length) {
return memcmp(left_values + left_start_idx_ + i,
right_values + right_start_idx_ + i, length * sizeof(CType)) == 0;
});
return Status::OK();
}
template <typename TypeClass>
Status CompareFloating(const TypeClass&) {
using CType = typename TypeClass::c_type;
const CType* left_values = left_.GetValues<CType>(1);
const CType* right_values = right_.GetValues<CType>(1);
auto visitor = [&](auto&& compare_func) {
VisitValues([&](int64_t i) {
const CType x = left_values[i + left_start_idx_];
const CType y = right_values[i + right_start_idx_];
return compare_func(x, y);
});
};
VisitFloatingEquality<CType>(options_, floating_approximate_, std::move(visitor));
return Status::OK();
}
template <typename TypeClass>
Status CompareBinary(const TypeClass&) {
const uint8_t* left_data = left_.GetValues<uint8_t>(2, 0);
const uint8_t* right_data = right_.GetValues<uint8_t>(2, 0);
if (left_data != nullptr && right_data != nullptr) {
const auto compare_ranges = [&](int64_t left_offset, int64_t right_offset,
int64_t length) -> bool {
return memcmp(left_data + left_offset, right_data + right_offset, length) == 0;
};
CompareWithOffsets<typename TypeClass::offset_type>(1, compare_ranges);
} else {
// One of the arrays is an array of empty strings and nulls.
// We just need to compare the offsets.
// (note we must not call memcmp() with null data pointers)
CompareWithOffsets<typename TypeClass::offset_type>(1, [](...) { return true; });
}
return Status::OK();
}
template <typename TypeClass>
Status CompareList(const TypeClass&) {
const ArrayData& left_data = *left_.child_data[0];
const ArrayData& right_data = *right_.child_data[0];
const auto compare_ranges = [&](int64_t left_offset, int64_t right_offset,
int64_t length) -> bool {
RangeDataEqualsImpl impl(options_, floating_approximate_, left_data, right_data,
left_offset, right_offset, length);
return impl.Compare();
};
CompareWithOffsets<typename TypeClass::offset_type>(1, compare_ranges);
return Status::OK();
}
template <typename offset_type, typename CompareRanges>
void CompareWithOffsets(int offsets_buffer_index, CompareRanges&& compare_ranges) {
const offset_type* left_offsets =
left_.GetValues<offset_type>(offsets_buffer_index) + left_start_idx_;
const offset_type* right_offsets =
right_.GetValues<offset_type>(offsets_buffer_index) + right_start_idx_;
const auto compare_runs = [&](int64_t i, int64_t length) {
for (int64_t j = i; j < i + length; ++j) {
if (left_offsets[j + 1] - left_offsets[j] !=
right_offsets[j + 1] - right_offsets[j]) {
return false;
}
}
if (!compare_ranges(left_offsets[i], right_offsets[i],
left_offsets[i + length] - left_offsets[i])) {
return false;
}
return true;
};
VisitValidRuns(compare_runs);
}
template <typename CompareValues>
void VisitValues(CompareValues&& compare_values) {
internal::VisitSetBitRunsVoid(left_.buffers[0], left_.offset + left_start_idx_,
range_length_, [&](int64_t position, int64_t length) {
for (int64_t i = 0; i < length; ++i) {
result_ &= compare_values(position + i);
}
});
}
// Visit and compare runs of non-null values
template <typename CompareRuns>
void VisitValidRuns(CompareRuns&& compare_runs) {
const uint8_t* left_null_bitmap = left_.GetValues<uint8_t>(0, 0);
if (left_null_bitmap == nullptr) {
result_ = compare_runs(0, range_length_);
return;
}
internal::SetBitRunReader reader(left_null_bitmap, left_.offset + left_start_idx_,
range_length_);
while (true) {
const auto run = reader.NextRun();
if (run.length == 0) {
return;
}
if (!compare_runs(run.position, run.length)) {
result_ = false;
return;
}
}
}
const EqualOptions& options_;
const bool floating_approximate_;
const ArrayData& left_;
const ArrayData& right_;
const int64_t left_start_idx_;
const int64_t right_start_idx_;
const int64_t range_length_;
bool result_;
};
bool CompareArrayRanges(const ArrayData& left, const ArrayData& right,
int64_t left_start_idx, int64_t left_end_idx,
int64_t right_start_idx, const EqualOptions& options,
bool floating_approximate) {
if (left.type->id() != right.type->id() ||
!TypeEquals(*left.type, *right.type, false /* check_metadata */)) {
return false;
}
const int64_t range_length = left_end_idx - left_start_idx;
DCHECK_GE(range_length, 0);
if (left_start_idx + range_length > left.length) {
// Left range too small
return false;
}
if (right_start_idx + range_length > right.length) {
// Right range too small
return false;
}
if (&left == &right && left_start_idx == right_start_idx &&
IdentityImpliesEquality(*left.type, options)) {
return true;
}
// Compare values
RangeDataEqualsImpl impl(options, floating_approximate, left, right, left_start_idx,
right_start_idx, range_length);
return impl.Compare();
}
class TypeEqualsVisitor {
public:
explicit TypeEqualsVisitor(const DataType& right, bool check_metadata)
: right_(right), check_metadata_(check_metadata), result_(false) {}
bool MetadataEqual(const Field& left, const Field& right) {
if (left.HasMetadata() && right.HasMetadata()) {
return left.metadata()->Equals(*right.metadata());
} else {
return !left.HasMetadata() && !right.HasMetadata();
}
}
Status VisitChildren(const DataType& left) {
if (left.num_fields() != right_.num_fields()) {
result_ = false;
return Status::OK();
}
for (int i = 0; i < left.num_fields(); ++i) {
if (!left.field(i)->Equals(right_.field(i), check_metadata_)) {
result_ = false;
return Status::OK();
}
}
result_ = true;
return Status::OK();
}
template <typename T>
enable_if_t<is_null_type<T>::value || is_primitive_ctype<T>::value ||
is_base_binary_type<T>::value,
Status>
Visit(const T&) {
result_ = true;
return Status::OK();
}
template <typename T>
enable_if_interval<T, Status> Visit(const T& left) {
const auto& right = checked_cast<const IntervalType&>(right_);
result_ = right.interval_type() == left.interval_type();
return Status::OK();
}
template <typename T>
enable_if_t<is_time_type<T>::value || is_date_type<T>::value ||
is_duration_type<T>::value,
Status>
Visit(const T& left) {
const auto& right = checked_cast<const T&>(right_);
result_ = left.unit() == right.unit();
return Status::OK();
}
Status Visit(const TimestampType& left) {
const auto& right = checked_cast<const TimestampType&>(right_);
result_ = left.unit() == right.unit() && left.timezone() == right.timezone();
return Status::OK();
}
Status Visit(const FixedSizeBinaryType& left) {
const auto& right = checked_cast<const FixedSizeBinaryType&>(right_);
result_ = left.byte_width() == right.byte_width();
return Status::OK();
}
Status Visit(const Decimal128Type& left) {
const auto& right = checked_cast<const Decimal128Type&>(right_);
result_ = left.precision() == right.precision() && left.scale() == right.scale();
return Status::OK();
}
Status Visit(const Decimal256Type& left) {
const auto& right = checked_cast<const Decimal256Type&>(right_);
result_ = left.precision() == right.precision() && left.scale() == right.scale();
return Status::OK();
}
template <typename T>
enable_if_t<is_list_like_type<T>::value, Status> Visit(const T& left) {
std::shared_ptr<Field> left_field = left.field(0);
std::shared_ptr<Field> right_field = checked_cast<const T&>(right_).field(0);
bool equal_names = !check_metadata_ || (left_field->name() == right_field->name());
bool equal_metadata = !check_metadata_ || MetadataEqual(*left_field, *right_field);
result_ = equal_names && equal_metadata &&
(left_field->nullable() == right_field->nullable()) &&
left_field->type()->Equals(*right_field->type(), check_metadata_);
return Status::OK();
}
template <typename T>
enable_if_t<is_struct_type<T>::value, Status> Visit(const T& left) {
return VisitChildren(left);
}
Status Visit(const MapType& left) {
const auto& right = checked_cast<const MapType&>(right_);
if (left.keys_sorted() != right.keys_sorted()) {
result_ = false;
return Status::OK();
}
if (check_metadata_ && (left.item_field()->name() != right.item_field()->name() ||
left.key_field()->name() != right.key_field()->name() ||
left.value_field()->name() != right.value_field()->name())) {
result_ = false;
return Status::OK();
}
if (check_metadata_ && !(MetadataEqual(*left.item_field(), *right.item_field()) &&
MetadataEqual(*left.key_field(), *right.key_field()) &&
MetadataEqual(*left.value_field(), *right.value_field()))) {
result_ = false;
return Status::OK();
}
result_ = left.key_type()->Equals(*right.key_type(), check_metadata_) &&
left.item_type()->Equals(*right.item_type(), check_metadata_);
return Status::OK();
}
Status Visit(const UnionType& left) {
const auto& right = checked_cast<const UnionType&>(right_);
if (left.mode() != right.mode() || left.type_codes() != right.type_codes()) {
result_ = false;
return Status::OK();
}
result_ = std::equal(
left.fields().begin(), left.fields().end(), right.fields().begin(),
[this](const std::shared_ptr<Field>& l, const std::shared_ptr<Field>& r) {
return l->Equals(r, check_metadata_);
});
return Status::OK();
}
Status Visit(const DictionaryType& left) {
const auto& right = checked_cast<const DictionaryType&>(right_);
result_ = left.index_type()->Equals(right.index_type()) &&
left.value_type()->Equals(right.value_type()) &&
(left.ordered() == right.ordered());
return Status::OK();
}
Status Visit(const ExtensionType& left) {
result_ = left.ExtensionEquals(static_cast<const ExtensionType&>(right_));
return Status::OK();
}
bool result() const { return result_; }
protected:
const DataType& right_;
bool check_metadata_;
bool result_;
};
bool ArrayEquals(const Array& left, const Array& right, const EqualOptions& opts,
bool floating_approximate);
bool ScalarEquals(const Scalar& left, const Scalar& right, const EqualOptions& options,
bool floating_approximate);
class ScalarEqualsVisitor {
public:
// PRE-CONDITIONS:
// - the types are equal
// - the scalars are non-null
explicit ScalarEqualsVisitor(const Scalar& right, const EqualOptions& opts,
bool floating_approximate)
: right_(right),
options_(opts),
floating_approximate_(floating_approximate),
result_(false) {}
Status Visit(const NullScalar& left) {
result_ = true;
return Status::OK();
}
Status Visit(const BooleanScalar& left) {
const auto& right = checked_cast<const BooleanScalar&>(right_);
result_ = left.value == right.value;
return Status::OK();
}
template <typename T>
typename std::enable_if<(is_primitive_ctype<typename T::TypeClass>::value ||
is_temporal_type<typename T::TypeClass>::value),
Status>::type
Visit(const T& left_) {
const auto& right = checked_cast<const T&>(right_);
result_ = right.value == left_.value;
return Status::OK();
}
Status Visit(const FloatScalar& left) { return CompareFloating(left); }
Status Visit(const DoubleScalar& left) { return CompareFloating(left); }
template <typename T>
typename std::enable_if<std::is_base_of<BaseBinaryScalar, T>::value, Status>::type
Visit(const T& left) {
const auto& right = checked_cast<const BaseBinaryScalar&>(right_);
result_ = internal::SharedPtrEquals(left.value, right.value);
return Status::OK();
}
Status Visit(const Decimal128Scalar& left) {
const auto& right = checked_cast<const Decimal128Scalar&>(right_);
result_ = left.value == right.value;
return Status::OK();
}
Status Visit(const Decimal256Scalar& left) {
const auto& right = checked_cast<const Decimal256Scalar&>(right_);
result_ = left.value == right.value;
return Status::OK();
}
Status Visit(const ListScalar& left) {
const auto& right = checked_cast<const ListScalar&>(right_);
result_ = ArrayEquals(*left.value, *right.value, options_, floating_approximate_);
return Status::OK();
}
Status Visit(const LargeListScalar& left) {
const auto& right = checked_cast<const LargeListScalar&>(right_);
result_ = ArrayEquals(*left.value, *right.value, options_, floating_approximate_);
return Status::OK();
}
Status Visit(const MapScalar& left) {
const auto& right = checked_cast<const MapScalar&>(right_);
result_ = ArrayEquals(*left.value, *right.value, options_, floating_approximate_);
return Status::OK();
}
Status Visit(const FixedSizeListScalar& left) {
const auto& right = checked_cast<const FixedSizeListScalar&>(right_);
result_ = ArrayEquals(*left.value, *right.value, options_, floating_approximate_);
return Status::OK();
}
Status Visit(const StructScalar& left) {
const auto& right = checked_cast<const StructScalar&>(right_);
if (right.value.size() != left.value.size()) {
result_ = false;
} else {
bool all_equals = true;
for (size_t i = 0; i < left.value.size() && all_equals; i++) {
all_equals &= ScalarEquals(*left.value[i], *right.value[i], options_,
floating_approximate_);
}
result_ = all_equals;
}
return Status::OK();
}
Status Visit(const DenseUnionScalar& left) {
const auto& right = checked_cast<const DenseUnionScalar&>(right_);
result_ = ScalarEquals(*left.value, *right.value, options_, floating_approximate_);
return Status::OK();
}
Status Visit(const SparseUnionScalar& left) {
const auto& right = checked_cast<const SparseUnionScalar&>(right_);
result_ = ScalarEquals(*left.value[left.child_id], *right.value[right.child_id],
options_, floating_approximate_);
return Status::OK();
}
Status Visit(const DictionaryScalar& left) {
const auto& right = checked_cast<const DictionaryScalar&>(right_);
result_ = ScalarEquals(*left.value.index, *right.value.index, options_,
floating_approximate_) &&
ArrayEquals(*left.value.dictionary, *right.value.dictionary, options_,
floating_approximate_);
return Status::OK();
}
Status Visit(const ExtensionScalar& left) {
const auto& right = checked_cast<const ExtensionScalar&>(right_);
result_ = ScalarEquals(*left.value, *right.value, options_, floating_approximate_);
return Status::OK();
}
bool result() const { return result_; }
protected:
template <typename ScalarType>
Status CompareFloating(const ScalarType& left) {
using CType = decltype(left.value);
const auto& right = checked_cast<const ScalarType&>(right_);
auto visitor = [&](auto&& compare_func) {
result_ = compare_func(left.value, right.value);
};
VisitFloatingEquality<CType>(options_, floating_approximate_, std::move(visitor));
return Status::OK();
}
const Scalar& right_;
const EqualOptions options_;
const bool floating_approximate_;
bool result_;
};
Status PrintDiff(const Array& left, const Array& right, std::ostream* os);
Status PrintDiff(const Array& left, const Array& right, int64_t left_offset,
int64_t left_length, int64_t right_offset, int64_t right_length,
std::ostream* os) {
if (os == nullptr) {
return Status::OK();
}
if (!left.type()->Equals(right.type())) {
*os << "# Array types differed: " << *left.type() << " vs " << *right.type()
<< std::endl;
return Status::OK();
}
if (left.type()->id() == Type::DICTIONARY) {
*os << "# Dictionary arrays differed" << std::endl;
const auto& left_dict = checked_cast<const DictionaryArray&>(left);
const auto& right_dict = checked_cast<const DictionaryArray&>(right);
*os << "## dictionary diff";
auto pos = os->tellp();
RETURN_NOT_OK(PrintDiff(*left_dict.dictionary(), *right_dict.dictionary(), os));
if (os->tellp() == pos) {
*os << std::endl;
}
*os << "## indices diff";
pos = os->tellp();
RETURN_NOT_OK(PrintDiff(*left_dict.indices(), *right_dict.indices(), os));
if (os->tellp() == pos) {
*os << std::endl;
}
return Status::OK();
}
const auto left_slice = left.Slice(left_offset, left_length);
const auto right_slice = right.Slice(right_offset, right_length);
ARROW_ASSIGN_OR_RAISE(auto edits,
Diff(*left_slice, *right_slice, default_memory_pool()));
ARROW_ASSIGN_OR_RAISE(auto formatter, MakeUnifiedDiffFormatter(*left.type(), os));
return formatter(*edits, *left_slice, *right_slice);
}
Status PrintDiff(const Array& left, const Array& right, std::ostream* os) {
return PrintDiff(left, right, 0, left.length(), 0, right.length(), os);
}
bool ArrayRangeEquals(const Array& left, const Array& right, int64_t left_start_idx,
int64_t left_end_idx, int64_t right_start_idx,
const EqualOptions& options, bool floating_approximate) {
bool are_equal =
CompareArrayRanges(*left.data(), *right.data(), left_start_idx, left_end_idx,
right_start_idx, options, floating_approximate);
if (!are_equal) {
ARROW_IGNORE_EXPR(PrintDiff(
left, right, left_start_idx, left_end_idx, right_start_idx,
right_start_idx + (left_end_idx - left_start_idx), options.diff_sink()));
}
return are_equal;
}
bool ArrayEquals(const Array& left, const Array& right, const EqualOptions& opts,
bool floating_approximate) {
if (left.length() != right.length()) {
ARROW_IGNORE_EXPR(PrintDiff(left, right, opts.diff_sink()));
return false;
}
return ArrayRangeEquals(left, right, 0, left.length(), 0, opts, floating_approximate);
}
bool ScalarEquals(const Scalar& left, const Scalar& right, const EqualOptions& options,
bool floating_approximate) {
if (&left == &right && IdentityImpliesEquality(*left.type, options)) {
return true;
}
if (!left.type->Equals(right.type)) {
return false;
}
if (left.is_valid != right.is_valid) {
return false;
}
if (!left.is_valid) {
return true;
}
ScalarEqualsVisitor visitor(right, options, floating_approximate);
auto error = VisitScalarInline(left, &visitor);
DCHECK_OK(error);
return visitor.result();
}
} // namespace
bool ArrayRangeEquals(const Array& left, const Array& right, int64_t left_start_idx,
int64_t left_end_idx, int64_t right_start_idx,
const EqualOptions& options) {
const bool floating_approximate = false;
return ArrayRangeEquals(left, right, left_start_idx, left_end_idx, right_start_idx,
options, floating_approximate);
}
bool ArrayRangeApproxEquals(const Array& left, const Array& right, int64_t left_start_idx,
int64_t left_end_idx, int64_t right_start_idx,
const EqualOptions& options) {
const bool floating_approximate = true;
return ArrayRangeEquals(left, right, left_start_idx, left_end_idx, right_start_idx,
options, floating_approximate);
}
bool ArrayEquals(const Array& left, const Array& right, const EqualOptions& opts) {
const bool floating_approximate = false;
return ArrayEquals(left, right, opts, floating_approximate);
}
bool ArrayApproxEquals(const Array& left, const Array& right, const EqualOptions& opts) {
const bool floating_approximate = true;
return ArrayEquals(left, right, opts, floating_approximate);
}
bool ScalarEquals(const Scalar& left, const Scalar& right, const EqualOptions& options) {
const bool floating_approximate = false;
return ScalarEquals(left, right, options, floating_approximate);
}
bool ScalarApproxEquals(const Scalar& left, const Scalar& right,
const EqualOptions& options) {
const bool floating_approximate = true;
return ScalarEquals(left, right, options, floating_approximate);
}
namespace {