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>();
for (const auto & constraint : constraints.constraints)
for (const auto & constraint : constraints.getConstraints())
res->children.push_back(constraint->clone());
return res;
@ -489,9 +489,11 @@ ColumnsDescription InterpreterCreateQuery::getColumnsDescription(
ConstraintsDescription InterpreterCreateQuery::getConstraintsDescription(const ASTExpressionList * constraints)
{
ConstraintsDescription res;
auto constraints_data = res.getConstraints();
if (constraints)
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;
}

View File

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

View File

@ -66,7 +66,7 @@ ASTs ConstraintsDescription::filterConstraints(ConstraintType selection) const
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;
for (const auto & constraint : filterConstraints(ConstraintsDescription::ConstraintType::ALWAYS_TRUE))
@ -97,7 +97,7 @@ std::vector<CNFQuery::AtomicFormula> ConstraintsDescription::getAtomicConstraint
return constraint_data;
}
ComparisonGraph ConstraintsDescription::getGraph() const
std::unique_ptr<ComparisonGraph> ConstraintsDescription::buildGraph() const
{
static const std::set<std::string> relations = {
"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,
@ -140,11 +140,33 @@ ConstraintsExpressions ConstraintsDescription::getExpressionsToCheck(const DB::C
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)
{
constraints.reserve(other.constraints.size());
for (const auto & constraint : other.constraints)
constraints.emplace_back(constraint->clone());
update();
}
ConstraintsDescription & ConstraintsDescription::operator=(const ConstraintsDescription & other)
@ -152,7 +174,14 @@ ConstraintsDescription & ConstraintsDescription::operator=(const ConstraintsDesc
constraints.resize(other.constraints.size());
for (size_t i = 0; i < constraints.size(); ++i)
constraints[i] = other.constraints[i]->clone();
update();
return *this;
}
void ConstraintsDescription::update()
{
cnf_constraints = buildConstraintData();
graph = buildGraph();
}
}

View File

@ -12,11 +12,8 @@ using ConstraintsExpressions = std::vector<ExpressionActionsPtr>;
struct ConstraintsDescription
{
std::vector<ASTPtr> constraints;
//std::vector<CNFQuery> cnf_constraints;
// TODO: перенести преобразование в КНФ + get constraitns
ConstraintsDescription() = default;
public:
ConstraintsDescription() { update(); }
bool empty() const { return constraints.empty(); }
String toString() const;
@ -32,15 +29,27 @@ struct ConstraintsDescription
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;
ComparisonGraph getGraph() const;
const ComparisonGraph & getGraph() const;
ConstraintsExpressions getExpressionsToCheck(const Context & context, const NamesAndTypesList & source_columns_) const;
ConstraintsDescription(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)
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)
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();
for (const auto & [name, ast] : column_ttl_asts)