Skip to content

Commit a510666

Browse files
authored
Merge pull request ClickHouse#18 from ClickHouse/merge-upstream
Merge some changes from artpaul/clickhouse-cpp
2 parents 7d694ad + bf46e5a commit a510666

36 files changed

+828
-145
lines changed

Diff for: .travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ matrix:
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
5454
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./ut/clickhouse-cpp-ut --gtest_filter='-Client/*' ; fi

Diff for: CMakeLists.txt

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ INCLUDE (cmake/cpp17.cmake)
44
INCLUDE (cmake/subdirs.cmake)
55

66
OPTION(BUILD_BENCHMARK "Build benchmark" OFF)
7+
OPTION(BUILD_TESTS "Build tests" OFF)
78

89
PROJECT (CLICKHOUSE-CLIENT)
910

1011
USE_CXX17()
1112

13+
IF ("${CMAKE_BUILD_TYPE}" STREQUAL "")
14+
set(CMAKE_BUILD_TYPE "Debug")
15+
ENDIF()
16+
1217
IF (UNIX)
1318
IF (APPLE)
1419
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror")
@@ -24,12 +29,17 @@ PROJECT (CLICKHOUSE-CLIENT)
2429
SUBDIRS (
2530
clickhouse
2631
contrib/cityhash
27-
contrib/gtest
2832
contrib/lz4
29-
tests/simple
30-
ut
3133
)
3234

3335
IF (BUILD_BENCHMARK)
3436
SUBDIRS(bench)
3537
ENDIF (BUILD_BENCHMARK)
38+
39+
IF (BUILD_TESTS)
40+
SUBDIRS(
41+
contrib/gtest
42+
tests/simple
43+
ut
44+
)
45+
ENDIF (BUILD_TESTS)

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ C++ client for [Yandex ClickHouse](https://clickhouse.yandex/)
1212
* Enum8, Enum16
1313
* FixedString(N)
1414
* Float32, Float64
15+
* IPv4, IPv6
1516
* Nullable(T)
1617
* String
1718
* Tuple
1819
* UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64
19-
* IPv4, IPv6
2020

2121
## Building
2222

2323
```sh
2424
$ mkdir build .
2525
$ cd build
26-
$ cmake ..
26+
$ cmake .. [-DBUILD_TESTS=ON]
2727
$ make
2828
```
2929

Diff for: clickhouse/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ SET ( clickhouse-cpp-lib-src
1111
columns/decimal.cpp
1212
columns/enum.cpp
1313
columns/factory.cpp
14+
columns/ip4.cpp
15+
columns/ip6.cpp
1416
columns/nullable.cpp
1517
columns/numeric.cpp
1618
columns/string.cpp
@@ -85,4 +87,3 @@ INSTALL(FILES columns/uuid.h DESTINATION include/clickhouse/columns/)
8587
# types
8688
INSTALL(FILES types/type_parser.h DESTINATION include/clickhouse/types/)
8789
INSTALL(FILES types/types.h DESTINATION include/clickhouse/types/)
88-

Diff for: clickhouse/base/socket.cpp

+41-22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# include <errno.h>
1212
# include <fcntl.h>
1313
# include <netdb.h>
14+
# include <netinet/tcp.h>
1415
# include <signal.h>
1516
# include <unistd.h>
1617
# include <netinet/tcp.h>
@@ -57,7 +58,18 @@ void SetNonBlock(SOCKET fd, bool value) {
5758
errno, std::system_category(), "fail to set nonblocking mode");
5859
}
5960
#elif defined(_win_)
60-
SetNonBlockSocket(fd, 1);
61+
unsigned long inbuf = value;
62+
unsigned long outbuf = 0;
63+
DWORD written = 0;
64+
65+
if (!inbuf) {
66+
WSAEventSelect(fd, nullptr, 0);
67+
}
68+
69+
if (WSAIoctl(fd, FIONBIO, &inbuf, sizeof(inbuf), &outbuf, sizeof(outbuf), &written, 0, 0) == SOCKET_ERROR) {
70+
throw std::system_error(
71+
errno, std::system_category(), "fail to set nonblocking mode");
72+
}
6173
#endif
6274
}
6375

@@ -112,7 +124,7 @@ SocketHolder::SocketHolder(SOCKET s)
112124
{
113125
}
114126

