show defaulted columns info in DESC TABLE query output. [#METR-12739]

This commit is contained in:
Andrey Mironov 2014-09-30 07:48:40 +04:00
parent 8052020218
commit 34dba980c7
5 changed files with 50 additions and 28 deletions

View File

@ -10,6 +10,8 @@
#include <DB/DataTypes/DataTypesNumberFixed.h>
#include <DB/DataTypes/DataTypeString.h>
#include <DB/Columns/ColumnString.h>
#include <DB/Parsers/formatAST.h>
namespace DB
@ -59,13 +61,18 @@ private:
col.name = "name";
col.type = new DataTypeString;
col.column = col.type->createColumn();
block.insert(col);
col.name = "type";
block.insert(col);
col.name = "default_type";
block.insert(col);
col.name = "default_expression";
block.insert(col);
return block;
}
@ -74,27 +81,48 @@ private:
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;
}
ColumnString * name_column = new ColumnString;
ColumnString * type_column = new ColumnString;
ColumnWithNameAndType name_column{new ColumnString, new DataTypeString, "name"};
ColumnWithNameAndType type_column{new ColumnString, new DataTypeString, "type" };
ColumnWithNameAndType default_type_column{new ColumnString, new DataTypeString, "default_type" };
ColumnWithNameAndType default_expression_column{new ColumnString, new DataTypeString, "default_expression" };;
Block block;
block.insert(ColumnWithNameAndType(name_column, new DataTypeString, "name"));
block.insert(ColumnWithNameAndType(type_column, new DataTypeString, "type"));
for (NamesAndTypesList::iterator it = columns.begin(); it != columns.end(); ++it)
auto has_defaults = false;
for (const auto column : columns)
{
name_column->insert(it->name);
type_column->insert(it->type->getName());
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
{
has_defaults = true;
default_type_column.column->insert(toString(it->second.type));
default_expression_column.column->insert(queryToString(
setAlias(it->second.expression->clone(), "")));
}
}
return new OneBlockInputStream(block);
return new OneBlockInputStream{
has_defaults
? Block{name_column, type_column, default_type_column, default_expression_column}
: Block{name_column, type_column}
};
}
};

View File

@ -73,4 +73,12 @@ void formatAST(const ASTShowProcesslistQuery & ast, std::ostream & s,
String formatColumnsForCreateQuery(NamesAndTypesList & columns);
String backQuoteIfNeed(const String & x);
inline String queryToString(const ASTPtr & query)
{
std::ostringstream out;
formatAST(*query, out, 0, false, true);
return out.str();
}
}

View File

@ -1,7 +1,8 @@
#pragma once
#include <DB/Storages/StorageDistributed.h>
#include <DB/Storages/Distributed/queryToString.h>
#include <DB/Parsers/formatAST.h>
#include <DB/IO/WriteBufferFromFile.h>
#include <DB/IO/CompressedWriteBuffer.h>

View File

@ -1,14 +0,0 @@
#pragma once
#include <DB/Parsers/formatAST.h>
namespace DB
{
inline std::string queryToString(const ASTPtr & query)
{
std::ostringstream s;
formatAST(*query, s, 0, false, true);
return s.str();
}
}

View File

@ -5,7 +5,6 @@
#include <DB/Storages/VirtualColumnFactory.h>
#include <DB/Storages/Distributed/DistributedBlockOutputStream.h>
#include <DB/Storages/Distributed/DirectoryMonitor.h>
#include <DB/Storages/Distributed/queryToString.h>
#include <DB/Common/escapeForFileName.h>
#include <DB/Interpreters/InterpreterSelectQuery.h>