@@ -275,7 +275,8 @@ struct AltrepVectorPrimitive : public AltrepVectorBase<AltrepVectorPrimitive<sex
275
275
auto altrep_data =
276
276
reinterpret_cast <ArrowAltrepData*>(R_ExternalPtrAddr (R_altrep_data1 (alt)));
277
277
auto resolve = altrep_data->locate (i);
278
- const auto & array = altrep_data->chunked_array ()->chunk (resolve.chunk_index );
278
+ const auto & array =
279
+ altrep_data->chunked_array ()->chunk (static_cast <int >(resolve.chunk_index ));
279
280
auto j = resolve.index_in_chunk ;
280
281
281
282
return array->IsNull (j) ? cpp11::na<c_type>()
@@ -466,10 +467,10 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
466
467
std::unique_ptr<arrow::DictionaryUnifier> unifier_ =
467
468
ValueOrStop (DictionaryUnifier::Make (arr_type.value_type ()));
468
469
469
- size_t n_arrays = chunked_array->num_chunks ();
470
+ int n_arrays = chunked_array->num_chunks ();
470
471
BufferVector arrays_transpose (n_arrays);
471
472
472
- for (size_t i = 0 ; i < n_arrays; i++) {
473
+ for (int i = 0 ; i < n_arrays; i++) {
473
474
const auto & dict_i =
474
475
*internal::checked_cast<const DictionaryArray&>(*chunked_array->chunk (i))
475
476
.dictionary ();
@@ -559,17 +560,14 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
559
560
return dup ;
560
561
}
561
562
562
- // The value at position i
563
- static int Elt (SEXP alt, R_xlen_t i) {
564
- if (Base::IsMaterialized (alt)) {
565
- return INTEGER_ELT (Representation (alt), i);
566
- }
567
-
563
+ // The value at position i as an int64_t (to make bounds checking less verbose)
564
+ static int64_t Elt64 (SEXP alt, R_xlen_t i) {
568
565
auto altrep_data =
569
566
reinterpret_cast <ArrowAltrepData*>(R_ExternalPtrAddr (R_altrep_data1 (alt)));
570
567
auto resolve = altrep_data->locate (i);
571
568
572
- const auto & array = altrep_data->chunked_array ()->chunk (resolve.chunk_index );
569
+ const auto & array =
570
+ altrep_data->chunked_array ()->chunk (static_cast <int >(resolve.chunk_index ));
573
571
auto j = resolve.index_in_chunk ;
574
572
575
573
if (!array->IsNull (j)) {
@@ -578,7 +576,7 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
578
576
579
577
if (WasUnified (alt)) {
580
578
const auto * transpose_data = reinterpret_cast <const int32_t *>(
581
- GetArrayTransposed (alt, resolve.chunk_index )->data ());
579
+ GetArrayTransposed (alt, static_cast < int >( resolve.chunk_index ) )->data ());
582
580
583
581
switch (indices->type_id ()) {
584
582
case Type::UINT8:
@@ -617,7 +615,7 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
617
615
case Type::INT64:
618
616
return indices->data ()->GetValues <int64_t >(1 )[j] + 1 ;
619
617
case Type::UINT64:
620
- return indices->data ()->GetValues <uint64_t >(1 )[j] + 1 ;
618
+ return static_cast < int64_t >( indices->data ()->GetValues <uint64_t >(1 )[j] + 1 ) ;
621
619
default :
622
620
break ;
623
621
}
@@ -628,6 +626,18 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
628
626
return NA_INTEGER;
629
627
}
630
628
629
+ // The value at position i as an int (which R needs because this is a factor)
630
+ static int Elt (SEXP alt, R_xlen_t i) {
631
+ if (Base::IsMaterialized (alt)) {
632
+ return INTEGER_ELT (Representation (alt), i);
633
+ }
634
+
635
+ int64_t elt64 = Elt64 (alt, i);
636
+ ARROW_R_DCHECK (elt64 == NA_INTEGER || elt64 >= 1 );
637
+ ARROW_R_DCHECK (elt64 <= std::numeric_limits<int >::max ());
638
+ return static_cast <int >(elt64);
639
+ }
640
+
631
641
static R_xlen_t Get_region (SEXP alt, R_xlen_t start, R_xlen_t n, int * buf) {
632
642
// If we have data2, we can just copy the region into buf
633
643
// using the standard Get_region for this R type
@@ -667,7 +677,7 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
667
677
// using the transpose data for this chunk
668
678
const auto * transpose_data =
669
679
reinterpret_cast <const int32_t *>(GetArrayTransposed (alt, j)->data ());
670
- auto transpose = [transpose_data](int x) { return transpose_data[x]; };
680
+ auto transpose = [transpose_data](int64_t x) { return transpose_data[x]; };
671
681
672
682
GetRegionDispatch (array, indices, transpose, out);
673
683
@@ -677,7 +687,7 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
677
687
678
688
} else {
679
689
// simpler case, identity transpose
680
- auto transpose = [](int x) { return x ; };
690
+ auto transpose = [](int64_t x) { return static_cast < int >(x) ; };
681
691
682
692
int * out = buf;
683
693
for (const auto & array : slice->chunks ()) {
@@ -718,7 +728,13 @@ struct AltrepFactor : public AltrepVectorBase<AltrepFactor> {
718
728
719
729
VisitArraySpanInline<Type>(
720
730
*array->data (),
721
- /* valid_func=*/ [&](index_type index ) { *out++ = transpose (index ) + 1 ; },
731
+ /* valid_func=*/
732
+ [&](index_type index ) {
733
+ int64_t transposed = transpose (index ) + 1 ;
734
+ ARROW_R_DCHECK (transposed >= 1 );
735
+ ARROW_R_DCHECK (transposed <= std::numeric_limits<int >::max ());
736
+ *out++ = static_cast <int >(transposed);
737
+ },
722
738
/* null_func=*/ [&]() { *out++ = cpp11::na<int >(); });
723
739
}
724
740
@@ -765,7 +781,8 @@ struct AltrepVectorString : public AltrepVectorBase<AltrepVectorString<Type>> {
765
781
bool no_nul = std::find (view_.begin (), view_.end (), ' \0 ' ) == view_.end ();
766
782
767
783
if (no_nul) {
768
- return Rf_mkCharLenCE (view_.data (), view_.size (), CE_UTF8);
784
+ ARROW_R_DCHECK (view_.size () <= std::numeric_limits<int >::max ());
785
+ return Rf_mkCharLenCE (view_.data (), static_cast <int >(view_.size ()), CE_UTF8);
769
786
} else if (strip_out_nuls_) {
770
787
return ConvertStripNul ();
771
788
} else {
@@ -802,7 +819,9 @@ struct AltrepVectorString : public AltrepVectorBase<AltrepVectorString<Type>> {
802
819
}
803
820
804
821
nul_was_stripped_ = true ;
805
- return Rf_mkCharLenCE (stripped_string_.data (), stripped_len, CE_UTF8);
822
+ ARROW_R_DCHECK (stripped_len <= std::numeric_limits<int >::max ());
823
+ return Rf_mkCharLenCE (stripped_string_.data (), static_cast <int >(stripped_len),
824
+ CE_UTF8);
806
825
}
807
826
808
827
bool nul_was_stripped () const { return nul_was_stripped_; }
@@ -847,7 +866,8 @@ struct AltrepVectorString : public AltrepVectorBase<AltrepVectorString<Type>> {
847
866
auto altrep_data =
848
867
reinterpret_cast <ArrowAltrepData*>(R_ExternalPtrAddr (R_altrep_data1 (alt)));
849
868
auto resolve = altrep_data->locate (i);
850
- const auto & array = altrep_data->chunked_array ()->chunk (resolve.chunk_index );
869
+ const auto & array =
870
+ altrep_data->chunked_array ()->chunk (static_cast <int >(resolve.chunk_index ));
851
871
auto j = resolve.index_in_chunk ;
852
872
853
873
SEXP s = NA_STRING;
0 commit comments