-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-24: C++: Implement a logical Table container type
A table enables us to interpret a collection of Arrow arrays as a logical table or "data frame"-like structure. Each column may consist of one or more "primitive" Arrow memory containers. Note that this currently has the limitation that the table column names must be strings. At least, this is consistent with most storage media and up-stack table implementations (e.g. R's data.frame). Currently this is somewhat limited in the arrangement of data (a vector of chunked columns -- the columns may contain only one data chunk) -- since a Table might be assembled from a vector of row batches (coming across the wire), "pivoting" the row batches might have performance implications that we can examine further on down the road. Author: Wes McKinney <[email protected]> Closes #16 from wesm/ARROW-24 and squashes the following commits: b701c76 [Wes McKinney] Test case for wrong number of columns passed 5faa5ac [Wes McKinney] cpplint 9a651cb [Wes McKinney] Basic table prototype. Move Schema code under arrow/table
- Loading branch information
Showing
13 changed files
with
358 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include <gtest/gtest.h> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "arrow/field.h" | ||
#include "arrow/table/column.h" | ||
#include "arrow/table/schema.h" | ||
#include "arrow/table/table.h" | ||
#include "arrow/table/test-common.h" | ||
#include "arrow/test-util.h" | ||
#include "arrow/type.h" | ||
#include "arrow/types/integer.h" | ||
|
||
using std::shared_ptr; | ||
using std::vector; | ||
|
||
namespace arrow { | ||
|
||
class TestTable : public TestBase { | ||
public: | ||
void MakeExample1(int length) { | ||
auto f0 = std::make_shared<Field>("f0", INT32); | ||
auto f1 = std::make_shared<Field>("f1", UINT8); | ||
auto f2 = std::make_shared<Field>("f2", INT16); | ||
|
||
vector<shared_ptr<Field> > fields = {f0, f1, f2}; | ||
schema_ = std::make_shared<Schema>(fields); | ||
|
||
columns_ = { | ||
std::make_shared<Column>(schema_->field(0), MakePrimitive<Int32Array>(length)), | ||
std::make_shared<Column>(schema_->field(1), MakePrimitive<UInt8Array>(length)), | ||
std::make_shared<Column>(schema_->field(2), MakePrimitive<Int16Array>(length)) | ||
}; | ||
} | ||
|
||
protected: | ||
std::unique_ptr<Table> table_; | ||
shared_ptr<Schema> schema_; | ||
vector<std::shared_ptr<Column> > columns_; | ||
}; | ||
|
||
TEST_F(TestTable, EmptySchema) { | ||
auto empty_schema = shared_ptr<Schema>(new Schema({})); | ||
table_.reset(new Table("data", empty_schema, columns_)); | ||
ASSERT_OK(table_->ValidateColumns()); | ||
ASSERT_EQ(0, table_->num_rows()); | ||
ASSERT_EQ(0, table_->num_columns()); | ||
} | ||
|
||
TEST_F(TestTable, Ctors) { | ||
int length = 100; | ||
MakeExample1(length); | ||
|
||
std::string name = "data"; | ||
|
||
table_.reset(new Table(name, schema_, columns_)); | ||
ASSERT_OK(table_->ValidateColumns()); | ||
ASSERT_EQ(name, table_->name()); | ||
ASSERT_EQ(length, table_->num_rows()); | ||
ASSERT_EQ(3, table_->num_columns()); | ||
|
||
table_.reset(new Table(name, schema_, columns_, length)); | ||
ASSERT_OK(table_->ValidateColumns()); | ||
ASSERT_EQ(name, table_->name()); | ||
ASSERT_EQ(length, table_->num_rows()); | ||
} | ||
|
||
TEST_F(TestTable, Metadata) { | ||
int length = 100; | ||
MakeExample1(length); | ||
|
||
std::string name = "data"; | ||
table_.reset(new Table(name, schema_, columns_)); | ||
|
||
ASSERT_TRUE(table_->schema()->Equals(schema_)); | ||
|
||
auto col = table_->column(0); | ||
ASSERT_EQ(schema_->field(0)->name, col->name()); | ||
ASSERT_EQ(schema_->field(0)->type, col->type()); | ||
} | ||
|
||
TEST_F(TestTable, InvalidColumns) { | ||
// Check that columns are all the same length | ||
int length = 100; | ||
MakeExample1(length); | ||
|
||
table_.reset(new Table("data", schema_, columns_, length - 1)); | ||
ASSERT_RAISES(Invalid, table_->ValidateColumns()); | ||
|
||
columns_.clear(); | ||
|
||
// Wrong number of columns | ||
table_.reset(new Table("data", schema_, columns_, length)); | ||
ASSERT_RAISES(Invalid, table_->ValidateColumns()); | ||
|
||
columns_ = { | ||
std::make_shared<Column>(schema_->field(0), MakePrimitive<Int32Array>(length)), | ||
std::make_shared<Column>(schema_->field(1), MakePrimitive<UInt8Array>(length)), | ||
std::make_shared<Column>(schema_->field(2), MakePrimitive<Int16Array>(length - 1)) | ||
}; | ||
|
||
table_.reset(new Table("data", schema_, columns_, length)); | ||
ASSERT_RAISES(Invalid, table_->ValidateColumns()); | ||
} | ||
|
||
} // namespace arrow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "arrow/table/table.h" | ||
|
||
#include <memory> | ||
#include <sstream> | ||
|
||
#include "arrow/field.h" | ||
#include "arrow/table/column.h" | ||
#include "arrow/table/schema.h" | ||
#include "arrow/util/status.h" | ||
|
||
namespace arrow { | ||
|
||
Table::Table(const std::string& name, const std::shared_ptr<Schema>& schema, | ||
const std::vector<std::shared_ptr<Column> >& columns) : | ||
name_(name), | ||
schema_(schema), | ||
columns_(columns) { | ||
if (columns.size() == 0) { | ||
num_rows_ = 0; | ||
} else { | ||
num_rows_ = columns[0]->length(); | ||
} | ||
} | ||
|
||
Table::Table(const std::string& name, const std::shared_ptr<Schema>& schema, | ||
const std::vector<std::shared_ptr<Column> >& columns, int64_t num_rows) : | ||
name_(name), | ||
schema_(schema), | ||
columns_(columns), | ||
num_rows_(num_rows) {} | ||
|
||
Status Table::ValidateColumns() const { | ||
if (num_columns() != schema_->num_fields()) { | ||
return Status::Invalid("Number of columns did not match schema"); | ||
} | ||
|
||
if (columns_.size() == 0) { | ||
return Status::OK(); | ||
} | ||
|
||
// Make sure columns are all the same length | ||
for (size_t i = 0; i < columns_.size(); ++i) { | ||
const Column* col = columns_[i].get(); | ||
if (col->length() != num_rows_) { | ||
std::stringstream ss; | ||
ss << "Column " << i << " expected length " | ||
<< num_rows_ | ||
<< " but got length " | ||
<< col->length(); | ||
return Status::Invalid(ss.str()); | ||
} | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace arrow |
Oops, something went wrong.