2019-05-17 04:08:03 +00:00
|
|
|
#include <Storages/ConstraintsDescription.h>
|
|
|
|
|
2020-12-10 23:56:57 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/TreeRewriter.h>
|
|
|
|
|
2019-05-17 04:08:03 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
|
2020-04-15 20:28:05 +00:00
|
|
|
#include <Core/Defines.h>
|
|
|
|
|
2019-05-17 04:08:03 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
String ConstraintsDescription::toString() const
|
|
|
|
{
|
|
|
|
if (constraints.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ASTExpressionList list;
|
|
|
|
for (const auto & constraint : constraints)
|
|
|
|
list.children.push_back(constraint);
|
|
|
|
|
|
|
|
return serializeAST(list, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstraintsDescription ConstraintsDescription::parse(const String & str)
|
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ConstraintsDescription res;
|
|
|
|
ParserConstraintDeclarationList parser;
|
2020-04-15 20:28:05 +00:00
|
|
|
ASTPtr list = parseQuery(parser, str, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
2019-05-17 04:08:03 +00:00
|
|
|
|
|
|
|
for (const auto & constraint : list->children)
|
2020-05-12 11:26:44 +00:00
|
|
|
res.constraints.push_back(constraint);
|
2019-05-17 04:08:03 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-05-25 14:07:45 +00:00
|
|
|
ConstraintsExpressions ConstraintsDescription::getExpressions(const DB::Context & context,
|
|
|
|
const DB::NamesAndTypesList & source_columns_) const
|
|
|
|
{
|
|
|
|
ConstraintsExpressions res;
|
|
|
|
res.reserve(constraints.size());
|
|
|
|
for (const auto & constraint : constraints)
|
|
|
|
{
|
2020-07-22 17:13:05 +00:00
|
|
|
// TreeRewriter::analyze has query as non-const argument so to avoid accidental query changes we clone it
|
2020-05-12 16:38:11 +00:00
|
|
|
auto * constraint_ptr = constraint->as<ASTConstraintDeclaration>();
|
2020-05-12 11:26:44 +00:00
|
|
|
ASTPtr expr = constraint_ptr->expr->clone();
|
2020-07-22 17:13:05 +00:00
|
|
|
auto syntax_result = TreeRewriter(context).analyze(expr, source_columns_);
|
2020-05-12 11:26:44 +00:00
|
|
|
res.push_back(ExpressionAnalyzer(constraint_ptr->expr->clone(), syntax_result, context).getActions(false));
|
2019-05-25 14:07:45 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-06-05 17:29:40 +00:00
|
|
|
ConstraintsDescription::ConstraintsDescription(const ConstraintsDescription & other)
|
|
|
|
{
|
|
|
|
constraints.reserve(other.constraints.size());
|
|
|
|
for (const auto & constraint : other.constraints)
|
|
|
|
constraints.emplace_back(constraint->clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstraintsDescription & ConstraintsDescription::operator=(const ConstraintsDescription & other)
|
|
|
|
{
|
|
|
|
constraints.resize(other.constraints.size());
|
|
|
|
for (size_t i = 0; i < constraints.size(); ++i)
|
|
|
|
constraints[i] = other.constraints[i]->clone();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-05-17 04:08:03 +00:00
|
|
|
}
|