first version of sultion

This commit is contained in:
Konstantin Vedernikov 2024-07-20 19:21:48 +00:00
parent 558bec1c94
commit c1ced0d9d8
4 changed files with 31 additions and 1 deletions

View File

@ -1158,6 +1158,7 @@ class IColumn;
M(Bool, input_format_values_accurate_types_of_literals, true, "For Values format: when parsing and interpreting expressions using template, check actual type of literal to avoid possible overflow and precision issues.", 0) \
M(Bool, input_format_values_allow_data_after_semicolon, false, "For Values format: allow extra data after semicolon (used by client to interpret comments).", 0) \
M(Bool, input_format_avro_allow_missing_fields, false, "For Avro/AvroConfluent format: when field is not found in schema use default value instead of error", 0) \
M(Bool, cast_keys_to_string_from_json, false, "Either to cast string to int in enumerating json or not", 0) \
/** This setting is obsolete and do nothing, left for compatibility reasons. */ \
M(Bool, input_format_avro_null_as_default, false, "For Avro/AvroConfluent format: insert default in case of null and non Nullable column", 0) \
M(UInt64, format_binary_max_string_size, 1_GiB, "The maximum allowed size for String in RowBinary format. It prevents allocating large amount of memory in case of corrupted data. 0 means there is no limit", 0) \

View File

@ -1,6 +1,9 @@
#include <DataTypes/EnumValues.h>
#include <boost/algorithm/string.hpp>
#include <base/sort.h>
#include "Core/Names.h"
#include <Common/CurrentThread.h>
#include <Interpreters/Context.h>
namespace DB
@ -52,7 +55,23 @@ void EnumValues<T>::fillMaps()
template <typename T>
T EnumValues<T>::getValue(StringRef field_name, bool try_treat_as_id) const
{
const auto it = name_to_value_map.find(field_name);
ContextPtr query_context;
if (CurrentThread::isInitialized())
{
query_context = CurrentThread::get().getGlobalContext();
}
auto it = name_to_value_map.find(field_name);
if (!it && query_context && query_context->getSettingsRef().cast_keys_to_string_from_json)
{
for (const auto& item : name_to_value_map)
{
if (item.getValue().second == std::stoi(field_name.toString()))
{
it = name_to_value_map.find(item.getKey());
}
}
}
if (!it)
{
/// It is used in CSV and TSV input formats. If we fail to find given string in

View File

@ -0,0 +1,10 @@
SET cast_keys_to_string_from_json=1;
DROP TABLE test;
CREATE TABLE test
(
`answer` Enum8('Question' = 1, 'Answer' = 2, 'Wiki' = 3, 'TagWikiExcerpt' = 4, 'TagWiki' = 5, 'ModeratorNomination' = 6, 'WikiPlaceholder' = 7, 'PrivilegeWiki' = 8)
)
ENGINE = Memory;
INSERT INTO test FORMAT JSONEachRow {"answer": 1};
INSERT INTO test FORMAT JSONEachRow {"answer": "2"};
SELECT * FROM test;