2017-01-05 07:25:23 +00:00
|
|
|
#include <DB/Analyzers/AnalyzeResultOfQuery.h>
|
|
|
|
#include <DB/Analyzers/CollectAliases.h>
|
|
|
|
#include <DB/Analyzers/CollectTables.h>
|
2017-01-14 05:19:48 +00:00
|
|
|
#include <DB/Analyzers/AnalyzeLambdas.h>
|
2017-01-05 07:25:23 +00:00
|
|
|
#include <DB/Analyzers/AnalyzeColumns.h>
|
|
|
|
#include <DB/Analyzers/TypeAndConstantInference.h>
|
|
|
|
#include <DB/Interpreters/Context.h>
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int UNEXPECTED_AST_STRUCTURE;
|
2017-01-05 07:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AnalyzeResultOfQuery::process(ASTPtr & ast, Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTSelectQuery * select = typeid_cast<const ASTSelectQuery *>(ast.get());
|
|
|
|
if (!select)
|
|
|
|
throw Exception("AnalyzeResultOfQuery::process was called for not a SELECT query", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
|
|
|
|
if (!select->select_expression_list)
|
|
|
|
throw Exception("SELECT query doesn't have select_expression_list", ErrorCodes::UNEXPECTED_AST_STRUCTURE);
|
2017-01-05 09:26:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AnalyzeLambdas analyze_lambdas;
|
|
|
|
analyze_lambdas.process(ast);
|
2017-01-14 05:19:48 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CollectAliases collect_aliases;
|
|
|
|
collect_aliases.process(ast);
|
2017-01-05 07:25:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
CollectTables collect_tables;
|
|
|
|
collect_tables.process(ast, context, collect_aliases);
|
2017-01-05 07:25:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AnalyzeColumns analyze_columns;
|
|
|
|
analyze_columns.process(ast, collect_aliases, collect_tables);
|
2017-01-05 07:25:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
TypeAndConstantInference inference;
|
|
|
|
inference.process(ast, context, collect_aliases, analyze_columns, analyze_lambdas);
|
2017-01-05 07:25:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const ASTPtr & child : select->select_expression_list->children)
|
|
|
|
{
|
|
|
|
auto it = inference.info.find(child->getColumnName());
|
|
|
|
if (it == inference.info.end())
|
|
|
|
throw Exception("Logical error: type information for result column of SELECT query was not inferred", ErrorCodes::LOGICAL_ERROR);
|
2017-01-05 07:25:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String name = child->getAliasOrColumnName();
|
|
|
|
const TypeAndConstantInference::ExpressionInfo & info = it->second;
|
2017-01-05 07:25:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
result.insert(ColumnWithTypeAndName(
|
|
|
|
info.is_constant_expression ? info.data_type->createConstColumn(1, info.value) : nullptr,
|
|
|
|
info.data_type,
|
|
|
|
std::move(name)));
|
|
|
|
}
|
2017-01-05 07:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AnalyzeResultOfQuery::dump(WriteBuffer & out) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(result.dumpStructure(), out);
|
2017-01-05 07:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|