Skip to content

Commit

Permalink
use yandex library for clickhouse-cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ildus committed Oct 19, 2020
1 parent 6455d07 commit 51b9b61
Show file tree
Hide file tree
Showing 67 changed files with 3,742 additions and 434 deletions.
12 changes: 5 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(clickhouse_fdw VERSION 1.2.0 LANGUAGES C CXX)

if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++17")
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++17")
elseif (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1)
set(CMAKE_CXX17_STANDARD_COMPILE_OPTION "-std=c++1z")
set(CMAKE_CXX17_EXTENSION_COMPILE_OPTION "-std=gnu++1z")
endif()
include (clickhouse-cpp/cmake/cpp17.cmake)

USE_CXX17()

set(CLICKHOUSE_CPP_ENABLE_INSTALL OFF CACHE BOOL "disable clickhouse-cpp installation" FORCE)
add_subdirectory (clickhouse-cpp)

target_compile_options(clickhouse-cpp-lib-static PRIVATE -fPIC)
target_compile_options(clickhouse-cpp-lib PRIVATE -fPIC)

Expand Down
82 changes: 64 additions & 18 deletions src/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#include <cassert>
#include <stdexcept>

#include "clickhouse/columns/lowcardinality.h"
#include "clickhouse/columns/nullable.h"
#include "clickhouse/columns/factory.h"
#include <clickhouse/client.h>
#include <clickhouse/types/types.h>

#if __cplusplus > 199711L
#define register // Deprecated in C++11.
#endif // #if __cplusplus > 199711L

