1
- // [[Rcpp::depends(rapidjsonr)]]
2
- # include < Rcpp.h >
1
+ # include < cpp11.hpp >
2
+
3
3
#include < rapidjson/document.h>
4
4
#include < rapidjson/istreamwrapper.h>
5
5
#include " rapidjson/filereadstream.h"
6
6
#include < cli/progress.h>
7
7
#include " integer64.h"
8
8
#include " base64.h"
9
9
10
+ #include < vector>
10
11
#include < ctime>
11
12
#include < cstdio>
12
13
#include < climits>
@@ -27,7 +28,7 @@ int64_t parse_int64(const char* x) {
27
28
// this will throw an exception with the appropriate error message
28
29
// if the package is not installed
29
30
void check_namespace (const char * pkg, const char * bq_type) {
30
- Rcpp::Function checkNamespaceFun ( " bq_check_namespace " , " bigrquery" );
31
+ auto checkNamespaceFun = cpp11::package ( " bigrquery" )[ " bq_check_namespace " ] ;
31
32
checkNamespaceFun (pkg, bq_type);
32
33
}
33
34
@@ -96,7 +97,7 @@ class BqField {
96
97
97
98
BqField (const rapidjson::Value& field) {
98
99
if (!field.IsObject ()) {
99
- Rcpp ::stop (" Invalid field spec" );
100
+ cpp11 ::stop (" Invalid field spec" );
100
101
}
101
102
102
103
name_ = field[" name" ].GetString ();
@@ -118,40 +119,44 @@ class BqField {
118
119
119
120
SEXP vectorInit (int n, bool array) const {
120
121
if (array) {
121
- return Rcpp::List (n);
122
+ return cpp11::writable::list (n);
122
123
}
123
124
124
125
switch (type_) {
125
126
case BQ_INTEGER: {
126
- Rcpp::DoubleVector out (n);
127
+ cpp11::writable::doubles out (n);
127
128
out.attr (" class" ) = " integer64" ;
128
129
return out;
129
130
}
130
131
case BQ_FLOAT:
131
- return Rcpp::DoubleVector (n);
132
+ return cpp11::writable::doubles (n);
132
133
case BQ_BOOLEAN:
133
- return Rcpp::LogicalVector (n);
134
+ return cpp11::writable::logicals (n);
134
135
case BQ_STRING:
135
- return Rcpp::CharacterVector (n);
136
- case BQ_TIMESTAMP:
137
- return Rcpp::DatetimeVector (n, " UTC" );
136
+ return cpp11::writable::strings (n);
137
+ case BQ_TIMESTAMP: {
138
+ cpp11::writable::doubles out (n);
139
+ out.attr (" class" ) = {" POSIXct" , " POSIXt" };
140
+ out.attr (" tzone" ) = " UTC" ;
141
+ return out;
142
+ }
138
143
case BQ_RECORD:
139
- return Rcpp::List (n);
144
+ return cpp11::writable::list (n);
140
145
case BQ_GEOGRAPHY: {
141
146
check_namespace (" wk" , " GEOGRAPHY" );
142
- Rcpp::CharacterVector out (n);
143
- out.attr (" class" ) = Rcpp::CharacterVector::create ( " wk_wkt" , " wk_vctr" ) ;
147
+ cpp11::writable::strings out (n);
148
+ out.attr (" class" ) = { " wk_wkt" , " wk_vctr" } ;
144
149
return out;
145
150
}
146
151
case BQ_BYTES: {
147
152
check_namespace (" blob" , " BYTES" );
148
- Rcpp::List out (n);
149
- out.attr (" class" ) = Rcpp::CharacterVector::create ( " blob" , " vctrs_list_of" , " vctrs_vctr" , " list" ) ;
150
- out.attr (" ptype" ) = Rcpp::RawVector::create ( );
153
+ cpp11::writable::list out (n);
154
+ out.attr (" class" ) = { " blob" , " vctrs_list_of" , " vctrs_vctr" , " list" } ;
155
+ out.attr (" ptype" ) = cpp11::writable::raws ((R_xlen_t) 0 );
151
156
return out;
152
157
}
153
158
default : {
154
- Rcpp::CharacterVector out (n);
159
+ cpp11::writable::strings out (n);
155
160
out.attr (" bq_type" ) = type_str_;
156
161
return out;
157
162
}
@@ -165,10 +170,10 @@ class BqField {
165
170
void vectorSet (SEXP x, int i, const rapidjson::Value& v, bool array) const {
166
171
if (array && type_ != BQ_RECORD) {
167
172
if (!v.IsArray ())
168
- Rcpp ::stop (" Not an array [1]" );
173
+ cpp11 ::stop (" Not an array [1]" );
169
174
170
175
int n = v.Size ();
171
- Rcpp::RObject out = vectorInit (n, false );
176
+ cpp11::sexp out = vectorInit (n, false );
172
177
for (int j = 0 ; j < n; ++j) {
173
178
vectorSet (out, j, v[j][" v" ], false );
174
179
}
@@ -196,7 +201,7 @@ class BqField {
196
201
case BQ_UNKNOWN:
197
202
case BQ_STRING:
198
203
if (v.IsString ()) {
199
- Rcpp::RObject chr = Rf_mkCharLenCE (v.GetString (), v.GetStringLength (), CE_UTF8);
204
+ cpp11::sexp chr = Rf_mkCharLenCE (v.GetString (), v.GetStringLength (), CE_UTF8);
200
205
SET_STRING_ELT (x, i, chr);
201
206
} else {
202
207
SET_STRING_ELT (x, i, NA_STRING);
@@ -207,17 +212,15 @@ class BqField {
207
212
break ;
208
213
case BQ_GEOGRAPHY:
209
214
if (v.IsString ()) {
210
- Rcpp::RObject chr = Rf_mkCharLenCE (v.GetString (), v.GetStringLength (), CE_UTF8);
215
+ cpp11::sexp chr = Rf_mkCharLenCE (v.GetString (), v.GetStringLength (), CE_UTF8);
211
216
SET_STRING_ELT (x, i, chr);
212
217
} else {
213
218
SET_STRING_ELT (x, i, NA_STRING);
214
219
}
215
220
break ;
216
221
case BQ_BYTES:
217
222
if (v.IsString ()) {
218
- Rcpp::RawVector chr (v.GetStringLength ());
219
- memcpy (&(chr[0 ]), v.GetString (), v.GetStringLength ());
220
- SET_VECTOR_ELT (x, i, base64_decode (chr));
223
+ SET_VECTOR_ELT (x, i, base64_decode (v.GetString (), v.GetStringLength ()));
221
224
} else {
222
225
SET_VECTOR_ELT (x, i, R_NilValue);
223
226
}
@@ -228,9 +231,8 @@ class BqField {
228
231
SEXP recordValue (const rapidjson::Value& v) const {
229
232
int p = fields_.size ();
230
233
231
- Rcpp::List out (p);
232
- Rcpp::CharacterVector names (p);
233
- out.attr (" names" ) = names;
234
+ cpp11::writable::list out (p);
235
+ cpp11::writable::strings names (p);
234
236
235
237
if (!array_) {
236
238
if (!v.IsObject ())
@@ -239,14 +241,14 @@ class BqField {
239
241
const rapidjson::Value& f = v[" f" ];
240
242
// f is array of fields
241
243
if (!f.IsArray ())
242
- Rcpp ::stop (" Not array [2]" );
244
+ cpp11 ::stop (" Not array [2]" );
243
245
244
246
for (int j = 0 ; j < p; ++j) {
245
247
const BqField& field = fields_[j];
246
248
const rapidjson::Value& vs = f[j][" v" ];
247
249
248
250
int n = (field.array_ ) ? vs.Size () : 1 ;
249
- Rcpp::RObject col = field.vectorInit (n, false );
251
+ cpp11::sexp col = field.vectorInit (n, false );
250
252
251
253
if (field.array_ ) {
252
254
for (int i = 0 ; i < n; ++i) {
@@ -267,23 +269,26 @@ class BqField {
267
269
out[j] = field.vectorInit (n);
268
270
names[j] = field.name_ ;
269
271
}
270
- out.attr (" class" ) = Rcpp::CharacterVector::create ( " tbl_df" , " tbl" , " data.frame" ) ;
271
- out.attr (" row.names" ) = Rcpp::IntegerVector::create ( NA_INTEGER, -n) ;
272
+ out.attr (" class" ) = { " tbl_df" , " tbl" , " data.frame" } ;
273
+ out.attr (" row.names" ) = { NA_INTEGER, -n} ;
272
274
273
- if (n == 0 )
275
+ if (n == 0 ) {
276
+ out.attr (" names" ) = names;
274
277
return out;
278
+ }
275
279
276
280
for (int i = 0 ; i < n; ++i) {
277
281
const rapidjson::Value& f = v[i][" v" ][" f" ];
278
282
if (!f.IsArray ())
279
- Rcpp ::stop (" Not an array [3]" );
283
+ cpp11 ::stop (" Not an array [3]" );
280
284
281
285
for (int j = 0 ; j < p; ++j) {
282
286
fields_[j].vectorSet (out[j], i, f[j][" v" ]);
283
287
}
284
288
}
285
289
}
286
290
291
+ out.attr (" names" ) = names;
287
292
return out;
288
293
}
289
294
@@ -306,24 +311,24 @@ std::vector<BqField> bq_fields_parse(const rapidjson::Value& meta) {
306
311
return fields;
307
312
}
308
313
309
- Rcpp::List bq_fields_init (const std::vector<BqField>& fields, int n) {
314
+ cpp11::list bq_fields_init (const std::vector<BqField>& fields, int n) {
310
315
int p = fields.size ();
311
316
312
- Rcpp::List out (p);
313
- Rcpp::CharacterVector names (p);
317
+ cpp11::writable::list out (p);
318
+ cpp11::writable::strings names (p);
314
319
for (int j = 0 ; j < p; ++j) {
315
320
out[j] = fields[j].vectorInit (n);
316
321
names[j] = fields[j].name ();
317
322
};
318
- out.attr (" class" ) = Rcpp::CharacterVector::create ( " tbl_df" , " tbl" , " data.frame" ) ;
323
+ out.attr (" class" ) = { " tbl_df" , " tbl" , " data.frame" } ;
319
324
out.attr (" names" ) = names;
320
- out.attr (" row.names" ) = Rcpp::IntegerVector::create ( NA_INTEGER, -n) ;
325
+ out.attr (" row.names" ) = { NA_INTEGER, -n} ;
321
326
322
327
return out;
323
328
}
324
329
325
330
int bq_fields_set (const rapidjson::Value& data,
326
- Rcpp::List out,
331
+ cpp11::writable::list out,
327
332
const std::vector<BqField>& fields,
328
333
int offset
329
334
) {
@@ -345,7 +350,7 @@ int bq_fields_set(const rapidjson::Value& data,
345
350
return n;
346
351
}
347
352
348
- // [[Rcpp::export ]]
353
+ [[cpp11:: register ]]
349
354
SEXP bq_parse (std::string meta_s, std::string data_s) {
350
355
rapidjson::Document meta_d;
351
356
meta_d.Parse (&meta_s[0 ]);
@@ -356,19 +361,19 @@ SEXP bq_parse(std::string meta_s, std::string data_s) {
356
361
357
362
int n = (values_d.HasMember (" rows" )) ? values_d[" rows" ].Size () : 0 ;
358
363
359
- Rcpp::List out = bq_fields_init (fields, n);
364
+ cpp11::writable::list out = bq_fields_init (fields, n);
360
365
bq_fields_set (values_d, out, fields, 0 );
361
366
362
367
return out;
363
368
}
364
369
365
- // [[Rcpp::export ]]
370
+ [[cpp11:: register ]]
366
371
SEXP bq_field_init (std::string json, std::string value = " " ) {
367
372
rapidjson::Document d1;
368
373
d1.Parse (&json[0 ]);
369
374
370
375
BqField field (d1);
371
- Rcpp::RObject out = field.vectorInit (1 );
376
+ cpp11::sexp out = field.vectorInit (1 );
372
377
373
378
if (value != " " ) {
374
379
rapidjson::Document d2;
@@ -380,7 +385,7 @@ SEXP bq_field_init(std::string json, std::string value = "") {
380
385
return out;
381
386
}
382
387
383
- // [[Rcpp::export ]]
388
+ [[cpp11:: register ]]
384
389
SEXP bq_parse_files (std::string schema_path,
385
390
std::vector<std::string> file_paths,
386
391
int n,
@@ -393,7 +398,7 @@ SEXP bq_parse_files(std::string schema_path,
393
398
schema_doc.ParseStream (schema_stream_w);
394
399
395
400
std::vector<BqField> fields = bq_fields_parse (schema_doc);
396
- Rcpp::List out = bq_fields_init (fields, n);
401
+ cpp11::writable::list out = bq_fields_init (fields, n);
397
402
398
403
std::vector<std::string>::const_iterator it = file_paths.begin (),
399
404
it_end = file_paths.end ();
@@ -414,15 +419,15 @@ SEXP bq_parse_files(std::string schema_path,
414
419
415
420
if (values_doc.HasParseError ()) {
416
421
UNPROTECT (2 );
417
- Rcpp ::stop (" Failed to parse '%s'" , *it );
422
+ cpp11 ::stop (" Failed to parse '%s'" , it-> c_str () );
418
423
fclose (values_file);
419
424
}
420
425
421
426
total_seen += bq_fields_set (values_doc, out, fields, total_seen);
422
427
if (!quiet) {
423
428
if (CLI_SHOULD_TICK) cli_progress_add (pb, 1 );
424
429
} else {
425
- Rcpp::checkUserInterrupt ();
430
+ cpp11::check_user_interrupt ();
426
431
};
427
432
428
433
fclose (values_file);
@@ -433,7 +438,7 @@ SEXP bq_parse_files(std::string schema_path,
433
438
434
439
if (total_seen != n) {
435
440
// Matches the error thrown from R if the first "test balloon" chunk is short.
436
- Rcpp ::stop (" %d rows were requested, but only %d rows were received.\n Leave `page_size` unspecified or use an even smaller value." , n, total_seen);
441
+ cpp11 ::stop (" %d rows were requested, but only %d rows were received.\n Leave `page_size` unspecified or use an even smaller value." , n, total_seen);
437
442
}
438
443
439
444
return out;
0 commit comments