forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalar_test.cc
More file actions
1763 lines (1422 loc) · 62.8 KB
/
Copy pathscalar_test.cc
File metadata and controls
1763 lines (1422 loc) · 62.8 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.
#include <chrono>
#include <limits>
#include <memory>
#include <ostream>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "arrow/array.h"
#include "arrow/array/util.h"
#include "arrow/buffer.h"
#include "arrow/memory_pool.h"
#include "arrow/scalar.h"
#include "arrow/status.h"
#include "arrow/testing/extension_type.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "arrow/type_traits.h"
namespace arrow {
using internal::checked_cast;
using internal::checked_pointer_cast;
std::shared_ptr<Scalar> CheckMakeNullScalar(const std::shared_ptr<DataType>& type) {
const auto scalar = MakeNullScalar(type);
ARROW_EXPECT_OK(scalar->Validate());
ARROW_EXPECT_OK(scalar->ValidateFull());
AssertTypeEqual(*type, *scalar->type);
EXPECT_FALSE(scalar->is_valid);
return scalar;
}
template <typename... MakeScalarArgs>
void AssertMakeScalar(const Scalar& expected, MakeScalarArgs&&... args) {
ASSERT_OK_AND_ASSIGN(auto scalar, MakeScalar(std::forward<MakeScalarArgs>(args)...));
ASSERT_OK(scalar->Validate());
ASSERT_OK(scalar->ValidateFull());
AssertScalarsEqual(expected, *scalar, /*verbose=*/true);
}
void AssertParseScalar(const std::shared_ptr<DataType>& type, std::string_view s,
const Scalar& expected) {
ASSERT_OK_AND_ASSIGN(auto scalar, Scalar::Parse(type, s));
ASSERT_OK(scalar->Validate());
ASSERT_OK(scalar->ValidateFull());
AssertScalarsEqual(expected, *scalar, /*verbose=*/true);
}
void AssertValidationFails(const Scalar& scalar) {
ASSERT_RAISES(Invalid, scalar.Validate());
ASSERT_RAISES(Invalid, scalar.ValidateFull());
}
TEST(TestNullScalar, Basics) {
NullScalar scalar;
ASSERT_FALSE(scalar.is_valid);
ASSERT_TRUE(scalar.type->Equals(*null()));
ASSERT_OK(scalar.ValidateFull());
ASSERT_OK_AND_ASSIGN(auto arr, MakeArrayOfNull(null(), 1));
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_TRUE(first->Equals(scalar));
ASSERT_OK(first->ValidateFull());
}
TEST(TestNullScalar, ValidateErrors) {
NullScalar scalar;
scalar.is_valid = true;
AssertValidationFails(scalar);
}
template <typename T>
class TestNumericScalar : public ::testing::Test {
public:
TestNumericScalar() = default;
};
TYPED_TEST_SUITE(TestNumericScalar, NumericArrowTypes);
TYPED_TEST(TestNumericScalar, Basics) {
using T = typename TypeParam::c_type;
using ScalarType = typename TypeTraits<TypeParam>::ScalarType;
T value = static_cast<T>(1);
auto scalar_val = std::make_shared<ScalarType>(value);
ASSERT_EQ(value, scalar_val->value);
ASSERT_TRUE(scalar_val->is_valid);
ASSERT_OK(scalar_val->ValidateFull());
auto expected_type = TypeTraits<TypeParam>::type_singleton();
ASSERT_TRUE(scalar_val->type->Equals(*expected_type));
T other_value = static_cast<T>(2);
auto scalar_other = std::make_shared<ScalarType>(other_value);
ASSERT_NE(*scalar_other, *scalar_val);
scalar_val->value = other_value;
ASSERT_EQ(other_value, scalar_val->value);
ASSERT_EQ(*scalar_other, *scalar_val);
ScalarType stack_val;
ASSERT_FALSE(stack_val.is_valid);
ASSERT_OK(stack_val.ValidateFull());
auto null_value = std::make_shared<ScalarType>();
ASSERT_FALSE(null_value->is_valid);
ASSERT_OK(null_value->ValidateFull());
// Nulls should be equals to itself following Array::Equals
ASSERT_EQ(*null_value, stack_val);
auto dyn_null_value = CheckMakeNullScalar(expected_type);
ASSERT_EQ(*null_value, *dyn_null_value);
// test Array.GetScalar
auto arr = ArrayFromJSON(expected_type, "[null, 1, 2]");
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto one, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto two, arr->GetScalar(2));
ASSERT_OK(null->ValidateFull());
ASSERT_OK(one->ValidateFull());
ASSERT_OK(two->ValidateFull());
ASSERT_TRUE(null->Equals(*null_value));
ASSERT_TRUE(one->Equals(ScalarType(1)));
ASSERT_FALSE(one->Equals(ScalarType(2)));
ASSERT_TRUE(two->Equals(ScalarType(2)));
ASSERT_FALSE(two->Equals(ScalarType(3)));
ASSERT_TRUE(null->ApproxEquals(*null_value));
ASSERT_TRUE(one->ApproxEquals(ScalarType(1)));
ASSERT_FALSE(one->ApproxEquals(ScalarType(2)));
ASSERT_TRUE(two->ApproxEquals(ScalarType(2)));
ASSERT_FALSE(two->ApproxEquals(ScalarType(3)));
}
TYPED_TEST(TestNumericScalar, Hashing) {
using T = typename TypeParam::c_type;
using ScalarType = typename TypeTraits<TypeParam>::ScalarType;
std::unordered_set<std::shared_ptr<Scalar>, Scalar::Hash, Scalar::PtrsEqual> set;
set.emplace(std::make_shared<ScalarType>());
for (T i = 0; i < 10; ++i) {
set.emplace(std::make_shared<ScalarType>(i));
}
ASSERT_FALSE(set.emplace(std::make_shared<ScalarType>()).second);
for (T i = 0; i < 10; ++i) {
ASSERT_FALSE(set.emplace(std::make_shared<ScalarType>(i)).second);
}
}
TYPED_TEST(TestNumericScalar, MakeScalar) {
using T = typename TypeParam::c_type;
using ScalarType = typename TypeTraits<TypeParam>::ScalarType;
auto type = TypeTraits<TypeParam>::type_singleton();
std::shared_ptr<Scalar> three = MakeScalar(static_cast<T>(3));
ASSERT_OK(three->ValidateFull());
ASSERT_EQ(ScalarType(3), *three);
AssertMakeScalar(ScalarType(3), type, static_cast<T>(3));
AssertParseScalar(type, "3", ScalarType(3));
}
template <typename T>
class TestRealScalar : public ::testing::Test {
public:
using CType = typename T::c_type;
using ScalarType = typename TypeTraits<T>::ScalarType;
void SetUp() {
type_ = TypeTraits<T>::type_singleton();
scalar_val_ = std::make_shared<ScalarType>(static_cast<CType>(1));
ASSERT_TRUE(scalar_val_->is_valid);
scalar_other_ = std::make_shared<ScalarType>(static_cast<CType>(1.1));
ASSERT_TRUE(scalar_other_->is_valid);
scalar_zero_ = std::make_shared<ScalarType>(static_cast<CType>(0.0));
scalar_other_zero_ = std::make_shared<ScalarType>(static_cast<CType>(0.0));
scalar_neg_zero_ = std::make_shared<ScalarType>(static_cast<CType>(-0.0));
const CType nan_value = std::numeric_limits<CType>::quiet_NaN();
scalar_nan_ = std::make_shared<ScalarType>(nan_value);
ASSERT_TRUE(scalar_nan_->is_valid);
const CType other_nan_value = std::numeric_limits<CType>::quiet_NaN();
scalar_other_nan_ = std::make_shared<ScalarType>(other_nan_value);
ASSERT_TRUE(scalar_other_nan_->is_valid);
}
void TestNanEquals() {
EqualOptions options = EqualOptions::Defaults();
ASSERT_FALSE(scalar_nan_->Equals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->Equals(*scalar_nan_, options));
ASSERT_FALSE(scalar_nan_->Equals(*scalar_other_nan_, options));
options = options.nans_equal(true);
ASSERT_FALSE(scalar_nan_->Equals(*scalar_val_, options));
ASSERT_TRUE(scalar_nan_->Equals(*scalar_nan_, options));
ASSERT_TRUE(scalar_nan_->Equals(*scalar_other_nan_, options));
}
void TestSignedZeroEquals() {
EqualOptions options = EqualOptions::Defaults();
ASSERT_FALSE(scalar_zero_->Equals(*scalar_val_, options));
ASSERT_TRUE(scalar_zero_->Equals(*scalar_other_zero_, options));
ASSERT_TRUE(scalar_zero_->Equals(*scalar_neg_zero_, options));
options = options.signed_zeros_equal(false);
ASSERT_FALSE(scalar_zero_->Equals(*scalar_val_, options));
ASSERT_TRUE(scalar_zero_->Equals(*scalar_other_zero_, options));
ASSERT_FALSE(scalar_zero_->Equals(*scalar_neg_zero_, options));
}
void TestApproxEquals() {
// The scalars are unequal with the small delta
EqualOptions options = EqualOptions::Defaults().atol(0.05);
ASSERT_FALSE(scalar_val_->ApproxEquals(*scalar_other_, options));
ASSERT_FALSE(scalar_other_->ApproxEquals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->ApproxEquals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->ApproxEquals(*scalar_other_nan_, options));
// After enlarging the delta, they become equal
options = options.atol(0.15);
ASSERT_TRUE(scalar_val_->ApproxEquals(*scalar_other_, options));
ASSERT_TRUE(scalar_other_->ApproxEquals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->ApproxEquals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->ApproxEquals(*scalar_other_nan_, options));
options = options.nans_equal(true);
ASSERT_TRUE(scalar_val_->ApproxEquals(*scalar_other_, options));
ASSERT_TRUE(scalar_other_->ApproxEquals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->ApproxEquals(*scalar_val_, options));
ASSERT_TRUE(scalar_nan_->ApproxEquals(*scalar_other_nan_, options));
options = options.atol(0.05);
ASSERT_FALSE(scalar_val_->ApproxEquals(*scalar_other_, options));
ASSERT_FALSE(scalar_other_->ApproxEquals(*scalar_val_, options));
ASSERT_FALSE(scalar_nan_->ApproxEquals(*scalar_val_, options));
ASSERT_TRUE(scalar_nan_->ApproxEquals(*scalar_other_nan_, options));
// Negative zeros
ASSERT_FALSE(scalar_zero_->ApproxEquals(*scalar_val_, options));
ASSERT_TRUE(scalar_zero_->ApproxEquals(*scalar_other_zero_, options));
ASSERT_TRUE(scalar_zero_->ApproxEquals(*scalar_neg_zero_, options));
options = options.signed_zeros_equal(false);
ASSERT_FALSE(scalar_zero_->ApproxEquals(*scalar_val_, options));
ASSERT_TRUE(scalar_zero_->ApproxEquals(*scalar_other_zero_, options));
ASSERT_FALSE(scalar_zero_->ApproxEquals(*scalar_neg_zero_, options));
}
void TestStructOf() {
auto ty = struct_({field("float", type_)});
StructScalar struct_val({scalar_val_}, ty);
StructScalar struct_other_val({scalar_other_}, ty);
StructScalar struct_nan({scalar_nan_}, ty);
StructScalar struct_other_nan({scalar_other_nan_}, ty);
EqualOptions options = EqualOptions::Defaults().atol(0.05);
ASSERT_FALSE(struct_val.Equals(struct_other_val, options));
ASSERT_FALSE(struct_other_val.Equals(struct_val, options));
ASSERT_FALSE(struct_nan.Equals(struct_val, options));
ASSERT_FALSE(struct_nan.Equals(struct_nan, options));
ASSERT_FALSE(struct_nan.Equals(struct_other_nan, options));
ASSERT_FALSE(struct_val.ApproxEquals(struct_other_val, options));
ASSERT_FALSE(struct_other_val.ApproxEquals(struct_val, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_val, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_nan, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_other_nan, options));
options = options.atol(0.15);
ASSERT_FALSE(struct_val.Equals(struct_other_val, options));
ASSERT_FALSE(struct_other_val.Equals(struct_val, options));
ASSERT_FALSE(struct_nan.Equals(struct_val, options));
ASSERT_FALSE(struct_nan.Equals(struct_nan, options));
ASSERT_FALSE(struct_nan.Equals(struct_other_nan, options));
ASSERT_TRUE(struct_val.ApproxEquals(struct_other_val, options));
ASSERT_TRUE(struct_other_val.ApproxEquals(struct_val, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_val, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_nan, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_other_nan, options));
options = options.nans_equal(true);
ASSERT_FALSE(struct_val.Equals(struct_other_val, options));
ASSERT_FALSE(struct_other_val.Equals(struct_val, options));
ASSERT_FALSE(struct_nan.Equals(struct_val, options));
ASSERT_TRUE(struct_nan.Equals(struct_nan, options));
ASSERT_TRUE(struct_nan.Equals(struct_other_nan, options));
ASSERT_TRUE(struct_val.ApproxEquals(struct_other_val, options));
ASSERT_TRUE(struct_other_val.ApproxEquals(struct_val, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_val, options));
ASSERT_TRUE(struct_nan.ApproxEquals(struct_nan, options));
ASSERT_TRUE(struct_nan.ApproxEquals(struct_other_nan, options));
options = options.atol(0.05);
ASSERT_FALSE(struct_val.Equals(struct_other_val, options));
ASSERT_FALSE(struct_other_val.Equals(struct_val, options));
ASSERT_FALSE(struct_nan.Equals(struct_val, options));
ASSERT_TRUE(struct_nan.Equals(struct_nan, options));
ASSERT_TRUE(struct_nan.Equals(struct_other_nan, options));
ASSERT_FALSE(struct_val.ApproxEquals(struct_other_val, options));
ASSERT_FALSE(struct_other_val.ApproxEquals(struct_val, options));
ASSERT_FALSE(struct_nan.ApproxEquals(struct_val, options));
ASSERT_TRUE(struct_nan.ApproxEquals(struct_nan, options));
ASSERT_TRUE(struct_nan.ApproxEquals(struct_other_nan, options));
}
void TestListOf() {
auto ty = list(type_);
ListScalar list_val(ArrayFromJSON(type_, "[0, null, 1.0]"), ty);
ListScalar list_other_val(ArrayFromJSON(type_, "[0, null, 1.1]"), ty);
ListScalar list_nan(ArrayFromJSON(type_, "[0, null, NaN]"), ty);
ListScalar list_other_nan(ArrayFromJSON(type_, "[0, null, NaN]"), ty);
EqualOptions options = EqualOptions::Defaults().atol(0.05);
ASSERT_TRUE(list_val.Equals(list_val, options));
ASSERT_FALSE(list_val.Equals(list_other_val, options));
ASSERT_FALSE(list_nan.Equals(list_val, options));
ASSERT_FALSE(list_nan.Equals(list_nan, options));
ASSERT_FALSE(list_nan.Equals(list_other_nan, options));
ASSERT_TRUE(list_val.ApproxEquals(list_val, options));
ASSERT_FALSE(list_val.ApproxEquals(list_other_val, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_val, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_nan, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_other_nan, options));
options = options.atol(0.15);
ASSERT_TRUE(list_val.Equals(list_val, options));
ASSERT_FALSE(list_val.Equals(list_other_val, options));
ASSERT_FALSE(list_nan.Equals(list_val, options));
ASSERT_FALSE(list_nan.Equals(list_nan, options));
ASSERT_FALSE(list_nan.Equals(list_other_nan, options));
ASSERT_TRUE(list_val.ApproxEquals(list_val, options));
ASSERT_TRUE(list_val.ApproxEquals(list_other_val, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_val, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_nan, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_other_nan, options));
options = options.nans_equal(true);
ASSERT_TRUE(list_val.Equals(list_val, options));
ASSERT_FALSE(list_val.Equals(list_other_val, options));
ASSERT_FALSE(list_nan.Equals(list_val, options));
ASSERT_TRUE(list_nan.Equals(list_nan, options));
ASSERT_TRUE(list_nan.Equals(list_other_nan, options));
ASSERT_TRUE(list_val.ApproxEquals(list_val, options));
ASSERT_TRUE(list_val.ApproxEquals(list_other_val, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_val, options));
ASSERT_TRUE(list_nan.ApproxEquals(list_nan, options));
ASSERT_TRUE(list_nan.ApproxEquals(list_other_nan, options));
options = options.atol(0.05);
ASSERT_TRUE(list_val.Equals(list_val, options));
ASSERT_FALSE(list_val.Equals(list_other_val, options));
ASSERT_FALSE(list_nan.Equals(list_val, options));
ASSERT_TRUE(list_nan.Equals(list_nan, options));
ASSERT_TRUE(list_nan.Equals(list_other_nan, options));
ASSERT_TRUE(list_val.ApproxEquals(list_val, options));
ASSERT_FALSE(list_val.ApproxEquals(list_other_val, options));
ASSERT_FALSE(list_nan.ApproxEquals(list_val, options));
ASSERT_TRUE(list_nan.ApproxEquals(list_nan, options));
ASSERT_TRUE(list_nan.ApproxEquals(list_other_nan, options));
}
protected:
std::shared_ptr<DataType> type_;
std::shared_ptr<Scalar> scalar_val_, scalar_other_, scalar_nan_, scalar_other_nan_,
scalar_zero_, scalar_other_zero_, scalar_neg_zero_;
};
TYPED_TEST_SUITE(TestRealScalar, RealArrowTypes);
TYPED_TEST(TestRealScalar, NanEquals) { this->TestNanEquals(); }
TYPED_TEST(TestRealScalar, SignedZeroEquals) { this->TestSignedZeroEquals(); }
TYPED_TEST(TestRealScalar, ApproxEquals) { this->TestApproxEquals(); }
TYPED_TEST(TestRealScalar, StructOf) { this->TestStructOf(); }
TYPED_TEST(TestRealScalar, ListOf) { this->TestListOf(); }
template <typename T>
class TestDecimalScalar : public ::testing::Test {
public:
using ScalarType = typename TypeTraits<T>::ScalarType;
using ValueType = typename ScalarType::ValueType;
void TestBasics() {
const auto ty = std::make_shared<T>(3, 2);
const auto pi = ScalarType(ValueType(314), ty);
const auto pi2 = ScalarType(ValueType(628), ty);
const auto null = CheckMakeNullScalar(ty);
ASSERT_OK(pi.ValidateFull());
ASSERT_TRUE(pi.is_valid);
ASSERT_EQ(pi.value, ValueType("3.14"));
ASSERT_OK(null->ValidateFull());
ASSERT_FALSE(null->is_valid);
ASSERT_FALSE(pi.Equals(pi2));
// Test Array::GetScalar
auto arr = ArrayFromJSON(ty, "[null, \"3.14\"]");
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto second, arr->GetScalar(1));
ASSERT_OK(first->ValidateFull());
ASSERT_OK(second->ValidateFull());
ASSERT_TRUE(first->Equals(null));
ASSERT_FALSE(first->Equals(pi));
ASSERT_TRUE(second->Equals(pi));
ASSERT_FALSE(second->Equals(null));
auto invalid = ScalarType(ValueType::GetMaxValue(6), std::make_shared<T>(5, 2));
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid,
::testing::HasSubstr("does not fit in precision of"),
invalid.ValidateFull());
}
};
TYPED_TEST_SUITE(TestDecimalScalar, DecimalArrowTypes);
TYPED_TEST(TestDecimalScalar, Basics) { this->TestBasics(); }
TEST(TestBinaryScalar, Basics) {
std::string data = "test data";
auto buf = std::make_shared<Buffer>(data);
BinaryScalar value(buf);
ASSERT_OK(value.ValidateFull());
ASSERT_TRUE(value.value->Equals(*buf));
ASSERT_TRUE(value.is_valid);
ASSERT_TRUE(value.type->Equals(*binary()));
auto ref_count = buf.use_count();
// Check that destructor doesn't fail to clean up a buffer
std::shared_ptr<Scalar> base_ref = std::make_shared<BinaryScalar>(buf);
base_ref = nullptr;
ASSERT_EQ(ref_count, buf.use_count());
BinaryScalar null_value;
ASSERT_FALSE(null_value.is_valid);
ASSERT_EQ(null_value.value, nullptr);
ASSERT_OK(null_value.ValidateFull());
StringScalar value2(buf);
ASSERT_OK(value2.ValidateFull());
ASSERT_TRUE(value2.value->Equals(*buf));
ASSERT_TRUE(value2.is_valid);
ASSERT_TRUE(value2.type->Equals(*utf8()));
// Same buffer, different type.
ASSERT_NE(value2, value);
StringScalar value3(buf);
// Same buffer, same type.
ASSERT_EQ(value2, value3);
StringScalar null_value2;
ASSERT_FALSE(null_value2.is_valid);
// test Array.GetScalar
auto arr = ArrayFromJSON(binary(), "[null, \"one\", \"two\"]");
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto one, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto two, arr->GetScalar(2));
ASSERT_OK(null->ValidateFull());
ASSERT_OK(one->ValidateFull());
ASSERT_OK(two->ValidateFull());
ASSERT_TRUE(null->Equals(null_value));
ASSERT_TRUE(one->Equals(BinaryScalar(Buffer::FromString("one"))));
ASSERT_TRUE(two->Equals(BinaryScalar(Buffer::FromString("two"))));
ASSERT_FALSE(two->Equals(BinaryScalar(Buffer::FromString("else"))));
}
TEST(TestBinaryScalar, Hashing) {
auto FromInt = [](int i) {
return std::make_shared<BinaryScalar>(Buffer::FromString(std::to_string(i)));
};
std::unordered_set<std::shared_ptr<Scalar>, Scalar::Hash, Scalar::PtrsEqual> set;
set.emplace(std::make_shared<BinaryScalar>());
for (int i = 0; i < 10; ++i) {
set.emplace(FromInt(i));
}
ASSERT_FALSE(set.emplace(std::make_shared<BinaryScalar>()).second);
for (int i = 0; i < 10; ++i) {
ASSERT_FALSE(set.emplace(FromInt(i)).second);
}
}
TEST(TestBinaryScalar, ValidateErrors) {
// Value must be null when the scalar is null
BinaryScalar scalar(Buffer::FromString("xxx"));
scalar.is_valid = false;
AssertValidationFails(scalar);
// Value must be non-null
auto null_scalar = MakeNullScalar(binary());
null_scalar->is_valid = true;
AssertValidationFails(*null_scalar);
}
template <typename T>
class TestStringScalar : public ::testing::Test {
public:
using ScalarType = typename TypeTraits<T>::ScalarType;
void SetUp() { type_ = TypeTraits<T>::type_singleton(); }
void TestMakeScalar() {
AssertMakeScalar(ScalarType("three"), type_, Buffer::FromString("three"));
AssertParseScalar(type_, "three", ScalarType("three"));
}
void TestArrayGetScalar() {
auto arr = ArrayFromJSON(type_, R"([null, "one", "two"])");
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto one, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto two, arr->GetScalar(2));
ASSERT_OK(null->ValidateFull());
ASSERT_OK(one->ValidateFull());
ASSERT_OK(two->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(type_)));
ASSERT_TRUE(one->Equals(ScalarType("one")));
ASSERT_TRUE(two->Equals(ScalarType("two")));
ASSERT_FALSE(two->Equals(Int64Scalar(1)));
}
void TestValidateErrors() {
// Inconsistent is_valid / value
ScalarType scalar(Buffer::FromString("xxx"));
scalar.is_valid = false;
AssertValidationFails(scalar);
auto null_scalar = MakeNullScalar(type_);
null_scalar->is_valid = true;
AssertValidationFails(*null_scalar);
// Invalid UTF8
scalar = ScalarType(Buffer::FromString("\xff"));
ASSERT_OK(scalar.Validate());
ASSERT_RAISES(Invalid, scalar.ValidateFull());
}
protected:
std::shared_ptr<DataType> type_;
};
TYPED_TEST_SUITE(TestStringScalar, StringArrowTypes);
TYPED_TEST(TestStringScalar, MakeScalar) { this->TestMakeScalar(); }
TYPED_TEST(TestStringScalar, ArrayGetScalar) { this->TestArrayGetScalar(); }
TYPED_TEST(TestStringScalar, ValidateErrors) { this->TestValidateErrors(); }
TEST(TestStringScalar, MakeScalarImplicit) {
// MakeScalar("string literal") creates a StringScalar
auto three = MakeScalar("three");
ASSERT_OK(three->ValidateFull());
ASSERT_EQ(StringScalar("three"), *three);
}
TEST(TestStringScalar, MakeScalarString) {
// MakeScalar(std::string) creates a StringScalar via FromBuffer
std::string buf = "three";
auto three = MakeScalar(std::move(buf));
ASSERT_OK(three->ValidateFull());
ASSERT_EQ(StringScalar("three"), *three);
}
TEST(TestFixedSizeBinaryScalar, Basics) {
std::string data = "test data";
auto buf = std::make_shared<Buffer>(data);
auto ex_type = fixed_size_binary(9);
FixedSizeBinaryScalar value(buf, ex_type);
ASSERT_OK(value.ValidateFull());
ASSERT_TRUE(value.value->Equals(*buf));
ASSERT_TRUE(value.is_valid);
ASSERT_TRUE(value.type->Equals(*ex_type));
FixedSizeBinaryScalar null_value(buf, ex_type, /*is_valid=*/false);
ASSERT_OK(null_value.ValidateFull());
ASSERT_FALSE(null_value.is_valid);
ASSERT_TRUE(null_value.value->Equals(*buf));
// test Array.GetScalar
auto ty = fixed_size_binary(3);
auto arr = ArrayFromJSON(ty, R"([null, "one", "two"])");
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto one, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto two, arr->GetScalar(2));
ASSERT_OK(null->ValidateFull());
ASSERT_OK(one->ValidateFull());
ASSERT_OK(two->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(ty)));
ASSERT_TRUE(one->Equals(FixedSizeBinaryScalar(Buffer::FromString("one"), ty)));
ASSERT_TRUE(two->Equals(FixedSizeBinaryScalar(Buffer::FromString("two"), ty)));
}
TEST(TestFixedSizeBinaryScalar, MakeScalar) {
std::string data = "test data";
auto buf = std::make_shared<Buffer>(data);
auto type = fixed_size_binary(9);
AssertMakeScalar(FixedSizeBinaryScalar(buf, type), type, buf);
AssertParseScalar(type, std::string_view(data), FixedSizeBinaryScalar(buf, type));
// Wrong length
ASSERT_RAISES(Invalid, MakeScalar(type, Buffer::FromString(data.substr(3))).status());
ASSERT_RAISES(Invalid, Scalar::Parse(type, std::string_view(data).substr(3)).status());
}
TEST(TestFixedSizeBinaryScalar, ValidateErrors) {
std::string data = "test data";
auto buf = std::make_shared<Buffer>(data);
auto type = fixed_size_binary(9);
FixedSizeBinaryScalar scalar(buf, type);
ASSERT_OK(scalar.ValidateFull());
scalar.value = SliceBuffer(buf, 1);
AssertValidationFails(scalar);
}
TEST(TestDateScalars, Basics) {
int32_t i32_val = 1;
Date32Scalar date32_val(i32_val);
Date32Scalar date32_null;
ASSERT_OK(date32_val.ValidateFull());
ASSERT_OK(date32_null.ValidateFull());
ASSERT_TRUE(date32_val.type->Equals(*date32()));
ASSERT_TRUE(date32_val.is_valid);
ASSERT_FALSE(date32_null.is_valid);
int64_t i64_val = 2;
Date64Scalar date64_val(i64_val);
Date64Scalar date64_null;
ASSERT_OK(date64_val.ValidateFull());
ASSERT_OK(date64_null.ValidateFull());
ASSERT_EQ(i64_val, date64_val.value);
ASSERT_TRUE(date64_val.type->Equals(*date64()));
ASSERT_TRUE(date64_val.is_valid);
ASSERT_FALSE(date64_null.is_valid);
// test Array.GetScalar
for (auto ty : {date32(), date64()}) {
auto arr = ArrayFromJSON(ty, "[5, null, 42]");
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto last, arr->GetScalar(2));
ASSERT_OK(first->ValidateFull());
ASSERT_OK(null->ValidateFull());
ASSERT_OK(last->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(ty)));
ASSERT_TRUE(first->Equals(MakeScalar(ty, 5).ValueOrDie()));
ASSERT_TRUE(last->Equals(MakeScalar(ty, 42).ValueOrDie()));
ASSERT_FALSE(last->Equals(MakeScalar("string")));
}
}
TEST(TestDateScalars, MakeScalar) {
AssertMakeScalar(Date32Scalar(1), date32(), int32_t(1));
AssertParseScalar(date32(), "1454-10-22", Date32Scalar(-188171));
AssertMakeScalar(Date64Scalar(1), date64(), int64_t(1));
AssertParseScalar(date64(), "1454-10-22",
Date64Scalar(-188171LL * 24 * 60 * 60 * 1000));
}
TEST(TestTimeScalars, Basics) {
auto type1 = time32(TimeUnit::MILLI);
auto type2 = time32(TimeUnit::SECOND);
auto type3 = time64(TimeUnit::MICRO);
auto type4 = time64(TimeUnit::NANO);
int32_t i32_val = 1;
Time32Scalar time32_val(i32_val, type1);
Time32Scalar time32_null(type2);
ASSERT_OK(time32_val.ValidateFull());
ASSERT_OK(time32_null.ValidateFull());
ASSERT_EQ(i32_val, time32_val.value);
ASSERT_TRUE(time32_val.type->Equals(*type1));
ASSERT_TRUE(time32_val.is_valid);
ASSERT_FALSE(time32_null.is_valid);
ASSERT_TRUE(time32_null.type->Equals(*type2));
int64_t i64_val = 2;
Time64Scalar time64_val(i64_val, type3);
Time64Scalar time64_null(type4);
ASSERT_OK(time64_val.ValidateFull());
ASSERT_OK(time64_null.ValidateFull());
ASSERT_EQ(i64_val, time64_val.value);
ASSERT_TRUE(time64_val.type->Equals(*type3));
ASSERT_TRUE(time64_val.is_valid);
ASSERT_FALSE(time64_null.is_valid);
ASSERT_TRUE(time64_null.type->Equals(*type4));
// test Array.GetScalar
for (auto ty : {type1, type2, type3, type4}) {
auto arr = ArrayFromJSON(ty, "[5, null, 42]");
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto last, arr->GetScalar(2));
ASSERT_OK(first->ValidateFull());
ASSERT_OK(null->ValidateFull());
ASSERT_OK(last->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(ty)));
ASSERT_TRUE(first->Equals(MakeScalar(ty, 5).ValueOrDie()));
ASSERT_TRUE(last->Equals(MakeScalar(ty, 42).ValueOrDie()));
ASSERT_FALSE(last->Equals(MakeScalar("string")));
}
}
TEST(TestTimeScalars, MakeScalar) {
auto type1 = time32(TimeUnit::SECOND);
auto type2 = time32(TimeUnit::MILLI);
auto type3 = time64(TimeUnit::MICRO);
auto type4 = time64(TimeUnit::NANO);
AssertMakeScalar(Time32Scalar(1, type1), type1, int32_t(1));
AssertMakeScalar(Time32Scalar(1, type2), type2, int32_t(1));
AssertMakeScalar(Time64Scalar(1, type3), type3, int32_t(1));
AssertMakeScalar(Time64Scalar(1, type4), type4, int32_t(1));
int64_t tententen = 60 * (60 * (10) + 10) + 10;
AssertParseScalar(type1, "10:10:10",
Time32Scalar(static_cast<int32_t>(tententen), type1));
tententen = 1000 * tententen + 123;
AssertParseScalar(type2, "10:10:10.123",
Time32Scalar(static_cast<int32_t>(tententen), type2));
tententen = 1000 * tententen + 456;
AssertParseScalar(type3, "10:10:10.123456", Time64Scalar(tententen, type3));
tententen = 1000 * tententen + 789;
AssertParseScalar(type4, "10:10:10.123456789", Time64Scalar(tententen, type4));
}
TEST(TestTimestampScalars, Basics) {
auto type1 = timestamp(TimeUnit::MILLI);
auto type2 = timestamp(TimeUnit::SECOND);
int64_t val1 = 1;
int64_t val2 = 2;
TimestampScalar ts_val1(val1, type1);
TimestampScalar ts_val2(val2, type2);
TimestampScalar ts_null(type1);
ASSERT_OK(ts_val1.ValidateFull());
ASSERT_OK(ts_val2.ValidateFull());
ASSERT_OK(ts_null.ValidateFull());
ASSERT_EQ(val1, ts_val1.value);
ASSERT_TRUE(ts_val1.type->Equals(*type1));
ASSERT_TRUE(ts_val2.type->Equals(*type2));
ASSERT_TRUE(ts_val1.is_valid);
ASSERT_FALSE(ts_null.is_valid);
ASSERT_TRUE(ts_null.type->Equals(*type1));
ASSERT_NE(ts_val1, ts_val2);
ASSERT_NE(ts_val1, ts_null);
ASSERT_NE(ts_val2, ts_null);
// test Array.GetScalar
for (auto ty : {type1, type2}) {
auto arr = ArrayFromJSON(ty, "[5, null, 42]");
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto last, arr->GetScalar(2));
ASSERT_OK(first->ValidateFull());
ASSERT_OK(null->ValidateFull());
ASSERT_OK(last->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(ty)));
ASSERT_TRUE(first->Equals(MakeScalar(ty, 5).ValueOrDie()));
ASSERT_TRUE(last->Equals(MakeScalar(ty, 42).ValueOrDie()));
ASSERT_FALSE(last->Equals(MakeScalar(int64(), 42).ValueOrDie()));
}
}
TEST(TestTimestampScalars, MakeScalar) {
auto type1 = timestamp(TimeUnit::MILLI);
auto type2 = timestamp(TimeUnit::SECOND);
auto type3 = timestamp(TimeUnit::MICRO);
auto type4 = timestamp(TimeUnit::NANO);
std::string_view epoch_plus_1s = "1970-01-01 00:00:01";
AssertMakeScalar(TimestampScalar(1, type1), type1, int64_t(1));
AssertParseScalar(type1, epoch_plus_1s, TimestampScalar(1000, type1));
AssertMakeScalar(TimestampScalar(1, type2), type2, int64_t(1));
AssertParseScalar(type2, epoch_plus_1s, TimestampScalar(1, type2));
AssertMakeScalar(TimestampScalar(1, type3), type3, int64_t(1));
AssertParseScalar(type3, epoch_plus_1s, TimestampScalar(1000 * 1000, type3));
AssertMakeScalar(TimestampScalar(1, type4), type4, int64_t(1));
AssertParseScalar(type4, epoch_plus_1s, TimestampScalar(1000 * 1000 * 1000, type4));
}
TEST(TestTimestampScalars, Cast) {
auto convert = [](TimeUnit::type in, TimeUnit::type out, int64_t value) -> int64_t {
auto scalar =
TimestampScalar(value, timestamp(in)).CastTo(timestamp(out)).ValueOrDie();
return internal::checked_pointer_cast<TimestampScalar>(scalar)->value;
};
EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::MILLI, 1), 1000);
EXPECT_EQ(convert(TimeUnit::SECOND, TimeUnit::NANO, 1), 1000000000);
EXPECT_EQ(convert(TimeUnit::NANO, TimeUnit::MICRO, 1234), 1);
EXPECT_EQ(convert(TimeUnit::MICRO, TimeUnit::MILLI, 4567), 4);
ASSERT_OK_AND_ASSIGN(auto str,
TimestampScalar(1024, timestamp(TimeUnit::MILLI)).CastTo(utf8()));
EXPECT_EQ(*str, StringScalar("1970-01-01 00:00:01.024"));
ASSERT_OK_AND_ASSIGN(auto i64,
TimestampScalar(1024, timestamp(TimeUnit::MILLI)).CastTo(int64()));
EXPECT_EQ(*i64, Int64Scalar(1024));
constexpr int64_t kMillisecondsInDay = 86400000;
ASSERT_OK_AND_ASSIGN(
auto d64, TimestampScalar(1024 * kMillisecondsInDay + 3, timestamp(TimeUnit::MILLI))
.CastTo(date64()));
EXPECT_EQ(*d64, Date64Scalar(1024 * kMillisecondsInDay));
}
TEST(TestDurationScalars, Basics) {
auto type1 = duration(TimeUnit::MILLI);
auto type2 = duration(TimeUnit::SECOND);
int64_t val1 = 1;
int64_t val2 = 2;
DurationScalar ts_val1(val1, type1);
DurationScalar ts_val2(val2, type2);
DurationScalar ts_null(type1);
ASSERT_OK(ts_val1.ValidateFull());
ASSERT_OK(ts_val2.ValidateFull());
ASSERT_OK(ts_null.ValidateFull());
ASSERT_EQ(val1, ts_val1.value);
ASSERT_TRUE(ts_val1.type->Equals(*type1));
ASSERT_TRUE(ts_val2.type->Equals(*type2));
ASSERT_TRUE(ts_val1.is_valid);
ASSERT_FALSE(ts_null.is_valid);
ASSERT_TRUE(ts_null.type->Equals(*type1));
ASSERT_NE(ts_val1, ts_val2);
ASSERT_NE(ts_val1, ts_null);
ASSERT_NE(ts_val2, ts_null);
// test Array.GetScalar
for (auto ty : {type1, type2}) {
auto arr = ArrayFromJSON(ty, "[5, null, 42]");
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto last, arr->GetScalar(2));
ASSERT_OK(first->ValidateFull());
ASSERT_OK(null->ValidateFull());
ASSERT_OK(last->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(ty)));
ASSERT_TRUE(first->Equals(MakeScalar(ty, 5).ValueOrDie()));
ASSERT_TRUE(last->Equals(MakeScalar(ty, 42).ValueOrDie()));
}
EXPECT_EQ(DurationScalar{std::chrono::nanoseconds{1235}},
DurationScalar(1235, TimeUnit::NANO));
EXPECT_EQ(DurationScalar{std::chrono::microseconds{58}},
DurationScalar(58, TimeUnit::MICRO));
EXPECT_EQ(DurationScalar{std::chrono::milliseconds{952}},
DurationScalar(952, TimeUnit::MILLI));
EXPECT_EQ(DurationScalar{std::chrono::seconds{625}},
DurationScalar(625, TimeUnit::SECOND));
EXPECT_EQ(DurationScalar{std::chrono::minutes{2}},
DurationScalar(120, TimeUnit::SECOND));
// finer than nanoseconds; we can't represent this without truncation
using picoseconds = std::chrono::duration<int64_t, std::pico>;
static_assert(!std::is_constructible_v<DurationScalar, picoseconds>);
// between seconds and milliseconds; we could represent this as milliseconds safely, but
// it's a pain to support
using centiseconds = std::chrono::duration<int64_t, std::centi>;
static_assert(!std::is_constructible_v<DurationScalar, centiseconds>);
}
TEST(TestMonthIntervalScalars, Basics) {
auto type = month_interval();
int32_t val1 = 1;
int32_t val2 = 2;
MonthIntervalScalar ts_val1(val1);
MonthIntervalScalar ts_val2(val2);
MonthIntervalScalar ts_null;
ASSERT_OK(ts_val1.ValidateFull());
ASSERT_OK(ts_val2.ValidateFull());
ASSERT_OK(ts_null.ValidateFull());
ASSERT_EQ(val1, ts_val1.value);
ASSERT_TRUE(ts_val1.type->Equals(*type));
ASSERT_TRUE(ts_val2.type->Equals(*type));
ASSERT_TRUE(ts_val1.is_valid);
ASSERT_FALSE(ts_null.is_valid);
ASSERT_TRUE(ts_null.type->Equals(*type));
ASSERT_NE(ts_val1, ts_val2);
ASSERT_NE(ts_val1, ts_null);
ASSERT_NE(ts_val2, ts_null);
// test Array.GetScalar
auto arr = ArrayFromJSON(type, "[5, null, 42]");
ASSERT_OK_AND_ASSIGN(auto first, arr->GetScalar(0));
ASSERT_OK_AND_ASSIGN(auto null, arr->GetScalar(1));
ASSERT_OK_AND_ASSIGN(auto last, arr->GetScalar(2));
ASSERT_OK(first->ValidateFull());
ASSERT_OK(null->ValidateFull());
ASSERT_OK(last->ValidateFull());
ASSERT_TRUE(null->Equals(CheckMakeNullScalar(type)));
ASSERT_TRUE(first->Equals(MakeScalar(type, 5).ValueOrDie()));
ASSERT_TRUE(last->Equals(MakeScalar(type, 42).ValueOrDie()));
}
TEST(TestDayTimeIntervalScalars, Basics) {
auto type = day_time_interval();
DayTimeIntervalType::DayMilliseconds val1 = {1, 1};
DayTimeIntervalType::DayMilliseconds val2 = {2, 2};
DayTimeIntervalScalar ts_val1(val1);
DayTimeIntervalScalar ts_val2(val2);
DayTimeIntervalScalar ts_null;
ASSERT_OK(ts_val1.ValidateFull());
ASSERT_OK(ts_val2.ValidateFull());
ASSERT_OK(ts_null.ValidateFull());
ASSERT_EQ(val1, ts_val1.value);
ASSERT_TRUE(ts_val1.type->Equals(*type));
ASSERT_TRUE(ts_val2.type->Equals(*type));
ASSERT_TRUE(ts_val1.is_valid);
ASSERT_FALSE(ts_null.is_valid);
ASSERT_TRUE(ts_null.type->Equals(*type));
ASSERT_NE(ts_val1, ts_val2);