2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2021-09-15 19:35:48 +00:00
|
|
|
#include <Processors/Sources/SourceFromSingleChunk.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <QueryPipeline/BlockIO.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Parsers/queryToString.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-11-01 14:34:05 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
|
2022-07-14 11:20:16 +00:00
|
|
|
#include <Interpreters/InterpreterSelectQueryAnalyzer.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/InterpreterDescribeQuery.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Access/Common/AccessFlags.h>
|
2017-11-01 14:34:05 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2017-11-01 14:34:05 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
2021-09-11 20:24:01 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2016-12-12 07:24:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-09-11 20:24:01 +00:00
|
|
|
Block InterpreterDescribeQuery::getSampleBlock(bool include_subcolumns)
|
2016-12-12 07:24:56 +00:00
|
|
|
{
|
|
|
|
Block block;
|
|
|
|
|
|
|
|
ColumnWithTypeAndName col;
|
|
|
|
col.name = "name";
|
|
|
|
col.type = std::make_shared<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);
|
|
|
|
|
2019-02-19 17:02:51 +00:00
|
|
|
col.name = "comment";
|
2018-11-06 13:26:43 +00:00
|
|
|
block.insert(col);
|
|
|
|
|
2018-12-25 10:04:38 +00:00
|
|
|
col.name = "codec_expression";
|
|
|
|
block.insert(col);
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
col.name = "ttl_expression";
|
|
|
|
block.insert(col);
|
|
|
|
|
2021-09-11 20:24:01 +00:00
|
|
|
if (include_subcolumns)
|
|
|
|
{
|
|
|
|
col.name = "is_subcolumn";
|
|
|
|
col.type = std::make_shared<DataTypeUInt8>();
|
|
|
|
col.column = col.type->createColumn();
|
|
|
|
block.insert(col);
|
|
|
|
}
|
|
|
|
|
2016-12-12 07:24:56 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:24:01 +00:00
|
|
|
BlockIO InterpreterDescribeQuery::execute()
|
2016-12-12 07:24:56 +00:00
|
|
|
{
|
2022-08-31 15:21:17 +00:00
|
|
|
std::vector<ColumnDescription> columns;
|
2021-07-09 14:54:53 +00:00
|
|
|
StorageSnapshotPtr storage_snapshot;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-16 21:46:53 +00:00
|
|
|
const auto & ast = query_ptr->as<ASTDescribeQuery &>();
|
|
|
|
const auto & table_expression = ast.table_expression->as<ASTTableExpression &>();
|
2021-09-14 14:02:32 +00:00
|
|
|
const auto & settings = getContext()->getSettingsRef();
|
2021-07-09 14:54:53 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (table_expression.subquery)
|
2018-02-28 04:55:43 +00:00
|
|
|
{
|
2022-07-14 11:20:16 +00:00
|
|
|
NamesAndTypesList names_and_types;
|
|
|
|
auto select_query = table_expression.subquery->children.at(0);
|
|
|
|
auto current_context = getContext();
|
|
|
|
|
2022-10-25 10:35:25 +00:00
|
|
|
if (settings.allow_experimental_analyzer)
|
2022-07-14 11:20:16 +00:00
|
|
|
{
|
|
|
|
SelectQueryOptions select_query_options;
|
2022-12-23 17:45:28 +00:00
|
|
|
names_and_types = InterpreterSelectQueryAnalyzer(select_query, current_context, select_query_options).getSampleBlock().getNamesAndTypesList();
|
2022-07-14 11:20:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
names_and_types = InterpreterSelectWithUnionQuery::getSampleBlock(select_query, current_context).getNamesAndTypesList();
|
|
|
|
}
|
|
|
|
|
2022-08-31 15:21:17 +00:00
|
|
|
for (auto && [name, type] : names_and_types)
|
|
|
|
{
|
|
|
|
ColumnDescription description;
|
|
|
|
description.name = std::move(name);
|
|
|
|
description.type = std::move(type);
|
|
|
|
columns.emplace_back(std::move(description));
|
|
|
|
}
|
2018-02-28 04:55:43 +00:00
|
|
|
}
|
2020-10-14 12:19:29 +00:00
|
|
|
else if (table_expression.table_function)
|
|
|
|
{
|
2021-09-14 14:02:32 +00:00
|
|
|
TableFunctionPtr table_function_ptr = TableFunctionFactory::instance().get(table_expression.table_function, getContext());
|
2022-08-31 15:21:17 +00:00
|
|
|
auto table_function_column_descriptions = table_function_ptr->getActualTableStructure(getContext());
|
|
|
|
for (const auto & table_function_column_description : table_function_column_descriptions)
|
|
|
|
columns.emplace_back(table_function_column_description);
|
2020-10-14 12:19:29 +00:00
|
|
|
}
|
2017-11-01 14:34:05 +00:00
|
|
|
else
|
2016-12-12 07:24:56 +00:00
|
|
|
{
|
2021-09-14 14:02:32 +00:00
|
|
|
auto table_id = getContext()->resolveStorageID(table_expression.database_and_table_name);
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_COLUMNS, table_id);
|
|
|
|
auto table = DatabaseCatalog::instance().getTable(table_id, getContext());
|
|
|
|
auto table_lock = table->lockForShare(getContext()->getInitialQueryId(), settings.lock_acquire_timeout);
|
2021-07-09 14:54:53 +00:00
|
|
|
|
2020-06-17 16:39:58 +00:00
|
|
|
auto metadata_snapshot = table->getInMemoryMetadataPtr();
|
2022-03-17 17:26:18 +00:00
|
|
|
storage_snapshot = table->getStorageSnapshot(metadata_snapshot, getContext());
|
2022-08-31 15:21:17 +00:00
|
|
|
auto metadata_column_descriptions = metadata_snapshot->getColumns();
|
|
|
|
for (const auto & metadata_column_description : metadata_column_descriptions)
|
|
|
|
columns.emplace_back(metadata_column_description);
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-12-01 02:58:24 +00:00
|
|
|
bool extend_object_types = settings.describe_extend_object_types && storage_snapshot;
|
|
|
|
bool include_subcolumns = settings.describe_include_subcolumns;
|
|
|
|
|
2021-09-11 20:24:01 +00:00
|
|
|
Block sample_block = getSampleBlock(include_subcolumns);
|
2017-12-15 18:23:05 +00:00
|
|
|
MutableColumns res_columns = sample_block.cloneEmptyColumns();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-11-01 14:34:05 +00:00
|
|
|
for (const auto & column : columns)
|
2016-12-12 07:24:56 +00:00
|
|
|
{
|
2017-12-15 18:23:05 +00:00
|
|
|
res_columns[0]->insert(column.name);
|
2021-07-09 14:54:53 +00:00
|
|
|
|
2021-12-01 02:58:24 +00:00
|
|
|
if (extend_object_types)
|
2021-07-09 14:54:53 +00:00
|
|
|
res_columns[1]->insert(storage_snapshot->getConcreteType(column.name)->getName());
|
|
|
|
else
|
|
|
|
res_columns[1]->insert(column.type->getName());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (column.default_desc.expression)
|
2016-12-12 07:24:56 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[2]->insert(toString(column.default_desc.kind));
|
|
|
|
res_columns[3]->insert(queryToString(column.default_desc.expression));
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[2]->insertDefault();
|
|
|
|
res_columns[3]->insertDefault();
|
2018-11-06 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[4]->insert(column.comment);
|
2018-12-25 10:04:38 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (column.codec)
|
2020-08-28 17:40:45 +00:00
|
|
|
res_columns[5]->insert(queryToString(column.codec->as<ASTFunction>()->arguments));
|
2018-12-25 10:04:38 +00:00
|
|
|
else
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[5]->insertDefault();
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
if (column.ttl)
|
|
|
|
res_columns[6]->insert(queryToString(column.ttl));
|
|
|
|
else
|
|
|
|
res_columns[6]->insertDefault();
|
2021-09-11 20:24:01 +00:00
|
|
|
|
|
|
|
if (include_subcolumns)
|
|
|
|
res_columns[7]->insertDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (include_subcolumns)
|
|
|
|
{
|
|
|
|
for (const auto & column : columns)
|
|
|
|
{
|
2021-12-01 02:58:24 +00:00
|
|
|
auto type = extend_object_types ? storage_snapshot->getConcreteType(column.name) : column.type;
|
|
|
|
|
2021-10-11 22:01:00 +00:00
|
|
|
IDataType::forEachSubcolumn([&](const auto & path, const auto & name, const auto & data)
|
2021-09-11 20:24:01 +00:00
|
|
|
{
|
|
|
|
res_columns[0]->insert(Nested::concatenateName(column.name, name));
|
2021-10-11 22:01:00 +00:00
|
|
|
res_columns[1]->insert(data.type->getName());
|
2021-09-11 20:24:01 +00:00
|
|
|
|
|
|
|
/// It's not trivial to calculate default expression for subcolumn.
|
|
|
|
/// So, leave it empty.
|
|
|
|
res_columns[2]->insertDefault();
|
|
|
|
res_columns[3]->insertDefault();
|
|
|
|
res_columns[4]->insert(column.comment);
|
|
|
|
|
|
|
|
if (column.codec && ISerialization::isSpecialCompressionAllowed(path))
|
|
|
|
res_columns[5]->insert(queryToString(column.codec->as<ASTFunction>()->arguments));
|
|
|
|
else
|
|
|
|
res_columns[5]->insertDefault();
|
|
|
|
|
|
|
|
if (column.ttl)
|
|
|
|
res_columns[6]->insert(queryToString(column.ttl));
|
|
|
|
else
|
|
|
|
res_columns[6]->insertDefault();
|
|
|
|
|
|
|
|
res_columns[7]->insert(1u);
|
2022-06-17 01:10:52 +00:00
|
|
|
}, ISerialization::SubstreamData(type->getDefaultSerialization()).withType(type));
|
2021-09-11 20:24:01 +00:00
|
|
|
}
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-09-11 20:24:01 +00:00
|
|
|
BlockIO res;
|
|
|
|
size_t num_rows = res_columns[0]->size();
|
|
|
|
auto source = std::make_shared<SourceFromSingleChunk>(sample_block, Chunk(std::move(res_columns), num_rows));
|
2021-09-17 12:38:11 +00:00
|
|
|
res.pipeline = QueryPipeline(std::move(source));
|
2021-09-11 20:24:01 +00:00
|
|
|
|
|
|
|
return res;
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|