2023-02-09 04:26:32 +00:00
|
|
|
#include <Interpreters/RewriteArrayExistsFunctionVisitor.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2023-02-09 08:30:53 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2023-02-09 04:26:32 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
void RewriteArrayExistsFunctionMatcher::visit(ASTPtr & ast, Data & data)
|
|
|
|
{
|
|
|
|
if (auto * func = ast->as<ASTFunction>())
|
|
|
|
{
|
|
|
|
if (func->is_window_function)
|
|
|
|
return;
|
|
|
|
|
|
|
|
visit(*func, ast, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RewriteArrayExistsFunctionMatcher::visit(const ASTFunction & func, ASTPtr & ast, Data &)
|
|
|
|
{
|
2023-02-09 08:30:53 +00:00
|
|
|
if (func.name != "arrayExists" || !func.arguments)
|
2023-02-09 04:26:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto & array_exists_arguments = func.arguments->children;
|
|
|
|
if (array_exists_arguments.size() != 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/// lambda function must be like: x -> x = elem
|
|
|
|
const auto * lambda_func = array_exists_arguments[0]->as<ASTFunction>();
|
|
|
|
if (!lambda_func || !lambda_func->is_lambda_function)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto & lambda_func_arguments = lambda_func->arguments->children;
|
2023-02-09 08:30:53 +00:00
|
|
|
if (lambda_func_arguments.size() != 2)
|
|
|
|
return;
|
|
|
|
|
2023-02-09 04:26:32 +00:00
|
|
|
const auto * tuple_func = lambda_func_arguments[0]->as<ASTFunction>();
|
|
|
|
if (!tuple_func || tuple_func->name != "tuple")
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto & tuple_arguments = tuple_func->arguments->children;
|
|
|
|
if (tuple_arguments.size() != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto * id = tuple_arguments[0]->as<ASTIdentifier>();
|
|
|
|
if (!id)
|
|
|
|
return;
|
|
|
|
|
2023-02-09 07:15:08 +00:00
|
|
|
const auto * filter_func = lambda_func_arguments[1]->as<ASTFunction>();
|
2023-02-09 04:26:32 +00:00
|
|
|
if (!filter_func || filter_func->name != "equals")
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto & filter_arguments = filter_func->arguments->children;
|
|
|
|
if (filter_arguments.size() != 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ASTIdentifier * filter_id = nullptr;
|
|
|
|
const ASTLiteral * filter_literal = nullptr;
|
|
|
|
if ((filter_id = filter_arguments[0]->as<ASTIdentifier>()) && (filter_literal = filter_arguments[1]->as<ASTLiteral>())
|
2023-02-09 07:15:08 +00:00
|
|
|
&& filter_id->full_name == id->full_name)
|
2023-02-09 04:26:32 +00:00
|
|
|
{
|
|
|
|
/// arrayExists(x -> x = elem, arr) -> has(arr, elem)
|
|
|
|
auto new_func = makeASTFunction("has", std::move(array_exists_arguments[1]), std::move(filter_arguments[1]));
|
|
|
|
new_func->setAlias(func.alias);
|
|
|
|
ast = std::move(new_func);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (
|
|
|
|
(filter_id = filter_arguments[1]->as<ASTIdentifier>()) && (filter_literal = filter_arguments[0]->as<ASTLiteral>())
|
2023-02-09 07:15:08 +00:00
|
|
|
&& filter_id->full_name == id->full_name)
|
2023-02-09 04:26:32 +00:00
|
|
|
{
|
|
|
|
/// arrayExists(x -> elem = x, arr) -> has(arr, elem)
|
|
|
|
auto new_func = makeASTFunction("has", std::move(array_exists_arguments[1]), std::move(filter_arguments[0]));
|
|
|
|
new_func->setAlias(func.alias);
|
|
|
|
ast = std::move(new_func);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|