2023-03-26 14:36:38 +00:00
|
|
|
#include <Interpreters/InterpreterShowColumnsQuery.h>
|
|
|
|
|
2023-05-05 11:03:47 +00:00
|
|
|
#include <Common/quoteString.h>
|
|
|
|
#include <IO/Operators.h>
|
2023-03-26 14:36:38 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <Parsers/ASTShowColumnsQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
InterpreterShowColumnsQuery::InterpreterShowColumnsQuery(const ASTPtr & query_ptr_, ContextMutablePtr context_)
|
|
|
|
: WithMutableContext(context_)
|
|
|
|
, query_ptr(query_ptr_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String InterpreterShowColumnsQuery::getRewrittenQuery()
|
|
|
|
{
|
|
|
|
const auto & query = query_ptr->as<ASTShowColumnsQuery &>();
|
|
|
|
|
2023-05-05 11:03:47 +00:00
|
|
|
WriteBufferFromOwnString buf_database;
|
|
|
|
String resolved_database = getContext()->resolveDatabase(query.database);
|
|
|
|
writeEscapedString(resolved_database, buf_database);
|
|
|
|
String database = buf_database.str();
|
|
|
|
|
|
|
|
WriteBufferFromOwnString buf_table;
|
|
|
|
writeEscapedString(query.table, buf_table);
|
|
|
|
String table = buf_table.str();
|
2023-03-26 14:36:38 +00:00
|
|
|
|
2023-05-02 12:14:17 +00:00
|
|
|
String rewritten_query = R"(
|
|
|
|
SELECT
|
|
|
|
name AS field,
|
|
|
|
type AS type,
|
2023-09-12 16:39:03 +00:00
|
|
|
if (startsWith(type, 'Nullable'), 'YES', 'NO') AS `null`,
|
2023-05-05 11:03:47 +00:00
|
|
|
trim(concatWithSeparator(' ', if (is_in_primary_key, 'PRI', ''), if (is_in_sorting_key, 'SOR', ''))) AS key,
|
|
|
|
if (default_kind IN ('ALIAS', 'DEFAULT', 'MATERIALIZED'), default_expression, NULL) AS default,
|
2023-05-02 12:14:17 +00:00
|
|
|
'' AS extra )";
|
2023-03-26 14:36:38 +00:00
|
|
|
|
2023-09-05 18:34:33 +00:00
|
|
|
// Known issue: Field 'null' is wrong for types like 'LowCardinality(Nullable(String))'. Can't simply replace 'startsWith' by
|
|
|
|
// `hasSubsequence` as that would return `true` for non-nullable types such as `Tuple(Nullable(String), String)`...
|
|
|
|
|
2023-03-26 14:36:38 +00:00
|
|
|
// TODO Interpret query.extended. It is supposed to show internal/virtual columns. Need to fetch virtual column names, see
|
|
|
|
// IStorage::getVirtuals(). We can't easily do that via SQL.
|
|
|
|
|
|
|
|
if (query.full)
|
|
|
|
{
|
|
|
|
/// "Full" mode is mostly for MySQL compat
|
|
|
|
/// - collation: no such thing in ClickHouse
|
|
|
|
/// - comment
|
|
|
|
/// - privileges: <not implemented, TODO ask system.grants>
|
2023-05-02 12:14:17 +00:00
|
|
|
rewritten_query += R"(,
|
|
|
|
NULL AS collation,
|
|
|
|
comment,
|
|
|
|
'' AS privileges )";
|
2023-03-26 14:36:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 12:14:17 +00:00
|
|
|
rewritten_query += fmt::format(R"(
|
2023-05-05 11:03:47 +00:00
|
|
|
FROM system.columns
|
|
|
|
WHERE
|
|
|
|
database = '{}'
|
|
|
|
AND table = '{}' )", database, table);
|
2023-03-26 14:36:38 +00:00
|
|
|
|
|
|
|
if (!query.like.empty())
|
2023-05-02 12:14:17 +00:00
|
|
|
{
|
|
|
|
rewritten_query += " AND name ";
|
|
|
|
if (query.not_like)
|
|
|
|
rewritten_query += "NOT ";
|
|
|
|
if (query.case_insensitive_like)
|
|
|
|
rewritten_query += "ILIKE ";
|
|
|
|
else
|
|
|
|
rewritten_query += "LIKE ";
|
|
|
|
rewritten_query += fmt::format("'{}'", query.like);
|
|
|
|
}
|
2023-03-26 14:36:38 +00:00
|
|
|
else if (query.where_expression)
|
2023-05-02 12:14:17 +00:00
|
|
|
rewritten_query += fmt::format(" AND ({})", query.where_expression);
|
2023-03-26 14:36:38 +00:00
|
|
|
|
|
|
|
/// Sorting is strictly speaking not necessary but 1. it is convenient for users, 2. SQL currently does not allow to
|
|
|
|
/// sort the output of SHOW COLUMNS otherwise (SELECT * FROM (SHOW COLUMNS ...) ORDER BY ...) is rejected) and 3. some
|
|
|
|
/// SQL tests can take advantage of this.
|
2023-05-02 12:14:17 +00:00
|
|
|
rewritten_query += " ORDER BY field, type, null, key, default, extra";
|
2023-03-26 14:36:38 +00:00
|
|
|
|
|
|
|
if (query.limit_length)
|
2023-05-02 12:14:17 +00:00
|
|
|
rewritten_query += fmt::format(" LIMIT {}", query.limit_length);
|
2023-03-26 14:36:38 +00:00
|
|
|
|
2023-05-02 12:14:17 +00:00
|
|
|
return rewritten_query;
|
2023-03-26 14:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterShowColumnsQuery::execute()
|
|
|
|
{
|
|
|
|
return executeQuery(getRewrittenQuery(), getContext(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|