Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-24: C++: Implement a logical Table container type #16

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ set(ARROW_SRCS
src/arrow/array.cc
src/arrow/builder.cc
src/arrow/field.cc
src/arrow/schema.cc
src/arrow/type.cc
)

Expand Down
1 change: 0 additions & 1 deletion cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ install(FILES
set(ARROW_TEST_LINK_LIBS arrow_test_util ${ARROW_MIN_TEST_LIBS})

ADD_ARROW_TEST(array-test)
ADD_ARROW_TEST(schema-test)
4 changes: 4 additions & 0 deletions cpp/src/arrow/table/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

set(TABLE_SRCS
column.cc
schema.cc
table.cc
)

set(TABLE_LIBS
Expand All @@ -37,3 +39,5 @@ install(FILES
DESTINATION include/arrow/table)

ADD_ARROW_TEST(column-test)
ADD_ARROW_TEST(schema-test)
ADD_ARROW_TEST(table-test)
37 changes: 9 additions & 28 deletions cpp/src/arrow/table/column-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,29 @@
#include <vector>

#include "arrow/field.h"
#include "arrow/schema.h"
#include "arrow/table/column.h"
#include "arrow/table/schema.h"
#include "arrow/table/test-common.h"
#include "arrow/test-util.h"
#include "arrow/type.h"
#include "arrow/types/integer.h"
#include "arrow/util/bit-util.h"
#include "arrow/util/buffer.h"
#include "arrow/util/memory-pool.h"
#include "arrow/util/status.h"

using std::shared_ptr;
using std::vector;

namespace arrow {

class TestColumn : public ::testing::Test {
public:
void SetUp() {
pool_ = GetDefaultMemoryPool();
}

template <typename ArrayType>
std::shared_ptr<Array> MakeArray(int32_t length, int32_t null_count = 0) {
auto data = std::make_shared<PoolBuffer>(pool_);
auto nulls = std::make_shared<PoolBuffer>(pool_);
data->Resize(length * sizeof(typename ArrayType::value_type));
nulls->Resize(util::bytes_for_bits(length));
return std::make_shared<ArrayType>(length, data, 10, nulls);
}

class TestColumn : public TestBase {
protected:
MemoryPool* pool_;

std::shared_ptr<ChunkedArray> data_;
std::unique_ptr<Column> column_;
};

TEST_F(TestColumn, BasicAPI) {
ArrayVector arrays;
arrays.push_back(MakeArray<Int32Array>(100));
arrays.push_back(MakeArray<Int32Array>(100, 10));
arrays.push_back(MakeArray<Int32Array>(100, 20));
arrays.push_back(MakePrimitive<Int32Array>(100));
arrays.push_back(MakePrimitive<Int32Array>(100, 10));
arrays.push_back(MakePrimitive<Int32Array>(100, 20));

auto field = std::make_shared<Field>("c0", INT32);
column_.reset(new Column(field, arrays));
Expand All @@ -77,15 +58,15 @@ TEST_F(TestColumn, BasicAPI) {

TEST_F(TestColumn, ChunksInhomogeneous) {
ArrayVector arrays;
arrays.push_back(MakeArray<Int32Array>(100));
arrays.push_back(MakeArray<Int32Array>(100, 10));
arrays.push_back(MakePrimitive<Int32Array>(100));
arrays.push_back(MakePrimitive<Int32Array>(100, 10));

auto field = std::make_shared<Field>("c0", INT32);
column_.reset(new Column(field, arrays));

ASSERT_OK(column_->ValidateData());

arrays.push_back(MakeArray<Int16Array>(100, 10));
arrays.push_back(MakePrimitive<Int16Array>(100, 10));
column_.reset(new Column(field, arrays));
ASSERT_RAISES(Invalid, column_->ValidateData());
}
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/table/column.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Column::Column(const std::shared_ptr<Field>& field, const ArrayVector& chunks) :
data_ = std::make_shared<ChunkedArray>(chunks);
}

Column::Column(const std::shared_ptr<Field>& field,
const std::shared_ptr<Array>& data) :
field_(field) {
data_ = std::make_shared<ChunkedArray>(ArrayVector({data}));
}

Column::Column(const std::shared_ptr<Field>& field,
const std::shared_ptr<ChunkedArray>& data) :
field_(field),
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/table/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Column {
Column(const std::shared_ptr<Field>& field,
const std::shared_ptr<ChunkedArray>& data);

Column(const std::shared_ptr<Field>& field, const std::shared_ptr<Array>& data);

int64_t length() const {
return data_->length();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <vector>

#include "arrow/field.h"
#include "arrow/schema.h"
#include "arrow/table/schema.h"
#include "arrow/type.h"
#include "arrow/types/string.h"

Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/schema.cc → cpp/src/arrow/table/schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

#include "arrow/schema.h"
#include "arrow/table/schema.h"

#include <memory>
#include <string>
Expand Down
File renamed without changes.
121 changes: 121 additions & 0 deletions cpp/src/arrow/table/table-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// 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();

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
74 changes: 74 additions & 0 deletions cpp/src/arrow/table/table.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider checking that columns[0] is valid? Do you have any objections to including glog as part of the tool chain, so we can use its asserts, if so I will open up a jira to do so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not leak glog in our public headers. We can adapt what I did here, or some variant thereof, though: https://github.com/apache/parquet-cpp/blob/master/src/parquet/util/logging.h

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(We could limit glog to only .cc files, but we may want to put DEBUG assets in headers, too)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneeded check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, thx

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_) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider checking that column pointer is valid.

std::stringstream ss;
ss << "Column " << i << " expected length "
<< num_rows_
<< " but got length "
<< col->length();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding column name to message as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressing both of these in #28

return Status::Invalid(ss.str());
}
}
return Status::OK();
}


} // namespace arrow
Loading