From b718b2ea88dabbabcf7aa9d0e4a8eb1943d05f03 Mon Sep 17 00:00:00 2001 From: avogar Date: Tue, 10 Dec 2024 14:27:58 +0000 Subject: [PATCH] Use default format settings during JSON parsing to avoid broken deserialization --- src/Formats/JSONExtractTree.cpp | 10 +++++++++- ...inary_serialization_use_default_setttings.reference | 1 + ...json_binary_serialization_use_default_setttings.sql | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.reference create mode 100644 tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.sql diff --git a/src/Formats/JSONExtractTree.cpp b/src/Formats/JSONExtractTree.cpp index 4ed65984c86..918acaf271a 100644 --- a/src/Formats/JSONExtractTree.cpp +++ b/src/Formats/JSONExtractTree.cpp @@ -1780,7 +1780,9 @@ private: paths_and_values_for_shared_data.emplace_back(current_path, ""); WriteBufferFromString buf(paths_and_values_for_shared_data.back().second); - dynamic_serialization->serializeBinary(*tmp_dynamic_column, 0, buf, format_settings); + /// Use default format settings for binary serialization. Non-default settings may change + /// the binary representation of the values and break the future deserialization. + dynamic_serialization->serializeBinary(*tmp_dynamic_column, 0, buf, getDefaultFormatSettings()); } return true; @@ -1807,6 +1809,12 @@ private: return false; } + const FormatSettings & getDefaultFormatSettings() const + { + static const FormatSettings settings; + return settings; + } + std::unordered_map>> typed_path_nodes; std::unordered_set paths_to_skip; std::vector sorted_paths_to_skip; diff --git a/tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.reference b/tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.reference new file mode 100644 index 00000000000..8cc71291a42 --- /dev/null +++ b/tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.reference @@ -0,0 +1 @@ +{"a":[{"b":"42"}]} diff --git a/tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.sql b/tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.sql new file mode 100644 index 00000000000..d9fa1e2043e --- /dev/null +++ b/tests/queries/0_stateless/03283_json_binary_serialization_use_default_setttings.sql @@ -0,0 +1,8 @@ +set enable_json_type=1; +set output_format_binary_write_json_as_string=1; + +drop table if exists test; +create table test (json JSON(max_dynamic_paths=0)) engine=Memory; +insert into test format JSONAsObject {"a" : [{"b" : 42}]}; + +select * from test;