2019-07-31 15:36:10 +00:00
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2020-06-08 22:49:19 +00:00
|
|
|
#include <Parsers/parseQuery.h>
|
2019-07-31 15:36:10 +00:00
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
2020-02-10 15:50:12 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2022-08-16 09:51:52 +00:00
|
|
|
#include <Interpreters/parseColumnsListForTableFunction.h>
|
2022-12-21 21:21:30 +00:00
|
|
|
#include <DataTypes/DataTypeLowCardinality.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2024-01-30 15:21:58 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2024-01-31 12:59:26 +00:00
|
|
|
#include <DataTypes/DataTypeVariant.h>
|
2024-01-30 15:21:58 +00:00
|
|
|
#include <DataTypes/DataTypeMap.h>
|
2019-07-31 15:36:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2022-12-21 21:21:30 +00:00
|
|
|
extern const int SUSPICIOUS_TYPE_FOR_LOW_CARDINALITY;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void validateDataType(const DataTypePtr & type, const DataTypeValidationSettings & settings)
|
|
|
|
{
|
|
|
|
if (!settings.allow_suspicious_low_cardinality_types)
|
|
|
|
{
|
|
|
|
if (const auto * lc_type = typeid_cast<const DataTypeLowCardinality *>(type.get()))
|
|
|
|
{
|
|
|
|
if (!isStringOrFixedString(*removeNullable(lc_type->getDictionaryType())))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::SUSPICIOUS_TYPE_FOR_LOW_CARDINALITY,
|
|
|
|
"Creating columns of type {} is prohibited by default due to expected negative impact on performance. "
|
|
|
|
"It can be enabled with the \"allow_suspicious_low_cardinality_types\" setting.",
|
|
|
|
lc_type->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!settings.allow_experimental_object_type)
|
|
|
|
{
|
|
|
|
if (type->hasDynamicSubcolumns())
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Cannot create column with type '{}' because experimental Object type is not allowed. "
|
|
|
|
"Set setting allow_experimental_object_type = 1 in order to allow it", type->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!settings.allow_suspicious_fixed_string_types)
|
|
|
|
{
|
2024-01-30 15:21:58 +00:00
|
|
|
if (const auto * fixed_string = typeid_cast<const DataTypeFixedString *>(type.get()))
|
2022-12-21 21:22:26 +00:00
|
|
|
{
|
|
|
|
if (fixed_string->getN() > MAX_FIXEDSTRING_SIZE_WITHOUT_SUSPICIOUS)
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Cannot create column with type '{}' because fixed string with size > {} is suspicious. "
|
|
|
|
"Set setting allow_suspicious_fixed_string_types = 1 in order to allow it",
|
|
|
|
type->getName(),
|
|
|
|
MAX_FIXEDSTRING_SIZE_WITHOUT_SUSPICIOUS);
|
|
|
|
}
|
2022-12-21 21:21:30 +00:00
|
|
|
}
|
2023-12-19 16:43:30 +00:00
|
|
|
|
|
|
|
if (!settings.allow_experimental_variant_type)
|
|
|
|
{
|
|
|
|
if (isVariant(type))
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Cannot create column with type '{}' because experimental Variant type is not allowed. "
|
|
|
|
"Set setting allow_experimental_variant_type = 1 in order to allow it", type->getName());
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 15:21:58 +00:00
|
|
|
|
|
|
|
if (const auto * nullable_type = typeid_cast<const DataTypeNullable *>(type.get()))
|
|
|
|
{
|
|
|
|
validateDataType(nullable_type->getNestedType(), settings);
|
|
|
|
}
|
|
|
|
else if (const auto * lc_type = typeid_cast<const DataTypeLowCardinality *>(type.get()))
|
|
|
|
{
|
|
|
|
validateDataType(lc_type->getDictionaryType(), settings);
|
|
|
|
}
|
|
|
|
else if (const auto * array_type = typeid_cast<const DataTypeArray *>(type.get()))
|
|
|
|
{
|
|
|
|
validateDataType(array_type->getNestedType(), settings);
|
|
|
|
}
|
|
|
|
else if (const auto * tuple_type = typeid_cast<const DataTypeTuple *>(type.get()))
|
|
|
|
{
|
|
|
|
for (const auto & element : tuple_type->getElements())
|
|
|
|
validateDataType(element, settings);
|
|
|
|
}
|
|
|
|
else if (const auto * map_type = typeid_cast<const DataTypeMap *>(type.get()))
|
|
|
|
{
|
|
|
|
validateDataType(map_type->getKeyType(), settings);
|
|
|
|
validateDataType(map_type->getValueType(), settings);
|
|
|
|
}
|
2024-01-31 12:59:26 +00:00
|
|
|
else if (const auto * variant_type = typeid_cast<const DataTypeVariant *>(type.get()))
|
|
|
|
{
|
|
|
|
for (const auto & variant : variant_type->getVariants())
|
|
|
|
validateDataType(variant, settings);
|
|
|
|
}
|
2019-07-31 15:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 21:21:30 +00:00
|
|
|
ColumnsDescription parseColumnsListFromString(const std::string & structure, const ContextPtr & context)
|
2019-07-31 15:36:10 +00:00
|
|
|
{
|
2022-07-04 14:46:10 +00:00
|
|
|
ParserColumnDeclarationList parser(true, true);
|
2021-04-10 23:33:54 +00:00
|
|
|
const Settings & settings = context->getSettingsRef();
|
2019-07-31 15:36:10 +00:00
|
|
|
|
2020-06-08 22:49:19 +00:00
|
|
|
ASTPtr columns_list_raw = parseQuery(parser, structure, "columns declaration list", settings.max_query_size, settings.max_parser_depth);
|
2019-07-31 15:36:10 +00:00
|
|
|
|
|
|
|
auto * columns_list = dynamic_cast<ASTExpressionList *>(columns_list_raw.get());
|
|
|
|
if (!columns_list)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Could not cast AST to ASTExpressionList");
|
2019-07-31 15:36:10 +00:00
|
|
|
|
2023-11-03 15:46:07 +00:00
|
|
|
auto columns = InterpreterCreateQuery::getColumnsDescription(*columns_list, context, false, false);
|
2022-12-21 21:23:12 +00:00
|
|
|
auto validation_settings = DataTypeValidationSettings(context->getSettingsRef());
|
2022-12-21 21:21:30 +00:00
|
|
|
for (const auto & [name, type] : columns.getAll())
|
2022-12-21 21:23:12 +00:00
|
|
|
validateDataType(type, validation_settings);
|
2022-12-21 21:21:30 +00:00
|
|
|
return columns;
|
2019-07-31 15:36:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 15:20:56 +00:00
|
|
|
bool tryParseColumnsListFromString(const std::string & structure, ColumnsDescription & columns, const ContextPtr & context, String & error)
|
2022-01-26 13:29:33 +00:00
|
|
|
{
|
2022-07-04 14:46:10 +00:00
|
|
|
ParserColumnDeclarationList parser(true, true);
|
2022-01-26 13:29:33 +00:00
|
|
|
const Settings & settings = context->getSettingsRef();
|
|
|
|
|
|
|
|
const char * start = structure.data();
|
|
|
|
const char * end = structure.data() + structure.size();
|
2022-01-26 13:48:21 +00:00
|
|
|
ASTPtr columns_list_raw = tryParseQuery(parser, start, end, error, false, "columns declaration list", false, settings.max_query_size, settings.max_parser_depth);
|
2022-01-26 13:29:33 +00:00
|
|
|
if (!columns_list_raw)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto * columns_list = dynamic_cast<ASTExpressionList *>(columns_list_raw.get());
|
|
|
|
if (!columns_list)
|
2023-02-27 15:20:56 +00:00
|
|
|
{
|
|
|
|
error = fmt::format("Invalid columns declaration list: \"{}\"", structure);
|
2022-01-26 13:29:33 +00:00
|
|
|
return false;
|
2023-02-27 15:20:56 +00:00
|
|
|
}
|
2022-01-26 13:29:33 +00:00
|
|
|
|
2022-08-16 09:41:32 +00:00
|
|
|
try
|
|
|
|
{
|
2023-11-03 15:46:07 +00:00
|
|
|
columns = InterpreterCreateQuery::getColumnsDescription(*columns_list, context, false, false);
|
2022-12-21 21:23:12 +00:00
|
|
|
auto validation_settings = DataTypeValidationSettings(context->getSettingsRef());
|
2022-12-21 21:21:30 +00:00
|
|
|
for (const auto & [name, type] : columns.getAll())
|
2022-12-21 21:23:12 +00:00
|
|
|
validateDataType(type, validation_settings);
|
2022-08-16 09:41:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2023-02-27 15:20:56 +00:00
|
|
|
error = getCurrentExceptionMessage(false);
|
2022-08-16 09:41:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-26 13:29:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 15:58:28 +00:00
|
|
|
}
|