Skip to content

Commit b56382c

Browse files
committed
Fixed ColumnIPv4i::Append()
Added tests, simplified implementation + few extra minor touches
1 parent 1ab8769 commit b56382c

File tree

10 files changed

+164
-47
lines changed

10 files changed

+164
-47
lines changed

clickhouse/columns/ip4.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
#include "ip4.h"
32

3+
#include "../base/socket.h" // for platform-specific IPv4-related functions
44
#include <stdexcept>
55

66
#if defined(_win_)
7-
using in_addr_t = unsigned long;
7+
#define inet_aton(string, addr) InetPtonW()
88
#endif
99

1010
namespace clickhouse {
@@ -25,29 +25,33 @@ ColumnIPv4::ColumnIPv4(ColumnRef data)
2525
}
2626

2727
void ColumnIPv4::Append(const std::string& str) {
28-
in_addr_t addr = inet_addr(str.c_str());
29-
if (addr == INADDR_NONE) {
28+
uint32_t address;
29+
if (inet_pton(AF_INET, str.c_str(), &address) != 1)
3030
throw std::runtime_error("invalid IPv4 format, ip: " + str);
31-
}
32-
data_->Append(htonl(addr));
31+
data_->Append(htonl(address));
3332
}
3433

3534
void ColumnIPv4::Append(uint32_t ip) {
3635
data_->Append(htonl(ip));
3736
}
3837

38+
void ColumnIPv4::Append(in_addr ip)
39+
{
40+
data_->Append(htonl(ip.s_addr));
41+
}
42+
3943
void ColumnIPv4::Clear() {
4044
data_->Clear();
4145
}
4246

4347
in_addr ColumnIPv4::At(size_t n) const {
44-
struct in_addr addr;
48+
in_addr addr;
4549
addr.s_addr = ntohl(data_->At(n));
4650
return addr;
4751
}
4852

4953
in_addr ColumnIPv4::operator [] (size_t n) const {
50-
struct in_addr addr;
54+
in_addr addr;
5155
addr.s_addr = ntohl(data_->operator[](n));
5256
return addr;
5357
}

clickhouse/columns/ip4.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

33
#include "numeric.h"
4-
#include "../base/socket.h"
4+
5+
struct in_addr;
56

