2020-05-28 13:03:18 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
2020-07-22 17:13:05 +00:00
|
|
|
#include <Interpreters/TreeRewriter.h>
|
2019-02-05 14:50:25 +00:00
|
|
|
#include <Storages/IndicesDescription.h>
|
|
|
|
|
2020-12-16 21:44:05 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2019-10-07 14:18:18 +00:00
|
|
|
#include <Parsers/ASTIndexDeclaration.h>
|
2021-11-26 16:54:57 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2019-02-05 14:50:25 +00:00
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2021-11-26 16:54:57 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
2019-02-05 14:50:25 +00:00
|
|
|
#include <Parsers/parseQuery.h>
|
2020-05-28 13:03:18 +00:00
|
|
|
#include <Storages/extractKeyExpressionList.h>
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-04-15 20:28:05 +00:00
|
|
|
#include <Core/Defines.h>
|
2023-07-12 12:47:24 +00:00
|
|
|
#include "Common/Exception.h"
|
2020-04-15 20:28:05 +00:00
|
|
|
|
|
|
|
|
2019-02-05 14:50:25 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-05-28 13:03:18 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int INCORRECT_QUERY;
|
2020-05-28 17:17:05 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2022-05-20 08:14:03 +00:00
|
|
|
}
|
2020-05-28 13:03:18 +00:00
|
|
|
|
2020-06-05 17:29:40 +00:00
|
|
|
IndexDescription::IndexDescription(const IndexDescription & other)
|
|
|
|
: definition_ast(other.definition_ast ? other.definition_ast->clone() : nullptr)
|
|
|
|
, expression_list_ast(other.expression_list_ast ? other.expression_list_ast->clone() : nullptr)
|
|
|
|
, name(other.name)
|
|
|
|
, type(other.type)
|
|
|
|
, arguments(other.arguments)
|
|
|
|
, column_names(other.column_names)
|
|
|
|
, data_types(other.data_types)
|
|
|
|
, sample_block(other.sample_block)
|
|
|
|
, granularity(other.granularity)
|
|
|
|
{
|
2020-06-12 14:32:47 +00:00
|
|
|
if (other.expression)
|
2020-11-03 11:28:28 +00:00
|
|
|
expression = other.expression->clone();
|
2020-06-05 17:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IndexDescription & IndexDescription::operator=(const IndexDescription & other)
|
|
|
|
{
|
2020-06-09 17:42:04 +00:00
|
|
|
if (&other == this)
|
|
|
|
return *this;
|
|
|
|
|
2020-06-05 17:29:40 +00:00
|
|
|
if (other.definition_ast)
|
|
|
|
definition_ast = other.definition_ast->clone();
|
|
|
|
else
|
|
|
|
definition_ast.reset();
|
|
|
|
|
|
|
|
if (other.expression_list_ast)
|
|
|
|
expression_list_ast = other.expression_list_ast->clone();
|
|
|
|
else
|
|
|
|
expression_list_ast.reset();
|
|
|
|
|
|
|
|
name = other.name;
|
|
|
|
type = other.type;
|
2020-06-12 14:32:47 +00:00
|
|
|
|
|
|
|
if (other.expression)
|
2020-11-03 11:28:28 +00:00
|
|
|
expression = other.expression->clone();
|
2020-06-12 14:32:47 +00:00
|
|
|
else
|
|
|
|
expression.reset();
|
|
|
|
|
2020-06-05 17:29:40 +00:00
|
|
|
arguments = other.arguments;
|
|
|
|
column_names = other.column_names;
|
|
|
|
data_types = other.data_types;
|
|
|
|
sample_block = other.sample_block;
|
|
|
|
granularity = other.granularity;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-05-28 13:03:18 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
IndexDescription IndexDescription::getIndexFromAST(const ASTPtr & definition_ast, const ColumnsDescription & columns, ContextPtr context)
|
2020-05-28 13:03:18 +00:00
|
|
|
{
|
|
|
|
const auto * index_definition = definition_ast->as<ASTIndexDeclaration>();
|
|
|
|
if (!index_definition)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot create skip index from non ASTIndexDeclaration AST");
|
2020-05-28 13:03:18 +00:00
|
|
|
|
|
|
|
if (index_definition->name.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::INCORRECT_QUERY, "Skip index must have name in definition.");
|
2020-05-28 13:03:18 +00:00
|
|
|
|
|
|
|
if (!index_definition->type)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::INCORRECT_QUERY, "TYPE is required for index");
|
2020-05-28 13:03:18 +00:00
|
|
|
|
|
|
|
if (index_definition->type->parameters && !index_definition->type->parameters->children.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::INCORRECT_QUERY, "Index type cannot have parameters");
|
2020-05-28 13:03:18 +00:00
|
|
|
|
2020-05-28 13:09:03 +00:00
|
|
|
IndexDescription result;
|
2020-05-28 13:03:18 +00:00
|
|
|
result.definition_ast = index_definition->clone();
|
|
|
|
result.name = index_definition->name;
|
|
|
|
result.type = Poco::toLower(index_definition->type->name);
|
|
|
|
result.granularity = index_definition->granularity;
|
|
|
|
|
2023-07-12 12:47:24 +00:00
|
|
|
ASTPtr expr_list;
|
|
|
|
if (index_definition->expr)
|
|
|
|
{
|
|
|
|
expr_list = extractKeyExpressionList(index_definition->expr->clone());
|
|
|
|
result.expression_list_ast = expr_list->clone();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expression is not set");
|
|
|
|
}
|
2020-05-28 13:03:18 +00:00
|
|
|
|
2020-07-22 17:13:05 +00:00
|
|
|
auto syntax = TreeRewriter(context).analyze(expr_list, columns.getAllPhysical());
|
2020-05-28 13:03:18 +00:00
|
|
|
result.expression = ExpressionAnalyzer(expr_list, syntax, context).getActions(true);
|
2023-02-24 19:17:44 +00:00
|
|
|
result.sample_block = result.expression->getSampleBlock();
|
2020-05-28 13:03:18 +00:00
|
|
|
|
2023-03-06 15:29:13 +00:00
|
|
|
for (auto & elem : result.sample_block)
|
2023-02-27 15:11:25 +00:00
|
|
|
{
|
2023-03-06 15:29:13 +00:00
|
|
|
if (!elem.column)
|
|
|
|
elem.column = elem.type->createColumn();
|
|
|
|
|
2023-02-27 15:11:25 +00:00
|
|
|
result.column_names.push_back(elem.name);
|
|
|
|
result.data_types.push_back(elem.type);
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:03:18 +00:00
|
|
|
const auto & definition_arguments = index_definition->type->arguments;
|
|
|
|
if (definition_arguments)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < definition_arguments->children.size(); ++i)
|
|
|
|
{
|
|
|
|
const auto * argument = definition_arguments->children[i]->as<ASTLiteral>();
|
|
|
|
if (!argument)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::INCORRECT_QUERY, "Only literals can be skip index arguments");
|
2020-05-28 13:03:18 +00:00
|
|
|
result.arguments.emplace_back(argument->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void IndexDescription::recalculateWithNewColumns(const ColumnsDescription & new_columns, ContextPtr context)
|
2020-06-19 10:53:20 +00:00
|
|
|
{
|
|
|
|
*this = getIndexFromAST(definition_ast, new_columns, context);
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-05-28 13:03:18 +00:00
|
|
|
bool IndicesDescription::has(const String & name) const
|
|
|
|
{
|
|
|
|
for (const auto & index : *this)
|
|
|
|
if (index.name == name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String IndicesDescription::toString() const
|
|
|
|
{
|
|
|
|
if (empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ASTExpressionList list;
|
|
|
|
for (const auto & index : *this)
|
|
|
|
list.children.push_back(index.definition_ast);
|
|
|
|
|
2023-07-19 18:02:09 +00:00
|
|
|
return serializeAST(list);
|
2020-05-28 13:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
IndicesDescription IndicesDescription::parse(const String & str, const ColumnsDescription & columns, ContextPtr context)
|
2020-05-28 13:03:18 +00:00
|
|
|
{
|
|
|
|
IndicesDescription result;
|
|
|
|
if (str.empty())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
ParserIndexDeclarationList parser;
|
|
|
|
ASTPtr list = parseQuery(parser, str, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
|
|
|
|
|
|
|
for (const auto & index : list->children)
|
2020-06-01 12:02:36 +00:00
|
|
|
result.emplace_back(IndexDescription::getIndexFromAST(index, columns, context));
|
2020-05-28 13:03:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
|
2020-06-19 10:53:20 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ExpressionActionsPtr IndicesDescription::getSingleExpressionForIndices(const ColumnsDescription & columns, ContextPtr context) const
|
2020-06-01 19:58:11 +00:00
|
|
|
{
|
|
|
|
ASTPtr combined_expr_list = std::make_shared<ASTExpressionList>();
|
|
|
|
for (const auto & index : *this)
|
|
|
|
for (const auto & index_expr : index.expression_list_ast->children)
|
|
|
|
combined_expr_list->children.push_back(index_expr->clone());
|
|
|
|
|
2020-07-22 17:13:05 +00:00
|
|
|
auto syntax_result = TreeRewriter(context).analyze(combined_expr_list, columns.getAllPhysical());
|
2020-06-01 19:58:11 +00:00
|
|
|
return ExpressionAnalyzer(combined_expr_list, syntax_result, context).getActions(false);
|
|
|
|
}
|
|
|
|
|
2022-02-20 11:45:13 +00:00
|
|
|
Names IndicesDescription::getAllRegisteredNames() const
|
|
|
|
{
|
|
|
|
Names result;
|
|
|
|
for (const auto & index : *this)
|
|
|
|
{
|
|
|
|
result.emplace_back(index.name);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2019-02-05 14:50:25 +00:00
|
|
|
}
|