115-
SocketHolder::SocketHolder(SocketHolder&& other)
127+
SocketHolder::SocketHolder(SocketHolder&& other) noexcept
116128
: handle_(other.handle_)
117129
{
118130
other.handle_ = -1;
@@ -137,6 +149,26 @@ bool SocketHolder::Closed() const noexcept {
137149
return handle_ == -1;
138150
}
139151

152+
void SocketHolder::SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept {
153+
int val = 1;
154+
155+
#if defined(_unix_)
156+
setsockopt(handle_, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
157+
# if defined(_linux_)
158+
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
159+
# elif defined(_darwin_)
160+
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPALIVE, &idle, sizeof(idle));
161+
# else
162+
# error "platform is not supported"
163+
# endif
164+
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl));
165+
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
166+
#else
167+
setsockopt(handle_, SOL_SOCKET, SO_KEEPALIVE, (const char*)&val, sizeof(val));
168+
std::ignore = idle = intvl = cnt;
169+
#endif
170+
}
171+
140172
SocketHolder& SocketHolder::operator = (SocketHolder&& other) noexcept {
141173
if (this != &other) {
142174
Close();
@@ -152,23 +184,6 @@ SocketHolder::operator SOCKET () const noexcept {
152184
return handle_;
153185
}
154186

155-
void SocketHolder::SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept {
156-
int val = 1;
157-
setsockopt(handle_, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof val);
158-
159-
#if defined _darwin_
160-
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPALIVE, &idle, sizeof idle);
161-
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof intvl);
162-
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof cnt);
163-
#elif defined(_linux_)
164-
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof idle);
165-
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof intvl);
166-
setsockopt(handle_, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof cnt);
167-
#else
168-
std::ignore = idle = intvl = cnt;
169-
#endif
170-
}
171-
172187

