@@ -23,6 +23,9 @@ using namespace arrow;
2323namespace arrow {
2424namespace r {
2525
26+ // the integer64 sentinel
27+ static const int64_t NA_INT64 = std::numeric_limits<int64_t >::min();
28+
2629template <int RTYPE, typename Type>
2730std::shared_ptr<Array> SimpleArray (SEXP x) {
2831 Rcpp::Vector<RTYPE> vec (x);
@@ -329,6 +332,47 @@ std::shared_ptr<Array> Date64Array_From_POSIXct(SEXP x) {
329332 return std::make_shared<Date64Array>(data);
330333}
331334
335+ std::shared_ptr<arrow::Array> Int64Array (SEXP x) {
336+ auto p_vec_start = reinterpret_cast <int64_t *>(REAL (x));
337+ auto n = Rf_xlength (x);
338+ int64_t null_count = 0 ;
339+
340+ std::vector<std::shared_ptr<Buffer>> buffers{nullptr ,
341+ std::make_shared<RBuffer<REALSXP>>(x)};
342+
343+ auto p_vec = std::find (p_vec_start, p_vec_start + n, NA_INT64);
344+ auto first_na = p_vec - p_vec_start;
345+ if (first_na < n) {
346+ R_ERROR_NOT_OK (AllocateBuffer (BitUtil::BytesForBits (n), &buffers[0 ]));
347+ internal::FirstTimeBitmapWriter bitmap_writer (buffers[0 ]->mutable_data (), 0 , n);
348+
349+ // first loop to clear all the bits before the first NA
350+ int i = 0 ;
351+ for (; i < first_na; i++, bitmap_writer.Next ()) {
352+ bitmap_writer.Set ();
353+ }
354+
355+ // then finish
356+ for (; i < n; i++, bitmap_writer.Next (), ++p_vec) {
357+ if (*p_vec == NA_INT64) {
358+ bitmap_writer.Clear ();
359+ null_count++;
360+ } else {
361+ bitmap_writer.Set ();
362+ }
363+ }
364+
365+ bitmap_writer.Finish ();
366+ }
367+
368+ auto data = ArrayData::Make (
369+ std::make_shared<Int64Type>(), n, std::move (buffers), null_count, 0 /* offset*/
370+ );
371+
372+ // return the right Array class
373+ return std::make_shared<typename TypeTraits<Int64Type>::ArrayType>(data);
374+ }
375+
332376} // namespace r
333377} // namespace arrow
334378
@@ -355,6 +399,9 @@ std::shared_ptr<arrow::Array> Array__from_vector(SEXP x) {
355399 if (Rf_inherits (x, " POSIXct" )) {
356400 return arrow::r::Date64Array_From_POSIXct<REALSXP>(x);
357401 }
402+ if (Rf_inherits (x, " integer64" )) {
403+ return arrow::r::Int64Array (x);
404+ }
358405 return arrow::r::SimpleArray<REALSXP, arrow::DoubleType>(x);
359406 case RAWSXP:
360407 return arrow::r::SimpleArray<RAWSXP, arrow::Int8Type>(x);
@@ -624,9 +671,6 @@ SEXP promotion_Array_to_Vector(const std::shared_ptr<Array>& array) {
624671}
625672
626673SEXP Int64Array (const std::shared_ptr<Array>& array) {
627- // the integer64 sentinel
628- static int64_t NA_INT64 = std::numeric_limits<int64_t >::min ();
629-
630674 auto n = array->length ();
631675 NumericVector vec (n);
632676 vec.attr (" class" ) = " integer64" ;
0 commit comments