Skip to content

Commit 8f9267a

Browse files
committed
port int64 to new class
1 parent c940d74 commit 8f9267a

File tree

3 files changed

+141
-7
lines changed

3 files changed

+141
-7
lines changed

r/src/vctr_builder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
#include "vctr_builder.h"
3131
#include "vctr_builder_base.h"
3232
#include "vctr_builder_dbl.h"
33-
#include "vctr_builder_lgl.h"
3433
#include "vctr_builder_int.h"
34+
#include "vctr_builder_int64.h"
35+
#include "vctr_builder_lgl.h"
3536
#include "vctr_builder_list_of.h"
3637
#include "vctr_builder_primitive.h"
3738
#include "vctr_builder_rcrd.h"

r/src/vctr_builder_int64.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#ifndef R_NANOARROW_VCTR_BUILDER_INT64_H_INCLUDED
19+
#define R_NANOARROW_VCTR_BUILDER_INT64_H_INCLUDED
20+
21+
#define R_NO_REMAP
22+
#include <R.h>
23+
#include <Rinternals.h>
24+
25+
#include "vctr_builder_base.h"
26+
27+
#define NA_INTEGER64 INT64_MIN
28+
29+
class Integer64Builder : public VctrBuilder {
30+
public:
31+
explicit Integer64Builder(SEXP ptype_sexp)
32+
: VctrBuilder(VECTOR_TYPE_INTEGER64, ptype_sexp) {}
33+
34+
ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override {
35+
NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error));
36+
SEXP value = PROTECT(Rf_allocVector(REALSXP, n));
37+
SetValue(value);
38+
UNPROTECT(1);
39+
return NANOARROW_OK;
40+
}
41+
42+
ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
43+
NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error));
44+
45+
int64_t* result = reinterpret_cast<int64_t*>(REAL(value_));
46+
int64_t n_bad_values = 0;
47+
48+
// True for all the types supported here
49+
const uint8_t* is_valid = array_view_.buffer_views[0].data.as_uint8;
50+
int64_t raw_src_offset = array_view_.offset;
51+
R_xlen_t length = array->length;
52+
53+
// Fill the buffer
54+
switch (array_view_.storage_type) {
55+
case NANOARROW_TYPE_NA:
56+
for (R_xlen_t i = 0; i < length; i++) {
57+
result[value_size_ + i] = NA_INTEGER64;
58+
}
59+
break;
60+
case NANOARROW_TYPE_INT64:
61+
memcpy(result + value_size_,
62+
array_view_.buffer_views[1].data.as_int32 + raw_src_offset,
63+
length * sizeof(int64_t));
64+
65+
// Set any nulls to NA_INTEGER64
66+
if (is_valid != NULL && array_view_.null_count != 0) {
67+
for (R_xlen_t i = 0; i < length; i++) {
68+
if (!ArrowBitGet(is_valid, raw_src_offset + i)) {
69+
result[value_size_ + i] = NA_INTEGER64;
70+
}
71+
}
72+
}
73+
break;
74+
case NANOARROW_TYPE_BOOL:
75+
case NANOARROW_TYPE_INT8:
76+
case NANOARROW_TYPE_UINT8:
77+
case NANOARROW_TYPE_INT16:
78+
case NANOARROW_TYPE_UINT16:
79+
case NANOARROW_TYPE_INT32:
80+
case NANOARROW_TYPE_UINT32:
81+
// No need to bounds check for these types
82+
for (R_xlen_t i = 0; i < length; i++) {
83+
result[value_size_ + i] = ArrowArrayViewGetIntUnsafe(&array_view_, i);
84+
}
85+
86+
// Set any nulls to NA_INTEGER
87+
if (is_valid != NULL && array_view_.null_count != 0) {
88+
for (R_xlen_t i = 0; i < length; i++) {
89+
if (!ArrowBitGet(is_valid, raw_src_offset + i)) {
90+
result[value_size_ + i] = NA_INTEGER64;
91+
}
92+
}
93+
}
94+
break;
95+
case NANOARROW_TYPE_UINT64:
96+
case NANOARROW_TYPE_FLOAT:
97+
case NANOARROW_TYPE_DOUBLE:
98+
// Loop + bounds check. Because we don't know what memory might be
99+
// in a null slot, we have to check nulls if there are any.
100+
if (is_valid != NULL && array_view_.null_count != 0) {
101+
for (R_xlen_t i = 0; i < length; i++) {
102+
if (ArrowBitGet(is_valid, raw_src_offset + i)) {
103+
int64_t value = ArrowArrayViewGetIntUnsafe(&array_view_, i);
104+
if (value > INT64_MAX || value <= NA_INTEGER64) {
105+
result[value_size_ + i] = NA_INTEGER64;
106+
n_bad_values++;
107+
} else {
108+
result[value_size_ + i] = value;
109+
}
110+
} else {
111+
result[value_size_ + i] = NA_INTEGER64;
112+
}
113+
}
114+
} else {
115+
for (R_xlen_t i = 0; i < length; i++) {
116+
int64_t value = ArrowArrayViewGetIntUnsafe(&array_view_, i);
117+
if (value > INT64_MAX || value <= NA_INTEGER64) {
118+
result[value_size_ + i] = NA_INTEGER64;
119+
n_bad_values++;
120+
} else {
121+
result[value_size_ + i] = value;
122+
}
123+
}
124+
}
125+
break;
126+
127+
default:
128+
return EINVAL;
129+
}
130+
131+
if (n_bad_values > 0) {
132+
warn_lossy_conversion(n_bad_values, "outside integer64 range set to NA");
133+
}
134+
135+
return NANOARROW_OK;
136+
}
137+
};
138+
139+
#endif

r/src/vctr_builder_primitive.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424

2525
#include "vctr_builder_base.h"
2626

27-
class Integer64Builder : public VctrBuilder {
28-
public:
29-
explicit Integer64Builder(SEXP ptype_sexp)
30-
: VctrBuilder(VECTOR_TYPE_INTEGER64, ptype_sexp) {}
31-
};
32-
3327
class ChrBuilder : public VctrBuilder {
3428
public:
3529
explicit ChrBuilder(SEXP ptype_sexp)

0 commit comments

Comments
 (0)