173188
SocketInput::SocketInput(SOCKET s)
174189
: s_(s)
@@ -210,7 +225,7 @@ void SocketOutput::DoWrite(const void* data, size_t len) {
210225
static const int flags = 0;
211226
#endif
212227

213-
if (::send(s_, (const char*)data, len, flags) != (int)len) {
228+
if (::send(s_, (const char*)data, (int)len, flags) != (int)len) {
214229
throw std::system_error(
215230
errno, std::system_category(), "fail to send data"
216231
);
@@ -240,6 +255,7 @@ NetrworkInitializer::NetrworkInitializer() {
240255

241256

242257
SOCKET SocketConnect(const NetworkAddress& addr) {
258+
int last_err = 0;
243259
for (auto res = addr.Info(); res != nullptr; res = res->ai_next) {
244260
SOCKET s(socket(res->ai_family, res->ai_socktype, res->ai_protocol));
245261

@@ -256,7 +272,7 @@ SOCKET SocketConnect(const NetworkAddress& addr) {
256272
fd.fd = s;
257273
fd.events = POLLOUT;
258274
fd.revents = 0;
259-
int rval = Poll(&fd, 1, 5000);
275+
ssize_t rval = Poll(&fd, 1, 5000);
260276

261277
if (rval == -1) {
262278
throw std::system_error(errno, std::system_category(), "fail to connect");
@@ -269,14 +285,17 @@ SOCKET SocketConnect(const NetworkAddress& addr) {
269285
SetNonBlock(s, false);
270286
return s;
271287
}
288+
last_err = err;
272289
}
273290
}
274291
} else {
275292
SetNonBlock(s, false);
276293
return s;
277294
}
278295
}
279-
296+
if (last_err > 0) {
297+
throw std::system_error(last_err, std::system_category(), "fail to connect");
298+
}
280299
throw std::system_error(
281300
errno, std::system_category(), "fail to connect"
282301
);

Diff for: clickhouse/base/socket.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# include <winsock2.h>
1414
# include <ws2tcpip.h>
1515
#else
16+
# include <arpa/inet.h>
1617
# include <sys/types.h>
1718
# include <sys/socket.h>
1819
# include <poll.h>
@@ -46,20 +47,25 @@ class SocketHolder {
4647
public:
4748
SocketHolder();
4849
SocketHolder(SOCKET s);
49-
SocketHolder(SocketHolder&& other);
50+
SocketHolder(SocketHolder&& other) noexcept;
5051

5152
~SocketHolder();
5253

5354
void Close() noexcept;
5455

5556
bool Closed() const noexcept;
5657

58+
/// @params idle the time (in seconds) the connection needs to remain
59+
/// idle before TCP starts sending keepalive probes.
60+
/// @params intvl the time (in seconds) between individual keepalive probes.
61+
/// @params cnt the maximum number of keepalive probes TCP should send
62+
/// before dropping the connection.
63+
void SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept;
64+
5765
SocketHolder& operator = (SocketHolder&& other) noexcept;
5866

5967
operator SOCKET () const noexcept;
6068

61-
void SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept;
62-
6369
private:
6470
SocketHolder(const SocketHolder&) = delete;
6571
SocketHolder& operator = (const SocketHolder&) = delete;

Diff for: clickhouse/client.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ void Client::Impl::ResetConnection() {
276276
throw std::system_error(errno, std::system_category());
277277
}
278278

279-
if(options_.tcp_keepalive){
280-
s.SetTcpKeepAlive(options_.tcp_keepalive_idle,
281-
options_.tcp_keepalive_intvl,
279+
if (options_.tcp_keepalive) {
280+
s.SetTcpKeepAlive(options_.tcp_keepalive_idle.count(),
281+
options_.tcp_keepalive_intvl.count(),
282282
options_.tcp_keepalive_cnt);
283283
}
284284

Diff for: clickhouse/client.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "columns/date.h"
88
#include "columns/decimal.h"
99
#include "columns/enum.h"
10+
#include "columns/ip4.h"
11+
#include "columns/ip6.h"
1012
#include "columns/nullable.h"
1113
#include "columns/numeric.h"
1214
#include "columns/string.h"
@@ -63,8 +65,8 @@ struct ClientOptions {
6365

6466
/// TCP Keep alive options
6567
DECLARE_FIELD(tcp_keepalive, bool, TcpKeepAlive, false);
66-
DECLARE_FIELD(tcp_keepalive_idle, int, SetTcpKeepAliveIdle, 60);
67-
DECLARE_FIELD(tcp_keepalive_intvl, int, SetTcpKeepAliveInterval, 5);
68+
DECLARE_FIELD(tcp_keepalive_idle, std::chrono::seconds, SetTcpKeepAliveIdle, std::chrono::seconds(60));
69+
DECLARE_FIELD(tcp_keepalive_intvl, std::chrono::seconds, SetTcpKeepAliveInterval, std::chrono::seconds(5));
6870
DECLARE_FIELD(tcp_keepalive_cnt, int, SetTcpKeepAliveCount, 3);
6971

7072
#undef DECLARE_FIELD

Diff for: clickhouse/columns/array.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "array.h"
2-
32
#include <stdexcept>
43

54
namespace clickhouse {
@@ -31,6 +30,18 @@ ColumnRef ColumnArray::GetAsColumn(size_t n) const {
3130
return data_->Slice(GetOffset(n), GetSize(n));
3231
}
3332

33+
ColumnRef ColumnArray::Slice(size_t begin, size_t size) {
34+
auto result = std::make_shared<ColumnArray>(GetAsColumn(begin));
35+
result->OffsetsIncrease(1);
36+
37+
for (size_t i = 1; i < size; i++)
38+
{
39+
result->Append(std::make_shared<ColumnArray>(GetAsColumn(begin + i)));
40+
}
41+
42+
return result;
43+
}
44+
3445
void ColumnArray::Append(ColumnRef column) {
3546
if (auto col = column->As<ColumnArray>()) {
3647
if (!col->data_->Type()->IsEqual(data_->Type())) {
@@ -67,6 +78,10 @@ size_t ColumnArray::Size() const {
6778
return offsets_->Size();
6879
}
6980

81+
void ColumnArray::OffsetsIncrease(size_t n) {
82+
offsets_->Append(n);
83+
}
84+
7085
size_t ColumnArray::GetOffset(size_t n) const {
7186
return (n == 0) ? 0 : (*offsets_)[n - 1];
7287
}

Diff for: clickhouse/columns/array.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ class ColumnArray : public Column {
2828

2929
/// Saves column data to output stream.
3030
void Save(CodedOutputStream* output) override;
31-
31+
3232
/// Clear column data .
3333
void Clear() override;
34-
34+
3535
/// Returns count of rows in the column.
3636
size_t Size() const override;
3737

3838
/// Makes slice of the current column.
39-
ColumnRef Slice(size_t, size_t) override { return ColumnRef(); }
39+
ColumnRef Slice(size_t, size_t) override;
40+
41+
void OffsetsIncrease(size_t);
4042

4143
private:
4244
size_t GetOffset(size_t n) const;

Diff for: clickhouse/columns/date.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ void ColumnDate::Clear() {
1818
}
1919

2020
std::time_t ColumnDate::At(size_t n) const {
21-
/// TODO: This code is fundamentally wrong.
22-
return data_->At(n) * std::time_t(86400);
21+
return static_cast<std::time_t>(data_->At(n)) * 86400;
2322
}
2423

2524
void ColumnDate::Append(ColumnRef column) {
@@ -95,5 +94,4 @@ ColumnRef ColumnDateTime::Slice(size_t begin, size_t len) {
9594
return result;
9695
}
9796

98-
9997
}

0 commit comments

Comments
 (0)