ClickHouse/dbms/src/Interpreters/InterpreterDescribeQuery.cpp

97 lines
2.8 KiB
C++
Raw Normal View History

#include <Storages/IStorage.h>
#include <Parsers/TablePropertiesQueriesASTs.h>
#include <DataStreams/OneBlockInputStream.h>
#include <DataStreams/BlockIO.h>
#include <DataStreams/copyData.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeString.h>
#include <Columns/ColumnString.h>
#include <Parsers/formatAST.h>
#include <Parsers/queryToString.h>
2017-05-23 18:01:50 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/InterpreterDescribeQuery.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
2016-12-12 07:24:56 +00:00
namespace DB
{
BlockIO InterpreterDescribeQuery::execute()
{
BlockIO res;
res.in = executeImpl();
res.in_sample = getSampleBlock();
2016-12-12 07:24:56 +00:00
return res;
2016-12-12 07:24:56 +00:00
}
Block InterpreterDescribeQuery::getSampleBlock()
{
Block block;
2016-12-12 07:24:56 +00:00
ColumnWithTypeAndName col;
col.name = "name";
col.type = std::make_shared<DataTypeString>();
col.column = col.type->createColumn();
block.insert(col);
2016-12-12 07:24:56 +00:00
col.name = "type";
block.insert(col);
2016-12-12 07:24:56 +00:00
col.name = "default_type";
block.insert(col);
2016-12-12 07:24:56 +00:00
col.name = "default_expression";
block.insert(col);
2016-12-12 07:24:56 +00:00
return block;
2016-12-12 07:24:56 +00:00
}
BlockInputStreamPtr InterpreterDescribeQuery::executeImpl()
{
const ASTDescribeQuery & ast = typeid_cast<const ASTDescribeQuery &>(*query_ptr);
NamesAndTypesList columns;
ColumnDefaults column_defaults;
{
StoragePtr table = context.getTable(ast.database, ast.table);
auto table_lock = table->lockStructure(false);
columns = table->getColumnsList();
columns.insert(std::end(columns), std::begin(table->alias_columns), std::end(table->alias_columns));
column_defaults = table->column_defaults;
}
ColumnWithTypeAndName name_column{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "name"};
ColumnWithTypeAndName type_column{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "type" };
ColumnWithTypeAndName default_type_column{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "default_type" };
ColumnWithTypeAndName default_expression_column{std::make_shared<ColumnString>(), std::make_shared<DataTypeString>(), "default_expression" };;
for (const auto column : columns)
{
name_column.column->insert(column.name);
type_column.column->insert(column.type->getName());
const auto it = column_defaults.find(column.name);
if (it == std::end(column_defaults))
{
default_type_column.column->insertDefault();
default_expression_column.column->insertDefault();
}
else
{
default_type_column.column->insert(toString(it->second.type));
default_expression_column.column->insert(queryToString(it->second.expression));
}
}
return std::make_shared<OneBlockInputStream>(
Block{name_column, type_column, default_type_column, default_expression_column});
2016-12-12 07:24:56 +00:00
}
}