2022-10-19 16:30:03 +00:00
|
|
|
#include <Parsers/FunctionParameterValuesVisitor.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Common/FieldVisitorToString.h>
|
|
|
|
#include <Parsers/ASTHelpers.h>
|
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FunctionParameterValuesVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FunctionParameterValuesVisitor(NameToNameMap & parameter_values_)
|
|
|
|
: parameter_values(parameter_values_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(const ASTPtr & ast)
|
|
|
|
{
|
2022-12-23 16:57:17 +00:00
|
|
|
if (const auto * function = ast->as<ASTFunction>())
|
|
|
|
visitFunction(*function);
|
2022-10-19 16:30:03 +00:00
|
|
|
for (const auto & child : ast->children)
|
|
|
|
visit(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NameToNameMap & parameter_values;
|
|
|
|
|
2022-12-23 16:57:17 +00:00
|
|
|
void visitFunction(const ASTFunction & parameter_function)
|
2022-10-19 16:30:03 +00:00
|
|
|
{
|
2022-12-23 16:57:17 +00:00
|
|
|
if (parameter_function.name != "equals" && parameter_function.children.size() != 1)
|
2022-11-07 14:22:45 +00:00
|
|
|
return;
|
|
|
|
|
2022-12-23 16:57:17 +00:00
|
|
|
const auto * expression_list = parameter_function.children[0]->as<ASTExpressionList>();
|
|
|
|
|
|
|
|
if (expression_list && expression_list->children.size() != 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const auto * identifier = expression_list->children[0]->as<ASTIdentifier>())
|
2022-10-19 16:30:03 +00:00
|
|
|
{
|
2022-12-23 16:57:17 +00:00
|
|
|
if (const auto * literal = expression_list->children[1]->as<ASTLiteral>())
|
2022-10-19 16:30:03 +00:00
|
|
|
{
|
2022-11-07 14:22:45 +00:00
|
|
|
parameter_values[identifier->name()] = convertFieldToString(literal->value);
|
|
|
|
}
|
2022-12-23 16:57:17 +00:00
|
|
|
else if (const auto * function = expression_list->children[1]->as<ASTFunction>())
|
2022-11-07 14:22:45 +00:00
|
|
|
{
|
|
|
|
if (isFunctionCast(function))
|
2022-10-19 16:30:03 +00:00
|
|
|
{
|
2022-11-07 14:22:45 +00:00
|
|
|
const auto * cast_expression = assert_cast<ASTExpressionList*>(function->arguments.get());
|
|
|
|
if (cast_expression->children.size() != 2)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function CAST must have exactly two arguments");
|
2022-11-07 14:22:45 +00:00
|
|
|
if (const auto * cast_literal = cast_expression->children[0]->as<ASTLiteral>())
|
2022-10-19 16:30:03 +00:00
|
|
|
{
|
2022-11-07 14:22:45 +00:00
|
|
|
parameter_values[identifier->name()] = convertFieldToString(cast_literal->value);
|
2022-10-19 16:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-16 11:27:08 +00:00
|
|
|
NameToNameMap analyzeFunctionParamValues(const ASTPtr & ast)
|
2022-10-19 16:30:03 +00:00
|
|
|
{
|
|
|
|
NameToNameMap parameter_values;
|
|
|
|
FunctionParameterValuesVisitor(parameter_values).visit(ast);
|
|
|
|
return parameter_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|