Skip to content

Commit e2987e2

Browse files
committed
Add NewColumn() to ColumnArray
Returns a new column of the proper type for inserting new values. Useful for contexts in which the item type isn't immediately known.
1 parent b4a9d70 commit e2987e2

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

clickhouse/columns/array.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ ColumnRef ColumnArray::GetAsColumn(size_t n) const {
5959
return data_->Slice(GetOffset(n), GetSize(n));
6060
}
6161

62+
ColumnRef ColumnArray::NewColumn() const {
63+
return data_->CloneEmpty();
64+
}
65+
6266
ColumnRef ColumnArray::Slice(size_t begin, size_t size) const {
6367
if (size && begin + size > Size())
6468
throw ValidationError("Slice indexes are out of bounds");

clickhouse/columns/array.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ class ColumnArray : public Column {
4646
return GetAsColumn(n)->AsStrict<T>();
4747
}
4848

49+
/// Create a new, empty column of the same type as the array element.
50+
ColumnRef NewColumn() const;
51+
52+
/// Shorthand to create a new column casted to a proper type.
53+
template <typename T>
54+
auto NewColumnAsType() const {
55+
return NewColumn()->AsStrict<T>();
56+
}
57+
4958
public:
5059
/// Increase the capacity of the column for large block insertion.
5160
void Reserve(size_t new_cap) override;

ut/column_array_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TEST(ColumnArray, Append) {
5353
auto arr1 = std::make_shared<ColumnArray>(std::make_shared<ColumnUInt64>());
5454
auto arr2 = std::make_shared<ColumnArray>(std::make_shared<ColumnUInt64>());
5555

56-
auto id = std::make_shared<ColumnUInt64>();
56+
auto id = arr1->NewColumnAsType<ColumnUInt64>();
5757
id->Append(1);
5858
arr1->AppendAsColumn(id);
5959

0 commit comments

Comments
 (0)