Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ColumnIPv4::Append() #148

Merged
merged 2 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions clickhouse/columns/ip4.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@

#include "ip4.h"

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

#if defined(_win_)
using in_addr_t = unsigned long;
#endif

namespace clickhouse {

ColumnIPv4::ColumnIPv4()
Expand All @@ -25,29 +21,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
1 change: 1 addition & 0 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 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
103 changes: 99 additions & 4 deletions ut/columns_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <clickhouse/columns/numeric.h>
#include <clickhouse/columns/string.h>
#include <clickhouse/columns/uuid.h>
#include <clickhouse/columns/ip4.h>
#include <clickhouse/columns/ip6.h>
#include <clickhouse/base/input.h>
#include <clickhouse/base/output.h>

Expand All @@ -16,6 +18,37 @@

#include <string_view>

#include <clickhouse/base/socket.h> // for ipv4-ipv6 platform-specific stuff

bool operator==(const in_addr & left, const in_addr & right)
{
return memcmp(&left, &right, sizeof(left)) == 0;
}

bool operator==(const in6_addr & left, const in6_addr & right)
{
return memcmp(&left, &right, sizeof(left)) == 0;
}

in_addr make_IPv4(uint32_t ip)
{
static_assert(sizeof(in_addr) == sizeof(ip));
in_addr result;
memcpy(&result, &ip, sizeof(ip));

return result;
}

std::ostream& operator<<(std::ostream& ostr, const in6_addr & addr)
{
char buf[INET6_ADDRSTRLEN];
const char* ip_str = inet_ntop(AF_INET6, &addr, buf, INET6_ADDRSTRLEN);

if (!ip_str)
return ostr << "<!INVALID IPv6 VALUE!>";

return ostr << ip_str;
}

namespace {

Expand Down Expand Up @@ -216,8 +249,8 @@ TEST(ColumnsCase, ArrayAppend) {
auto col = arr1->GetAsColumn(1);

ASSERT_EQ(arr1->Size(), 2u);
//ASSERT_EQ(col->As<ColumnUInt64>()->At(0), 1u);
//ASSERT_EQ(col->As<ColumnUInt64>()->At(1), 3u);
ASSERT_EQ(col->As<ColumnUInt64>()->At(0), 1u);
ASSERT_EQ(col->As<ColumnUInt64>()->At(1), 3u);
}

TEST(ColumnsCase, TupleAppend){
Expand Down Expand Up @@ -253,6 +286,7 @@ TEST(ColumnsCase, TupleSlice){
ASSERT_EQ((*tuple2)[1]->As<ColumnString>()->At(0), "3");
}


TEST(ColumnsCase, DateAppend) {
auto col1 = std::make_shared<ColumnDate>();
auto col2 = std::make_shared<ColumnDate>();
Expand Down Expand Up @@ -505,6 +539,68 @@ TEST(ColumnsCase, Int128) {
EXPECT_EQ(0, col->At(4));
}

TEST(ColumnsCase, ColumnIPv4)
{
// TODO: split into proper method-level unit-tests
auto col = ColumnIPv4();

col.Append("255.255.255.255");
col.Append("127.0.0.1");
col.Append(3585395774);
col.Append(0);
const in_addr ip = make_IPv4(0x12345678);
col.Append(ip);

ASSERT_EQ(5u, col.Size());
EXPECT_EQ(make_IPv4(0xffffffff), col.At(0));
EXPECT_EQ(make_IPv4(0x0100007f), col.At(1));
EXPECT_EQ(make_IPv4(3585395774), col.At(2));
EXPECT_EQ(make_IPv4(0), col.At(3));
EXPECT_EQ(ip, col.At(4));

EXPECT_EQ("255.255.255.255", col.AsString(0));
EXPECT_EQ("127.0.0.1", col.AsString(1));
EXPECT_EQ("62.204.180.213", col.AsString(2));
EXPECT_EQ("0.0.0.0", col.AsString(3));
EXPECT_EQ("120.86.52.18", col.AsString(4));

col.Clear();
EXPECT_EQ(0u, col.Size());
}

TEST(ColumnsCase, ColumnIPv6)
{
// TODO: split into proper method-level unit-tests
auto col = ColumnIPv6();
col.Append("0:0:0:0:0:0:0:1");
col.Append("::");
col.Append("::FFFF:204.152.189.116");

const auto ipv6 = in6_addr{{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}};
col.Append(ipv6);

ASSERT_EQ(4u, col.Size());
const auto first_val = in6_addr{{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}};
EXPECT_EQ(first_val, col.At(0));

const auto second_val = in6_addr{{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}};
EXPECT_EQ(second_val, col.At(1));

const auto third_val = in6_addr{{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 204, 152, 189, 116}}};
EXPECT_EQ(third_val, col.At(2));

EXPECT_EQ(ipv6, col.At(3));


EXPECT_EQ("::1", col.AsString(0));
EXPECT_EQ("::", col.AsString(1));
EXPECT_EQ("::ffff:204.152.189.116", col.AsString(2));
EXPECT_EQ("1:203:405:607:809:a0b:c0d:e0f", col.AsString(3));

col.Clear();
EXPECT_EQ(0u, col.Size());
}

TEST(ColumnsCase, ColumnDecimal128_from_string) {
auto col = std::make_shared<ColumnDecimal>(38, 0);

Expand All @@ -516,8 +612,7 @@ TEST(ColumnsCase, ColumnDecimal128_from_string) {
std::numeric_limits<Int128>::max(),
};

for (size_t i = 0; i < values.size(); ++i)
{
for (size_t i = 0; i < values.size(); ++i) {
const auto value = values.begin()[i];
SCOPED_TRACE(::testing::Message() << "# index: " << i << " Int128 value: " << value);

Expand Down
2 changes: 2 additions & 0 deletions ut/performance_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <clickhouse/columns/string.h>
#include <clickhouse/columns/uuid.h>
#include <clickhouse/client.h>
#include <clickhouse/base/output.h>
#include <clickhouse/base/input.h>

#include <gtest/gtest.h>

Expand Down