Skip to content

Commit f5e3eff

Browse files
attempt RecordBatch$to_file
1 parent 79205fb commit f5e3eff

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

r/DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Imports:
1919
purrr,
2020
assertthat,
2121
glue,
22-
R6
22+
R6,
23+
vctrs,
24+
fs
2325
Roxygen: list(markdown = TRUE)
2426
RoxygenNote: 6.0.1.9000
2527
Suggests:

r/R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Table_schema <- function(x) {
101101
.Call(`_arrow_Table_schema`, x)
102102
}
103103

104+
RecordBatch_to_file <- function(batch, path) {
105+
.Call(`_arrow_RecordBatch_to_file`, batch, path)
106+
}
107+
104108
Field_initialize <- function(name, type, nullable = TRUE) {
105109
.Call(`_arrow_Field_initialize`, name, type, nullable)
106110
}

r/R/array.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ array <- function(...){
9797
},
9898
num_columns = function() RecordBatch_num_columns(self),
9999
num_rows = function() RecordBatch_num_rows(self),
100-
schema = function() schema(.xp = RecordBatch_schema(self))
100+
schema = function() schema(.xp = RecordBatch_schema(self)),
101+
to_file = function(path) RecordBatch_to_file(self, fs::path_abs(path))
101102
)
102103
)
103104

r/src/RcppExports.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,18 @@ BEGIN_RCPP
288288
return rcpp_result_gen;
289289
END_RCPP
290290
}
291+
// RecordBatch_to_file
292+
int RecordBatch_to_file(const std::shared_ptr<arrow::RecordBatch>& batch, std::string path);
293+
RcppExport SEXP _arrow_RecordBatch_to_file(SEXP batchSEXP, SEXP pathSEXP) {
294+
BEGIN_RCPP
295+
Rcpp::RObject rcpp_result_gen;
296+
Rcpp::RNGScope rcpp_rngScope_gen;
297+
Rcpp::traits::input_parameter< const std::shared_ptr<arrow::RecordBatch>& >::type batch(batchSEXP);
298+
Rcpp::traits::input_parameter< std::string >::type path(pathSEXP);
299+
rcpp_result_gen = Rcpp::wrap(RecordBatch_to_file(batch, path));
300+
return rcpp_result_gen;
301+
END_RCPP
302+
}
291303
// Field_initialize
292304
std::shared_ptr<arrow::Field> Field_initialize(const std::string& name, const std::shared_ptr<arrow::DataType>& type, bool nullable);
293305
RcppExport SEXP _arrow_Field_initialize(SEXP nameSEXP, SEXP typeSEXP, SEXP nullableSEXP) {
@@ -1598,6 +1610,7 @@ static const R_CallMethodDef CallEntries[] = {
15981610
{"_arrow_Table_num_columns", (DL_FUNC) &_arrow_Table_num_columns, 1},
15991611
{"_arrow_Table_num_rows", (DL_FUNC) &_arrow_Table_num_rows, 1},
16001612
{"_arrow_Table_schema", (DL_FUNC) &_arrow_Table_schema, 1},
1613+
{"_arrow_RecordBatch_to_file", (DL_FUNC) &_arrow_RecordBatch_to_file, 2},
16011614
{"_arrow_Field_initialize", (DL_FUNC) &_arrow_Field_initialize, 3},
16021615
{"_arrow_Field_ToString", (DL_FUNC) &_arrow_Field_ToString, 1},
16031616
{"_arrow_Field_name", (DL_FUNC) &_arrow_Field_name, 1},

r/src/buffer.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717

1818
#include "arrow_types.h"
19+
#include <arrow/io/file.h>
20+
#include <arrow/ipc/writer.h>
1921

2022
using namespace Rcpp;
2123
using namespace arrow;
@@ -135,3 +137,22 @@ int Table_num_rows(const std::shared_ptr<arrow::Table>& x){
135137
std::shared_ptr<arrow::Schema> Table_schema(const std::shared_ptr<arrow::Table>& x){
136138
return x->schema();
137139
}
140+
141+
// [[Rcpp::export]]
142+
int RecordBatch_to_file(const std::shared_ptr<arrow::RecordBatch>& batch, std::string path) {
143+
std::shared_ptr<arrow::io::OutputStream> stream;
144+
auto s = arrow::io::FileOutputStream::Open(path, &stream);
145+
146+
std::shared_ptr<arrow::ipc::RecordBatchWriter> file_writer;
147+
s = arrow::ipc::RecordBatchFileWriter::Open(stream.get(), batch->schema(), &file_writer);
148+
s = file_writer->WriteRecordBatch(*batch, true);
149+
s = file_writer->Close();
150+
int64_t offset;
151+
s = stream->Tell(&offset);
152+
s = stream->Close();
153+
return offset;
154+
}
155+
156+
157+
158+

0 commit comments

Comments
 (0)