2023-03-20 12:27:53 +00:00
|
|
|
#include <Interpreters/ComparisonTupleEliminationVisitor.h>
|
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
ASTs splitTuple(const ASTPtr & node)
|
|
|
|
{
|
|
|
|
if (const auto * func = node->as<ASTFunction>(); func && func->name == "tuple")
|
|
|
|
return func->arguments->children;
|
|
|
|
|
|
|
|
if (const auto * literal = node->as<ASTLiteral>(); literal && literal->value.getType() == Field::Types::Tuple)
|
|
|
|
{
|
|
|
|
ASTs result;
|
|
|
|
const auto & tuple = literal->value.get<const Tuple &>();
|
|
|
|
for (const auto & child : tuple)
|
|
|
|
result.emplace_back(std::make_shared<ASTLiteral>(child));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr concatWithAnd(const ASTs & nodes)
|
|
|
|
{
|
|
|
|
if (nodes.empty())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot concat empty list of nodes");
|
|
|
|
|
|
|
|
if (nodes.size() == 1)
|
|
|
|
return nodes[0];
|
|
|
|
|
|
|
|
auto result = makeASTFunction("and");
|
|
|
|
result->arguments->children = nodes;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:23:09 +00:00
|
|
|
class SplitTupleComparsionExpressionMatcher
|
2023-03-20 12:27:53 +00:00
|
|
|
{
|
2023-03-21 16:23:09 +00:00
|
|
|
public:
|
|
|
|
using Data = ComparisonTupleEliminationMatcher::Data;
|
2023-03-20 12:27:53 +00:00
|
|
|
|
2023-03-21 16:23:09 +00:00
|
|
|
static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; }
|
|
|
|
static void visit(ASTPtr & ast, Data &)
|
2023-03-20 12:27:53 +00:00
|
|
|
{
|
2023-03-21 16:23:09 +00:00
|
|
|
auto * func = ast->as<ASTFunction>();
|
|
|
|
if (!func || func->arguments->children.size() != 2)
|
|
|
|
return;
|
2023-03-20 12:27:53 +00:00
|
|
|
|
2023-03-21 16:23:09 +00:00
|
|
|
if (func->name != "equals" && func->name != "notEquals")
|
2023-03-20 12:27:53 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto lhs = splitTuple(func->arguments->children[0]);
|
|
|
|
auto rhs = splitTuple(func->arguments->children[1]);
|
2023-03-21 16:23:09 +00:00
|
|
|
if (lhs.size() != rhs.size() || lhs.empty())
|
2023-03-20 12:27:53 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
ASTs new_args;
|
2023-03-21 16:23:09 +00:00
|
|
|
new_args.reserve(lhs.size());
|
2023-03-20 12:27:53 +00:00
|
|
|
for (size_t i = 0; i < lhs.size(); ++i)
|
|
|
|
{
|
|
|
|
new_args.emplace_back(makeASTFunction("equals", lhs[i], rhs[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (func->name == "notEquals")
|
2023-03-21 16:23:09 +00:00
|
|
|
ast = makeASTFunction("not", concatWithAnd(new_args));
|
2023-03-20 12:27:53 +00:00
|
|
|
else
|
2023-03-21 16:23:09 +00:00
|
|
|
ast = concatWithAnd(new_args);
|
2023-03-20 12:27:53 +00:00
|
|
|
}
|
2023-03-21 16:23:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using SplitTupleComparsionExpressionVisitor = InDepthNodeVisitor<SplitTupleComparsionExpressionMatcher, true>;
|
2023-03-20 12:27:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComparisonTupleEliminationMatcher::needChildVisit(ASTPtr &, const ASTPtr &)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:23:09 +00:00
|
|
|
void ComparisonTupleEliminationMatcher::visit(ASTPtr & ast, Data & data)
|
2023-03-20 12:27:53 +00:00
|
|
|
{
|
|
|
|
auto * select_ast = ast->as<ASTSelectQuery>();
|
|
|
|
if (!select_ast || !select_ast->where())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (select_ast->where())
|
2023-03-21 16:23:09 +00:00
|
|
|
SplitTupleComparsionExpressionVisitor(data).visit(select_ast->refWhere());
|
2023-03-20 12:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|