Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clients that lack absl to built and use the library #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
21 changes: 13 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ before_install: |
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

# 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 --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
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -43,4 +43,4 @@ PROJECT (CLICKHOUSE-CLIENT)
tests/simple
ut
)
ENDIF (BUILD_TESTS)
ENDIF (BUILD_TESTS)
1 change: 1 addition & 0 deletions clickhouse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
2 changes: 2 additions & 0 deletions clickhouse/columns/date.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "date.h"

#include "../types/int128.h"

namespace clickhouse {

ColumnDate::ColumnDate()
Expand Down
10 changes: 10 additions & 0 deletions clickhouse/columns/decimal.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "decimal.h"

#include "../types/int128.h"

namespace
{
using namespace clickhouse;
Expand Down Expand Up @@ -117,6 +119,10 @@ ColumnDecimal::ColumnDecimal(TypeRef type, ColumnRef data)
{
}

void ColumnDecimal::Append(Int64 value) {
Append(static_cast<Int128>(value));
}

void ColumnDecimal::Append(const Int128& value) {
if (data_->Type()->GetCode() == Type::Int32) {
data_->As<ColumnInt32>()->Append(static_cast<ColumnInt32::DataType>(value));
Expand Down Expand Up @@ -191,6 +197,10 @@ Int128 ColumnDecimal::At(size_t i) const {
}
}

Int64 ColumnDecimal::AtAsInt64(size_t i) const {
return static_cast<Int64>(At(i));
}

void ColumnDecimal::Append(ColumnRef column) {
if (auto col = column->As<ColumnDecimal>()) {
data_->Append(col->data_);
Expand Down
2 changes: 2 additions & 0 deletions clickhouse/columns/decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions clickhouse/columns/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "uuid.h"

#include "../types/type_parser.h"
#include "../types/int128.h"

#include <stdexcept>

Expand Down
2 changes: 2 additions & 0 deletions clickhouse/columns/numeric.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "numeric.h"
#include "utils.h"

#include "../types/int128.h"

namespace clickhouse {

template <typename T>
Expand Down
6 changes: 5 additions & 1 deletion clickhouse/columns/numeric.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once

#include "column.h"
#include "absl/numeric/int128.h"

namespace absl
{
class int128;
}

namespace clickhouse {

Expand Down
9 changes: 9 additions & 0 deletions clickhouse/types/int128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

/// In a separate header to allow basic non-Int128 functionality on systems that lack absl.
#include <absl/numeric/int128.h>

namespace clickhouse
{
using Int128 = absl::int128;
}
7 changes: 5 additions & 2 deletions clickhouse/types/types.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#pragma once

#include "absl/numeric/int128.h"

#include <map>
#include <memory>
#include <string>
#include <vector>
#include <stdexcept>

namespace absl
{
class int128;
}

Comment on lines +9 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better to move all Int128 specific code and support into clickhouse/types/int128.h so that when user doesn't include that header he can't even refer to any type related/implemented via Int128?

namespace clickhouse {

using Int128 = absl::int128;
Expand Down
8 changes: 7 additions & 1 deletion tests/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)

if (BUILD_TESTS_WITH_INT128)
target_compile_definitions(simple-test WITH_INT128)
endif()
20 changes: 14 additions & 6 deletions tests/simple/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <clickhouse/error_codes.h>
#include <clickhouse/types/type_parser.h>

#if WITH_INT128
#include <clickhouse/types/int128.h>
#endif

#include <stdexcept>
#include <iostream>
#include <cmath>
Expand Down Expand Up @@ -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<ColumnDateTime64>(6);
d->Append(std::time(nullptr) * 1000000 + 123456);
b.AppendColumn("d", d);
{
auto dt64 = std::make_shared<ColumnDateTime64>(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<ColumnDateTime64>();
Expand Down Expand Up @@ -183,7 +190,7 @@ inline void DecimalExample(Client& client) {
{
for (size_t c = 0; c < block.GetRowCount(); ++c) {
auto col = block[0]->As<ColumnDecimal>();
cout << (int)col->At(c) << endl;
cout << (int)col->AtAsInt64(c) << endl;
}
}
);
Expand All @@ -192,7 +199,7 @@ inline void DecimalExample(Client& client) {
{
for (size_t c = 0; c < block.GetRowCount(); ++c) {
auto col = block[0]->As<ColumnDecimal>();
cout << (int)col->At(c) << endl;
cout << (int)col->AtAsInt64(c) << endl;
}
}
);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ut/client_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <clickhouse/client.h>
#include <contrib/gtest/gtest.h>

#include <clickhouse/types/int128.h>

#include <cmath>

using namespace clickhouse;
Expand Down
1 change: 1 addition & 0 deletions ut/columns_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <clickhouse/columns/numeric.h>
#include <clickhouse/columns/string.h>
#include <clickhouse/columns/uuid.h>
#include <clickhouse/types/int128.h>

#include <contrib/gtest/gtest.h>
#include "utils.h"
Expand Down
1 change: 1 addition & 0 deletions ut/itemview_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <clickhouse/columns/itemview.h>
#include <clickhouse/columns/numeric.h>
#include <clickhouse/types/int128.h>

#include <contrib/gtest/gtest.h>
#include "utils.h"
Expand Down