ClickHouse/src/Interpreters/addTypeConversionToAST.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.8 KiB
C++
Raw Normal View History

2019-06-14 19:39:56 +00:00
#include "addTypeConversionToAST.h"
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTWithAlias.h>
2020-12-12 16:42:15 +00:00
#include <Storages/ColumnsDescription.h>
#include <Interpreters/Context.h>
#include <Interpreters/TreeRewriter.h>
#include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/ExpressionActions.h>
2019-06-14 19:39:56 +00:00
namespace DB
{
2020-12-12 16:42:15 +00:00
namespace ErrorCodes
{
extern const int THERE_IS_NO_DEFAULT_VALUE;
}
2019-06-14 19:39:56 +00:00
ASTPtr addTypeConversionToAST(ASTPtr && ast, const String & type_name)
{
2021-08-07 08:11:40 +00:00
auto func = makeASTFunction("_CAST", ast, std::make_shared<ASTLiteral>(type_name));
2019-06-14 19:39:56 +00:00
2019-06-16 16:47:47 +00:00
if (ASTWithAlias * ast_with_alias = dynamic_cast<ASTWithAlias *>(ast.get()))
2019-06-14 19:39:56 +00:00
{
func->alias = ast_with_alias->alias;
func->prefer_alias_to_column_name = ast_with_alias->prefer_alias_to_column_name;
ast_with_alias->alias.clear();
}
2019-06-16 16:47:47 +00:00
return func;
2019-06-14 19:39:56 +00:00
}
2021-06-01 12:20:52 +00:00
ASTPtr addTypeConversionToAST(ASTPtr && ast, const String & type_name, const NamesAndTypesList & all_columns, ContextPtr context)
2020-12-12 16:42:15 +00:00
{
auto syntax_analyzer_result = TreeRewriter(context).analyze(ast, all_columns);
const auto actions = ExpressionAnalyzer(ast,
syntax_analyzer_result,
const_pointer_cast<Context>(context)).getActions(true);
2020-12-12 16:42:15 +00:00
for (const auto & action : actions->getActions())
if (action.node->type == ActionsDAG::ActionType::ARRAY_JOIN)
throw Exception("Unsupported default value that requires ARRAY JOIN action", ErrorCodes::THERE_IS_NO_DEFAULT_VALUE);
auto block = actions->getSampleBlock();
2021-05-21 17:01:21 +00:00
auto desc_type = block.getByName(ast->getAliasOrColumnName()).type;
2020-12-12 16:42:15 +00:00
if (desc_type->getName() != type_name)
return addTypeConversionToAST(std::move(ast), type_name);
return std::move(ast);
}
2019-06-14 19:39:56 +00:00
}