Skip to content

Commit 0f794b4

Browse files
Backport #75357 to 24.11: Fix crash in protobuf schema cache
1 parent 79f22c3 commit 0f794b4

9 files changed

+47
-9
lines changed

src/Processors/Formats/Impl/ProtobufListInputFormat.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ ProtobufListInputFormat::ProtobufListInputFormat(
1919
const String & google_protos_path)
2020
: IRowInputFormat(header_, in_, params_)
2121
, reader(std::make_unique<ProtobufReader>(in_))
22+
, descriptor_holder(ProtobufSchemas::instance().getMessageTypeForFormatSchema(
23+
schema_info_.getSchemaInfo(), ProtobufSchemas::WithEnvelope::Yes, google_protos_path))
2224
, serializer(ProtobufSerializer::create(
2325
header_.getNames(),
2426
header_.getDataTypes(),
2527
missing_column_indices,
26-
ProtobufSchemas::instance().getMessageTypeForFormatSchema(
27-
schema_info_.getSchemaInfo(), ProtobufSchemas::WithEnvelope::Yes, google_protos_path),
28+
descriptor_holder,
2829
/* with_length_delimiter = */ true,
2930
/* with_envelope = */ true,
3031
flatten_google_wrappers_,

src/Processors/Formats/Impl/ProtobufListInputFormat.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# include <Formats/FormatSchemaInfo.h>
77
# include <Processors/Formats/IRowInputFormat.h>
88
# include <Processors/Formats/ISchemaReader.h>
9+
# include <Formats/ProtobufSchemas.h>
910

1011
namespace DB
1112
{
@@ -44,6 +45,7 @@ class ProtobufListInputFormat final : public IRowInputFormat
4445

4546
std::unique_ptr<ProtobufReader> reader;
4647
std::vector<size_t> missing_column_indices;
48+
ProtobufSchemas::DescriptorHolder descriptor_holder;
4749
std::unique_ptr<ProtobufSerializer> serializer;
4850
};
4951

src/Processors/Formats/Impl/ProtobufListOutputFormat.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ ProtobufListOutputFormat::ProtobufListOutputFormat(
1717
const String & google_protos_path)
1818
: IRowOutputFormat(header_, out_)
1919
, writer(std::make_unique<ProtobufWriter>(out))
20+
, descriptor_holder(ProtobufSchemas::instance().getMessageTypeForFormatSchema(
21+
schema_info_.getSchemaInfo(), ProtobufSchemas::WithEnvelope::Yes, google_protos_path))
2022
, serializer(ProtobufSerializer::create(
2123
header_.getNames(),
2224
header_.getDataTypes(),
23-
ProtobufSchemas::instance().getMessageTypeForFormatSchema(
24-
schema_info_.getSchemaInfo(), ProtobufSchemas::WithEnvelope::Yes, google_protos_path),
25+
descriptor_holder,
2526
/* with_length_delimiter = */ true,
2627
/* with_envelope = */ true,
2728
defaults_for_nullable_google_wrappers_,

src/Processors/Formats/Impl/ProtobufListOutputFormat.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
#if USE_PROTOBUF
66
# include <Processors/Formats/IRowOutputFormat.h>
7-
# include <Formats/FormatSchemaInfo.h>
7+
# include <Formats/FormatSchemaInfo.h>
8+
# include <Formats/ProtobufSchemas.h>
89

910
namespace DB
1011
{
@@ -42,6 +43,7 @@ class ProtobufListOutputFormat final : public IRowOutputFormat
4243
void resetFormatterImpl() override;
4344

4445
std::unique_ptr<ProtobufWriter> writer;
46+
ProtobufSchemas::DescriptorHolder descriptor_holder;
4547
std::unique_ptr<ProtobufSerializer> serializer;
4648
};
4749

src/Processors/Formats/Impl/ProtobufRowInputFormat.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class ProtobufRowInputFormat final : public IRowInputFormat
5555

5656
std::unique_ptr<ProtobufReader> reader;
5757
std::vector<size_t> missing_column_indices;
58+
const ProtobufSchemas::DescriptorHolder descriptor;
5859
std::unique_ptr<ProtobufSerializer> serializer;
5960

60-
const ProtobufSchemas::DescriptorHolder descriptor;
6161
bool with_length_delimiter;
6262
bool flatten_google_wrappers;
6363
};

src/Processors/Formats/Impl/ProtobufRowOutputFormat.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ ProtobufRowOutputFormat::ProtobufRowOutputFormat(
2424
bool with_length_delimiter_)
2525
: IRowOutputFormat(header_, out_)
2626
, writer(std::make_unique<ProtobufWriter>(out))
27+
, descriptor_holder(ProtobufSchemas::instance().getMessageTypeForFormatSchema(
28+
schema_info_.getSchemaInfo(), ProtobufSchemas::WithEnvelope::No, settings_.protobuf.google_protos_path))
2729
, serializer(ProtobufSerializer::create(
2830
header_.getNames(),
2931
header_.getDataTypes(),
30-
ProtobufSchemas::instance().getMessageTypeForFormatSchema(
31-
schema_info_.getSchemaInfo(), ProtobufSchemas::WithEnvelope::No, settings_.protobuf.google_protos_path),
32+
descriptor_holder,
3233
with_length_delimiter_,
3334
/* with_envelope = */ false,
3435
settings_.protobuf.output_nullables_with_google_wrappers,

src/Processors/Formats/Impl/ProtobufRowOutputFormat.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
#if USE_PROTOBUF
66
# include <Processors/Formats/IRowOutputFormat.h>
7-
# include <Formats/FormatSchemaInfo.h>
7+
# include <Formats/FormatSchemaInfo.h>
8+
# include <Formats/ProtobufSchemas.h>
89

910
namespace DB
1011
{
@@ -42,6 +43,7 @@ class ProtobufRowOutputFormat final : public IRowOutputFormat
4243
void writeField(const IColumn &, const ISerialization &, size_t) override {}
4344

4445
std::unique_ptr<ProtobufWriter> writer;
46+
ProtobufSchemas::DescriptorHolder descriptor_holder;
4547
std::unique_ptr<ProtobufSerializer> serializer;
4648
const bool allow_multiple_rows;
4749
};

tests/queries/0_stateless/03326_protobuf_schemas_race.reference

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
# Tags: no-fasttest
3+
4+
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
5+
. "$CURDIR"/../shell_config.sh
6+
7+
mkdir -p "${CLICKHOUSE_SCHEMA_FILES}"
8+
mkdir -p "${CLICKHOUSE_SCHEMA_FILES}/${CLICKHOUSE_TEST_UNIQUE_NAME}"
9+
SOURCE_SCHEMA_FILE="${CURDIR}/format_schemas/03234_proto_simple_nested_repeated_noexception.proto"
10+
TARGET_SCHEMA_FILE="${CLICKHOUSE_SCHEMA_FILES}/${CLICKHOUSE_TEST_UNIQUE_NAME}/03234_proto_simple_nested_repeated_noexception.proto"
11+
cp "${SOURCE_SCHEMA_FILE}" "${TARGET_SCHEMA_FILE}"
12+
13+
echo "DROP TABLE IF EXISTS table_file;
14+
CREATE TABLE table_file (
15+
u UInt32,
16+
\`v.w\` Array(UInt32),
17+
\`v.x\` Array(UInt32),
18+
\`v.y\` Array(Array(UInt32)),
19+
\`v.z\` Array(Array(UInt32))
20+
) ENGINE File(Protobuf) SETTINGS format_schema = '$CLICKHOUSE_TEST_UNIQUE_NAME/03234_proto_simple_nested_repeated_noexception.proto:M';
21+
INSERT INTO table_file SELECT * FROM generateRandom() limit 1000000;
22+
DROP TABLE table_file;" | $CLICKHOUSE_CLIENT -m &
23+
24+
for i in $(seq 1 100)
25+
do
26+
$CLICKHOUSE_CLIENT -q "SYSTEM DROP FORMAT SCHEMA CACHE"
27+
done
28+
29+
rm -rf "${CLICKHOUSE_SCHEMA_FILES}/${CLICKHOUSE_TEST_UNIQUE_NAME}"

0 commit comments

Comments
 (0)