From bf03ba00cfcec306bdf41e7be61329e5dfeb4bbc Mon Sep 17 00:00:00 2001 From: Vasily Nemkov Date: Fri, 16 Apr 2021 12:53:44 +0300 Subject: [PATCH 1/3] 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. --- .travis.yml | 18 +++++++++++------- CMakeLists.txt | 8 ++++---- clickhouse/CMakeLists.txt | 1 + clickhouse/columns/date.cpp | 2 ++ clickhouse/columns/decimal.cpp | 10 ++++++++++ clickhouse/columns/decimal.h | 2 ++ clickhouse/columns/factory.cpp | 1 + clickhouse/columns/numeric.cpp | 2 ++ clickhouse/columns/numeric.h | 6 +++++- clickhouse/types/int128.h | 9 +++++++++ clickhouse/types/types.h | 7 +++++-- tests/simple/CMakeLists.txt | 8 +++++++- tests/simple/main.cpp | 20 ++++++++++++++------ ut/client_ut.cpp | 2 ++ ut/columns_ut.cpp | 1 + ut/itemview_ut.cpp | 1 + 16 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 clickhouse/types/int128.h diff --git a/.travis.yml b/.travis.yml index 22d42065..da279ad5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -49,10 +49,14 @@ before_install: | fi # Build steps -script: - - eval "${MATRIX_EVAL}" - - mkdir build - - cd build - - cmake .. -DBUILD_TESTS=ON && make - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./ut/clickhouse-cpp-ut ; fi - - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi +script: | + eval "${MATRIX_EVAL}" + mkdir build + cd build + cmake .. -DBUILD_TESTS=ON && cmake --build . --target all + if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./ut/clickhouse-cpp-ut ; fi + if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi + # Test clients that do not have absl in the system still can be built + cmake --build . --target install . && cmake --build . --target clean + cd ./tests/simple && mkdir build && cd ./build + cmake .. -DBUILD_SAMPLE_WITH_INT128=ON -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON && cmake --build . --target all diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f6c5e00..802662c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,9 +10,9 @@ PROJECT (CLICKHOUSE-CLIENT) USE_CXX17() - IF ("${CMAKE_BUILD_TYPE}" STREQUAL "") - set(CMAKE_BUILD_TYPE "Debug") - ENDIF() + IF ("${CMAKE_BUILD_TYPE}" STREQUAL "") + set(CMAKE_BUILD_TYPE "Debug") + ENDIF() IF (UNIX) IF (APPLE) @@ -43,4 +43,4 @@ PROJECT (CLICKHOUSE-CLIENT) tests/simple ut ) - ENDIF (BUILD_TESTS) + ENDIF (BUILD_TESTS) diff --git a/clickhouse/CMakeLists.txt b/clickhouse/CMakeLists.txt index 7e10ffd3..c0d429a1 100644 --- a/clickhouse/CMakeLists.txt +++ b/clickhouse/CMakeLists.txt @@ -100,3 +100,4 @@ INSTALL(FILES columns/uuid.h DESTINATION include/clickhouse/columns/) # types INSTALL(FILES types/type_parser.h DESTINATION include/clickhouse/types/) INSTALL(FILES types/types.h DESTINATION include/clickhouse/types/) +INSTALL(FILES types/int128.h DESTINATION include/clickhouse/types/) diff --git a/clickhouse/columns/date.cpp b/clickhouse/columns/date.cpp index fcf0df0a..cbdc303e 100644 --- a/clickhouse/columns/date.cpp +++ b/clickhouse/columns/date.cpp @@ -1,5 +1,7 @@ #include "date.h" +#include "../types/int128.h" + namespace clickhouse { ColumnDate::ColumnDate() diff --git a/clickhouse/columns/decimal.cpp b/clickhouse/columns/decimal.cpp index 7334bb12..6bfe7c40 100644 --- a/clickhouse/columns/decimal.cpp +++ b/clickhouse/columns/decimal.cpp @@ -1,5 +1,7 @@ #include "decimal.h" +#include "../types/int128.h" + namespace { using namespace clickhouse; @@ -117,6 +119,10 @@ ColumnDecimal::ColumnDecimal(TypeRef type, ColumnRef data) { } +void ColumnDecimal::Append(Int64 value) { + Append(static_cast(value)); +} + void ColumnDecimal::Append(const Int128& value) { if (data_->Type()->GetCode() == Type::Int32) { data_->As()->Append(static_cast(value)); @@ -191,6 +197,10 @@ Int128 ColumnDecimal::At(size_t i) const { } } +Int64 ColumnDecimal::AtAsInt64(size_t i) const { + return static_cast(At(i)); +} + void ColumnDecimal::Append(ColumnRef column) { if (auto col = column->As()) { data_->Append(col->data_); diff --git a/clickhouse/columns/decimal.h b/clickhouse/columns/decimal.h index 8b8cd38c..67dee080 100644 --- a/clickhouse/columns/decimal.h +++ b/clickhouse/columns/decimal.h @@ -12,10 +12,12 @@ class ColumnDecimal : public Column { public: ColumnDecimal(size_t precision, size_t scale); + void Append(Int64 value); /// When Int128 is not supported by\not available to users. void Append(const Int128& value); void Append(const std::string& value); Int128 At(size_t i) const; + Int64 AtAsInt64(size_t i) const; /// result will overflow if value doesn't fit into Int64. public: void Append(ColumnRef column) override; diff --git a/clickhouse/columns/factory.cpp b/clickhouse/columns/factory.cpp index 08b362e9..1d452687 100644 --- a/clickhouse/columns/factory.cpp +++ b/clickhouse/columns/factory.cpp @@ -16,6 +16,7 @@ #include "uuid.h" #include "../types/type_parser.h" +#include "../types/int128.h" #include diff --git a/clickhouse/columns/numeric.cpp b/clickhouse/columns/numeric.cpp index 68cc495d..39be9763 100644 --- a/clickhouse/columns/numeric.cpp +++ b/clickhouse/columns/numeric.cpp @@ -1,6 +1,8 @@ #include "numeric.h" #include "utils.h" +#include "../types/int128.h" + namespace clickhouse { template diff --git a/clickhouse/columns/numeric.h b/clickhouse/columns/numeric.h index 04a18aa2..23770bb3 100644 --- a/clickhouse/columns/numeric.h +++ b/clickhouse/columns/numeric.h @@ -1,7 +1,11 @@ #pragma once #include "column.h" -#include "absl/numeric/int128.h" + +namespace absl +{ +class int128; +} namespace clickhouse { diff --git a/clickhouse/types/int128.h b/clickhouse/types/int128.h new file mode 100644 index 00000000..07dc938e --- /dev/null +++ b/clickhouse/types/int128.h @@ -0,0 +1,9 @@ +#pragma once + +/// In a separate header to allow basic non-Int128 functionality on systems that lack absl. +#include + +namespace clickhouse +{ +using Int128 = absl::int128; +} diff --git a/clickhouse/types/types.h b/clickhouse/types/types.h index 4592d5e5..891f7a86 100644 --- a/clickhouse/types/types.h +++ b/clickhouse/types/types.h @@ -1,13 +1,16 @@ #pragma once -#include "absl/numeric/int128.h" - #include #include #include #include #include +namespace absl +{ +class int128; +} + namespace clickhouse { using Int128 = absl::int128; diff --git a/tests/simple/CMakeLists.txt b/tests/simple/CMakeLists.txt index e4a71e7d..0bcf4e22 100644 --- a/tests/simple/CMakeLists.txt +++ b/tests/simple/CMakeLists.txt @@ -2,6 +2,12 @@ ADD_EXECUTABLE (simple-test main.cpp ) +OPTION(BUILD_SAMPLE_WITH_INT128 "Build sample program with Int128 support" ON) + TARGET_LINK_LIBRARIES (simple-test clickhouse-cpp-lib -) \ No newline at end of file +) + +if (BUILD_TESTS_WITH_INT128) + target_compile_definitions(simple-test WITH_INT128) +endif() diff --git a/tests/simple/main.cpp b/tests/simple/main.cpp index 41855514..2e5d095c 100644 --- a/tests/simple/main.cpp +++ b/tests/simple/main.cpp @@ -2,6 +2,10 @@ #include #include +#if WITH_INT128 +#include +#endif + #include #include #include @@ -145,12 +149,15 @@ inline void DateTime64Example(Client& client) { /// Create a table. client.Execute("CREATE TABLE IF NOT EXISTS test.datetime64 (dt64 DateTime64(6)) ENGINE = Memory"); - auto d = std::make_shared(6); - d->Append(std::time(nullptr) * 1000000 + 123456); - b.AppendColumn("d", d); + { + auto dt64 = std::make_shared(6); + dt64->Append(std::time(nullptr) * 1000000 + 123456); + b.AppendColumn("dt64", dt64); + } + client.Insert("test.datetime64", b); - client.Select("SELECT d FROM test.date", [](const Block& block) + client.Select("SELECT dt64 FROM test.datetime64", [](const Block& block) { for (size_t c = 0; c < block.GetRowCount(); ++c) { auto col = block[0]->As(); @@ -183,7 +190,7 @@ inline void DecimalExample(Client& client) { { for (size_t c = 0; c < block.GetRowCount(); ++c) { auto col = block[0]->As(); - cout << (int)col->At(c) << endl; + cout << (int)col->AtAsInt64(c) << endl; } } ); @@ -192,7 +199,7 @@ inline void DecimalExample(Client& client) { { for (size_t c = 0; c < block.GetRowCount(); ++c) { auto col = block[0]->As(); - cout << (int)col->At(c) << endl; + cout << (int)col->AtAsInt64(c) << endl; } } ); @@ -478,6 +485,7 @@ inline void IPExample(Client &client) { } static void RunTests(Client& client) { + client.Execute("CREATE DATABASE IF NOT EXISTS test"); ArrayExample(client); CancelableExample(client); DateExample(client); diff --git a/ut/client_ut.cpp b/ut/client_ut.cpp index 717cff82..91f96300 100644 --- a/ut/client_ut.cpp +++ b/ut/client_ut.cpp @@ -1,6 +1,8 @@ #include #include +#include + #include using namespace clickhouse; diff --git a/ut/columns_ut.cpp b/ut/columns_ut.cpp index 0229ba66..ef7cad63 100644 --- a/ut/columns_ut.cpp +++ b/ut/columns_ut.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "utils.h" diff --git a/ut/itemview_ut.cpp b/ut/itemview_ut.cpp index 0be60392..d3b8048e 100644 --- a/ut/itemview_ut.cpp +++ b/ut/itemview_ut.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include "utils.h" From 92648592e98cc41a996b4f19375b14eac884098d Mon Sep 17 00:00:00 2001 From: Vasily Nemkov Date: Fri, 16 Apr 2021 19:10:21 +0300 Subject: [PATCH 2/3] Attempt to debug clickhouse installation errors on TravisCI --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index da279ad5..348e34a0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,11 +40,13 @@ matrix: before_install: | if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + set -x sudo apt-get install apt-transport-https ca-certificates dirmngr sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4 echo "deb https://repo.clickhouse.tech/deb/stable/ main/" | sudo tee \ /etc/apt/sources.list.d/clickhouse.list - sudo apt-get update -q && sudo apt-get install -q -y --allow-unauthenticated clickhouse-server + sudo apt-get update -q + sudo apt-get install -q -y --allow-unauthenticated clickhouse-server sudo service clickhouse-server start fi From e2e0ef5b6e21b97dace4c2c2d1952170c9b6cd1d Mon Sep 17 00:00:00 2001 From: Vasily Nemkov Date: Fri, 16 Apr 2021 19:23:57 +0300 Subject: [PATCH 3/3] Fixed path to the sample test program to build --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 348e34a0..dab8c060 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,6 @@ matrix: before_install: | if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then - set -x sudo apt-get install apt-transport-https ca-certificates dirmngr sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4 echo "deb https://repo.clickhouse.tech/deb/stable/ main/" | sudo tee \ @@ -59,6 +58,6 @@ script: | if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./ut/clickhouse-cpp-ut ; fi if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi # Test clients that do not have absl in the system still can be built - cmake --build . --target install . && cmake --build . --target clean - cd ./tests/simple && mkdir build && cd ./build + cmake --install . && cmake --build . --target clean + cd ../tests/simple && mkdir build && cd ./build cmake .. -DBUILD_SAMPLE_WITH_INT128=ON -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON && cmake --build . --target all