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

Allow write/read unnamed tuple as nested Message in Protobuf format #48390

Merged
merged 3 commits into from
Apr 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/Formats/ProtobufSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3453,15 +3453,35 @@ namespace
const auto & tuple_data_type = assert_cast<const DataTypeTuple &>(*data_type);
size_t size_of_tuple = tuple_data_type.getElements().size();

if (tuple_data_type.haveExplicitNames() && field_descriptor.message_type())
if (const auto * message_type = field_descriptor.message_type())
{
bool have_explicit_names = tuple_data_type.haveExplicitNames();
Names element_names;
if (have_explicit_names)
{
element_names = tuple_data_type.getElementNames();
}
else
{
/// Match unnamed Tuple elements and Message fields by position.
size_t field_count = message_type->field_count();
if (field_count != size_of_tuple)
throw Exception(
ErrorCodes::NO_COLUMNS_SERIALIZED_TO_PROTOBUF_FIELDS,
"The number of fields in Protobuf message ({}) is not equal to the number of elements in unnamed Tuple ({})",
field_count,
size_of_tuple);
for (size_t i = 0; i != field_count; ++i)
element_names.push_back(message_type->field(static_cast<int>(i))->name());
}

/// Try to serialize as a nested message.
std::vector<size_t> used_column_indices;
auto message_serializer = buildMessageSerializerImpl(
size_of_tuple,
tuple_data_type.getElementNames().data(),
element_names.data(),
tuple_data_type.getElements().data(),
*field_descriptor.message_type(),
*message_type,
/* with_length_delimiter = */ false,
google_wrappers_special_treatment,
&field_descriptor,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(42,'Hello',[1,2,3])
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/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

$CLICKHOUSE_LOCAL -q "select tuple(42, 'Hello', [1,2,3]) as x format Protobuf settings format_schema='$SCHEMADIR/02707_schema:Message'" | $CLICKHOUSE_LOCAL --input-format Protobuf --structure='x Tuple(UInt32, String, Array(UInt32))' -q "select * from table" --format_schema="$SCHEMADIR/02707_schema:Message"

11 changes: 11 additions & 0 deletions tests/queries/0_stateless/format_schemas/02707_schema.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

message Nested {
int32 a = 1;
string b = 2;
repeated int32 c = 3;
};

message Message {
Nested x = 1;
};