Skip to content

Commit bf4b978

Browse files
authored
Merge branch 'master' into support-date32
2 parents 7342794 + a87ce63 commit bf4b978

35 files changed

+1487
-704
lines changed

BUCK

Lines changed: 0 additions & 20 deletions
This file was deleted.

clickhouse/client.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,12 @@ void Client::Impl::WriteBlock(const Block& block, OutputStream& output) {
653653
WireFormat::WriteString(output, bi.Name());
654654
WireFormat::WriteString(output, bi.Type()->GetName());
655655

656-
bi.Column()->Save(&output);
656+
// Empty columns are not serialized and occupy exactly 0 bytes.
657+
// ref https://github.com/ClickHouse/ClickHouse/blob/39b37a3240f74f4871c8c1679910e065af6bea19/src/Formats/NativeWriter.cpp#L163
658+
const bool containsData = block.GetRowCount() > 0;
659+
if (containsData) {
660+
bi.Column()->Save(&output);
661+
}
657662
}
658663
output.Flush();
659664
}

clickhouse/client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct ClientOptions {
8686
// TCP options
8787
DECLARE_FIELD(tcp_nodelay, bool, TcpNoDelay, true);
8888

89+
// TODO deprecate setting
8990
/** It helps to ease migration of the old codebases, which can't afford to switch
9091
* to using ColumnLowCardinalityT or ColumnLowCardinality directly,
9192
* but still want to benefit from smaller on-wire LowCardinality bandwidth footprint.

clickhouse/columns/array.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "array.h"
22
#include "numeric.h"
3+
34
#include <stdexcept>
45

56
namespace clickhouse {
@@ -45,10 +46,9 @@ ColumnRef ColumnArray::Slice(size_t begin, size_t size) const {
4546
if (size && begin + size > Size())
4647
throw ValidationError("Slice indexes are out of bounds");
4748

48-
auto result = std::make_shared<ColumnArray>(data_->CloneEmpty());
49-
for (size_t i = 0; i < size; i++) {
50-
result->AppendAsColumn(GetAsColumn(begin + i));
51-
}
49+
auto result = std::make_shared<ColumnArray>(data_->Slice(GetOffset(begin), GetOffset(begin + size) - GetOffset(begin)));
50+
for (size_t i = 0; i < size; i++)
51+
result->AddOffset(GetSize(begin + i));
5252

5353
return result;
5454
}

clickhouse/columns/date.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void ColumnDate::Swap(Column& other) {
5858
}
5959

6060
ItemView ColumnDate::GetItem(size_t index) const {
61-
return data_->GetItem(index);
61+
return ItemView(Type::Date, data_->GetItem(index));
6262
}
6363

6464

@@ -179,7 +179,7 @@ ColumnRef ColumnDateTime::Slice(size_t begin, size_t len) const {
179179
}
180180

181181
ColumnRef ColumnDateTime::CloneEmpty() const {
182-
return std::make_shared<ColumnDate>();
182+
return std::make_shared<ColumnDateTime>();
183183
}
184184

185185
void ColumnDateTime::Swap(Column& other) {
@@ -188,7 +188,7 @@ void ColumnDateTime::Swap(Column& other) {
188188
}
189189

190190
ItemView ColumnDateTime::GetItem(size_t index) const {
191-
return data_->GetItem(index);
191+
return ItemView(Type::DateTime, data_->GetItem(index));
192192
}
193193

194194
ColumnDateTime64::ColumnDateTime64(size_t precision)
@@ -246,7 +246,7 @@ size_t ColumnDateTime64::Size() const {
246246
}
247247

248248
ItemView ColumnDateTime64::GetItem(size_t index) const {
249-
return data_->GetItem(index);
249+
return ItemView(Type::DateTime64, data_->GetItem(index));
250250
}
251251

252252
void ColumnDateTime64::Swap(Column& other) {

clickhouse/columns/decimal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void ColumnDecimal::Swap(Column& other) {
229229
}
230230

231231
ItemView ColumnDecimal::GetItem(size_t index) const {
232-
return data_->GetItem(index);
232+
return ItemView{GetType().GetCode(), data_->GetItem(index)};
233233
}
234234

235235
size_t ColumnDecimal::GetScale() const

clickhouse/columns/factory.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast, CreateColumnByTypeSetti
165165
return std::make_shared<LowCardinalitySerializationAdaptor<ColumnString>>();
166166
case Type::FixedString:
167167
return std::make_shared<LowCardinalitySerializationAdaptor<ColumnFixedString>>(nested.elements.front().value);
168+
case Type::Nullable:
169+
throw UnimplementedError("LowCardinality(" + nested.name + ") is not supported with LowCardinalityAsWrappedColumn on");
168170
default:
169171
throw UnimplementedError("LowCardinality(" + nested.name + ") is not supported");
170172
}
@@ -176,6 +178,13 @@ static ColumnRef CreateColumnFromAst(const TypeAst& ast, CreateColumnByTypeSetti
176178
return std::make_shared<ColumnLowCardinalityT<ColumnString>>();
177179
case Type::FixedString:
178180
return std::make_shared<ColumnLowCardinalityT<ColumnFixedString>>(nested.elements.front().value);
181+
case Type::Nullable:
182+
return std::make_shared<ColumnLowCardinality>(
183+
std::make_shared<ColumnNullable>(
184+
CreateColumnFromAst(nested.elements.front(), settings),
185+
std::make_shared<ColumnUInt8>()
186+
)
187+
);
179188
default:
180189
throw UnimplementedError("LowCardinality(" + nested.name + ") is not supported");
181190
}

clickhouse/columns/ip4.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void ColumnIPv4::Swap(Column& other) {
9797
}
9898

9999
ItemView ColumnIPv4::GetItem(size_t index) const {
100-
return data_->GetItem(index);
100+
return ItemView(Type::IPv4, data_->GetItem(index));
101101
}
102102

103103
}

clickhouse/columns/ip6.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void ColumnIPv6::Swap(Column& other) {
9797
}
9898

9999
ItemView ColumnIPv6::GetItem(size_t index) const {
100-
return data_->GetItem(index);
100+
return ItemView{Type::IPv6, data_->GetItem(index)};
101101
}
102102

103103
}

clickhouse/columns/itemview.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,71 @@
11
#include "../columns/itemview.h"
22

3+
#include <algorithm>
4+
#include <sstream>
5+
6+
namespace {
7+
8+
template <typename Container>
9+
std::string ContainerToString(Container container, const char * separator = ", ") {
10+
std::stringstream sstr;
11+
const auto end = std::end(container);
12+
for (auto i = std::begin(container); i != end; /*intentionally no ++i*/) {
13+
const auto & elem = *i;
14+
sstr << elem;
15+
16+
if (++i != end) {
17+
sstr << separator;
18+
}
19+
}
20+
21+
return sstr.str();
22+
}
23+
24+
}
25+
326
namespace clickhouse {
427

528
void ItemView::ValidateData(Type::Code type, DataType data) {
6-
int expected_size = 0;
29+
30+
auto AssertSize = [type, &data](std::initializer_list<int> allowed_sizes) -> void {
31+
const auto end = std::end(allowed_sizes);
32+
if (std::find(std::begin(allowed_sizes), end, static_cast<int>(data.size())) == end) {
33+
throw AssertionError(std::string("ItemView value size mismatch for ")
34+
+ Type::TypeName(type)
35+
+ " expected: " + ContainerToString(allowed_sizes, " or ")
36+
+ ", got: " + std::to_string(data.size()));
37+
}
38+
};
39+
740
switch (type) {
841
case Type::Code::Void:
9-
expected_size = 0;
10-
break;
42+
return AssertSize({0});
1143

1244
case Type::Code::Int8:
1345
case Type::Code::UInt8:
1446
case Type::Code::Enum8:
15-
expected_size = 1;
16-
break;
47+
return AssertSize({1});
1748

1849
case Type::Code::Int16:
1950
case Type::Code::UInt16:
2051
case Type::Code::Date:
2152
case Type::Code::Enum16:
22-
expected_size = 2;
23-
break;
53+
return AssertSize({2});
2454

2555
case Type::Code::Int32:
2656
case Type::Code::UInt32:
2757
case Type::Code::Float32:
2858
case Type::Code::DateTime:
2959
case Type::Code::IPv4:
3060
case Type::Code::Decimal32:
31-
expected_size = 4;
32-
break;
61+
return AssertSize({4});
3362

3463
case Type::Code::Int64:
3564
case Type::Code::UInt64:
3665
case Type::Code::Float64:
3766
case Type::Code::DateTime64:
38-
case Type::Code::IPv6:
3967
case Type::Code::Decimal64:
40-
expected_size = 8;
41-
break;
68+
return AssertSize({8});
4269

4370
case Type::Code::String:
4471
case Type::Code::FixedString:
@@ -49,24 +76,21 @@ void ItemView::ValidateData(Type::Code type, DataType data) {
4976
case Type::Code::Nullable:
5077
case Type::Code::Tuple:
5178
case Type::Code::LowCardinality:
52-
throw UnimplementedError("Unsupported type in ItemView: " + std::to_string(static_cast<int>(type)));
79+
throw AssertionError("Unsupported type in ItemView: " + std::string(Type::TypeName(type)));
5380

81+
case Type::Code::IPv6:
5482
case Type::Code::UUID:
5583
case Type::Code::Int128:
56-
case Type::Code::Decimal:
5784
case Type::Code::Decimal128:
58-
expected_size = 16;
59-
break;
85+
return AssertSize({16});
86+
87+
case Type::Code::Decimal:
88+
// Could be either Decimal32, Decimal64 or Decimal128
89+
return AssertSize({4, 8, 16});
6090

6191
default:
6292
throw UnimplementedError("Unknon type code:" + std::to_string(static_cast<int>(type)));
6393
}
64-
65-
if (expected_size != static_cast<int>(data.size())) {
66-
throw AssertionError("Value size mismatch for type "
67-
+ std::to_string(static_cast<int>(type)) + " expected: "
68-
+ std::to_string(expected_size) + ", got: " + std::to_string(data.size()));
69-
}
7094
}
7195

7296
}

0 commit comments

Comments
 (0)