Skip to content

Commit 79205fb

Browse files
initial stab at arrow::table(data.frame)
1 parent f6f1775 commit 79205fb

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

r/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export(list_of)
2424
export(null)
2525
export(schema)
2626
export(struct)
27+
export(table)
2728
export(time32)
2829
export(time64)
2930
export(timestamp)

r/R/RcppExports.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ RecordBatch_schema <- function(x) {
8585
.Call(`_arrow_RecordBatch_schema`, x)
8686
}
8787

88+
dataframe_to_Table <- function(tbl) {
89+
.Call(`_arrow_dataframe_to_Table`, tbl)
90+
}
91+
92+
Table_num_columns <- function(x) {
93+
.Call(`_arrow_Table_num_columns`, x)
94+
}
95+
96+
Table_num_rows <- function(x) {
97+
.Call(`_arrow_Table_num_rows`, x)
98+
}
99+
100+
Table_schema <- function(x) {
101+
.Call(`_arrow_Table_schema`, x)
102+
}
103+
88104
Field_initialize <- function(name, type, nullable = TRUE) {
89105
.Call(`_arrow_Field_initialize`, name, type, nullable)
90106
}

r/R/array.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,23 @@ array <- function(...){
109109
record_batch <- function(.data){
110110
`arrow::RecordBatch`$new(.data)
111111
}
112+
113+
`arrow::Table` <- R6Class("arrow::Table", inherit = `arrow::Object`,
114+
public = list(
115+
initialize = function(.data){
116+
self$set_pointer(dataframe_to_Table(.data))
117+
},
118+
num_columns = function() Table_num_columns(self),
119+
num_rows = function() Table_num_rows(self),
120+
schema = function() schema(.xp = Table_schema(self))
121+
)
122+
)
123+
124+
#' Create an arrow::Table from a data frame
125+
#'
126+
#' @param .data a data frame
127+
#'
128+
#' @export
129+
table <- function(.data){
130+
`arrow::Table`$new(.data)
131+
}

r/man/table.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/src/RcppExports.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,50 @@ BEGIN_RCPP
244244
return rcpp_result_gen;
245245
END_RCPP
246246
}
247+
// dataframe_to_Table
248+
std::shared_ptr<arrow::Table> dataframe_to_Table(DataFrame tbl);
249+
RcppExport SEXP _arrow_dataframe_to_Table(SEXP tblSEXP) {
250+
BEGIN_RCPP
251+
Rcpp::RObject rcpp_result_gen;
252+
Rcpp::RNGScope rcpp_rngScope_gen;
253+
Rcpp::traits::input_parameter< DataFrame >::type tbl(tblSEXP);
254+
rcpp_result_gen = Rcpp::wrap(dataframe_to_Table(tbl));
255+
return rcpp_result_gen;
256+
END_RCPP
257+
}
258+
// Table_num_columns
259+
int Table_num_columns(const std::shared_ptr<arrow::Table>& x);
260+
RcppExport SEXP _arrow_Table_num_columns(SEXP xSEXP) {
261+
BEGIN_RCPP
262+
Rcpp::RObject rcpp_result_gen;
263+
Rcpp::RNGScope rcpp_rngScope_gen;
264+
Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Table>& >::type x(xSEXP);
265+
rcpp_result_gen = Rcpp::wrap(Table_num_columns(x));
266+
return rcpp_result_gen;
267+
END_RCPP
268+
}
269+
// Table_num_rows
270+
int Table_num_rows(const std::shared_ptr<arrow::Table>& x);
271+
RcppExport SEXP _arrow_Table_num_rows(SEXP xSEXP) {
272+
BEGIN_RCPP
273+
Rcpp::RObject rcpp_result_gen;
274+
Rcpp::RNGScope rcpp_rngScope_gen;
275+
Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Table>& >::type x(xSEXP);
276+
rcpp_result_gen = Rcpp::wrap(Table_num_rows(x));
277+
return rcpp_result_gen;
278+
END_RCPP
279+
}
280+
// Table_schema
281+
std::shared_ptr<arrow::Schema> Table_schema(const std::shared_ptr<arrow::Table>& x);
282+
RcppExport SEXP _arrow_Table_schema(SEXP xSEXP) {
283+
BEGIN_RCPP
284+
Rcpp::RObject rcpp_result_gen;
285+
Rcpp::RNGScope rcpp_rngScope_gen;
286+
Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Table>& >::type x(xSEXP);
287+
rcpp_result_gen = Rcpp::wrap(Table_schema(x));
288+
return rcpp_result_gen;
289+
END_RCPP
290+
}
247291
// Field_initialize
248292
std::shared_ptr<arrow::Field> Field_initialize(const std::string& name, const std::shared_ptr<arrow::DataType>& type, bool nullable);
249293
RcppExport SEXP _arrow_Field_initialize(SEXP nameSEXP, SEXP typeSEXP, SEXP nullableSEXP) {
@@ -1550,6 +1594,10 @@ static const R_CallMethodDef CallEntries[] = {
15501594
{"_arrow_RecordBatch_num_columns", (DL_FUNC) &_arrow_RecordBatch_num_columns, 1},
15511595
{"_arrow_RecordBatch_num_rows", (DL_FUNC) &_arrow_RecordBatch_num_rows, 1},
15521596
{"_arrow_RecordBatch_schema", (DL_FUNC) &_arrow_RecordBatch_schema, 1},
1597+
{"_arrow_dataframe_to_Table", (DL_FUNC) &_arrow_dataframe_to_Table, 1},
1598+
{"_arrow_Table_num_columns", (DL_FUNC) &_arrow_Table_num_columns, 1},
1599+
{"_arrow_Table_num_rows", (DL_FUNC) &_arrow_Table_num_rows, 1},
1600+
{"_arrow_Table_schema", (DL_FUNC) &_arrow_Table_schema, 1},
15531601
{"_arrow_Field_initialize", (DL_FUNC) &_arrow_Field_initialize, 3},
15541602
{"_arrow_Field_ToString", (DL_FUNC) &_arrow_Field_ToString, 1},
15551603
{"_arrow_Field_name", (DL_FUNC) &_arrow_Field_name, 1},

r/src/buffer.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,27 @@ int RecordBatch_num_rows(const std::shared_ptr<arrow::RecordBatch>& x){
111111
std::shared_ptr<arrow::Schema> RecordBatch_schema(const std::shared_ptr<arrow::RecordBatch>& x){
112112
return x->schema();
113113
}
114+
115+
// [[Rcpp::export]]
116+
std::shared_ptr<arrow::Table> dataframe_to_Table(DataFrame tbl){
117+
auto rb = dataframe_to_RecordBatch(tbl);
118+
119+
std::shared_ptr<arrow::Table> out;
120+
auto status = arrow::Table::FromRecordBatches({ std::move(rb) }, &out);
121+
return out;
122+
}
123+
124+
// [[Rcpp::export]]
125+
int Table_num_columns(const std::shared_ptr<arrow::Table>& x){
126+
return x->num_columns();
127+
}
128+
129+
// [[Rcpp::export]]
130+
int Table_num_rows(const std::shared_ptr<arrow::Table>& x){
131+
return x->num_rows();
132+
}
133+
134+
// [[Rcpp::export]]
135+
std::shared_ptr<arrow::Schema> Table_schema(const std::shared_ptr<arrow::Table>& x){
136+
return x->schema();
137+
}

0 commit comments

Comments
 (0)