From 413800627c07e2e65df9783e54cd531e8b61e675 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Wed, 11 Dec 2024 12:10:56 +0000 Subject: [PATCH] Backport #73082 to 24.10: Check for duplicate JSON keys during Tuple parsing --- src/DataTypes/Serializations/SerializationTuple.cpp | 3 +++ .../03284_json_object_as_tuple_duplicate_keys.reference | 2 ++ .../03284_json_object_as_tuple_duplicate_keys.sql | 9 +++++++++ 3 files changed, 14 insertions(+) create mode 100644 tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.reference create mode 100644 tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.sql diff --git a/src/DataTypes/Serializations/SerializationTuple.cpp b/src/DataTypes/Serializations/SerializationTuple.cpp index 366949e7ac0..98127fc09fb 100644 --- a/src/DataTypes/Serializations/SerializationTuple.cpp +++ b/src/DataTypes/Serializations/SerializationTuple.cpp @@ -387,6 +387,9 @@ ReturnType SerializationTuple::deserializeTupleJSONImpl(IColumn & column, ReadBu return false; } + if (seen_elements[element_pos]) + throw Exception(ErrorCodes::INCORRECT_DATA, "JSON object contains duplicate key '{}'", name); + seen_elements[element_pos] = 1; auto & element_column = extractElementColumn(column, element_pos); diff --git a/tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.reference b/tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.reference new file mode 100644 index 00000000000..4fe371d5dfc --- /dev/null +++ b/tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.reference @@ -0,0 +1,2 @@ +a Tuple(\n b Nullable(String)) +a Tuple(\n b Nullable(String)) diff --git a/tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.sql b/tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.sql new file mode 100644 index 00000000000..a60b8c9488b --- /dev/null +++ b/tests/queries/0_stateless/03284_json_object_as_tuple_duplicate_keys.sql @@ -0,0 +1,9 @@ +select * from format(JSONEachRow, 'a Tuple(b UInt32)', '{"a" : {"b" : 1, "b" : 2}}'); -- {serverError INCORRECT_DATA} +select * from format(JSONEachRow, '{"a" : {"b" : 1, "b" : 2}}'); -- {serverError INCORRECT_DATA} +select * from format(JSONEachRow, '{"a" : {"b" : 1, "b" : 2, "b" : 3}, "c" : 42}'); -- {serverError INCORRECT_DATA} +set input_format_json_use_string_type_for_ambiguous_paths_in_named_tuples_inference_from_objects=1; +desc format(JSONEachRow, '{"a" : {"b" : 1, "b" : "Hello"}}'); +select * from format(JSONEachRow, '{"a" : {"b" : 1, "b" : "Hello"}}'); -- {serverError INCORRECT_DATA} +desc format(JSONEachRow, '{"a" : {"b" : 1, "b" : {"c" : "Hello"}}}'); +select * from format(JSONEachRow, '{"a" : {"b" : 1, "b" : {"c" : "Hello"}}}'); -- {serverError INCORRECT_DATA} +