Skip to content

Commit

Permalink
Fixed ColumnIPv4i::Append()
Browse files Browse the repository at this point in the history
Added tests, simplified implementation + few extra minor touches
  • Loading branch information
Enmk committed Feb 2, 2022
1 parent 1ab8769 commit b56382c
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 47 deletions.
20 changes: 12 additions & 8 deletions clickhouse/columns/ip4.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

#include "ip4.h"

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

#if defined(_win_)
using in_addr_t = unsigned long;
#define inet_aton(string, addr) InetPtonW()
#endif

namespace clickhouse {
Expand All @@ -25,29 +25,33 @@ ColumnIPv4::ColumnIPv4(ColumnRef data)
}

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

void ColumnIPv4::Append(uint32_t ip) {
data_->Append(htonl(ip));
}

void ColumnIPv4::Append(in_addr ip)
{
data_->Append(htonl(ip.s_addr));
}

void ColumnIPv4::Clear() {
data_->Clear();
}

in_addr ColumnIPv4::At(size_t n) const {
struct in_addr addr;
in_addr addr;
addr.s_addr = ntohl(data_->At(n));
return addr;
}

in_addr ColumnIPv4::operator [] (size_t n) const {
struct in_addr addr;
in_addr addr;
addr.s_addr = ntohl(data_->operator[](n));
return addr;
}
Expand Down
6 changes: 5 additions & 1 deletion clickhouse/columns/ip4.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "numeric.h"
#include "../base/socket.h"

struct in_addr;

namespace clickhouse {

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

///
void Append(in_addr ip);

/// Returns element at given row number.
in_addr At(size_t n) const;

Expand Down
9 changes: 7 additions & 2 deletions clickhouse/columns/ip6.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "ip6.h"
#include "../base/socket.h" // for IPv6 platform-specific stuff

#include <stdexcept>

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

void ColumnIPv6::Append(const in6_addr* addr) {
data_->Append(std::string((const char*)addr->s6_addr, 16));
data_->Append(std::string_view((const char*)addr->s6_addr, 16));
}

void ColumnIPv6::Append(const in6_addr& addr) {
Append(&addr);
}

void ColumnIPv6::Clear() {
Expand Down
5 changes: 4 additions & 1 deletion clickhouse/columns/ip6.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "string.h"
#include "../base/socket.h"
#include <memory>

struct in6_addr;

namespace clickhouse {

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

void Append(const in6_addr* addr);
void Append(const in6_addr& addr);

/// Returns element at given row number.
in6_addr At(size_t n) const;
Expand Down
33 changes: 17 additions & 16 deletions clickhouse/columns/nothing.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "column.h"
#include "../base/input.h"

#include <stdexcept>
#include <utility>
Expand All @@ -13,13 +14,13 @@ namespace clickhouse {
*/
class ColumnNothing : public Column {
public:
ColumnNothing()
ColumnNothing()
: Column(Type::CreateNothing())
, size_(0)
{
}

explicit ColumnNothing(size_t n)
explicit ColumnNothing(size_t n)
: Column(Type::CreateNothing())
, size_(n)
{
Expand All @@ -29,37 +30,37 @@ class ColumnNothing : public Column {
void Append(std::unique_ptr<void*>) { ++size_; }

/// Returns element at given row number.
std::nullptr_t At(size_t) const { return nullptr; };
std::nullptr_t At(size_t) const { return nullptr; };

/// Returns element at given row number.
std::nullptr_t operator [] (size_t) const { return nullptr; };
std::nullptr_t operator [] (size_t) const { return nullptr; };

/// Makes slice of the current column.
ColumnRef Slice(size_t, size_t len) const override {
return std::make_shared<ColumnNothing>(len);
}
return std::make_shared<ColumnNothing>(len);
}

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

public:
/// Appends content of given column to the end of current one.
void Append(ColumnRef column) override {
if (auto col = column->As<ColumnNothing>()) {
size_ += col->Size();
}
}
if (auto col = column->As<ColumnNothing>()) {
size_ += col->Size();
}
}

/// Loads column data from input stream.
bool Load(InputStream* input, size_t rows) override {
input->Skip(rows);
size_ += rows;
return true;
}
input->Skip(rows);
size_ += rows;
return true;
}

/// Saves column data to output stream.
void Save(OutputStream*) override {
throw std::runtime_error("method Save is not supported for Nothing column");
}
}

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

private:
size_t size_;
size_t size_;
};

}
12 changes: 12 additions & 0 deletions clickhouse/columns/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ ColumnString::ColumnString(const std::vector<std::string> & data)
}
}

ColumnString::ColumnString(const std::vector<std::string_view> & data)
: Column(Type::CreateString())
{
items_.reserve(data.size());
blocks_.emplace_back(ComputeTotalSize(data));

for (const auto & s : data)
{
AppendUnsafe(s);
}
}

ColumnString::~ColumnString()
{}

Expand Down
2 changes: 2 additions & 0 deletions clickhouse/columns/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class ColumnString : public Column {
~ColumnString();

explicit ColumnString(const std::vector<std::string> & data);
explicit ColumnString(const std::vector<std::string_view> & data);

ColumnString& operator=(const ColumnString&) = delete;
ColumnString(const ColumnString&) = delete;

Expand Down
1 change: 1 addition & 0 deletions tests/simple/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <clickhouse/client.h>
#include <clickhouse/error_codes.h>
#include <clickhouse/types/type_parser.h>
#include <clickhouse/base/socket.h>

#include <stdexcept>
#include <iostream>
Expand Down
Loading

0 comments on commit b56382c

Please sign in to comment.