ClickHouse/src/Storages/ConstraintsDescription.h

94 lines
2.9 KiB
C++
Raw Normal View History

#pragma once
#include <Parsers/ASTConstraintDeclaration.h>
#include <Interpreters/ExpressionActions.h>
2021-03-04 12:11:43 +00:00
#include <Interpreters/TreeCNFConverter.h>
2021-04-03 12:12:45 +00:00
#include <Interpreters/ComparisonGraph.h>
#include <Analyzer/Passes/CNF.h>
namespace DB
{
2019-05-19 05:27:00 +00:00
using ConstraintsExpressions = std::vector<ExpressionActionsPtr>;
struct ConstraintsDescription
{
2021-04-26 14:19:18 +00:00
public:
ConstraintsDescription() { update(); }
explicit ConstraintsDescription(const ASTs & constraints_);
2021-05-05 11:17:49 +00:00
ConstraintsDescription(const ConstraintsDescription & other);
ConstraintsDescription & operator=(const ConstraintsDescription & other);
ConstraintsDescription(ConstraintsDescription && other) noexcept;
ConstraintsDescription & operator=(ConstraintsDescription && other) noexcept;
bool empty() const { return constraints.empty(); }
String toString() const;
static ConstraintsDescription parse(const String & str);
2019-05-19 05:27:00 +00:00
2021-11-10 17:57:59 +00:00
enum class ConstraintType : UInt8
2021-05-04 18:43:58 +00:00
{
2021-01-04 20:55:32 +00:00
CHECK = 1,
ASSUME = 2,
ALWAYS_TRUE = CHECK | ASSUME,
ALL = CHECK | ASSUME,
};
ASTs filterConstraints(ConstraintType selection) const;
2021-04-03 12:12:45 +00:00
2021-11-10 17:57:59 +00:00
const ASTs & getConstraints() const;
2021-04-26 14:19:18 +00:00
const std::vector<std::vector<CNFQuery::AtomicFormula>> & getConstraintData() const;
2021-04-03 12:12:45 +00:00
std::vector<CNFQuery::AtomicFormula> getAtomicConstraintData() const;
2023-03-17 13:38:01 +00:00
const ComparisonGraph<ASTPtr> & getGraph() const;
2021-01-03 15:02:00 +00:00
ConstraintsExpressions getExpressions(ContextPtr context, const NamesAndTypesList & source_columns_) const;
2020-06-05 17:29:40 +00:00
2021-05-05 11:17:49 +00:00
struct AtomId
{
2021-11-10 17:57:59 +00:00
size_t group_id;
size_t atom_id;
2021-05-05 11:17:49 +00:00
};
using AtomIds = std::vector<AtomId>;
std::optional<AtomIds> getAtomIds(const ASTPtr & ast) const;
std::vector<CNFQuery::AtomicFormula> getAtomsById(const AtomIds & ids) const;
2021-04-26 14:19:18 +00:00
class QueryTreeData
{
public:
const QueryTreeNodes & getConstraints() const;
const std::vector<std::vector<Analyzer::CNF::AtomicFormula>> & getConstraintData() const;
std::optional<AtomIds> getAtomIds(const QueryTreeNodePtrWithHash & node_with_hash) const;
std::vector<Analyzer::CNF::AtomicFormula> getAtomsById(const AtomIds & ids) const;
const ComparisonGraph<QueryTreeNodePtr> & getGraph() const;
private:
QueryTreeNodes constraints;
std::vector<std::vector<Analyzer::CNF::AtomicFormula>> cnf_constraints;
QueryTreeNodePtrWithHashMap<AtomIds> query_node_to_atom_ids;
std::unique_ptr<ComparisonGraph<QueryTreeNodePtr>> graph;
friend ConstraintsDescription;
};
2023-03-16 14:57:07 +00:00
QueryTreeData getQueryTreeData(const ContextPtr & context, const QueryTreeNodePtr & table_node) const;
2021-04-26 14:19:18 +00:00
private:
std::vector<std::vector<CNFQuery::AtomicFormula>> buildConstraintData() const;
2023-03-17 13:38:01 +00:00
std::unique_ptr<ComparisonGraph<ASTPtr>> buildGraph() const;
2021-04-26 14:19:18 +00:00
void update();
2021-11-10 17:57:59 +00:00
ASTs constraints;
2021-04-26 14:19:18 +00:00
std::vector<std::vector<CNFQuery::AtomicFormula>> cnf_constraints;
2021-05-05 11:17:49 +00:00
std::map<IAST::Hash, AtomIds> ast_to_atom_ids;
2023-03-17 13:38:01 +00:00
std::unique_ptr<ComparisonGraph<ASTPtr>> graph;
};
}