Skip to content

Commit 51b9b61

Browse files
committed
use yandex library for clickhouse-cpp
1 parent 6455d07 commit 51b9b61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3742
-434
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
22
project(clickhouse_fdw VERSION 1.2.0 LANGUAGES C CXX)
33

4-
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
5-
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++17")
6-
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++17")
7-
elseif (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1)
8-
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++1z")
9-
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++1z")
10-
endif()
4+
include (clickhouse-cpp/cmake/cpp17.cmake)
5+
6+
USE_CXX17()
117

8+
set(CLICKHOUSE_CPP_ENABLE_INSTALL OFF CACHE BOOL "disable clickhouse-cpp installation" FORCE)
129
add_subdirectory (clickhouse-cpp)
10+
1311
target_compile_options(clickhouse-cpp-lib-static PRIVATE -fPIC)
1412
target_compile_options(clickhouse-cpp-lib PRIVATE -fPIC)
1513

src/binary.cc

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
#include <cassert>
33
#include <stdexcept>
44

5+
#include "clickhouse/columns/lowcardinality.h"
56
#include "clickhouse/columns/nullable.h"
67
#include "clickhouse/columns/factory.h"
78
#include <clickhouse/client.h>
89
#include <clickhouse/types/types.h>
910

11+
#if __cplusplus > 199711L
12+
#define register // Deprecated in C++11.
13+
#endif // #if __cplusplus > 199711L
14+
1015
extern "C" {
1116

1217
#include "postgres.h"
@@ -31,14 +36,14 @@ using namespace clickhouse;
3136

3237
#if defined( __APPLE__) // Byte ordering on OS X
3338

34-
#include <machine/endian.h>
35-
#include <libkern/OSByteOrder.h>
36-
#define HOST_TO_BIG_ENDIAN_64(x) OSSwapHostToBigInt64(x)
39+
#include <machine/endian.h>
40+
#include <libkern/OSByteOrder.h>
41+
#define HOST_TO_BIG_ENDIAN_64(x) OSSwapHostToBigInt64(x)
3742

3843
#else
3944

40-
#include <endian.h>
41-
#define HOST_TO_BIG_ENDIAN_64(x) htobe64(x)
45+
#include <endian.h>
46+
#define HOST_TO_BIG_ENDIAN_64(x) htobe64(x)
4247

4348
#endif
4449

@@ -231,8 +236,11 @@ get_corr_postgres_type(const TypeRef &type)
231236
case Type::Code::Enum8:
232237
case Type::Code::Enum16:
233238
case Type::Code::String: return TEXTOID;
239+
case Type::Code::LowCardinality:
240+
return get_corr_postgres_type(type->As<LowCardinalityType>()->GetNestedType());
234241
case Type::Code::Date: return DATEOID;
235242
case Type::Code::DateTime: return TIMESTAMPOID;
243+
case Type::Code::DateTime64: return TIMESTAMPOID;
236244
case Type::Code::UUID: return UUIDOID;
237245
case Type::Code::Array:
238246
{
@@ -305,7 +313,13 @@ ch_binary_prepare_insert(void *conn, char *query, ch_binary_insert_state *state)
305313
bool error = false;
306314
clickhouse::ColumnRef col = sample_block[i];
307315

308-
Oid pgtype = get_corr_postgres_type(col->Type());
316+
auto chtype = col->Type();
317+
if (chtype->GetCode() == Type::LowCardinality) {
318+
chtype = col->As<ColumnLowCardinality>()->GetNestedType();
319+
}
320+
321+
Oid pgtype = get_corr_postgres_type(chtype);
322+
309323
vec->push_back(clickhouse::CreateColumnByType(col->Type()->GetName()));
310324
const char *colname = sample_block.GetColumnName(i).c_str();
311325

@@ -459,6 +473,12 @@ column_append(clickhouse::ColumnRef col, Datum val, Oid valtype, bool isnull)
459473
case Type::Code::Enum16:
460474
col->As<ColumnEnum16>()->Append(s);
461475
break;
476+
case Type::Code::LowCardinality:
477+
{
478+
auto item = ItemView{Type::String, std::string_view(s)};
479+
col->As<ColumnLowCardinality>()->Append(item);
480+
break;
481+
}
462482
default:
463483
throw std::runtime_error("unexpected column "
464484
"type for TEXT: " + col->Type()->GetName());
@@ -507,7 +527,7 @@ column_append(clickhouse::ColumnRef col, Datum val, Oid valtype, bool isnull)
507527
{
508528
auto arrcol = col->As<ColumnArray>();
509529

510-
arrcol->AppendOffset(arr->len);
530+
arrcol->OffsetsIncrease(arr->len);
511531
for (size_t i = 0; i < arr->len; i++)
512532
column_append(arrcol->Nested(), arr->datums[i],
513533
arr->item_type, arr->nulls[i]);
@@ -643,11 +663,13 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
643663
{
644664
Datum ret = (Datum) 0;
645665

646-
nested:
666+
nested_col:
667+
auto type_code = col->Type()->GetCode();
668+
647669
*valtype = InvalidOid;
648670
*is_null = false;
649671

650-
switch (col->Type()->GetCode())
672+
switch (type_code)
651673
{
652674
case Type::Code::UInt8:
653675
{
@@ -724,29 +746,29 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
724746
break;
725747
case Type::Code::FixedString:
726748
{
727-
const char *str = col->As<ColumnFixedString>()->At(row).c_str();
728-
ret = CStringGetTextDatum(str);
749+
auto s = std::string(col->As<ColumnFixedString>()->At(row));
750+
ret = CStringGetTextDatum(s.c_str());
729751
*valtype = TEXTOID;
730752
}
731753
break;
732754
case Type::Code::String:
733755
{
734-
const char *str = col->As<ColumnString>()->At(row).c_str();
735-
ret = CStringGetTextDatum(str);
756+
auto s = std::string(col->As<ColumnString>()->At(row));
757+
ret = CStringGetTextDatum(s.c_str());
736758
*valtype = TEXTOID;
737759
}
738760
break;
739761
case Type::Code::Enum8:
740762
{
741-
const char *str = col->As<ColumnEnum8>()->NameAt(row).c_str();
742-
ret = CStringGetTextDatum(str);
763+
auto s = std::string(col->As<ColumnEnum8>()->NameAt(row));
764+
ret = CStringGetTextDatum(s.c_str());
743765
*valtype = TEXTOID;
744766
}
745767
break;
746768
case Type::Code::Enum16:
747769
{
748-
const char *str = col->As<ColumnEnum16>()->NameAt(row).c_str();
749-
ret = CStringGetTextDatum(str);
770+
auto s = std::string(col->As<ColumnEnum16>()->NameAt(row));
771+
ret = CStringGetTextDatum(s.c_str());
750772
*valtype = TEXTOID;
751773
}
752774
break;
@@ -779,6 +801,22 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
779801
}
780802
}
781803
break;
804+
case Type::Code::DateTime64:
805+
{
806+
auto dt_col = col->As<ColumnDateTime64>();
807+
auto val = dt_col->At(row);
808+
809+
*valtype = TIMESTAMPOID;
810+
811+
if (val == 0)
812+
*is_null = true;
813+
else
814+
{
815+
ret = (val / pow(10.0, dt_col->GetPrecision()) -
816+
(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY) * USECS_PER_SEC;
817+
}
818+
}
819+
break;
782820
case Type::Code::UUID:
783821
{
784822
/* we form char[16] from two uint64 numbers, and they should
@@ -805,7 +843,7 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
805843
else
806844
{
807845
col = nullable->Nested();
808-
goto nested;
846+
goto nested_col;
809847
}
810848
}
811849
break;
@@ -871,6 +909,14 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
871909
*valtype = RECORDOID;
872910
}
873911
break;
912+
case Type::Code::LowCardinality:
913+
{
914+
auto item = col->As<ColumnLowCardinality>()->GetItem(row);
915+
auto data = item.AsBinaryData();
916+
ret = PointerGetDatum(cstring_to_text_with_len(data.data(), data.size()));
917+
*valtype = TEXTOID;
918+
}
919+
break;
874920
default:
875921
throw std::runtime_error("clickhouse_fdw: unsupported type");
876922
}

src/clickhouse-cpp/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,7 @@ BUCKAROO_DEPS
274274

275275
# Visual Studio Code
276276
/.vscode/
277+
278+
# Vim
279+
*.swp
280+
*.swo

src/clickhouse-cpp/.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ matrix:
3535
- MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"
3636

3737
- os: osx
38-
osx_image: xcode8.2
38+
osx_image: xcode9.4
3939
compiler: clang
4040

4141
before_install:
4242
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then echo 'deb http://repo.yandex.ru/clickhouse/deb/stable main/' | sudo tee /etc/apt/sources.list.d/clickhouse.list ; fi
4343
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-key adv --keyserver keyserver.ubuntu.com --recv E0C56BD4 ; fi
44-
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -q && sudo apt-get install -q -y clickhouse-server-common ; fi
44+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -q && sudo apt-get install -q -y --allow-unauthenticated clickhouse-server-common ; fi
4545
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo service clickhouse-server start ; fi
4646

4747
# Build steps
4848
script:
4949
- eval "${MATRIX_EVAL}"
5050
- mkdir build
5151
- cd build
52-
- cmake .. && make
52+
- cmake .. -DBUILD_TESTS=ON && make
5353
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ./ut/clickhouse-cpp-ut ; fi
54-
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter='-Client/*' ; fi
54+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi

src/clickhouse-cpp/CMakeLists.txt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
1+
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2)
22

33
INCLUDE (cmake/cpp17.cmake)
44
INCLUDE (cmake/subdirs.cmake)
55

66
OPTION(BUILD_BENCHMARK "Build benchmark" OFF)
77
OPTION(BUILD_TESTS "Build tests" OFF)
8+
OPTION(CLICKHOUSE_CPP_ENABLE_INSTALL "Enable installation of the libraries and headers files" ON)
89

9-
PROJECT (CLICKHOUSE-CLIENT LANGUAGES CXX)
10+
PROJECT (CLICKHOUSE-CLIENT)
1011

1112
USE_CXX17()
1213

14+
IF ("${CMAKE_BUILD_TYPE}" STREQUAL "")
15+
set(CMAKE_BUILD_TYPE "Debug")
16+
ENDIF()
17+
18+
IF (UNIX)
19+
IF (APPLE)
20+
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror")
21+
ELSE ()
22+
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread -Wall -Wextra -Werror")
23+
ENDIF ()
24+
SET (CMAKE_EXE_LINKER_FLAGS, "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
25+
ENDIF ()
26+
1327
INCLUDE_DIRECTORIES(.)
1428
INCLUDE_DIRECTORIES(contrib)
1529

@@ -19,14 +33,14 @@ PROJECT (CLICKHOUSE-CLIENT LANGUAGES CXX)
1933
contrib/lz4
2034
)
2135

22-
IF (BUILD_TESTS)
23-
SUBDIRS(
24-
contrib/gtest
25-
tests/simple
26-
ut
27-
)
28-
ENDIF (BUILD_TESTS)
29-
3036
IF (BUILD_BENCHMARK)
3137
SUBDIRS(bench)
3238
ENDIF (BUILD_BENCHMARK)
39+
40+
IF (BUILD_TESTS)
41+
SUBDIRS(
42+
contrib/gtest
43+
tests/simple
44+
ut
45+
)
46+
ENDIF (BUILD_TESTS)

src/clickhouse-cpp/LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2018-2019 Yandex LLC
1+
Copyright 2018-2020 Yandex LLC
22
Copyright 2017 Pavel Artemkin
33

44
Apache License
@@ -189,7 +189,7 @@ Copyright 2017 Pavel Artemkin
189189
same "printed page" as the copyright notice for easier
190190
identification within third-party archives.
191191

192-
Copyright 2018-2019 Yandex LLC
192+
Copyright 2018-2020 Yandex LLC
193193
Copyright 2017 Pavel Artemkin
194194

195195

src/clickhouse-cpp/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ClickHouse C++ client [![Build Status](https://travis-ci.org/artpaul/clickhouse-cpp.svg?branch=master)](https://travis-ci.org/artpaul/clickhouse-cpp)
1+
ClickHouse C++ client [![Build Status](https://travis-ci.org/ClickHouse/clickhouse-cpp.svg?branch=master)](https://travis-ci.org/ClickHouse/clickhouse-cpp)
22
=====
33

44
C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)
@@ -7,11 +7,12 @@ C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)
77

88
* Array(T)
99
* Date
10-
* DateTime
10+
* DateTime, DateTime64
1111
* Decimal32, Decimal64, Decimal128
1212
* Enum8, Enum16
1313
* FixedString(N)
1414
* Float32, Float64
15+
* IPv4, IPv6
1516
* Nullable(T)
1617
* String
1718
* Tuple
@@ -22,7 +23,7 @@ C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)
2223
```sh
2324
$ mkdir build .
2425
$ cd build
25-
$ cmake ..
26+
$ cmake .. [-DBUILD_TESTS=ON]
2627
$ make
2728
```
2829

0 commit comments

Comments
 (0)