-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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(); | ||
} | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unneeded check? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider adding column name to message as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added https://issues.apache.org/jira/browse/ARROW-70 about this