You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2018/11/29 02:24:43 UTC

[arrow] branch master updated: ARROW-3807: [R] Missing Field API

This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new cd74741  ARROW-3807: [R] Missing Field API
cd74741 is described below

commit cd74741c635361c617383b1ace3a578ab42e7f01
Author: Romain Francois <ro...@purrple.cat>
AuthorDate: Wed Nov 28 20:24:32 2018 -0600

    ARROW-3807: [R] Missing Field API
    
    Exposed the `field()` function, +Field$type(), +Field$Equals()
    
    Author: Romain Francois <ro...@purrple.cat>
    
    Closes #2981 from romainfrancois/field and squashes the following commits:
    
    ae3dcef53 <Romain Francois> ARROW-3807:  Missing Field API
---
 r/NAMESPACE                                  |  1 +
 r/R/Field.R                                  | 21 ++++++++--
 r/R/RcppExports.R                            | 24 ++++++++----
 r/man/field.Rd                               | 22 +++++++++++
 r/src/RcppExports.cpp                        | 57 ++++++++++++++++++++--------
 r/src/field.cpp                              | 27 +++++++++----
 r/{R/Field.R => tests/testthat/test-field.R} | 40 ++++---------------
 7 files changed, 125 insertions(+), 67 deletions(-)

diff --git a/r/NAMESPACE b/r/NAMESPACE
index e538878..490d211 100644
--- a/r/NAMESPACE
+++ b/r/NAMESPACE
@@ -94,6 +94,7 @@ export(decimal)
 export(dictionary)
 export(feather_table_reader)
 export(feather_table_writer)
+export(field)
 export(file_open)
 export(file_output_stream)
 export(fixed_size_buffer_writer)
diff --git a/r/R/Field.R b/r/R/Field.R
index e999b97..79c0f33 100644
--- a/r/R/Field.R
+++ b/r/R/Field.R
@@ -17,8 +17,7 @@
 
 #' @include R6.R
 
-`arrow::Field` <- R6Class("arrow::Field",
-  inherit = `arrow::Object`,
+`arrow::Field` <- R6Class("arrow::Field", inherit = `arrow::Object`,
   public = list(
     ToString = function() {
       Field__ToString(self)
@@ -31,6 +30,9 @@
     },
     Equals = function(other) {
       inherits(other, "arrow::Field") && Field__Equals(self, other)
+    },
+    type = function() {
+      `arrow::DataType`$dispatch(Field__type(self))
     }
   )
 )
@@ -40,7 +42,20 @@
   lhs$Equals(rhs)
 }
 
