Skip to content

Commit bf03ba0

Browse files
committed
Allow clients that lack absl to built and use the library
Moved Int128 definition to a separate header Added few extra methods to ColumnDecimal that do not rely on Int128 Ensured that client programs can be built without absl headers present.
1 parent 1415b59 commit bf03ba0

16 files changed

+77
-21
lines changed

.travis.yml

+11-7
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ before_install: |
4949
fi
5050
5151
# Build steps
52-
script:
53-
- eval "${MATRIX_EVAL}"
54-
- mkdir build
55-
- cd build
56-
- cmake .. -DBUILD_TESTS=ON && make
57-
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./ut/clickhouse-cpp-ut ; fi
58-
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi
52+
script: |
53+
eval "${MATRIX_EVAL}"
54+
mkdir build
55+
cd build
56+
cmake .. -DBUILD_TESTS=ON && cmake --build . --target all
57+
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./ut/clickhouse-cpp-ut ; fi
58+
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi
59+
# Test clients that do not have absl in the system still can be built
60+
cmake --build . --target install . && cmake --build . --target clean
61+
cd ./tests/simple && mkdir build && cd ./build
62+
cmake .. -DBUILD_SAMPLE_WITH_INT128=ON -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON && cmake --build . --target all

CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ PROJECT (CLICKHOUSE-CLIENT)
1010

1111
USE_CXX17()
1212

13-
IF ("${CMAKE_BUILD_TYPE}" STREQUAL "")
14-
set(CMAKE_BUILD_TYPE "Debug")
15-
ENDIF()
13+
IF ("${CMAKE_BUILD_TYPE}" STREQUAL "")
14+
set(CMAKE_BUILD_TYPE "Debug")
15+
ENDIF()
1616

1717
IF (UNIX)
1818
IF (APPLE)
@@ -43,4 +43,4 @@ PROJECT (CLICKHOUSE-CLIENT)
4343
tests/simple
4444
ut
4545
)
46-
ENDIF (BUILD_TESTS)
46+
ENDIF (BUILD_TESTS)

clickhouse/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@ INSTALL(FILES columns/uuid.h DESTINATION include/clickhouse/columns/)
100100
# types
101101
INSTALL(FILES types/type_parser.h DESTINATION include/clickhouse/types/)
102102
INSTALL(FILES types/types.h DESTINATION include/clickhouse/types/)
103+
INSTALL(FILES types/int128.h DESTINATION include/clickhouse/types/)

clickhouse/columns/date.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "date.h"
22

