2021-03-04 12:11:43 +00:00
|
|
|
|
#include <Interpreters/ComparisonGraph.h>
|
2021-03-05 09:54:13 +00:00
|
|
|
|
#include <Parsers/IAST.h>
|
2021-03-05 12:46:42 +00:00
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2021-03-05 09:54:13 +00:00
|
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-11-10 17:57:59 +00:00
|
|
|
|
#include <Parsers/queryToString.h>
|
|
|
|
|
#include <Common/FieldVisitorsAccurateComparison.h>
|
2021-03-04 12:11:43 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int VIOLATED_CONSTRAINT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace
|
2021-03-04 12:11:43 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
/// Make function a > b or a >= b
|
|
|
|
|
ASTPtr normalizeAtom(const ASTPtr & atom)
|
|
|
|
|
{
|
|
|
|
|
static const std::map<std::string, std::string> inverse_relations =
|
|
|
|
|
{
|
|
|
|
|
{"lessOrEquals", "greaterOrEquals"},
|
|
|
|
|
{"less", "greater"},
|
2021-03-05 09:54:13 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ASTPtr res = atom->clone();
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (const auto * func = res->as<ASTFunction>())
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (const auto it = inverse_relations.find(func->name); it != std::end(inverse_relations))
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
res = makeASTFunction(it->second, func->arguments->children[1]->clone(), func->arguments->children[0]->clone());
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
bool less(const Field & lhs, const Field & rhs) { return applyVisitor(FieldVisitorAccurateLess{}, lhs, rhs); }
|
|
|
|
|
bool greater(const Field & lhs, const Field & rhs) { return applyVisitor(FieldVisitorAccurateLess{}, rhs, lhs); }
|
|
|
|
|
bool equals(const Field & lhs, const Field & rhs) { return applyVisitor(FieldVisitorAccurateEquals{}, lhs, rhs); }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ComparisonGraph::ComparisonGraph(const ASTs & atomic_formulas)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-05-07 10:49:05 +00:00
|
|
|
|
if (atomic_formulas.empty())
|
|
|
|
|
return;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-05-10 10:27:47 +00:00
|
|
|
|
static const std::unordered_map<std::string, Edge::Type> relation_to_enum =
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
{"equals", Edge::EQUAL},
|
|
|
|
|
{"greater", Edge::GREATER},
|
|
|
|
|
{"greaterOrEquals", Edge::GREATER_OR_EQUAL},
|
2021-03-05 09:54:13 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// Firstly build an intermediate graph,
|
|
|
|
|
/// in which each vertex corresponds to one expression.
|
|
|
|
|
/// That means that if we have edge (A, B) with type GREATER, then always A > B.
|
|
|
|
|
/// If we have EQUAL relation, then we add both edges (A, B) and (B, A).
|
|
|
|
|
|
2021-03-05 09:54:13 +00:00
|
|
|
|
Graph g;
|
2021-05-04 18:43:58 +00:00
|
|
|
|
for (const auto & atom_raw : atomic_formulas)
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
const auto atom = normalizeAtom(atom_raw);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
auto get_index = [](const ASTPtr & ast, Graph & asts_graph) -> std::optional<size_t>
|
2021-05-10 10:27:47 +00:00
|
|
|
|
{
|
2021-03-05 09:54:13 +00:00
|
|
|
|
const auto it = asts_graph.ast_hash_to_component.find(ast->getTreeHash());
|
|
|
|
|
if (it != std::end(asts_graph.ast_hash_to_component))
|
|
|
|
|
{
|
|
|
|
|
if (!std::any_of(
|
2021-05-04 18:43:58 +00:00
|
|
|
|
std::cbegin(asts_graph.vertices[it->second].asts),
|
|
|
|
|
std::cend(asts_graph.vertices[it->second].asts),
|
2021-05-10 10:27:47 +00:00
|
|
|
|
[ast](const ASTPtr & constraint_ast)
|
|
|
|
|
{
|
2021-03-05 09:54:13 +00:00
|
|
|
|
return constraint_ast->getTreeHash() == ast->getTreeHash()
|
|
|
|
|
&& constraint_ast->getColumnName() == ast->getColumnName();
|
|
|
|
|
}))
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
return {};
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
asts_graph.ast_hash_to_component[ast->getTreeHash()] = asts_graph.vertices.size();
|
2021-11-10 17:57:59 +00:00
|
|
|
|
asts_graph.vertices.push_back(EqualComponent{{ast}, std::nullopt});
|
2021-03-05 09:54:13 +00:00
|
|
|
|
asts_graph.edges.emplace_back();
|
2021-05-04 18:43:58 +00:00
|
|
|
|
return asts_graph.vertices.size() - 1;
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto * func = atom->as<ASTFunction>();
|
2021-05-08 09:19:18 +00:00
|
|
|
|
if (func && func->arguments->children.size() == 2)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
auto index_left = get_index(func->arguments->children[0], g);
|
|
|
|
|
auto index_right = get_index(func->arguments->children[1], g);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (index_left && index_right)
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (const auto it = relation_to_enum.find(func->name); it != std::end(relation_to_enum))
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
g.edges[*index_left].push_back(Edge{it->second, *index_right});
|
|
|
|
|
if (it->second == Edge::EQUAL)
|
|
|
|
|
g.edges[*index_right].push_back(Edge{it->second, *index_left});
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// Now expressions A and B are equal, if and only if
|
|
|
|
|
/// we have both paths from A to B and from B to A in graph.
|
|
|
|
|
/// That means that equivalence classes of expressions
|
|
|
|
|
/// are the same as strongly connected components in graph.
|
|
|
|
|
/// So, we find such components and build graph on them.
|
|
|
|
|
/// All expressions from one equivalence class will be stored
|
|
|
|
|
/// in the corresponding vertex of new graph.
|
|
|
|
|
|
|
|
|
|
graph = buildGraphFromAstsGraph(g);
|
|
|
|
|
dists = buildDistsFromGraph(graph);
|
2021-04-10 20:46:53 +00:00
|
|
|
|
std::tie(ast_const_lower_bound, ast_const_upper_bound) = buildConstBounds();
|
2021-03-05 09:54:13 +00:00
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// Find expressions that are known to be unequal.
|
|
|
|
|
static const std::unordered_set<String> not_equals_functions = {"notEquals", "greater"};
|
|
|
|
|
|
|
|
|
|
/// Explicitly save unequal components.
|
|
|
|
|
/// TODO: Build a graph for unequal components.
|
2021-05-08 09:19:18 +00:00
|
|
|
|
for (const auto & atom_raw : atomic_formulas)
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
const auto atom = normalizeAtom(atom_raw);
|
|
|
|
|
const auto * func = atom->as<ASTFunction>();
|
|
|
|
|
|
|
|
|
|
if (func && not_equals_functions.contains(func->name))
|
2021-05-08 09:19:18 +00:00
|
|
|
|
{
|
|
|
|
|
auto index_left = graph.ast_hash_to_component.at(func->arguments->children[0]->getTreeHash());
|
|
|
|
|
auto index_right = graph.ast_hash_to_component.at(func->arguments->children[1]->getTreeHash());
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
if (index_left == index_right)
|
|
|
|
|
throw Exception(ErrorCodes::VIOLATED_CONSTRAINT,
|
|
|
|
|
"Found expression '{}', but its arguments considered equal according to constraints",
|
|
|
|
|
queryToString(atom));
|
|
|
|
|
|
2021-05-08 09:19:18 +00:00
|
|
|
|
not_equal.emplace(index_left, index_right);
|
|
|
|
|
not_equal.emplace(index_right, index_left);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
ComparisonGraph::CompareResult ComparisonGraph::pathToCompareResult(Path path, bool inverse)
|
2021-03-05 12:13:00 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
switch (path)
|
2021-04-10 20:46:53 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
case Path::GREATER: return inverse ? CompareResult::LESS : CompareResult::GREATER;
|
|
|
|
|
case Path::GREATER_OR_EQUAL: return inverse ? CompareResult::LESS_OR_EQUAL : CompareResult::GREATER_OR_EQUAL;
|
2021-04-10 20:46:53 +00:00
|
|
|
|
}
|
2021-11-10 17:57:59 +00:00
|
|
|
|
__builtin_unreachable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<ComparisonGraph::Path> ComparisonGraph::findPath(const size_t start, const size_t finish) const
|
|
|
|
|
{
|
|
|
|
|
const auto it = dists.find(std::make_pair(start, finish));
|
|
|
|
|
if (it == std::end(dists))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
/// Since path can be only GREATER or GREATER_OR_EQUALS,
|
|
|
|
|
/// we can strengthen the condition.
|
|
|
|
|
return not_equal.contains({start, finish}) ? Path::GREATER : it->second;
|
2021-03-05 12:13:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 09:54:13 +00:00
|
|
|
|
ComparisonGraph::CompareResult ComparisonGraph::compare(const ASTPtr & left, const ASTPtr & right) const
|
|
|
|
|
{
|
|
|
|
|
size_t start = 0;
|
|
|
|
|
size_t finish = 0;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
/// TODO: check full ast
|
|
|
|
|
const auto it_left = graph.ast_hash_to_component.find(left->getTreeHash());
|
|
|
|
|
const auto it_right = graph.ast_hash_to_component.find(right->getTreeHash());
|
|
|
|
|
|
|
|
|
|
if (it_left == std::end(graph.ast_hash_to_component) || it_right == std::end(graph.ast_hash_to_component))
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
CompareResult result = CompareResult::UNKNOWN;
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
const auto left_bound = getConstLowerBound(left);
|
|
|
|
|
const auto right_bound = getConstUpperBound(right);
|
|
|
|
|
|
|
|
|
|
if (left_bound && right_bound)
|
2021-04-10 20:46:53 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (greater(left_bound->first, right_bound->first))
|
|
|
|
|
result = CompareResult::GREATER;
|
|
|
|
|
else if (equals(left_bound->first, right_bound->first))
|
|
|
|
|
result = left_bound->second || right_bound->second
|
|
|
|
|
? CompareResult::GREATER : CompareResult::GREATER_OR_EQUAL;
|
2021-04-10 20:46:53 +00:00
|
|
|
|
}
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
const auto left_bound = getConstUpperBound(left);
|
|
|
|
|
const auto right_bound = getConstLowerBound(right);
|
|
|
|
|
|
|
|
|
|
if (left_bound && right_bound)
|
|
|
|
|
{
|
|
|
|
|
if (less(left_bound->first, right_bound->first))
|
|
|
|
|
result = CompareResult::LESS;
|
|
|
|
|
else if (equals(left_bound->first, right_bound->first))
|
|
|
|
|
result = left_bound->second || right_bound->second
|
|
|
|
|
? CompareResult::LESS : CompareResult::LESS_OR_EQUAL;
|
|
|
|
|
}
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start = it_left->second;
|
|
|
|
|
finish = it_right->second;
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (start == finish)
|
|
|
|
|
return CompareResult::EQUAL;
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (auto path = findPath(start, finish))
|
|
|
|
|
return pathToCompareResult(*path, /*inverse=*/ false);
|
2021-03-05 12:13:00 +00:00
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (auto path = findPath(finish, start))
|
|
|
|
|
return pathToCompareResult(*path, /*inverse=*/ true);
|
2021-03-05 12:13:00 +00:00
|
|
|
|
|
2021-05-04 10:47:23 +00:00
|
|
|
|
if (not_equal.contains({start, finish}))
|
|
|
|
|
return CompareResult::NOT_EQUAL;
|
|
|
|
|
|
2021-03-05 09:54:13 +00:00
|
|
|
|
return CompareResult::UNKNOWN;
|
2021-03-04 12:11:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 19:16:40 +00:00
|
|
|
|
bool ComparisonGraph::isPossibleCompare(const CompareResult expected, const ASTPtr & left, const ASTPtr & right) const
|
|
|
|
|
{
|
|
|
|
|
const auto result = compare(left, right);
|
|
|
|
|
|
|
|
|
|
if (expected == CompareResult::UNKNOWN || result == CompareResult::UNKNOWN)
|
|
|
|
|
return true;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-05-02 19:16:40 +00:00
|
|
|
|
if (expected == result)
|
|
|
|
|
return true;
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
static const std::set<std::pair<CompareResult, CompareResult>> possible_pairs =
|
|
|
|
|
{
|
2021-05-02 19:16:40 +00:00
|
|
|
|
{CompareResult::EQUAL, CompareResult::LESS_OR_EQUAL},
|
|
|
|
|
{CompareResult::EQUAL, CompareResult::GREATER_OR_EQUAL},
|
|
|
|
|
{CompareResult::LESS_OR_EQUAL, CompareResult::LESS},
|
|
|
|
|
{CompareResult::LESS_OR_EQUAL, CompareResult::EQUAL},
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{CompareResult::LESS_OR_EQUAL, CompareResult::NOT_EQUAL},
|
2021-05-02 19:16:40 +00:00
|
|
|
|
{CompareResult::GREATER_OR_EQUAL, CompareResult::GREATER},
|
|
|
|
|
{CompareResult::GREATER_OR_EQUAL, CompareResult::EQUAL},
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{CompareResult::GREATER_OR_EQUAL, CompareResult::NOT_EQUAL},
|
2021-05-02 19:16:40 +00:00
|
|
|
|
{CompareResult::LESS, CompareResult::LESS},
|
|
|
|
|
{CompareResult::LESS, CompareResult::LESS_OR_EQUAL},
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{CompareResult::LESS, CompareResult::NOT_EQUAL},
|
2021-05-02 19:16:40 +00:00
|
|
|
|
{CompareResult::GREATER, CompareResult::GREATER},
|
|
|
|
|
{CompareResult::GREATER, CompareResult::GREATER_OR_EQUAL},
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{CompareResult::GREATER, CompareResult::NOT_EQUAL},
|
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::LESS},
|
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::GREATER},
|
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::LESS_OR_EQUAL},
|
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::GREATER_OR_EQUAL},
|
2021-05-02 19:16:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return possible_pairs.contains({expected, result});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ComparisonGraph::isAlwaysCompare(const CompareResult expected, const ASTPtr & left, const ASTPtr & right) const
|
|
|
|
|
{
|
|
|
|
|
const auto result = compare(left, right);
|
|
|
|
|
|
|
|
|
|
if (expected == CompareResult::UNKNOWN || result == CompareResult::UNKNOWN)
|
|
|
|
|
return false;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-05-02 19:16:40 +00:00
|
|
|
|
if (expected == result)
|
|
|
|
|
return true;
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
static const std::set<std::pair<CompareResult, CompareResult>> possible_pairs =
|
|
|
|
|
{
|
2021-05-02 19:16:40 +00:00
|
|
|
|
{CompareResult::LESS_OR_EQUAL, CompareResult::LESS},
|
|
|
|
|
{CompareResult::LESS_OR_EQUAL, CompareResult::EQUAL},
|
|
|
|
|
{CompareResult::GREATER_OR_EQUAL, CompareResult::GREATER},
|
|
|
|
|
{CompareResult::GREATER_OR_EQUAL, CompareResult::EQUAL},
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::GREATER},
|
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::LESS},
|
2021-05-02 19:16:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return possible_pairs.contains({expected, result});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
ASTs ComparisonGraph::getEqual(const ASTPtr & ast) const
|
2021-04-26 11:26:54 +00:00
|
|
|
|
{
|
|
|
|
|
const auto res = getComponentId(ast);
|
|
|
|
|
if (!res)
|
|
|
|
|
return {};
|
|
|
|
|
else
|
|
|
|
|
return getComponent(res.value());
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
std::optional<size_t> ComparisonGraph::getComponentId(const ASTPtr & ast) const
|
2021-03-04 12:11:43 +00:00
|
|
|
|
{
|
2021-03-05 09:54:13 +00:00
|
|
|
|
const auto hash_it = graph.ast_hash_to_component.find(ast->getTreeHash());
|
2021-03-05 12:46:42 +00:00
|
|
|
|
if (hash_it == std::end(graph.ast_hash_to_component))
|
2021-03-04 12:11:43 +00:00
|
|
|
|
return {};
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-03-04 12:11:43 +00:00
|
|
|
|
const size_t index = hash_it->second;
|
|
|
|
|
if (std::any_of(
|
2021-05-04 18:43:58 +00:00
|
|
|
|
std::cbegin(graph.vertices[index].asts),
|
|
|
|
|
std::cend(graph.vertices[index].asts),
|
2021-04-26 11:26:54 +00:00
|
|
|
|
[ast](const ASTPtr & constraint_ast)
|
|
|
|
|
{
|
|
|
|
|
return constraint_ast->getTreeHash() == ast->getTreeHash() &&
|
|
|
|
|
constraint_ast->getColumnName() == ast->getColumnName();
|
2021-05-04 18:43:58 +00:00
|
|
|
|
}))
|
|
|
|
|
{
|
2021-04-26 11:26:54 +00:00
|
|
|
|
return index;
|
2021-05-04 18:43:58 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-04 12:11:43 +00:00
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 19:08:26 +00:00
|
|
|
|
bool ComparisonGraph::hasPath(const size_t left, const size_t right) const
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
return findPath(left, right) || findPath(right, left);
|
2021-05-03 19:08:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
ASTs ComparisonGraph::getComponent(const size_t id) const
|
2021-04-26 11:26:54 +00:00
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
return graph.vertices[id].asts;
|
2021-04-26 11:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
bool ComparisonGraph::EqualComponent::hasConstant() const
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
return constant_index.has_value();
|
2021-04-10 15:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
ASTPtr ComparisonGraph::EqualComponent::getConstant() const
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
assert(constant_index);
|
|
|
|
|
return asts[*constant_index];
|
2021-04-10 15:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
void ComparisonGraph::EqualComponent::buildConstants()
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
constant_index.reset();
|
2021-04-10 15:47:50 +00:00
|
|
|
|
for (size_t i = 0; i < asts.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if (asts[i]->as<ASTLiteral>())
|
|
|
|
|
{
|
|
|
|
|
constant_index = i;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
ComparisonGraph::CompareResult ComparisonGraph::atomToCompareResult(const CNFQuery::AtomicFormula & atom)
|
|
|
|
|
{
|
|
|
|
|
if (const auto * func = atom.ast->as<ASTFunction>())
|
|
|
|
|
{
|
|
|
|
|
auto expected = functionNameToCompareResult(func->name);
|
|
|
|
|
if (atom.negative)
|
|
|
|
|
expected = inverseCompareResult(expected);
|
|
|
|
|
return expected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ComparisonGraph::CompareResult::UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ComparisonGraph::CompareResult ComparisonGraph::functionNameToCompareResult(const std::string & name)
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
static const std::unordered_map<std::string, CompareResult> relation_to_compare =
|
|
|
|
|
{
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{"equals", CompareResult::EQUAL},
|
|
|
|
|
{"notEquals", CompareResult::NOT_EQUAL},
|
|
|
|
|
{"less", CompareResult::LESS},
|
|
|
|
|
{"lessOrEquals", CompareResult::LESS_OR_EQUAL},
|
|
|
|
|
{"greaterOrEquals", CompareResult::GREATER_OR_EQUAL},
|
|
|
|
|
{"greater", CompareResult::GREATER},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto it = relation_to_compare.find(name);
|
|
|
|
|
return it == std::end(relation_to_compare) ? CompareResult::UNKNOWN : it->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ComparisonGraph::CompareResult ComparisonGraph::inverseCompareResult(const CompareResult result)
|
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
static const std::unordered_map<CompareResult, CompareResult> inverse_relations =
|
|
|
|
|
{
|
2021-05-04 10:47:23 +00:00
|
|
|
|
{CompareResult::NOT_EQUAL, CompareResult::EQUAL},
|
|
|
|
|
{CompareResult::EQUAL, CompareResult::NOT_EQUAL},
|
|
|
|
|
{CompareResult::GREATER_OR_EQUAL, CompareResult::LESS},
|
|
|
|
|
{CompareResult::GREATER, CompareResult::LESS_OR_EQUAL},
|
|
|
|
|
{CompareResult::LESS, CompareResult::GREATER_OR_EQUAL},
|
|
|
|
|
{CompareResult::LESS_OR_EQUAL, CompareResult::GREATER},
|
|
|
|
|
{CompareResult::UNKNOWN, CompareResult::UNKNOWN},
|
|
|
|
|
};
|
|
|
|
|
return inverse_relations.at(result);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 12:46:42 +00:00
|
|
|
|
std::optional<ASTPtr> ComparisonGraph::getEqualConst(const ASTPtr & ast) const
|
|
|
|
|
{
|
|
|
|
|
const auto hash_it = graph.ast_hash_to_component.find(ast->getTreeHash());
|
|
|
|
|
if (hash_it == std::end(graph.ast_hash_to_component))
|
|
|
|
|
return std::nullopt;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-03-05 12:46:42 +00:00
|
|
|
|
const size_t index = hash_it->second;
|
2021-05-04 18:43:58 +00:00
|
|
|
|
return graph.vertices[index].hasConstant()
|
|
|
|
|
? std::optional<ASTPtr>{graph.vertices[index].getConstant()}
|
2021-04-10 15:47:50 +00:00
|
|
|
|
: std::nullopt;
|
2021-03-05 12:46:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 20:46:53 +00:00
|
|
|
|
std::optional<std::pair<Field, bool>> ComparisonGraph::getConstUpperBound(const ASTPtr & ast) const
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (const auto * literal = ast->as<ASTLiteral>())
|
|
|
|
|
return std::make_pair(literal->value, false);
|
|
|
|
|
|
2021-04-10 20:46:53 +00:00
|
|
|
|
const auto it = graph.ast_hash_to_component.find(ast->getTreeHash());
|
|
|
|
|
if (it == std::end(graph.ast_hash_to_component))
|
|
|
|
|
return std::nullopt;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-04-10 20:46:53 +00:00
|
|
|
|
const size_t to = it->second;
|
|
|
|
|
const ssize_t from = ast_const_upper_bound[to];
|
|
|
|
|
if (from == -1)
|
|
|
|
|
return std::nullopt;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
return std::make_pair(graph.vertices[from].getConstant()->as<ASTLiteral>()->value, dists.at({from, to}) == Path::GREATER);
|
2021-04-10 20:46:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<std::pair<Field, bool>> ComparisonGraph::getConstLowerBound(const ASTPtr & ast) const
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (const auto * literal = ast->as<ASTLiteral>())
|
|
|
|
|
return std::make_pair(literal->value, false);
|
|
|
|
|
|
2021-04-10 20:46:53 +00:00
|
|
|
|
const auto it = graph.ast_hash_to_component.find(ast->getTreeHash());
|
|
|
|
|
if (it == std::end(graph.ast_hash_to_component))
|
|
|
|
|
return std::nullopt;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-04-10 20:46:53 +00:00
|
|
|
|
const size_t from = it->second;
|
|
|
|
|
const ssize_t to = ast_const_lower_bound[from];
|
|
|
|
|
if (to == -1)
|
2021-11-10 17:57:59 +00:00
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
|
|
return std::make_pair(graph.vertices[to].getConstant()->as<ASTLiteral>()->value, dists.at({from, to}) == Path::GREATER);
|
2021-04-10 20:46:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 08:29:24 +00:00
|
|
|
|
void ComparisonGraph::dfsOrder(const Graph & asts_graph, size_t v, std::vector<bool> & visited, std::vector<size_t> & order)
|
2021-03-04 12:11:43 +00:00
|
|
|
|
{
|
2021-03-05 09:54:13 +00:00
|
|
|
|
visited[v] = true;
|
|
|
|
|
for (const auto & edge : asts_graph.edges[v])
|
|
|
|
|
if (!visited[edge.to])
|
|
|
|
|
dfsOrder(asts_graph, edge.to, visited, order);
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-03-05 09:54:13 +00:00
|
|
|
|
order.push_back(v);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 08:29:24 +00:00
|
|
|
|
ComparisonGraph::Graph ComparisonGraph::reverseGraph(const Graph & asts_graph)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
|
|
|
|
Graph g;
|
|
|
|
|
g.ast_hash_to_component = asts_graph.ast_hash_to_component;
|
2021-05-04 18:43:58 +00:00
|
|
|
|
g.vertices = asts_graph.vertices;
|
|
|
|
|
g.edges.resize(g.vertices.size());
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
for (size_t v = 0; v < asts_graph.vertices.size(); ++v)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
for (const auto & edge : asts_graph.edges[v])
|
|
|
|
|
g.edges[edge.to].push_back(Edge{edge.type, v});
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
|
|
|
|
return g;
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
std::vector<ASTs> ComparisonGraph::getVertices() const
|
2021-04-28 17:35:51 +00:00
|
|
|
|
{
|
|
|
|
|
std::vector<ASTs> result;
|
2021-05-04 18:43:58 +00:00
|
|
|
|
for (const auto & vertex : graph.vertices)
|
2021-04-28 17:35:51 +00:00
|
|
|
|
{
|
|
|
|
|
result.emplace_back();
|
|
|
|
|
for (const auto & ast : vertex.asts)
|
|
|
|
|
result.back().push_back(ast);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 09:54:13 +00:00
|
|
|
|
void ComparisonGraph::dfsComponents(
|
2021-11-10 17:57:59 +00:00
|
|
|
|
const Graph & reversed_graph, size_t v,
|
|
|
|
|
OptionalIndices & components, const size_t component)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
|
|
|
|
components[v] = component;
|
|
|
|
|
for (const auto & edge : reversed_graph.edges[v])
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (!components[edge.to])
|
|
|
|
|
dfsComponents(reversed_graph, edge.to, components, component);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
ComparisonGraph::Graph ComparisonGraph::buildGraphFromAstsGraph(const Graph & asts_graph)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// Find strongly connected component by using 2 dfs traversals.
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
|
2021-05-04 18:43:58 +00:00
|
|
|
|
const auto n = asts_graph.vertices.size();
|
2021-03-05 09:54:13 +00:00
|
|
|
|
|
|
|
|
|
std::vector<size_t> order;
|
|
|
|
|
{
|
|
|
|
|
std::vector<bool> visited(n, false);
|
|
|
|
|
for (size_t v = 0; v < n; ++v)
|
|
|
|
|
{
|
|
|
|
|
if (!visited[v])
|
2021-11-10 17:57:59 +00:00
|
|
|
|
dfsOrder(asts_graph, v, visited, order);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
OptionalIndices components(n);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
size_t component = 0;
|
|
|
|
|
{
|
|
|
|
|
const Graph reversed_graph = reverseGraph(asts_graph);
|
2021-11-10 17:57:59 +00:00
|
|
|
|
for (auto it = order.rbegin(); it != order.rend(); ++it)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (!components[*it])
|
2021-03-05 09:54:13 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
dfsComponents(reversed_graph, *it, components, component);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
++component;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Graph result;
|
2021-05-04 18:43:58 +00:00
|
|
|
|
result.vertices.resize(component);
|
2021-03-05 09:54:13 +00:00
|
|
|
|
result.edges.resize(component);
|
|
|
|
|
for (const auto & [hash, index] : asts_graph.ast_hash_to_component)
|
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
assert(components[index]);
|
|
|
|
|
result.ast_hash_to_component[hash] = *components[index];
|
|
|
|
|
result.vertices[*components[index]].asts.insert(
|
|
|
|
|
std::end(result.vertices[*components[index]].asts),
|
2021-05-04 18:43:58 +00:00
|
|
|
|
std::begin(asts_graph.vertices[index].asts),
|
|
|
|
|
std::end(asts_graph.vertices[index].asts)); // asts_graph has only one ast per vertex
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 15:47:50 +00:00
|
|
|
|
/// Calculate constants
|
2021-05-04 18:43:58 +00:00
|
|
|
|
for (auto & vertex : result.vertices)
|
2021-04-10 15:47:50 +00:00
|
|
|
|
vertex.buildConstants();
|
2021-03-05 09:54:13 +00:00
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// For each edge in initial graph, we add an edge between components in condensation graph.
|
2021-03-05 09:54:13 +00:00
|
|
|
|
for (size_t v = 0; v < n; ++v)
|
|
|
|
|
{
|
|
|
|
|
for (const auto & edge : asts_graph.edges[v])
|
2021-11-10 17:57:59 +00:00
|
|
|
|
result.edges[*components[v]].push_back(Edge{edge.type, *components[edge.to]});
|
|
|
|
|
|
|
|
|
|
/// TODO: make edges unique (left most strict)
|
2021-03-05 09:54:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// If we have constansts in two components, we can compare them and add and extra edge.
|
2021-05-04 18:43:58 +00:00
|
|
|
|
for (size_t v = 0; v < result.vertices.size(); ++v)
|
2021-04-10 15:47:50 +00:00
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
for (size_t u = 0; u < result.vertices.size(); ++u)
|
2021-04-10 15:47:50 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
if (v != u && result.vertices[v].hasConstant() && result.vertices[u].hasConstant())
|
2021-04-10 15:47:50 +00:00
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
const auto * left = result.vertices[v].getConstant()->as<ASTLiteral>();
|
|
|
|
|
const auto * right = result.vertices[u].getConstant()->as<ASTLiteral>();
|
2021-04-10 15:47:50 +00:00
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// Only GREATER. Equal constant fields = equal literals so it was already considered above.
|
|
|
|
|
if (greater(left->value, right->value))
|
|
|
|
|
result.edges[v].push_back(Edge{Edge::GREATER, u});
|
2021-04-10 15:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 09:54:13 +00:00
|
|
|
|
return result;
|
2021-03-04 12:11:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 17:57:59 +00:00
|
|
|
|
std::map<std::pair<size_t, size_t>, ComparisonGraph::Path> ComparisonGraph::buildDistsFromGraph(const Graph & g)
|
2021-04-03 16:30:49 +00:00
|
|
|
|
{
|
2021-11-10 17:57:59 +00:00
|
|
|
|
/// Min path : -1 means GREATER, 0 means GREATER_OR_EQUALS.
|
|
|
|
|
/// We use Floyd–Warshall algorithm to find distances between all pairs of vertices.
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm
|
|
|
|
|
|
|
|
|
|
constexpr auto inf = std::numeric_limits<Int8>::max();
|
2021-05-06 08:29:24 +00:00
|
|
|
|
const size_t n = g.vertices.size();
|
2021-11-10 17:57:59 +00:00
|
|
|
|
std::vector<std::vector<Int8>> results(n, std::vector<Int8>(n, inf));
|
|
|
|
|
|
2021-04-03 16:30:49 +00:00
|
|
|
|
for (size_t v = 0; v < n; ++v)
|
2021-04-10 20:46:53 +00:00
|
|
|
|
{
|
|
|
|
|
results[v][v] = 0;
|
2021-04-03 16:30:49 +00:00
|
|
|
|
for (const auto & edge : g.edges[v])
|
2021-11-10 17:57:59 +00:00
|
|
|
|
results[v][edge.to] = std::min(results[v][edge.to], static_cast<Int8>(edge.type == Edge::GREATER ? -1 : 0));
|
2021-04-10 20:46:53 +00:00
|
|
|
|
}
|
2021-04-03 16:30:49 +00:00
|
|
|
|
|
|
|
|
|
for (size_t k = 0; k < n; ++k)
|
|
|
|
|
for (size_t v = 0; v < n; ++v)
|
|
|
|
|
for (size_t u = 0; u < n; ++u)
|
|
|
|
|
if (results[v][k] != inf && results[k][u] != inf)
|
|
|
|
|
results[v][u] = std::min(results[v][u], std::min(results[v][k], results[k][u]));
|
|
|
|
|
|
|
|
|
|
std::map<std::pair<size_t, size_t>, Path> path;
|
|
|
|
|
for (size_t v = 0; v < n; ++v)
|
|
|
|
|
for (size_t u = 0; u < n; ++u)
|
|
|
|
|
if (results[v][u] != inf)
|
2021-11-10 17:57:59 +00:00
|
|
|
|
path[std::make_pair(v, u)] = (results[v][u] == -1 ? Path::GREATER : Path::GREATER_OR_EQUAL);
|
2021-05-04 10:47:23 +00:00
|
|
|
|
|
2021-04-03 16:30:49 +00:00
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 20:46:53 +00:00
|
|
|
|
std::pair<std::vector<ssize_t>, std::vector<ssize_t>> ComparisonGraph::buildConstBounds() const
|
|
|
|
|
{
|
2021-05-04 18:43:58 +00:00
|
|
|
|
const size_t n = graph.vertices.size();
|
2021-04-10 20:46:53 +00:00
|
|
|
|
std::vector<ssize_t> lower(n, -1);
|
|
|
|
|
std::vector<ssize_t> upper(n, -1);
|
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
auto get_value = [this](const size_t vertex) -> Field
|
|
|
|
|
{
|
|
|
|
|
return graph.vertices[vertex].getConstant()->as<ASTLiteral>()->value;
|
2021-04-10 20:46:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const auto & [edge, path] : dists)
|
|
|
|
|
{
|
|
|
|
|
const auto [from, to] = edge;
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
if (graph.vertices[to].hasConstant())
|
|
|
|
|
{
|
2021-04-10 20:46:53 +00:00
|
|
|
|
if (lower[from] == -1
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|| greater(get_value(to), get_value(lower[from]))
|
|
|
|
|
|| (equals(get_value(to), get_value(lower[from])) && path == Path::GREATER))
|
2021-04-10 20:46:53 +00:00
|
|
|
|
lower[from] = to;
|
|
|
|
|
}
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|
2021-05-04 18:43:58 +00:00
|
|
|
|
if (graph.vertices[from].hasConstant())
|
|
|
|
|
{
|
2021-04-10 20:46:53 +00:00
|
|
|
|
if (upper[to] == -1
|
2021-11-10 17:57:59 +00:00
|
|
|
|
|| less(get_value(from), get_value(upper[to]))
|
|
|
|
|
|| (equals(get_value(from), get_value(upper[to])) && path == Path::GREATER))
|
2021-04-10 20:46:53 +00:00
|
|
|
|
upper[to] = from;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {lower, upper};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 12:11:43 +00:00
|
|
|
|
}
|