Skip to content

Commit b350409

Browse files
committed
Minor fixes + more tests
1 parent c9e4841 commit b350409

File tree

7 files changed

+40
-19
lines changed

7 files changed

+40
-19
lines changed

clickhouse/columns/decimal.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "decimal.h"
22

3-
#include <cassert>
4-
53
namespace clickhouse {
64

75
ColumnDecimal::ColumnDecimal(size_t precision, size_t scale)
@@ -16,8 +14,9 @@ ColumnDecimal::ColumnDecimal(size_t precision, size_t scale)
1614
}
1715
}
1816

19-
ColumnDecimal::ColumnDecimal(TypeRef type)
20-
: Column(type)
17+
ColumnDecimal::ColumnDecimal(TypeRef type, ColumnRef data)
18+
: Column(type),
19+
data_(data)
2120
{
2221
}
2322

@@ -91,7 +90,7 @@ Int128 ColumnDecimal::At(size_t i) const {
9190
case Type::Int128:
9291
return data_->As<ColumnInt128>()->At(i);
9392
default:
94-
assert(false && "Invalid data_ column type in ColumnDecimal");
93+
throw std::runtime_error("Invalid data_ column type in ColumnDecimal");
9594
return 0;
9695
}
9796
}
@@ -119,9 +118,8 @@ size_t ColumnDecimal::Size() const {
119118
}
120119

121120
ColumnRef ColumnDecimal::Slice(size_t begin, size_t len) {
122-
std::shared_ptr<ColumnDecimal> slice(new ColumnDecimal(type_));
123-
slice->data_ = data_->Slice(begin, len);
124-
return slice;
121+
// coundn't use std::make_shared since this c-tor is private
122+
return ColumnRef{new ColumnDecimal(type_, data_->Slice(begin, len))};
125123
}
126124

127125
void ColumnDecimal::Swap(Column& other) {

clickhouse/columns/decimal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ColumnDecimal : public Column {
3737
/// - ColumnInt128
3838
ColumnRef data_;
3939

40-
explicit ColumnDecimal(TypeRef type); // for `Slice(…)`
40+
explicit ColumnDecimal(TypeRef type, ColumnRef data);
4141
};
4242

4343
}

clickhouse/columns/numeric.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ ColumnVector<T>::ColumnVector()
1010
}
1111

1212
template <typename T>
13-
ColumnVector<T>::ColumnVector(std::vector<T> data)
13+
ColumnVector<T>::ColumnVector(const std::vector<T> & data)
1414
: Column(Type::CreateSimple<T>())
15-
, data_(std::move(data))
15+
, data_(data)
16+
{
17+
}
18+
19+
template <typename T>
20+
ColumnVector<T>::ColumnVector(std::vector<T> && data)
21+
: Column(Type::CreateSimple<T>())
22+
, data_(data)
1623
{
1724
}
1825

clickhouse/columns/numeric.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class ColumnVector : public Column {
1414

1515
ColumnVector();
1616

17-
explicit ColumnVector(std::vector<T> data);
17+
explicit ColumnVector(const std::vector<T>& data);
18+
explicit ColumnVector(std::vector<T> && data);
1819

1920
/// Appends one element to the end of column.
2021
void Append(const T& value);

clickhouse/types/type_parser.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace clickhouse {
1010

1111
bool TypeAst::operator==(const TypeAst & other) const {
12-
// TODO: operator == must be a friend function.
1312
return meta == other.meta
1413
&& code == other.code
1514
&& name == other.name

ut/client_ut.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,6 @@ TEST_P(ClientCase, DateTime64) {
769769

770770
ASSERT_EQ(1U, block.GetColumnCount());
771771
if (auto col = block[0]->As<ColumnDateTime64>()) {
772-
ASSERT_EQ(data.size(), col->Size());
773772
for (size_t i = 0; i < col->Size(); ++i) {
774773
EXPECT_EQ(data[i], col->At(i)) << " at index: " << i;
775774
}

ut/columns_ut.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ TEST(ColumnsCase, DateTime64_Swap) {
224224
}
225225

226226
auto column2 = std::make_shared<ColumnDateTime64>(6ul);
227+
const auto single_dt64_value = 1'234'567'890'123'456'789ll;
228+
column2->Append(single_dt64_value);
227229
column->Swap(*column2);
228230

229231
// Validate that all items were transferred to column2.
230-
ASSERT_EQ(0u, column->Size());
231-
ASSERT_EQ(data.size(), column2->Size());
232-
233-
// Clearing doesn't affects precision value.
234-
ASSERT_EQ(6ul, column->GetPrecision());
232+
ASSERT_EQ(1u, column->Size());
233+
EXPECT_EQ(single_dt64_value, column->At(0));
235234

235+
ASSERT_EQ(data.size(), column2->Size());
236236
for (size_t i = 0; i < data.size(); ++i) {
237237
ASSERT_EQ(data[i], column2->At(i));
238238
}
@@ -289,6 +289,23 @@ TEST(ColumnsCase, DateTime64_Slice) {
289289
}
290290
}
291291

292+
TEST(ColumnsCase, DateTime64_Slice_OUTOFBAND) {
293+
// Slice() shouldn't throw exceptions on invalid parameters, just clamp values to the nearest bounds.
294+
295+
auto column = std::make_shared<ColumnDateTime64>(6ul);
296+
297+
// Non-Empty slice on empty column
298+
EXPECT_EQ(0u, column->Slice(0, 10)->Size());
299+
300+
const auto data = MakeDateTime64s();
301+
for (const auto & v : data) {
302+
column->Append(v);
303+
}
304+
305+
EXPECT_EQ(column->Slice(0, data.size() + 1)->Size(), data.size());
306+
EXPECT_EQ(column->Slice(data.size() + 1, 1)->Size(), 0u);
307+
}
308+
292309
TEST(ColumnsCase, DateTime64_Swap_EXCEPTION) {
293310
auto column1 = std::make_shared<ColumnDateTime64>(6ul);
294311
auto column2 = std::make_shared<ColumnDateTime64>(0ul);

0 commit comments

Comments
 (0)