Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clickhouse/columns/decimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ ColumnRef ColumnDecimal::CloneEmpty() const {

void ColumnDecimal::Swap(Column& other) {
auto & col = dynamic_cast<ColumnDecimal &>(other);

if (col.GetPrecision() != GetPrecision()) {
throw ValidationError("Can't swap Decimal columns when precisions are not the same: "
+ std::to_string(GetPrecision()) + "(this) != "
+ std::to_string(col.GetPrecision()) + "(that)");
}

if (col.GetScale() != GetScale()) {
throw ValidationError("Can't swap Decimal columns when scales are not the same: "
+ std::to_string(GetScale()) + "(this) != "
+ std::to_string(col.GetScale()) + "(that)");
}

data_.swap(col.data_);
}

Expand Down
54 changes: 54 additions & 0 deletions ut/columns_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,60 @@ TEST(ColumnsCase, DecimalStringAt) {

}

TEST(ColumnsCase, DecimalSwap) {
auto column1 = std::make_shared<ColumnDecimal>(18, 2);
auto column2 = std::make_shared<ColumnDecimal>(18, 2);
column1->Append("1.23");
column2->Append("4.56");

column1->Swap(*column2);

EXPECT_EQ(column1->StringAt(0), "4.56");
EXPECT_EQ(column2->StringAt(0), "1.23");
}

TEST(ColumnsCase, DecimalSwapDifferentScale) {
auto column1 = std::make_shared<ColumnDecimal>(18, 2);
auto column2 = std::make_shared<ColumnDecimal>(18, 3);
column1->Append("1.23");
column2->Append("4.567");

EXPECT_THROW(column1->Swap(*column2), ValidationError);

EXPECT_EQ(column1->GetType().GetName(), "Decimal(18,2)");
EXPECT_EQ(column2->GetType().GetName(), "Decimal(18,3)");
EXPECT_EQ(column1->StringAt(0), "1.23");
EXPECT_EQ(column2->StringAt(0), "4.567");
}

TEST(ColumnsCase, DecimalSwapDifferentPrecision) {
auto column1 = std::make_shared<ColumnDecimal>(9, 2);
auto column2 = std::make_shared<ColumnDecimal>(18, 2);
column1->Append("1.23");
column2->Append("4.56");

EXPECT_THROW(column1->Swap(*column2), ValidationError);

EXPECT_EQ(column1->GetType().GetName(), "Decimal(9,2)");
EXPECT_EQ(column2->GetType().GetName(), "Decimal(18,2)");
EXPECT_EQ(column1->StringAt(0), "1.23");
EXPECT_EQ(column2->StringAt(0), "4.56");
}

TEST(ColumnsCase, DecimalSwapDifferentPrecisionSameStorageType) {
auto column1 = std::make_shared<ColumnDecimal>(17, 2);
auto column2 = std::make_shared<ColumnDecimal>(18, 2);
column1->Append("1.23");
column2->Append("4.56");

EXPECT_THROW(column1->Swap(*column2), ValidationError);

EXPECT_EQ(column1->GetType().GetName(), "Decimal(17,2)");
EXPECT_EQ(column2->GetType().GetName(), "Decimal(18,2)");
EXPECT_EQ(column1->StringAt(0), "1.23");
EXPECT_EQ(column2->StringAt(0), "4.56");
}

TEST(ColumnsCase, NumericInit) {
auto col = std::make_shared<ColumnUInt32>(MakeNumbers());

Expand Down
Loading