Merge pull request #49933 from ClickHouse/fix-ipv6-proto-serialization

Fix IPv6 encoding in protobuf
This commit is contained in:
Yakov Olkhovskiy 2023-05-18 23:02:15 -04:00 committed by GitHub
commit a2c3de5082
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 8 deletions

View File

@ -1852,25 +1852,26 @@ namespace
write_function = [this](IPv6 value)
{
ipToString(value, text_buffer);
text_buffer = String(IPV6_BINARY_LENGTH, '\0');
memcpy(text_buffer.data(), &value.toUnderType(), IPV6_BINARY_LENGTH);
writeStr(text_buffer);
};
read_function = [this]() -> IPv6
{
readStr(text_buffer);
return parse<IPv6>(text_buffer);
if (text_buffer.size() != IPV6_BINARY_LENGTH)
throw Exception(ErrorCodes::PROTOBUF_BAD_CAST,
"Could not convert bytes field {} to IPv6 for inserting into column {} - field size {} is not equal to IPv6 size {}",
field_descriptor.full_name(), column_name, text_buffer.size(), IPV6_BINARY_LENGTH);
IPv6 value;
memcpy(&value.toUnderType(), text_buffer.data(), IPV6_BINARY_LENGTH);
return value;
};
default_function = [this]() -> IPv6 { return parse<IPv6>(field_descriptor.default_value_string()); };
}
static void ipToString(const IPv6 & ip, String & str)
{
WriteBufferFromString buf{str};
writeText(ip, buf);
}
std::function<void(IPv6)> write_function;
std::function<IPv6()> read_function;
std::function<IPv6()> default_function;

View File

@ -0,0 +1,2 @@
::ffff:1.2.3.4
::ffff:1.2.3.4

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
# Tags: no-fasttest
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
SCHEMADIR=$CURDIR/format_schemas
echo -ne '\x12\x1a\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x01\x02\x03\x04' | $CLICKHOUSE_LOCAL --input-format Protobuf --format_schema="$SCHEMADIR/02751_protobuf_ipv6:Message" --structure="ipv6_bytes IPv6" -q "select * from table"
$CLICKHOUSE_LOCAL -q "select '::ffff:1.2.3.4'::IPv6 as ipv6_bytes format Protobuf settings format_schema = '$SCHEMADIR/02751_protobuf_ipv6:Message'" | $CLICKHOUSE_LOCAL --input-format Protobuf --format_schema="$SCHEMADIR/02751_protobuf_ipv6:Message" --structure="ipv6_bytes IPv6" -q "select * from table"

View File

@ -0,0 +1,6 @@
syntax = "proto3";
message Message
{
bytes ipv6_bytes = 3;
}