arrow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From w...@apache.org
Subject arrow git commit: ARROW-1334: [C++] Add alternate Table constructor that takes vector of Array
Date Tue, 08 Aug 2017 13:29:14 GMT
Repository: arrow
Updated Branches:
  refs/heads/master 939957f33 -> 5281a8264


ARROW-1334: [C++] Add alternate Table constructor that takes vector of Array

Author: Wes McKinney <wes.mckinney@twosigma.com>

Closes #950 from wesm/ARROW-1334 and squashes the following commits:

f0655408 [Wes McKinney] Fix compiler warning
d9559682 [Wes McKinney] Add Table constructor that takes vector of Array instead of Column


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/5281a826
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/5281a826
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/5281a826

Branch: refs/heads/master
Commit: 5281a8264e0af807043b4fd70a7213ee4b176742
Parents: 939957f
Author: Wes McKinney <wes.mckinney@twosigma.com>
Authored: Tue Aug 8 09:29:09 2017 -0400
Committer: Wes McKinney <wes.mckinney@twosigma.com>
Committed: Tue Aug 8 09:29:09 2017 -0400

----------------------------------------------------------------------
 cpp/src/arrow/python/python-test.cc |  4 +---
 cpp/src/arrow/table-test.cc         |  3 +++
 cpp/src/arrow/table.cc              | 32 ++++++++++++++++++++++++++------
 cpp/src/arrow/table.h               | 18 +++++++++++-------
 4 files changed, 41 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/5281a826/cpp/src/arrow/python/python-test.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/python/python-test.cc b/cpp/src/arrow/python/python-test.cc
index 433ce9b..dd95646 100644
--- a/cpp/src/arrow/python/python-test.cc
+++ b/cpp/src/arrow/python/python-test.cc
@@ -85,9 +85,7 @@ TEST(PandasConversionTest, TestObjectBlockWriteFails) {
   auto f2 = field("f2", utf8());
   auto f3 = field("f3", utf8());
   std::vector<std::shared_ptr<Field>> fields = {f1, f2, f3};
-  std::vector<std::shared_ptr<Column>> cols = {std::make_shared<Column>(f1,
arr),
-                                               std::make_shared<Column>(f2, arr),
-                                               std::make_shared<Column>(f3, arr)};
+  std::vector<std::shared_ptr<Array>> cols = {arr, arr, arr};
 
   auto schema = std::make_shared<Schema>(fields);
   auto table = std::make_shared<Table>(schema, cols);

http://git-wip-us.apache.org/repos/asf/arrow/blob/5281a826/cpp/src/arrow/table-test.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/table-test.cc b/cpp/src/arrow/table-test.cc
index 1ba7a2f..4b67492 100644
--- a/cpp/src/arrow/table-test.cc
+++ b/cpp/src/arrow/table-test.cc
@@ -230,6 +230,9 @@ TEST_F(TestTable, Ctors) {
   ASSERT_EQ(length, table_->num_rows());
   ASSERT_EQ(3, table_->num_columns());
 
+  auto array_ctor = std::make_shared<Table>(schema_, arrays_);
+  ASSERT_TRUE(table_->Equals(*array_ctor));
+
   table_.reset(new Table(schema_, columns_, length));
   ASSERT_OK(table_->ValidateColumns());
   ASSERT_EQ(length, table_->num_rows());

http://git-wip-us.apache.org/repos/asf/arrow/blob/5281a826/cpp/src/arrow/table.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/table.cc b/cpp/src/arrow/table.cc
index 665ce2d..1f0c6d7 100644
--- a/cpp/src/arrow/table.cc
+++ b/cpp/src/arrow/table.cc
@@ -273,18 +273,38 @@ Status RecordBatch::Validate() const {
 // Table methods
 
 Table::Table(const std::shared_ptr<Schema>& schema,
-             const std::vector<std::shared_ptr<Column>>& columns)
+             const std::vector<std::shared_ptr<Column>>& columns, int64_t
num_rows)
     : schema_(schema), columns_(columns) {
-  if (columns.size() == 0) {
-    num_rows_ = 0;
+  if (num_rows < 0) {
+    if (columns.size() == 0) {
+      num_rows_ = 0;
+    } else {
+      num_rows_ = columns[0]->length();
+    }
   } else {
-    num_rows_ = columns[0]->length();
+    num_rows_ = num_rows;
   }
 }
 
 Table::Table(const std::shared_ptr<Schema>& schema,
-             const std::vector<std::shared_ptr<Column>>& columns, int64_t
num_rows)
-    : schema_(schema), columns_(columns), num_rows_(num_rows) {}
+             const std::vector<std::shared_ptr<Array>>& columns, int64_t
num_rows)
+    : schema_(schema) {
+  if (num_rows < 0) {
+    if (columns.size() == 0) {
+      num_rows_ = 0;
+    } else {
+      num_rows_ = columns[0]->length();
+    }
+  } else {
+    num_rows_ = num_rows;
+  }
+
+  columns_.resize(columns.size());
+  for (size_t i = 0; i < columns.size(); ++i) {
+    columns_[i] = std::make_shared<Column>(schema->field(static_cast<int>(i)),
+                                           columns[i]);
+  }
+}
 
 std::shared_ptr<Table> Table::ReplaceSchemaMetadata(
     const std::shared_ptr<const KeyValueMetadata>& metadata) const {

http://git-wip-us.apache.org/repos/asf/arrow/blob/5281a826/cpp/src/arrow/table.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/table.h b/cpp/src/arrow/table.h
index 6afd618..31ca97a 100644
--- a/cpp/src/arrow/table.h
+++ b/cpp/src/arrow/table.h
@@ -192,16 +192,20 @@ class ARROW_EXPORT RecordBatch {
 // Immutable container of fixed-length columns conforming to a particular schema
 class ARROW_EXPORT Table {
  public:
-  // If columns is zero-length, the table's number of rows is zero
+  /// \brief Construct Table from schema and columns
+  /// If columns is zero-length, the table's number of rows is zero
+  /// \param schema
+  /// \param columns
+  /// \param num_rows number of rows in table, -1 (default) to infer from columns
   Table(const std::shared_ptr<Schema>& schema,
-        const std::vector<std::shared_ptr<Column>>& columns);
+        const std::vector<std::shared_ptr<Column>>& columns, int64_t num_rows
= -1);
 
-  // num_rows is a parameter to allow for tables of a particular size not
-  // having any materialized columns. Each column should therefore have the
-  // same length as num_rows -- you can validate this using
-  // Table::ValidateColumns
+  /// \brief Construct Table from schema and arrays
+  /// \param schema
+  /// \param arrays
+  /// \param num_rows number of rows in table, -1 (default) to infer from columns
   Table(const std::shared_ptr<Schema>& schema,
-        const std::vector<std::shared_ptr<Column>>& columns, int64_t num_rows);
+        const std::vector<std::shared_ptr<Array>>& arrays, int64_t num_rows
= -1);
 
   // Construct table from RecordBatch, but only if all of the batch schemas are
   // equal. Returns Status::Invalid if there is some problem


Mime
View raw message