Skip to content

Commit 05f7234

Browse files
committed
wire up more converters
1 parent f2dc607 commit 05f7234

File tree

5 files changed

+175
-12
lines changed

5 files changed

+175
-12
lines changed

r/src/vctr_builder.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "vctr_builder_chr.h"
3434
#include "vctr_builder_date.h"
3535
#include "vctr_builder_dbl.h"
36+
#include "vctr_builder_difftime.h"
37+
#include "vctr_builder_hms.h"
3638
#include "vctr_builder_int.h"
3739
#include "vctr_builder_int64.h"
3840
#include "vctr_builder_lgl.h"

r/src/vctr_builder_dbl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DblBuilder : public VctrBuilder {
4242
return NANOARROW_OK;
4343
}
4444

45-
ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
45+
virtual ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
4646
NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error));
4747

4848
double* result = REAL(value_);

r/src/vctr_builder_difftime.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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_DIFFTIME_H_INCLUDED
19+
#define R_NANOARROW_VCTR_BUILDER_DIFFTIME_H_INCLUDED
20+
21+
#define R_NO_REMAP
22+
#include <R.h>
23+
#include <Rinternals.h>
24+
25+
#include "vctr_builder_dbl.h"
26+
27+
class DifftimeBuilder : public DblBuilder {
28+
public:
29+
explicit DifftimeBuilder(SEXP ptype_sexp, VectorType vector_type = VECTOR_TYPE_DIFFTIME)
30+
: DblBuilder(ptype_sexp, vector_type), scale_(0) {}
31+
32+
ArrowErrorCode Init(const ArrowSchema* schema, VctrBuilderOptions options,
33+
ArrowError* error) override {
34+
NANOARROW_RETURN_NOT_OK(DblBuilder::Init(schema, options, error));
35+
switch (schema_view_.type) {
36+
case NANOARROW_TYPE_NA:
37+
case NANOARROW_TYPE_TIME32:
38+
case NANOARROW_TYPE_TIME64:
39+
case NANOARROW_TYPE_DURATION:
40+
break;
41+
default:
42+
StopCantConvert();
43+
}
44+
45+
switch (GetTimeUnits(ptype_sexp_)) {
46+
case R_TIME_UNIT_MINUTES:
47+
scale_ = 1.0 / 60;
48+
break;
49+
case R_TIME_UNIT_HOURS:
50+
scale_ = 1.0 / (60 * 60);
51+
break;
52+
case R_TIME_UNIT_DAYS:
53+
scale_ = 1.0 / (60 * 60 * 24);
54+
break;
55+
case R_TIME_UNIT_WEEKS:
56+
scale_ = 1.0 / (60 * 60 * 24 * 7);
57+
break;
58+
default:
59+
scale_ = 1.0;
60+
break;
61+
}
62+
63+
switch (schema_view_.time_unit) {
64+
case NANOARROW_TIME_UNIT_SECOND:
65+
scale_ *= 1;
66+
break;
67+
case NANOARROW_TIME_UNIT_MILLI:
68+
scale_ *= 1e-3;
69+
break;
70+
case NANOARROW_TIME_UNIT_MICRO:
71+
scale_ *= 1e-6;
72+
break;
73+
case NANOARROW_TIME_UNIT_NANO:
74+
scale_ *= 1e-9;
75+
break;
76+
default:
77+
return EINVAL;
78+
}
79+
80+
return NANOARROW_OK;
81+
}
82+
83+
virtual ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
84+
R_xlen_t value_size0 = value_size_;
85+
NANOARROW_RETURN_NOT_OK(DblBuilder::PushNext(array, error));
86+
87+
if (scale_ != 1) {
88+
double* result = REAL(value_);
89+
for (int64_t i = 0; i < array_view_.length; i++) {
90+
result[value_size0 + i] = result[value_size0 + i] * scale_;
91+
}
92+
}
93+
94+
return NANOARROW_OK;
95+
}
96+
97+
private:
98+
double scale_;
99+
100+
static RTimeUnits GetTimeUnits(SEXP ptype) {
101+
SEXP units_attr = Rf_getAttrib(ptype, Rf_install("units"));
102+
if (units_attr == R_NilValue || TYPEOF(units_attr) != STRSXP ||
103+
Rf_length(units_attr) != 1) {
104+
Rf_error("Expected difftime 'units' attribute of type character(1)");
105+
}
106+
107+
const char* dst_units = Rf_translateCharUTF8(STRING_ELT(units_attr, 0));
108+
if (strcmp(dst_units, "secs") == 0) {
109+
return R_TIME_UNIT_SECONDS;
110+
} else if (strcmp(dst_units, "mins") == 0) {
111+
return R_TIME_UNIT_MINUTES;
112+
} else if (strcmp(dst_units, "hours") == 0) {
113+
return R_TIME_UNIT_HOURS;
114+
} else if (strcmp(dst_units, "days") == 0) {
115+
return R_TIME_UNIT_DAYS;
116+
} else if (strcmp(dst_units, "weeks") == 0) {
117+
return R_TIME_UNIT_WEEKS;
118+
} else {
119+
Rf_error("Unexpected value for difftime 'units' attribute");
120+
return R_TIME_UNIT_SECONDS;
121+
}
122+
}
123+
};
124+
125+
#endif

r/src/vctr_builder_hms.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_HMS_H_INCLUDED
19+
#define R_NANOARROW_VCTR_BUILDER_HMS_H_INCLUDED
20+
21+
#define R_NO_REMAP
22+
#include <R.h>
23+
#include <Rinternals.h>
24+
25+
#include "vctr_builder_difftime.h"
26+
27+
class HmsBuilder : public DifftimeBuilder {
28+
public:
29+
explicit HmsBuilder(SEXP ptype_sexp) : DifftimeBuilder(ptype_sexp, VECTOR_TYPE_HMS) {}
30+
31+
ArrowErrorCode Init(const ArrowSchema* schema, VctrBuilderOptions options,
32+
ArrowError* error) {
33+
NANOARROW_RETURN_NOT_OK(DifftimeBuilder::Init(schema, options, error));
34+
switch (schema_view_.type) {
35+
case NANOARROW_TYPE_NA:
36+
case NANOARROW_TYPE_TIME32:
37+
case NANOARROW_TYPE_TIME64:
38+
break;
39+
default:
40+
StopCantConvert();
41+
}
42+
43+
return NANOARROW_OK;
44+
}
45+
};
46+
47+
#endif

r/src/vctr_builder_primitive.h

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

2525
#include "vctr_builder_base.h"
2626

27-
class HmsBuilder : public VctrBuilder {
28-
public:
29-
explicit HmsBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_HMS, ptype_sexp) {}
30-
};
31-
3227
class PosixctBuilder : public VctrBuilder {
3328
public:
3429
explicit PosixctBuilder(SEXP ptype_sexp)
3530
: VctrBuilder(VECTOR_TYPE_POSIXCT, ptype_sexp) {}
3631
};
3732

38-
class DifftimeBuilder : public VctrBuilder {
39-
public:
40-
explicit DifftimeBuilder(SEXP ptype_sexp)
41-
: VctrBuilder(VECTOR_TYPE_DIFFTIME, ptype_sexp) {}
42-
};
43-
4433
class OtherBuilder : public VctrBuilder {
4534
public:
4635
explicit OtherBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_OTHER, ptype_sexp) {}

0 commit comments

Comments
 (0)