3+
#include "../types/int128.h"
4+
35
namespace clickhouse {
46

57
ColumnDate::ColumnDate()

clickhouse/columns/decimal.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "decimal.h"
22

3+
#include "../types/int128.h"
4+
35
namespace
46
{
57
using namespace clickhouse;
@@ -117,6 +119,10 @@ ColumnDecimal::ColumnDecimal(TypeRef type, ColumnRef data)
117119
{
118120
}
119121

122+
void ColumnDecimal::Append(Int64 value) {
123+
Append(static_cast<Int128>(value));
124+
}
125+
120126
void ColumnDecimal::Append(const Int128& value) {
121127
if (data_->Type()->GetCode() == Type::Int32) {
122128
data_->As<ColumnInt32>()->Append(static_cast<ColumnInt32::DataType>(value));
@@ -191,6 +197,10 @@ Int128 ColumnDecimal::At(size_t i) const {
191197
}
192198
}
193199

200+
Int64 ColumnDecimal::AtAsInt64(size_t i) const {
201+
return static_cast<Int64>(At(i));
202+
}
203+
194204
void ColumnDecimal::Append(ColumnRef column) {
195205
if (auto col = column->As<ColumnDecimal>()) {
196206
data_->Append(col->data_);

clickhouse/columns/decimal.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ class ColumnDecimal : public Column {
1212
public:
1313
ColumnDecimal(size_t precision, size_t scale);
1414

15+
void Append(Int64 value); /// When Int128 is not supported by\not available to users.
1516
void Append(const Int128& value);
1617
void Append(const std::string& value);
1718

1819
Int128 At(size_t i) const;
20+
Int64 AtAsInt64(size_t i) const; /// result will overflow if value doesn't fit into Int64.
1921

2022
public:
2123
void Append(ColumnRef column) override;

clickhouse/columns/factory.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "uuid.h"
1717

1818
#include "../types/type_parser.h"
19+
#include "../types/int128.h"
1920

2021
#include <stdexcept>
2122

clickhouse/columns/numeric.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "numeric.h"
22
#include "utils.h"
33

4+
#include "../types/int128.h"
5+
46
namespace clickhouse {
57

68
template <typename T>

clickhouse/columns/numeric.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#pragma once
22

33
#include "column.h"
4-
#include "absl/numeric/int128.h"
4+
5+
namespace absl
6+
{
7+
class int128;
8+
}
59

610
namespace clickhouse {
711

clickhouse/types/int128.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
/// In a separate header to allow basic non-Int128 functionality on systems that lack absl.
4+
#include <absl/numeric/int128.h>
5+
6+
namespace clickhouse
7+
{
8+
using Int128 = absl::int128;
9+
}

clickhouse/types/types.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#pragma once
22

3-
#include "absl/numeric/int128.h"
4-
53
#include <map>
64
#include <memory>
75
#include <string>
86
#include <vector>
97
#include <stdexcept>
108

9+
namespace absl
10+
{
11+
class int128;
12+
}
13+
1114
namespace clickhouse {
1215

1316
using Int128 = absl::int128;

tests/simple/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ ADD_EXECUTABLE (simple-test
22
main.cpp
33
)
44

5+
OPTION(BUILD_SAMPLE_WITH_INT128 "Build sample program with Int128 support" ON)
6+
57
TARGET_LINK_LIBRARIES (simple-test
68
clickhouse-cpp-lib
7-
)
9+
)
10+
11+
if (BUILD_TESTS_WITH_INT128)
12+
target_compile_definitions(simple-test WITH_INT128)
13+
endif()

tests/simple/main.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <clickhouse/error_codes.h>
33
#include <clickhouse/types/type_parser.h>
44

5+
#if WITH_INT128
6+
#include <clickhouse/types/int128.h>
7+
#endif
8+
59
#include <stdexcept>
610
#include <iostream>
711
#include <cmath>
@@ -145,12 +149,15 @@ inline void DateTime64Example(Client& client) {
145149
/// Create a table.
146150
client.Execute("CREATE TABLE IF NOT EXISTS test.datetime64 (dt64 DateTime64(6)) ENGINE = Memory");
147151

148-
auto d = std::make_shared<ColumnDateTime64>(6);
149-
d->Append(std::time(nullptr) * 1000000 + 123456);
150-
b.AppendColumn("d", d);
152+
{
153+
auto dt64 = std::make_shared<ColumnDateTime64>(6);
154+
dt64->Append(std::time(nullptr) * 1000000 + 123456);
155+
b.AppendColumn("dt64", dt64);
156+
}
157+
151158
client.Insert("test.datetime64", b);
152159

153-
client.Select("SELECT d FROM test.date", [](const Block& block)
160+
client.Select("SELECT dt64 FROM test.datetime64", [](const Block& block)
154161
{
155162
for (size_t c = 0; c < block.GetRowCount(); ++c) {
156163
auto col = block[0]->As<ColumnDateTime64>();
@@ -183,7 +190,7 @@ inline void DecimalExample(Client& client) {
183190
{
184191
for (size_t c = 0; c < block.GetRowCount(); ++c) {
185192
auto col = block[0]->As<ColumnDecimal>();
186-
cout << (int)col->At(c) << endl;
193+
cout << (int)col->AtAsInt64(c) << endl;
187194
}
188195
}
189196
);
@@ -192,7 +199,7 @@ inline void DecimalExample(Client& client) {
192199
{
193200
for (size_t c = 0; c < block.GetRowCount(); ++c) {
194201
auto col = block[0]->As<ColumnDecimal>();
195-
cout << (int)col->At(c) << endl;
202+
cout << (int)col->AtAsInt64(c) << endl;
196203
}
197204
}
198205
);
@@ -478,6 +485,7 @@ inline void IPExample(Client &client) {
478485
}
479486

480487
static void RunTests(Client& client) {
488+
client.Execute("CREATE DATABASE IF NOT EXISTS test");
481489
ArrayExample(client);
482490
CancelableExample(client);
483491
DateExample(client);

ut/client_ut.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <clickhouse/client.h>
22
#include <contrib/gtest/gtest.h>
33

4+
#include <clickhouse/types/int128.h>
5+
46
#include <cmath>
57

68
using namespace clickhouse;

ut/columns_ut.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <clickhouse/columns/numeric.h>
88
#include <clickhouse/columns/string.h>
99
#include <clickhouse/columns/uuid.h>
10+
#include <clickhouse/types/int128.h>
1011

1112
#include <contrib/gtest/gtest.h>
1213
#include "utils.h"

ut/itemview_ut.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <clickhouse/columns/itemview.h>
22
#include <clickhouse/columns/numeric.h>
3+
#include <clickhouse/types/int128.h>
34

45
#include <contrib/gtest/gtest.h>
56
#include "utils.h"

0 commit comments

Comments
 (0)