2013-02-21 10:02:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Storages/IStorage.h>
|
|
|
|
#include <DB/Parsers/TablePropertiesQueriesASTs.h>
|
|
|
|
#include <DB/Interpreters/Context.h>
|
2015-06-18 02:11:05 +00:00
|
|
|
#include <DB/Interpreters/IInterpreter.h>
|
2013-02-21 10:02:33 +00:00
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DB/DataStreams/BlockIO.h>
|
|
|
|
#include <DB/DataStreams/copyData.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2014-09-30 03:48:40 +00:00
|
|
|
#include <DB/Parsers/formatAST.h>
|
|
|
|
|
2013-02-21 10:02:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
/** Вернуть названия и типы столбцов указанной таблицы.
|
|
|
|
*/
|
2015-06-18 02:11:05 +00:00
|
|
|
class InterpreterDescribeQuery : public IInterpreter
|
2013-02-22 09:41:13 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-01-28 01:00:27 +00:00
|
|
|
InterpreterDescribeQuery(ASTPtr query_ptr_, const Context & context_)
|
2013-02-22 09:41:13 +00:00
|
|
|
: query_ptr(query_ptr_), context(context_) {}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2015-06-18 02:11:05 +00:00
|
|
|
BlockIO execute() override
|
2013-02-21 10:02:33 +00:00
|
|
|
{
|
2013-02-22 09:41:13 +00:00
|
|
|
BlockIO res;
|
|
|
|
res.in = executeImpl();
|
|
|
|
res.in_sample = getSampleBlock();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
return res;
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
private:
|
|
|
|
ASTPtr query_ptr;
|
|
|
|
Context context;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
Block getSampleBlock()
|
|
|
|
{
|
|
|
|
Block block;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
ColumnWithTypeAndName col;
|
2013-02-22 09:41:13 +00:00
|
|
|
col.name = "name";
|
|
|
|
col.type = new DataTypeString;
|
2013-06-18 14:12:10 +00:00
|
|
|
col.column = col.type->createColumn();
|
2013-02-22 09:41:13 +00:00
|
|
|
block.insert(col);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
col.name = "type";
|
2014-09-30 03:48:40 +00:00
|
|
|
block.insert(col);
|
|
|
|
|
|
|
|
col.name = "default_type";
|
|
|
|
block.insert(col);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2014-09-30 03:48:40 +00:00
|
|
|
col.name = "default_expression";
|
2013-02-22 09:41:13 +00:00
|
|
|
block.insert(col);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2014-09-30 03:48:40 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
return block;
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
BlockInputStreamPtr executeImpl()
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
const ASTDescribeQuery & ast = typeid_cast<const ASTDescribeQuery &>(*query_ptr);
|
|
|
|
|
2013-02-22 09:41:13 +00:00
|
|
|
NamesAndTypesList columns;
|
2014-09-30 03:48:40 +00:00
|
|
|
ColumnDefaults column_defaults;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2013-02-21 10:02:33 +00:00
|
|
|
{
|
2014-03-19 10:45:13 +00:00
|
|
|
StoragePtr table = context.getTable(ast.database, ast.table);
|
|
|
|
auto table_lock = table->lockStructure(false);
|
|
|
|
columns = table->getColumnsList();
|
2014-09-30 03:48:40 +00:00
|
|
|
columns.insert(std::end(columns), std::begin(table->alias_columns), std::end(table->alias_columns));
|
|
|
|
column_defaults = table->column_defaults;
|
2013-02-21 10:02:33 +00:00
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
ColumnWithTypeAndName name_column{std::make_shared<ColumnString>(), new DataTypeString, "name"};
|
|
|
|
ColumnWithTypeAndName type_column{std::make_shared<ColumnString>(), new DataTypeString, "type" };
|
|
|
|
ColumnWithTypeAndName default_type_column{std::make_shared<ColumnString>(), new DataTypeString, "default_type" };
|
|
|
|
ColumnWithTypeAndName default_expression_column{std::make_shared<ColumnString>(), new DataTypeString, "default_expression" };;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2014-09-30 03:48:40 +00:00
|
|
|
for (const auto column : columns)
|
2013-02-21 10:02:33 +00:00
|
|
|
{
|
2014-09-30 03:48:40 +00:00
|
|
|
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));
|
2014-10-21 12:11:20 +00:00
|
|
|
default_expression_column.column->insert(queryToString(it->second.expression));
|
2014-09-30 03:48:40 +00:00
|
|
|
}
|
2013-02-21 10:02:33 +00:00
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2014-09-30 03:48:40 +00:00
|
|
|
return new OneBlockInputStream{
|
2014-10-01 11:17:44 +00:00
|
|
|
{name_column, type_column, default_type_column, default_expression_column}
|
2014-09-30 03:48:40 +00:00
|
|
|
};
|
2013-02-22 09:41:13 +00:00
|
|
|
}
|
|
|
|
};
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
2013-02-21 10:02:33 +00:00
|
|
|
}
|