extern "C" {

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

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

#include <machine/endian.h>
#include <libkern/OSByteOrder.h>
#define HOST_TO_BIG_ENDIAN_64(x) OSSwapHostToBigInt64(x)
#include <machine/endian.h>
#include <libkern/OSByteOrder.h>
#define HOST_TO_BIG_ENDIAN_64(x) OSSwapHostToBigInt64(x)

#else

#include <endian.h>
#define HOST_TO_BIG_ENDIAN_64(x) htobe64(x)
#include <endian.h>
#define HOST_TO_BIG_ENDIAN_64(x) htobe64(x)

#endif

Expand Down Expand Up @@ -231,8 +236,11 @@ get_corr_postgres_type(const TypeRef &type)
case Type::Code::Enum8:
case Type::Code::Enum16:
case Type::Code::String: return TEXTOID;
case Type::Code::LowCardinality:
return get_corr_postgres_type(type->As<LowCardinalityType>()->GetNestedType());
case Type::Code::Date: return DATEOID;
case Type::Code::DateTime: return TIMESTAMPOID;
case Type::Code::DateTime64: return TIMESTAMPOID;
case Type::Code::UUID: return UUIDOID;
case Type::Code::Array:
{
Expand Down Expand Up @@ -305,7 +313,13 @@ ch_binary_prepare_insert(void *conn, char *query, ch_binary_insert_state *state)
bool error = false;
clickhouse::ColumnRef col = sample_block[i];

Oid pgtype = get_corr_postgres_type(col->Type());
auto chtype = col->Type();
if (chtype->GetCode() == Type::LowCardinality) {
chtype = col->As<ColumnLowCardinality>()->GetNestedType();
}

Oid pgtype = get_corr_postgres_type(chtype);

vec->push_back(clickhouse::CreateColumnByType(col->Type()->GetName()));
const char *colname = sample_block.GetColumnName(i).c_str();

Expand Down Expand Up @@ -459,6 +473,12 @@ column_append(clickhouse::ColumnRef col, Datum val, Oid valtype, bool isnull)
case Type::Code::Enum16:
col->As<ColumnEnum16>()->Append(s);
break;
case Type::Code::LowCardinality:
{
auto item = ItemView{Type::String, std::string_view(s)};
col->As<ColumnLowCardinality>()->Append(item);
break;
}
default:
throw std::runtime_error("unexpected column "
"type for TEXT: " + col->Type()->GetName());
Expand Down Expand Up @@ -507,7 +527,7 @@ column_append(clickhouse::ColumnRef col, Datum val, Oid valtype, bool isnull)
{
auto arrcol = col->As<ColumnArray>();

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

nested:
nested_col:
auto type_code = col->Type()->GetCode();

*valtype = InvalidOid;
*is_null = false;

switch (col->Type()->GetCode())
switch (type_code)
{
case Type::Code::UInt8:
{
Expand Down Expand Up @@ -724,29 +746,29 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
break;
case Type::Code::FixedString:
{
const char *str = col->As<ColumnFixedString>()->At(row).c_str();
ret = CStringGetTextDatum(str);
auto s = std::string(col->As<ColumnFixedString>()->At(row));
ret = CStringGetTextDatum(s.c_str());
*valtype = TEXTOID;
}
break;
case Type::Code::String:
{
const char *str = col->As<ColumnString>()->At(row).c_str();
ret = CStringGetTextDatum(str);
auto s = std::string(col->As<ColumnString>()->At(row));
ret = CStringGetTextDatum(s.c_str());
*valtype = TEXTOID;
}
break;
case Type::Code::Enum8:
{
const char *str = col->As<ColumnEnum8>()->NameAt(row).c_str();
ret = CStringGetTextDatum(str);
auto s = std::string(col->As<ColumnEnum8>()->NameAt(row));
ret = CStringGetTextDatum(s.c_str());
*valtype = TEXTOID;
}
break;
case Type::Code::Enum16:
{
const char *str = col->As<ColumnEnum16>()->NameAt(row).c_str();
ret = CStringGetTextDatum(str);
auto s = std::string(col->As<ColumnEnum16>()->NameAt(row));
ret = CStringGetTextDatum(s.c_str());
*valtype = TEXTOID;
}
break;
Expand Down Expand Up @@ -779,6 +801,22 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
}
}
break;
case Type::Code::DateTime64:
{
auto dt_col = col->As<ColumnDateTime64>();
auto val = dt_col->At(row);

*valtype = TIMESTAMPOID;

if (val == 0)
*is_null = true;
else
{
ret = (val / pow(10.0, dt_col->GetPrecision()) -
(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY) * USECS_PER_SEC;
}
}
break;
case Type::Code::UUID:
{
/* we form char[16] from two uint64 numbers, and they should
Expand All @@ -805,7 +843,7 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
else
{
col = nullable->Nested();
goto nested;
goto nested_col;
}
}
break;
Expand Down Expand Up @@ -871,6 +909,14 @@ make_datum(clickhouse::ColumnRef col, size_t row, Oid *valtype, bool *is_null)
*valtype = RECORDOID;
}
break;
case Type::Code::LowCardinality:
{
auto item = col->As<ColumnLowCardinality>()->GetItem(row);
auto data = item.AsBinaryData();
ret = PointerGetDatum(cstring_to_text_with_len(data.data(), data.size()));
*valtype = TEXTOID;
}
break;
default:
throw std::runtime_error("clickhouse_fdw: unsupported type");
}
Expand Down
4 changes: 4 additions & 0 deletions src/clickhouse-cpp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,7 @@ BUCKAROO_DEPS

# Visual Studio Code
/.vscode/

# Vim
*.swp
*.swo
8 changes: 4 additions & 4 deletions src/clickhouse-cpp/.travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ matrix:
- MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"

- os: osx
osx_image: xcode8.2
osx_image: xcode9.4
compiler: clang

before_install:
- 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
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-key adv --keyserver keyserver.ubuntu.com --recv E0C56BD4 ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -q && sudo apt-get install -q -y clickhouse-server-common ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -q && sudo apt-get install -q -y --allow-unauthenticated clickhouse-server-common ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo service clickhouse-server start ; fi

# Build steps
script:
- eval "${MATRIX_EVAL}"
- mkdir build
- cd build
- cmake .. && make
- 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/*' ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter=-"Client/*:*Performance*" ; fi
34 changes: 24 additions & 10 deletions src/clickhouse-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2)

INCLUDE (cmake/cpp17.cmake)
INCLUDE (cmake/subdirs.cmake)

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

PROJECT (CLICKHOUSE-CLIENT LANGUAGES CXX)
PROJECT (CLICKHOUSE-CLIENT)

USE_CXX17()

IF ("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE "Debug")
ENDIF()

IF (UNIX)
IF (APPLE)
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror")
ELSE ()
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread -Wall -Wextra -Werror")
ENDIF ()
SET (CMAKE_EXE_LINKER_FLAGS, "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
ENDIF ()

INCLUDE_DIRECTORIES(.)
INCLUDE_DIRECTORIES(contrib)

Expand All @@ -19,14 +33,14 @@ PROJECT (CLICKHOUSE-CLIENT LANGUAGES CXX)
contrib/lz4
)

IF (BUILD_TESTS)
SUBDIRS(
contrib/gtest
tests/simple
ut
)
ENDIF (BUILD_TESTS)

IF (BUILD_BENCHMARK)
SUBDIRS(bench)
ENDIF (BUILD_BENCHMARK)

IF (BUILD_TESTS)
SUBDIRS(
contrib/gtest
tests/simple
ut
)
ENDIF (BUILD_TESTS)
4 changes: 2 additions & 2 deletions src/clickhouse-cpp/LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2018-2019 Yandex LLC
Copyright 2018-2020 Yandex LLC
Copyright 2017 Pavel Artemkin

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

Copyright 2018-2019 Yandex LLC
Copyright 2018-2020 Yandex LLC
Copyright 2017 Pavel Artemkin


Expand Down
7 changes: 4 additions & 3 deletions src/clickhouse-cpp/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ClickHouse C++ client [![Build Status](https://travis-ci.org/artpaul/clickhouse-cpp.svg?branch=master)](https://travis-ci.org/artpaul/clickhouse-cpp)
ClickHouse C++ client [![Build Status](https://travis-ci.org/ClickHouse/clickhouse-cpp.svg?branch=master)](https://travis-ci.org/ClickHouse/clickhouse-cpp)
=====

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

* Array(T)
* Date
* DateTime
* DateTime, DateTime64
* Decimal32, Decimal64, Decimal128
* Enum8, Enum16
* FixedString(N)
* Float32, Float64
* IPv4, IPv6
* Nullable(T)
* String
* Tuple
Expand All @@ -22,7 +23,7 @@ C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)
```sh
$ mkdir build .
$ cd build
$ cmake ..
$ cmake .. [-DBUILD_TESTS=ON]
$ make
```

Expand Down
Loading

0 comments on commit 51b9b61

Please sign in to comment.