improve constrdescr

This commit is contained in:
Nikita Vasilev 2021-04-26 17:19:18 +03:00
parent 2896f9aa75
commit 4650e36d05
5 changed files with 70 additions and 22 deletions

View File

@ -354,7 +354,7 @@ ASTPtr InterpreterCreateQuery::formatConstraints(const ConstraintsDescription &
{ {
auto res = std::make_shared<ASTExpressionList>(); auto res = std::make_shared<ASTExpressionList>();
for (const auto & constraint : constraints.constraints) for (const auto & constraint : constraints.getConstraints())
res->children.push_back(constraint->clone()); res->children.push_back(constraint->clone());
return res; return res;
@ -489,9 +489,11 @@ ColumnsDescription InterpreterCreateQuery::getColumnsDescription(
ConstraintsDescription InterpreterCreateQuery::getConstraintsDescription(const ASTExpressionList * constraints) ConstraintsDescription InterpreterCreateQuery::getConstraintsDescription(const ASTExpressionList * constraints)
{ {
ConstraintsDescription res; ConstraintsDescription res;
auto constraints_data = res.getConstraints();
if (constraints) if (constraints)
for (const auto & constraint : constraints->children) for (const auto & constraint : constraints->children)
res.constraints.push_back(std::dynamic_pointer_cast<ASTConstraintDeclaration>(constraint->clone())); constraints_data.push_back(std::dynamic_pointer_cast<ASTConstraintDeclaration>(constraint->clone()));
res.updateConstraints(constraints_data);
return res; return res;
} }

View File

@ -462,9 +462,10 @@ void AlterCommand::apply(StorageInMemoryMetadata & metadata, const Context & con
} }
else if (type == ADD_CONSTRAINT) else if (type == ADD_CONSTRAINT)
{ {
auto constraints = metadata.constraints.getConstraints();
if (std::any_of( if (std::any_of(
metadata.constraints.constraints.cbegin(), constraints.cbegin(),
metadata.constraints.constraints.cend(), constraints.cend(),
[this](const ASTPtr & constraint_ast) [this](const ASTPtr & constraint_ast)
{ {
return constraint_ast->as<ASTConstraintDeclaration &>().name == constraint_name; return constraint_ast->as<ASTConstraintDeclaration &>().name == constraint_name;
@ -476,28 +477,31 @@ void AlterCommand::apply(StorageInMemoryMetadata & metadata, const Context & con
ErrorCodes::ILLEGAL_COLUMN); ErrorCodes::ILLEGAL_COLUMN);
} }
auto insert_it = metadata.constraints.constraints.end(); auto insert_it = constraints.end();
metadata.constraints.constraints.emplace(insert_it, std::dynamic_pointer_cast<ASTConstraintDeclaration>(constraint_decl)); constraints.emplace(insert_it, std::dynamic_pointer_cast<ASTConstraintDeclaration>(constraint_decl));
metadata.constraints.updateConstraints(constraints);
} }
else if (type == DROP_CONSTRAINT) else if (type == DROP_CONSTRAINT)
{ {
auto constraints = metadata.constraints.getConstraints();
auto erase_it = std::find_if( auto erase_it = std::find_if(
metadata.constraints.constraints.begin(), constraints.begin(),
metadata.constraints.constraints.end(), constraints.end(),
[this](const ASTPtr & constraint_ast) [this](const ASTPtr & constraint_ast)
{ {
return constraint_ast->as<ASTConstraintDeclaration &>().name == constraint_name; return constraint_ast->as<ASTConstraintDeclaration &>().name == constraint_name;
}); });
if (erase_it == metadata.constraints.constraints.end()) if (erase_it == constraints.end())
{ {
if (if_exists) if (if_exists)
return; return;
throw Exception("Wrong constraint name. Cannot find constraint `" + constraint_name + "` to drop.", throw Exception("Wrong constraint name. Cannot find constraint `" + constraint_name + "` to drop.",
ErrorCodes::BAD_ARGUMENTS); ErrorCodes::BAD_ARGUMENTS);
} }
metadata.constraints.constraints.erase(erase_it); constraints.erase(erase_it);
metadata.constraints.updateConstraints(constraints);
} }
else if (type == MODIFY_TTL) else if (type == MODIFY_TTL)
{ {
@ -543,8 +547,10 @@ void AlterCommand::apply(StorageInMemoryMetadata & metadata, const Context & con
if (metadata.table_ttl.definition_ast) if (metadata.table_ttl.definition_ast)
rename_visitor.visit(metadata.table_ttl.definition_ast); rename_visitor.visit(metadata.table_ttl.definition_ast);
for (auto & constraint : metadata.constraints.constraints) auto constraints_data = metadata.constraints.getConstraints();
for (auto & constraint : constraints_data)
rename_visitor.visit(constraint); rename_visitor.visit(constraint);
metadata.constraints.updateConstraints(constraints_data);
if (metadata.isSortingKeyDefined()) if (metadata.isSortingKeyDefined())
rename_visitor.visit(metadata.sorting_key.definition_ast); rename_visitor.visit(metadata.sorting_key.definition_ast);

View File

@ -66,7 +66,7 @@ ASTs ConstraintsDescription::filterConstraints(ConstraintType selection) const
return res; return res;
} }
std::vector<std::vector<CNFQuery::AtomicFormula>> ConstraintsDescription::getConstraintData() const std::vector<std::vector<CNFQuery::AtomicFormula>> ConstraintsDescription::buildConstraintData() const
{ {
std::vector<std::vector<CNFQuery::AtomicFormula>> constraint_data; std::vector<std::vector<CNFQuery::AtomicFormula>> constraint_data;
for (const auto & constraint : filterConstraints(ConstraintsDescription::ConstraintType::ALWAYS_TRUE)) for (const auto & constraint : filterConstraints(ConstraintsDescription::ConstraintType::ALWAYS_TRUE))
@ -97,7 +97,7 @@ std::vector<CNFQuery::AtomicFormula> ConstraintsDescription::getAtomicConstraint
return constraint_data; return constraint_data;
} }
ComparisonGraph ConstraintsDescription::getGraph() const std::unique_ptr<ComparisonGraph> ConstraintsDescription::buildGraph() const
{ {
static const std::set<std::string> relations = { static const std::set<std::string> relations = {
"equals", "less", "lessOrEquals", "greaterOrEquals", "greater"}; "equals", "less", "lessOrEquals", "greaterOrEquals", "greater"};
@ -118,7 +118,7 @@ ComparisonGraph ConstraintsDescription::getGraph() const
} }
} }
return ComparisonGraph(constraints_for_graph); return std::make_unique<ComparisonGraph>(constraints_for_graph);
} }
ConstraintsExpressions ConstraintsDescription::getExpressionsToCheck(const DB::Context & context, ConstraintsExpressions ConstraintsDescription::getExpressionsToCheck(const DB::Context & context,
@ -140,11 +140,33 @@ ConstraintsExpressions ConstraintsDescription::getExpressionsToCheck(const DB::C
return res; return res;
} }
const ComparisonGraph & ConstraintsDescription::getGraph() const
{
return *graph;
}
const std::vector<std::vector<CNFQuery::AtomicFormula>> & ConstraintsDescription::getConstraintData() const
{
return cnf_constraints;
}
const std::vector<ASTPtr> & ConstraintsDescription::getConstraints() const
{
return constraints;
}
void ConstraintsDescription::updateConstraints(const std::vector<ASTPtr> & constraints_)
{
constraints = constraints_;
update();
}
ConstraintsDescription::ConstraintsDescription(const ConstraintsDescription & other) ConstraintsDescription::ConstraintsDescription(const ConstraintsDescription & other)
{ {
constraints.reserve(other.constraints.size()); constraints.reserve(other.constraints.size());
for (const auto & constraint : other.constraints) for (const auto & constraint : other.constraints)
constraints.emplace_back(constraint->clone()); constraints.emplace_back(constraint->clone());
update();
} }
ConstraintsDescription & ConstraintsDescription::operator=(const ConstraintsDescription & other) ConstraintsDescription & ConstraintsDescription::operator=(const ConstraintsDescription & other)
@ -152,7 +174,14 @@ ConstraintsDescription & ConstraintsDescription::operator=(const ConstraintsDesc
constraints.resize(other.constraints.size()); constraints.resize(other.constraints.size());
for (size_t i = 0; i < constraints.size(); ++i) for (size_t i = 0; i < constraints.size(); ++i)
constraints[i] = other.constraints[i]->clone(); constraints[i] = other.constraints[i]->clone();
update();
return *this; return *this;
} }
void ConstraintsDescription::update()
{
cnf_constraints = buildConstraintData();
graph = buildGraph();
}
} }

View File

@ -12,11 +12,8 @@ using ConstraintsExpressions = std::vector<ExpressionActionsPtr>;
struct ConstraintsDescription struct ConstraintsDescription
{ {
std::vector<ASTPtr> constraints; public:
//std::vector<CNFQuery> cnf_constraints; ConstraintsDescription() { update(); }
// TODO: перенести преобразование в КНФ + get constraitns
ConstraintsDescription() = default;
bool empty() const { return constraints.empty(); } bool empty() const { return constraints.empty(); }
String toString() const; String toString() const;
@ -32,15 +29,27 @@ struct ConstraintsDescription
ASTs filterConstraints(ConstraintType selection) const; ASTs filterConstraints(ConstraintType selection) const;
std::vector<std::vector<CNFQuery::AtomicFormula>> getConstraintData() const; const std::vector<ASTPtr> & getConstraints() const;
void updateConstraints(const std::vector<ASTPtr> & constraints);
const std::vector<std::vector<CNFQuery::AtomicFormula>> & getConstraintData() const;
std::vector<CNFQuery::AtomicFormula> getAtomicConstraintData() const; std::vector<CNFQuery::AtomicFormula> getAtomicConstraintData() const;
ComparisonGraph getGraph() const; const ComparisonGraph & getGraph() const;
ConstraintsExpressions getExpressionsToCheck(const Context & context, const NamesAndTypesList & source_columns_) const; ConstraintsExpressions getExpressionsToCheck(const Context & context, const NamesAndTypesList & source_columns_) const;
ConstraintsDescription(const ConstraintsDescription & other); ConstraintsDescription(const ConstraintsDescription & other);
ConstraintsDescription & operator=(const ConstraintsDescription & other); ConstraintsDescription & operator=(const ConstraintsDescription & other);
private:
std::vector<std::vector<CNFQuery::AtomicFormula>> buildConstraintData() const;
std::unique_ptr<ComparisonGraph> buildGraph() const;
void update();
std::vector<ASTPtr> constraints;
std::vector<std::vector<CNFQuery::AtomicFormula>> cnf_constraints;
std::unique_ptr<ComparisonGraph> graph;
}; };
} }

View File

@ -658,9 +658,11 @@ static StoragePtr create(const StorageFactory::Arguments & args)
for (auto & index : args.query.columns_list->indices->children) for (auto & index : args.query.columns_list->indices->children)
metadata.secondary_indices.push_back(IndexDescription::getIndexFromAST(index, args.columns, args.context)); metadata.secondary_indices.push_back(IndexDescription::getIndexFromAST(index, args.columns, args.context));
auto constraints = metadata.constraints.getConstraints();
if (args.query.columns_list && args.query.columns_list->constraints) if (args.query.columns_list && args.query.columns_list->constraints)
for (auto & constraint : args.query.columns_list->constraints->children) for (auto & constraint : args.query.columns_list->constraints->children)
metadata.constraints.constraints.push_back(constraint); constraints.push_back(constraint);
metadata.constraints.updateConstraints(constraints);
auto column_ttl_asts = args.columns.getColumnTTLs(); auto column_ttl_asts = args.columns.getColumnTTLs();
for (const auto & [name, ast] : column_ttl_asts) for (const auto & [name, ast] : column_ttl_asts)