Skip to content

Commit 612fbc7

Browse files
committed
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 <wesm@apache.org> 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
1 parent 9c2b954 commit 612fbc7

File tree

13 files changed

+358
-32
lines changed

13 files changed

+358
-32
lines changed

cpp/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ set(ARROW_SRCS
470470
src/arrow/array.cc
471471
src/arrow/builder.cc
472472
src/arrow/field.cc
473-
src/arrow/schema.cc
474473
src/arrow/type.cc
475474
)
476475

cpp/src/arrow/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ install(FILES
3030
set(ARROW_TEST_LINK_LIBS arrow_test_util ${ARROW_MIN_TEST_LIBS})
3131

3232
ADD_ARROW_TEST(array-test)
33-
ADD_ARROW_TEST(schema-test)

cpp/src/arrow/table/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
set(TABLE_SRCS
2323
column.cc
24+
schema.cc
25+
table.cc
2426
)
2527

2628
set(TABLE_LIBS
@@ -37,3 +39,5 @@ install(FILES
3739
DESTINATION include/arrow/table)
3840

3941
ADD_ARROW_TEST(column-test)
42+
ADD_ARROW_TEST(schema-test)
43+
ADD_ARROW_TEST(table-test)

cpp/src/arrow/table/column-test.cc

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,29 @@
2222
#include <vector>
2323

2424
#include "arrow/field.h"
25-
#include "arrow/schema.h"
2625
#include "arrow/table/column.h"
26+
#include "arrow/table/schema.h"
27+
#include "arrow/table/test-common.h"
2728
#include "arrow/test-util.h"
2829
#include "arrow/type.h"
2930
#include "arrow/types/integer.h"
30-
#include "arrow/util/bit-util.h"
31-
#include "arrow/util/buffer.h"
32-
#include "arrow/util/memory-pool.h"
33-
#include "arrow/util/status.h"
3431

3532
using std::shared_ptr;
3633
using std::vector;
3734

3835
namespace arrow {
3936

40-
class TestColumn : public ::testing::Test {
41-
public:
42-
void SetUp() {
43-
pool_ = GetDefaultMemoryPool();
44-
}
45-
46-
template <typename ArrayType>
47-
std::shared_ptr<Array> MakeArray(int32_t length, int32_t null_count = 0) {
48-
auto data = std::make_shared<PoolBuffer>(pool_);
49-
auto nulls = std::make_shared<PoolBuffer>(pool_);
50-
data->Resize(length * sizeof(typename ArrayType::value_type));
51-
nulls->Resize(util::bytes_for_bits(length));
52-
return std::make_shared<ArrayType>(length, data, 10, nulls);
53-
}
54-
37+
class TestColumn : public TestBase {
5538
protected:
56-
MemoryPool* pool_;
57-
5839
std::shared_ptr<ChunkedArray> data_;
5940
std::unique_ptr<Column> column_;
6041
};
6142

6243
TEST_F(TestColumn, BasicAPI) {
6344
ArrayVector arrays;
64-
arrays.push_back(MakeArray<Int32Array>(100));
65-
arrays.push_back(MakeArray<Int32Array>(100, 10));
66-
arrays.push_back(MakeArray<Int32Array>(100, 20));
45+
arrays.push_back(MakePrimitive<Int32Array>(100));
46+
arrays.push_back(MakePrimitive<Int32Array>(100, 10));
47+
arrays.push_back(MakePrimitive<Int32Array>(100, 20));
6748

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

7859
TEST_F(TestColumn, ChunksInhomogeneous) {
7960
ArrayVector arrays;
80-
arrays.push_back(MakeArray<Int32Array>(100));
81-
arrays.push_back(MakeArray<Int32Array>(100, 10));
61+
arrays.push_back(MakePrimitive<Int32Array>(100));
62+
arrays.push_back(MakePrimitive<Int32Array>(100, 10));
8263

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

8667
ASSERT_OK(column_->ValidateData());
8768

88-
arrays.push_back(MakeArray<Int16Array>(100, 10));
69+
arrays.push_back(MakePrimitive<Int16Array>(100, 10));
8970
column_.reset(new Column(field, arrays));
9071
ASSERT_RAISES(Invalid, column_->ValidateData());
9172
}

cpp/src/arrow/table/column.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ Column::Column(const std::shared_ptr<Field>& field, const ArrayVector& chunks) :
3939
data_ = std::make_shared<ChunkedArray>(chunks);
4040
}
4141

42+
Column::Column(const std::shared_ptr<Field>& field,
43+
const std::shared_ptr<Array>& data) :
44+
field_(field) {
45+
data_ = std::make_shared<ChunkedArray>(ArrayVector({data}));
46+
}
47+
4248
Column::Column(const std::shared_ptr<Field>& field,
4349
const std::shared_ptr<ChunkedArray>& data) :
4450
field_(field),

cpp/src/arrow/table/column.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class Column {
6767
Column(const std::shared_ptr<Field>& field,
6868
const std::shared_ptr<ChunkedArray>& data);
6969

70+
Column(const std::shared_ptr<Field>& field, const std::shared_ptr<Array>& data);
71+
7072
int64_t length() const {
7173
return data_->length();
7274
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <vector>
2222

2323
#include "arrow/field.h"
24-
#include "arrow/schema.h"
24+
#include "arrow/table/schema.h"
2525
#include "arrow/type.h"
2626
#include "arrow/types/string.h"
2727

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
#include "arrow/schema.h"
18+
#include "arrow/table/schema.h"
1919

2020
#include <memory>
2121
#include <string>

cpp/src/arrow/table/table-test.cc

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include <gtest/gtest.h>
19+
#include <cstdint>
20+
#include <memory>
21+
#include <string>
22+
#include <vector>
23+
24+
#include "arrow/field.h"
25+
#include "arrow/table/column.h"
26+
#include "arrow/table/schema.h"
27+
#include "arrow/table/table.h"
28+
#include "arrow/table/test-common.h"
29+
#include "arrow/test-util.h"
30+
#include "arrow/type.h"
31+
#include "arrow/types/integer.h"
32+
33+
using std::shared_ptr;
34+
using std::vector;
35+
36+
namespace arrow {
37+
38+
class TestTable : public TestBase {
39+
public:
40+
void MakeExample1(int length) {
41+
auto f0 = std::make_shared<Field>("f0", INT32);
42+
auto f1 = std::make_shared<Field>("f1", UINT8);
43+
auto f2 = std::make_shared<Field>("f2", INT16);
44+
45+
vector<shared_ptr<Field> > fields = {f0, f1, f2};
46+
schema_ = std::make_shared<Schema>(fields);
47+
48+
columns_ = {
49+
std::make_shared<Column>(schema_->field(0), MakePrimitive<Int32Array>(length)),
50+
std::make_shared<Column>(schema_->field(1), MakePrimitive<UInt8Array>(length)),
51+
std::make_shared<Column>(schema_->field(2), MakePrimitive<Int16Array>(length))
52+
};
53+
}
54+
55+
protected:
56+
std::unique_ptr<Table> table_;
57+
shared_ptr<Schema> schema_;
58+
vector<std::shared_ptr<Column> > columns_;
59+
};
60+
61+
TEST_F(TestTable, EmptySchema) {
62+
auto empty_schema = shared_ptr<Schema>(new Schema({}));
63+
table_.reset(new Table("data", empty_schema, columns_));
64+
ASSERT_OK(table_->ValidateColumns());
65+
ASSERT_EQ(0, table_->num_rows());
66+
ASSERT_EQ(0, table_->num_columns());
67+
}
68+
69+
TEST_F(TestTable, Ctors) {
70+
int length = 100;
71+
MakeExample1(length);
72+
73+
std::string name = "data";
74+
75+
table_.reset(new Table(name, schema_, columns_));
76+
ASSERT_OK(table_->ValidateColumns());
77+
ASSERT_EQ(name, table_->name());
78+
ASSERT_EQ(length, table_->num_rows());
79+
ASSERT_EQ(3, table_->num_columns());
80+
81+
table_.reset(new Table(name, schema_, columns_, length));
82+
ASSERT_OK(table_->ValidateColumns());
83+
ASSERT_EQ(name, table_->name());
84+
ASSERT_EQ(length, table_->num_rows());
85+
}
86+
87+
TEST_F(TestTable, Metadata) {
88+
int length = 100;
89+
MakeExample1(length);
90+
91+
std::string name = "data";
92+
table_.reset(new Table(name, schema_, columns_));
93+
94+
ASSERT_TRUE(table_->schema()->Equals(schema_));
95+
96+
auto col = table_->column(0);
97+
ASSERT_EQ(schema_->field(0)->name, col->name());
98+
ASSERT_EQ(schema_->field(0)->type, col->type());
99+
}
100+
101+
TEST_F(TestTable, InvalidColumns) {
102+
// Check that columns are all the same length
103+
int length = 100;
104+
MakeExample1(length);
105+
106+
table_.reset(new Table("data", schema_, columns_, length - 1));
107+
ASSERT_RAISES(Invalid, table_->ValidateColumns());
108+
109+
columns_.clear();
110+
111+
// Wrong number of columns
112+
table_.reset(new Table("data", schema_, columns_, length));
113+
ASSERT_RAISES(Invalid, table_->ValidateColumns());
114+
115+
columns_ = {
116+
std::make_shared<Column>(schema_->field(0), MakePrimitive<Int32Array>(length)),
117+
std::make_shared<Column>(schema_->field(1), MakePrimitive<UInt8Array>(length)),
118+
std::make_shared<Column>(schema_->field(2), MakePrimitive<Int16Array>(length - 1))
119+
};
120+
121+
table_.reset(new Table("data", schema_, columns_, length));
122+
ASSERT_RAISES(Invalid, table_->ValidateColumns());
123+
}
124+
125+
} // namespace arrow

0 commit comments

Comments
 (0)