Skip to content

Commit cf7530e

Browse files
committed
add double impl
1 parent 5c27b6f commit cf7530e

File tree

4 files changed

+135
-13
lines changed

4 files changed

+135
-13
lines changed

r/src/vctr_builder.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "vctr_builder.h"
3131
#include "vctr_builder_base.h"
32+
#include "vctr_builder_dbl.h"
3233
#include "vctr_builder_int.h"
3334
#include "vctr_builder_list_of.h"
3435
#include "vctr_builder_primitive.h"

r/src/vctr_builder_dbl.h

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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_DBL_H_INCLUDED
19+
#define R_NANOARROW_VCTR_BUILDER_DBL_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+
// bit64::as.integer64(2^53)
28+
#define MAX_DBL_AS_INTEGER 9007199254740992
29+
30+
class DblBuilder : public VctrBuilder {
31+
public:
32+
explicit DblBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_DBL, ptype_sexp) {}
33+
34+
SEXP GetPtype() override { return Rf_allocVector(REALSXP, 0); }
35+
36+
ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override {
37+
NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error));
38+
SEXP value = PROTECT(Rf_allocVector(REALSXP, n));
39+
SetValue(value);
40+
UNPROTECT(1);
41+
return NANOARROW_OK;
42+
}
43+
44+
ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
45+
NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error));
46+
47+
double* result = REAL(value_);
48+
int64_t n_bad_values = 0;
49+
50+
// True for all the types supported here
51+
const uint8_t* is_valid = array_view_.buffer_views[0].data.as_uint8;
52+
int64_t raw_src_offset = array_view_.offset;
53+
R_xlen_t length = array_view_.length;
54+
55+
// Fill the buffer
56+
switch (array_view_.storage_type) {
57+
case NANOARROW_TYPE_NA:
58+
for (R_xlen_t i = 0; i < length; i++) {
59+
result[value_size_ + i] = NA_REAL;
60+
}
61+
break;
62+
case NANOARROW_TYPE_DOUBLE:
63+
memcpy(result + value_size_,
64+
array_view_.buffer_views[1].data.as_double + raw_src_offset,
65+
length * sizeof(double));
66+
67+
// Set any nulls to NA_REAL
68+
if (is_valid != NULL && array_view_.null_count != 0) {
69+
for (R_xlen_t i = 0; i < length; i++) {
70+
if (!ArrowBitGet(is_valid, raw_src_offset + i)) {
71+
result[value_size_ + i] = NA_REAL;
72+
}
73+
}
74+
}
75+
break;
76+
case NANOARROW_TYPE_BOOL:
77+
case NANOARROW_TYPE_INT8:
78+
case NANOARROW_TYPE_UINT8:
79+
case NANOARROW_TYPE_INT16:
80+
case NANOARROW_TYPE_UINT16:
81+
case NANOARROW_TYPE_INT32:
82+
case NANOARROW_TYPE_UINT32:
83+
case NANOARROW_TYPE_FLOAT:
84+
// No need to bounds check these types
85+
for (R_xlen_t i = 0; i < length; i++) {
86+
result[value_size_ + i] = ArrowArrayViewGetDoubleUnsafe(&array_view_, i);
87+
}
88+
89+
// Set any nulls to NA_REAL
90+
if (is_valid != NULL && array_view_.null_count != 0) {
91+
for (R_xlen_t i = 0; i < length; i++) {
92+
if (!ArrowBitGet(is_valid, raw_src_offset + i)) {
93+
result[value_size_ + i] = NA_REAL;
94+
}
95+
}
96+
}
97+
break;
98+
99+
case NANOARROW_TYPE_INT64:
100+
case NANOARROW_TYPE_UINT64:
101+
for (R_xlen_t i = 0; i < length; i++) {
102+
double value = ArrowArrayViewGetDoubleUnsafe(&array_view_, i);
103+
if (value > MAX_DBL_AS_INTEGER || value < -MAX_DBL_AS_INTEGER) {
104+
// Content of null slot is undefined
105+
n_bad_values += is_valid == NULL || ArrowBitGet(is_valid, raw_src_offset + i);
106+
}
107+
108+
result[value_size_ + i] = value;
109+
}
110+
111+
// Set any nulls to NA_REAL
112+
if (is_valid != NULL && array_view_.null_count != 0) {
113+
for (R_xlen_t i = 0; i < length; i++) {
114+
if (!ArrowBitGet(is_valid, raw_src_offset + i)) {
115+
result[value_size_ + i] = NA_REAL;
116+
}
117+
}
118+
}
119+
break;
120+
121+
default:
122+
return EINVAL;
123+
}
124+
125+
if (n_bad_values > 0) {
126+
warn_lossy_conversion(
127+
n_bad_values, "may have incurred loss of precision in conversion to double()");
128+
}
129+
130+
return NANOARROW_OK;
131+
}
132+
};
133+
134+
#endif

r/src/vctr_builder_int.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ class IntBuilder : public VctrBuilder {
2828
public:
2929
explicit IntBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_INT, ptype_sexp) {}
3030

31-
ArrowErrorCode Init(const ArrowSchema* schema, VctrBuilderOptions options,
32-
ArrowError* error) override {
33-
NANOARROW_RETURN_NOT_OK(VctrBuilder::Init(schema, options, error));
34-
return NANOARROW_OK;
35-
}
36-
3731
SEXP GetPtype() override { return Rf_allocVector(INTSXP, 0); }
3832

3933
ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override {

r/src/vctr_builder_primitive.h

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

2525
#include "vctr_builder_base.h"
2626

27-
class DblBuilder : public VctrBuilder {
28-
public:
29-
explicit DblBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_DBL, ptype_sexp) {}
30-
31-
SEXP GetPtype() override { return Rf_allocVector(REALSXP, 0); }
32-
};
33-
3427
class LglBuilder : public VctrBuilder {
3528
public:
3629
explicit LglBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_LGL, ptype_sexp) {}

0 commit comments

Comments
 (0)