1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#include <Interpreters/InterpreterShowColumnsQuery.h>
#include <Common/quoteString.h>
#include <IO/Operators.h>
#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 &>();
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();
String rewritten_query = R"(
SELECT
name AS field,
type AS type,
startsWith(type, 'Nullable') AS null,
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,
'' AS extra )";
// 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>
rewritten_query += R"(,
NULL AS collation,
comment,
'' AS privileges )";
}
rewritten_query += fmt::format(R"(
FROM system.columns
WHERE
database = '{}'
AND table = '{}' )", database, table);
if (!query.like.empty())
{
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);
}
else if (query.where_expression)
rewritten_query += fmt::format(" AND ({})", query.where_expression);
/// 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.
rewritten_query += " ORDER BY field, type, null, key, default, extra";
if (query.limit_length)
rewritten_query += fmt::format(" LIMIT {}", query.limit_length);
return rewritten_query;
}
BlockIO InterpreterShowColumnsQuery::execute()
{
return executeQuery(getRewrittenQuery(), getContext(), true);
}
}
|