2019-05-18 23:57:26 +00:00
|
|
|
#include <Columns/IColumn.h>
|
2019-05-18 21:07:23 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2022-09-04 14:26:31 +00:00
|
|
|
#include <DataTypes/IDataType.h>
|
2019-05-18 21:07:23 +00:00
|
|
|
#include <Formats/FormatSettings.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
2020-11-02 10:03:52 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2019-05-18 21:07:23 +00:00
|
|
|
#include <Interpreters/ReplaceQueryParameterVisitor.h>
|
2019-06-14 17:15:30 +00:00
|
|
|
#include <Interpreters/addTypeConversionToAST.h>
|
2022-09-04 14:26:31 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTQueryParameter.h>
|
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
|
|
|
#include <Common/quoteString.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2019-06-14 17:15:30 +00:00
|
|
|
|
2019-05-18 21:07:23 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-06-16 17:32:37 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int UNKNOWN_QUERY_PARAMETER;
|
|
|
|
extern const int BAD_QUERY_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-18 21:07:23 +00:00
|
|
|
void ReplaceQueryParameterVisitor::visit(ASTPtr & ast)
|
2020-06-24 21:36:18 +00:00
|
|
|
{
|
|
|
|
if (ast->as<ASTQueryParameter>())
|
|
|
|
visitQueryParameter(ast);
|
2021-04-15 07:33:15 +00:00
|
|
|
else if (ast->as<ASTIdentifier>() || ast->as<ASTTableIdentifier>())
|
2020-11-02 10:03:52 +00:00
|
|
|
visitIdentifier(ast);
|
2020-06-24 21:36:18 +00:00
|
|
|
else
|
2022-09-04 14:26:31 +00:00
|
|
|
{
|
|
|
|
if (auto * describe_query = dynamic_cast<ASTDescribeQuery *>(ast.get()); describe_query && describe_query->table_expression)
|
|
|
|
visitChildren(describe_query->table_expression);
|
|
|
|
else
|
|
|
|
visitChildren(ast);
|
|
|
|
}
|
2020-06-24 21:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ReplaceQueryParameterVisitor::visitChildren(ASTPtr & ast)
|
2019-05-18 21:07:23 +00:00
|
|
|
{
|
|
|
|
for (auto & child : ast->children)
|
2020-06-24 21:36:18 +00:00
|
|
|
visit(child);
|
2019-05-18 21:07:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 16:18:24 +00:00
|
|
|
const String & ReplaceQueryParameterVisitor::getParamValue(const String & name)
|
2019-05-18 21:07:23 +00:00
|
|
|
{
|
2019-06-15 17:52:53 +00:00
|
|
|
auto search = query_parameters.find(name);
|
|
|
|
if (search != query_parameters.end())
|
2019-05-18 21:07:23 +00:00
|
|
|
return search->second;
|
|
|
|
else
|
2019-06-16 17:32:37 +00:00
|
|
|
throw Exception("Substitution " + backQuote(name) + " is not set", ErrorCodes::UNKNOWN_QUERY_PARAMETER);
|
2019-05-18 21:07:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 16:18:24 +00:00
|
|
|
void ReplaceQueryParameterVisitor::visitQueryParameter(ASTPtr & ast)
|
2019-05-18 21:07:23 +00:00
|
|
|
{
|
2019-06-14 16:18:24 +00:00
|
|
|
const auto & ast_param = ast->as<ASTQueryParameter &>();
|
|
|
|
const String & value = getParamValue(ast_param.name);
|
2019-06-14 17:15:30 +00:00
|
|
|
const String & type_name = ast_param.type;
|
2020-06-24 13:14:04 +00:00
|
|
|
String alias = ast_param.alias;
|
2019-06-04 18:15:32 +00:00
|
|
|
|
2019-06-14 17:15:30 +00:00
|
|
|
const auto data_type = DataTypeFactory::instance().get(type_name);
|
2019-05-18 21:07:23 +00:00
|
|
|
auto temp_column_ptr = data_type->createColumn();
|
2019-05-18 23:57:26 +00:00
|
|
|
IColumn & temp_column = *temp_column_ptr;
|
2019-05-18 21:07:23 +00:00
|
|
|
ReadBufferFromString read_buffer{value};
|
|
|
|
FormatSettings format_settings;
|
2021-03-09 14:46:52 +00:00
|
|
|
data_type->getDefaultSerialization()->deserializeTextEscaped(temp_column, read_buffer, format_settings);
|
2019-05-18 21:07:23 +00:00
|
|
|
|
2019-06-05 21:04:17 +00:00
|
|
|
if (!read_buffer.eof())
|
2020-09-19 15:13:51 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_QUERY_PARAMETER,
|
|
|
|
"Value {} cannot be parsed as {} for query parameter '{}'"
|
|
|
|
" because it isn't parsed completely: only {} of {} bytes was parsed: {}",
|
|
|
|
value, type_name, ast_param.name, read_buffer.count(), value.size(), value.substr(0, read_buffer.count()));
|
2019-06-05 21:04:17 +00:00
|
|
|
|
2022-01-25 07:09:17 +00:00
|
|
|
Field literal;
|
2022-01-25 07:10:04 +00:00
|
|
|
/// If data type has custom serialization, we should use CAST from String,
|
|
|
|
/// because CAST from field may not work correctly (for example for type IPv6).
|
2022-01-25 07:09:17 +00:00
|
|
|
if (data_type->getCustomSerialization())
|
|
|
|
literal = value;
|
|
|
|
else
|
|
|
|
literal = temp_column[0];
|
|
|
|
ast = addTypeConversionToAST(std::make_shared<ASTLiteral>(literal), type_name);
|
2020-09-19 15:13:51 +00:00
|
|
|
|
|
|
|
/// Keep the original alias.
|
2020-06-24 13:14:04 +00:00
|
|
|
ast->setAlias(alias);
|
2019-05-18 21:07:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:52 +00:00
|
|
|
void ReplaceQueryParameterVisitor::visitIdentifier(ASTPtr & ast)
|
|
|
|
{
|
2021-04-15 07:33:15 +00:00
|
|
|
auto ast_identifier = dynamic_pointer_cast<ASTIdentifier>(ast);
|
|
|
|
if (ast_identifier->children.empty())
|
2020-11-02 10:03:52 +00:00
|
|
|
return;
|
|
|
|
|
2021-04-15 07:33:15 +00:00
|
|
|
auto & name_parts = ast_identifier->name_parts;
|
2020-11-02 10:03:52 +00:00
|
|
|
for (size_t i = 0, j = 0, size = name_parts.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
if (name_parts[i].empty())
|
|
|
|
{
|
2021-04-15 07:33:15 +00:00
|
|
|
const auto & ast_param = ast_identifier->children[j++]->as<ASTQueryParameter &>();
|
2020-11-02 10:03:52 +00:00
|
|
|
name_parts[i] = getParamValue(ast_param.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:33:15 +00:00
|
|
|
/// FIXME: what should this mean?
|
|
|
|
if (!ast_identifier->semantic->special && name_parts.size() >= 2)
|
|
|
|
ast_identifier->semantic->table = ast_identifier->name_parts.end()[-2];
|
2020-11-02 10:03:52 +00:00
|
|
|
|
2021-04-15 07:33:15 +00:00
|
|
|
ast_identifier->resetFullName();
|
|
|
|
ast_identifier->children.clear();
|
2020-11-02 10:03:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 21:07:23 +00:00
|
|
|
}
|