-field <- function(name, type) {
+#' Factory for a `arrow::Field`
+#'
+#' @param name field name
+#' @param type logical type, instance of `arrow::DataType`
+#' @param metadata currently ignored
+#'
+#' @examples
+#' field("x", int32())
+#'
+#' @export
+field <- function(name, type, metadata) {
+  assert_that(inherits(name, "character"), length(name) == 1L)
+  assert_that(inherits(type, "arrow::DataType"))
+  assert_that(missing(metadata), msg = "metadata= is currently ignored")
   shared_ptr(`arrow::Field`, Field__initialize(name, type))
 }
 
diff --git a/r/R/RcppExports.R b/r/R/RcppExports.R
index c8df2fa..324510c 100644
--- a/r/R/RcppExports.R
+++ b/r/R/RcppExports.R
@@ -453,20 +453,28 @@ ipc___feather___TableReader__Open <- function(stream) {
     .Call(`_arrow_ipc___feather___TableReader__Open`, stream)
 }
 
-Field__initialize <- function(name, type, nullable = TRUE) {
-    .Call(`_arrow_Field__initialize`, name, type, nullable)
+Field__initialize <- function(name, field, nullable = TRUE) {
+    .Call(`_arrow_Field__initialize`, name, field, nullable)
 }
 
-Field__ToString <- function(type) {
-    .Call(`_arrow_Field__ToString`, type)
+Field__ToString <- function(field) {
+    .Call(`_arrow_Field__ToString`, field)
 }
 
-Field__name <- function(type) {
-    .Call(`_arrow_Field__name`, type)
+Field__name <- function(field) {
+    .Call(`_arrow_Field__name`, field)
 }
 
-Field__nullable <- function(type) {
-    .Call(`_arrow_Field__nullable`, type)
+Field__Equals <- function(field, other) {
+    .Call(`_arrow_Field__Equals`, field, other)
+}
+
+Field__nullable <- function(field) {
+    .Call(`_arrow_Field__nullable`, field)
+}
+
+Field__type <- function(field) {
+    .Call(`_arrow_Field__type`, field)
 }
 
 io___Readable__Read <- function(x, nbytes) {
diff --git a/r/man/field.Rd b/r/man/field.Rd
new file mode 100644
index 0000000..e7af66d
--- /dev/null
+++ b/r/man/field.Rd
@@ -0,0 +1,22 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/Field.R
+\name{field}
+\alias{field}
+\title{Factor for a \code{arrow::Field}}
+\usage{
+field(name, type, metadata)
+}
+\arguments{
+\item{name}{field name}
+
+\item{type}{logical type, instance of \code{arrow::DataType}}
+
+\item{metadata}{currently ignored}
+}
+\description{
+Factor for a \code{arrow::Field}
+}
+\examples{
+field("x", int32())
+
+}
diff --git a/r/src/RcppExports.cpp b/r/src/RcppExports.cpp
index f1573a6..2c549ad 100644
--- a/r/src/RcppExports.cpp
+++ b/r/src/RcppExports.cpp
@@ -1266,48 +1266,71 @@ BEGIN_RCPP
 END_RCPP
 }
 // Field__initialize
-std::shared_ptr<arrow::Field> Field__initialize(const std::string& name, const std::shared_ptr<arrow::DataType>& type, bool nullable);
-RcppExport SEXP _arrow_Field__initialize(SEXP nameSEXP, SEXP typeSEXP, SEXP nullableSEXP) {
+std::shared_ptr<arrow::Field> Field__initialize(const std::string& name, const std::shared_ptr<arrow::DataType>& field, bool nullable);
+RcppExport SEXP _arrow_Field__initialize(SEXP nameSEXP, SEXP fieldSEXP, SEXP nullableSEXP) {
 BEGIN_RCPP
     Rcpp::RObject rcpp_result_gen;
     Rcpp::RNGScope rcpp_rngScope_gen;
     Rcpp::traits::input_parameter< const std::string& >::type name(nameSEXP);
-    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::DataType>& >::type type(typeSEXP);
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::DataType>& >::type field(fieldSEXP);
     Rcpp::traits::input_parameter< bool >::type nullable(nullableSEXP);
-    rcpp_result_gen = Rcpp::wrap(Field__initialize(name, type, nullable));
+    rcpp_result_gen = Rcpp::wrap(Field__initialize(name, field, nullable));
     return rcpp_result_gen;
 END_RCPP
 }
 // Field__ToString
-std::string Field__ToString(const std::shared_ptr<arrow::Field>& type);
-RcppExport SEXP _arrow_Field__ToString(SEXP typeSEXP) {
+std::string Field__ToString(const std::shared_ptr<arrow::Field>& field);
+RcppExport SEXP _arrow_Field__ToString(SEXP fieldSEXP) {
 BEGIN_RCPP
     Rcpp::RObject rcpp_result_gen;
     Rcpp::RNGScope rcpp_rngScope_gen;
-    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type type(typeSEXP);
-    rcpp_result_gen = Rcpp::wrap(Field__ToString(type));
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type field(fieldSEXP);
+    rcpp_result_gen = Rcpp::wrap(Field__ToString(field));
     return rcpp_result_gen;
 END_RCPP
 }
 // Field__name
-std::string Field__name(const std::shared_ptr<arrow::Field>& type);
-RcppExport SEXP _arrow_Field__name(SEXP typeSEXP) {
+std::string Field__name(const std::shared_ptr<arrow::Field>& field);
+RcppExport SEXP _arrow_Field__name(SEXP fieldSEXP) {
+BEGIN_RCPP
+    Rcpp::RObject rcpp_result_gen;
+    Rcpp::RNGScope rcpp_rngScope_gen;
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type field(fieldSEXP);
+    rcpp_result_gen = Rcpp::wrap(Field__name(field));
+    return rcpp_result_gen;
+END_RCPP
+}
+// Field__Equals
+bool Field__Equals(const std::shared_ptr<arrow::Field>& field, const std::shared_ptr<arrow::Field>& other);
+RcppExport SEXP _arrow_Field__Equals(SEXP fieldSEXP, SEXP otherSEXP) {
 BEGIN_RCPP
     Rcpp::RObject rcpp_result_gen;
     Rcpp::RNGScope rcpp_rngScope_gen;
-    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type type(typeSEXP);
-    rcpp_result_gen = Rcpp::wrap(Field__name(type));
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type field(fieldSEXP);
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type other(otherSEXP);
+    rcpp_result_gen = Rcpp::wrap(Field__Equals(field, other));
     return rcpp_result_gen;
 END_RCPP
 }
 // Field__nullable
-bool Field__nullable(const std::shared_ptr<arrow::Field>& type);
-RcppExport SEXP _arrow_Field__nullable(SEXP typeSEXP) {
+bool Field__nullable(const std::shared_ptr<arrow::Field>& field);
+RcppExport SEXP _arrow_Field__nullable(SEXP fieldSEXP) {
+BEGIN_RCPP
+    Rcpp::RObject rcpp_result_gen;
+    Rcpp::RNGScope rcpp_rngScope_gen;
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type field(fieldSEXP);
+    rcpp_result_gen = Rcpp::wrap(Field__nullable(field));
+    return rcpp_result_gen;
+END_RCPP
+}
+// Field__type
+std::shared_ptr<arrow::DataType> Field__type(const std::shared_ptr<arrow::Field>& field);
+RcppExport SEXP _arrow_Field__type(SEXP fieldSEXP) {
 BEGIN_RCPP
     Rcpp::RObject rcpp_result_gen;
     Rcpp::RNGScope rcpp_rngScope_gen;
-    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type type(typeSEXP);
-    rcpp_result_gen = Rcpp::wrap(Field__nullable(type));
+    Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Field>& >::type field(fieldSEXP);
+    rcpp_result_gen = Rcpp::wrap(Field__type(field));
     return rcpp_result_gen;
 END_RCPP
 }
@@ -2178,7 +2201,9 @@ static const R_CallMethodDef CallEntries[] = {
     {"_arrow_Field__initialize", (DL_FUNC) &_arrow_Field__initialize, 3},
     {"_arrow_Field__ToString", (DL_FUNC) &_arrow_Field__ToString, 1},
     {"_arrow_Field__name", (DL_FUNC) &_arrow_Field__name, 1},
+    {"_arrow_Field__Equals", (DL_FUNC) &_arrow_Field__Equals, 2},
     {"_arrow_Field__nullable", (DL_FUNC) &_arrow_Field__nullable, 1},
+    {"_arrow_Field__type", (DL_FUNC) &_arrow_Field__type, 1},
     {"_arrow_io___Readable__Read", (DL_FUNC) &_arrow_io___Readable__Read, 2},
     {"_arrow_io___InputStream__Close", (DL_FUNC) &_arrow_io___InputStream__Close, 1},
     {"_arrow_io___OutputStream__Close", (DL_FUNC) &_arrow_io___OutputStream__Close, 1},
diff --git a/r/src/field.cpp b/r/src/field.cpp
index 146525b..1c44e8b 100644
--- a/r/src/field.cpp
+++ b/r/src/field.cpp
@@ -21,22 +21,33 @@ using namespace Rcpp;
 
 // [[Rcpp::export]]
 std::shared_ptr<arrow::Field> Field__initialize(
-    const std::string& name, const std::shared_ptr<arrow::DataType>& type,
+    const std::string& name, const std::shared_ptr<arrow::DataType>& field,
     bool nullable = true) {
-  return arrow::field(name, type, nullable);
+  return arrow::field(name, field, nullable);
 }
 
 // [[Rcpp::export]]
-std::string Field__ToString(const std::shared_ptr<arrow::Field>& type) {
-  return type->ToString();
+std::string Field__ToString(const std::shared_ptr<arrow::Field>& field) {
+  return field->ToString();
 }
 
 // [[Rcpp::export]]
-std::string Field__name(const std::shared_ptr<arrow::Field>& type) {
-  return type->name();
+std::string Field__name(const std::shared_ptr<arrow::Field>& field) {
+  return field->name();
 }
 
 // [[Rcpp::export]]
-bool Field__nullable(const std::shared_ptr<arrow::Field>& type) {
-  return type->nullable();
+bool Field__Equals(const std::shared_ptr<arrow::Field>& field,
+                   const std::shared_ptr<arrow::Field>& other) {
+  return field->Equals(other);
+}
+
+// [[Rcpp::export]]
+bool Field__nullable(const std::shared_ptr<arrow::Field>& field) {
+  return field->nullable();
+}
+
+// [[Rcpp::export]]
+std::shared_ptr<arrow::DataType> Field__type(const std::shared_ptr<arrow::Field>& field) {
+  return field->type();
 }
diff --git a/r/R/Field.R b/r/tests/testthat/test-field.R
similarity index 54%
copy from r/R/Field.R
copy to r/tests/testthat/test-field.R
index e999b97..08bf4db 100644
--- a/r/R/Field.R
+++ b/r/tests/testthat/test-field.R
@@ -15,36 +15,12 @@
 # specific language governing permissions and limitations
 # under the License.
 
-#' @include R6.R
+context("arrow::Field")
 
-`arrow::Field` <- R6Class("arrow::Field",
-  inherit = `arrow::Object`,
-  public = list(
-    ToString = function() {
-      Field__ToString(self)
-    },
-    name = function() {
-      Field__name(self)
-    },
-    nullable = function() {
-      Field__nullable(self)
-    },
-    Equals = function(other) {
-      inherits(other, "arrow::Field") && Field__Equals(self, other)
-    }
-  )
-)
-
-#' @export
-`==.arrow::Field` <- function(lhs, rhs){
-  lhs$Equals(rhs)
-}
-
-field <- function(name, type) {
-  shared_ptr(`arrow::Field`, Field__initialize(name, type))
-}
-
-.fields <- function(.list){
-  assert_that( !is.null(nms <- names(.list)) )
-  map2(nms, .list, field)
-}
+test_that("field() factory", {
+  x <- field("x", int32())
+  expect_equal(x$type(), int32())
+  expect_equal(x$name(), "x")
+  expect_true(x == x)
+  expect_false(x == field("x", int64()))
+})