67
namespace clickhouse {
78

@@ -16,6 +17,9 @@ class ColumnIPv4 : public Column {
1617
/// @params ip numeric value with host byte order.
1718
void Append(uint32_t ip);
1819

20+
///
21+
void Append(in_addr ip);
22+
1923
/// Returns element at given row number.
2024
in_addr At(size_t n) const;
2125

clickhouse/columns/ip6.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include "ip6.h"
3+
#include "../base/socket.h" // for IPv6 platform-specific stuff
34

45
#include <stdexcept>
56

@@ -27,11 +28,15 @@ void ColumnIPv6::Append(const std::string& ip) {
2728
if (inet_pton(AF_INET6, ip.c_str(), buf) != 1) {
2829
throw std::runtime_error("invalid IPv6 format, ip: " + ip);
2930
}
30-
data_->Append(std::string((const char*)buf, 16));
31+
data_->Append(std::string_view((const char*)buf, 16));
3132
}
3233

3334
void ColumnIPv6::Append(const in6_addr* addr) {
34-
data_->Append(std::string((const char*)addr->s6_addr, 16));
35+
data_->Append(std::string_view((const char*)addr->s6_addr, 16));
36+
}
37+
38+
void ColumnIPv6::Append(const in6_addr& addr) {
39+
Append(&addr);
3540
}
3641

3742
void ColumnIPv6::Clear() {

clickhouse/columns/ip6.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

33
#include "string.h"
4-
#include "../base/socket.h"
4+
#include <memory>
5+
6+
struct in6_addr;
57

68
namespace clickhouse {
79

@@ -14,6 +16,7 @@ class ColumnIPv6 : public Column{
1416
void Append(const std::string& str);
1517

1618
void Append(const in6_addr* addr);
19+
void Append(const in6_addr& addr);
1720

1821
/// Returns element at given row number.
1922
in6_addr At(size_t n) const;

clickhouse/columns/nothing.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#pragma once
33

44
#include "column.h"
5+
#include "../base/input.h"
56

67
#include <stdexcept>
78
#include <utility>
@@ -13,13 +14,13 @@ namespace clickhouse {
1314
*/
1415
class ColumnNothing : public Column {
1516
public:
16-
ColumnNothing()
17+
ColumnNothing()
1718
: Column(Type::CreateNothing())
1819
, size_(0)
1920
{
2021
}
2122

22-
explicit ColumnNothing(size_t n)
23+
explicit ColumnNothing(size_t n)
2324
: Column(Type::CreateNothing())
2425
, size_(n)
2526
{
@@ -29,37 +30,37 @@ class ColumnNothing : public Column {
2930
void Append(std::unique_ptr<void*>) { ++size_; }
3031

3132
/// Returns element at given row number.
32-
std::nullptr_t At(size_t) const { return nullptr; };
33+
std::nullptr_t At(size_t) const { return nullptr; };
3334

3435
/// Returns element at given row number.
35-
std::nullptr_t operator [] (size_t) const { return nullptr; };
36+
std::nullptr_t operator [] (size_t) const { return nullptr; };
3637

3738
/// Makes slice of the current column.
3839
ColumnRef Slice(size_t, size_t len) const override {
39-
return std::make_shared<ColumnNothing>(len);
40-
}
40+
return std::make_shared<ColumnNothing>(len);
41+
}
4142

4243
ItemView GetItem(size_t /*index*/) const override { return ItemView{}; }
4344

4445
public:
4546
/// Appends content of given column to the end of current one.
4647
void Append(ColumnRef column) override {
47-
if (auto col = column->As<ColumnNothing>()) {
48-
size_ += col->Size();
49-
}
50-
}
48+
if (auto col = column->As<ColumnNothing>()) {
49+
size_ += col->Size();
50+
}
51+
}
5152

5253
/// Loads column data from input stream.
5354
bool Load(InputStream* input, size_t rows) override {
54-
input->Skip(rows);
55-
size_ += rows;
56-
return true;
57-
}
55+
input->Skip(rows);
56+
size_ += rows;
57+
return true;
58+
}
5859

5960
/// Saves column data to output stream.
6061
void Save(OutputStream*) override {
6162
throw std::runtime_error("method Save is not supported for Nothing column");
62-
}
63+
}
6364

6465
/// Clear column data .
6566
void Clear() override { size_ = 0; }
@@ -73,7 +74,7 @@ class ColumnNothing : public Column {
7374
}
7475

7576
private:
76-
size_t size_;
77+
size_t size_;
7778
};
7879

7980
}

clickhouse/columns/string.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ ColumnString::ColumnString(const std::vector<std::string> & data)
175175
}
176176
}
177177

178+
ColumnString::ColumnString(const std::vector<std::string_view> & data)
179+
: Column(Type::CreateString())
180+
{
181+
items_.reserve(data.size());
182+
blocks_.emplace_back(ComputeTotalSize(data));
183+
184+
for (const auto & s : data)
185+
{
186+
AppendUnsafe(s);
187+
}
188+
}
189+
178190
ColumnString::~ColumnString()
179191
{}
180192

clickhouse/columns/string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class ColumnString : public Column {
7878
~ColumnString();
7979

8080
explicit ColumnString(const std::vector<std::string> & data);
81+
explicit ColumnString(const std::vector<std::string_view> & data);
82+
8183
ColumnString& operator=(const ColumnString&) = delete;
8284
ColumnString(const ColumnString&) = delete;
8385

tests/simple/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <clickhouse/client.h>
22
#include <clickhouse/error_codes.h>
33
#include <clickhouse/types/type_parser.h>
4+
#include <clickhouse/base/socket.h>
45

56
#include <stdexcept>
67
#include <iostream>

0 commit comments

Comments
 (0)