2023-03-26 14:36:38 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTShowTablesQuery.h>
|
2019-09-15 16:07:27 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-02-03 12:54:36 +00:00
|
|
|
#include <Interpreters/DatabaseCatalog.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/executeQuery.h>
|
|
|
|
#include <Interpreters/InterpreterShowTablesQuery.h>
|
2022-06-23 15:46:27 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Storages/ColumnsDescription.h>
|
2022-08-26 17:23:46 +00:00
|
|
|
#include <Interpreters/Cache/FileCacheFactory.h>
|
2022-06-23 15:46:27 +00:00
|
|
|
#include <Processors/Sources/SourceFromSingleChunk.h>
|
|
|
|
#include <Access/Common/AccessFlags.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2020-11-10 18:22:26 +00:00
|
|
|
#include <IO/Operators.h>
|
2018-02-08 19:14:59 +00:00
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-02-08 19:53:34 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
InterpreterShowTablesQuery::InterpreterShowTablesQuery(const ASTPtr & query_ptr_, ContextMutablePtr context_)
|
2023-03-26 14:36:38 +00:00
|
|
|
: WithMutableContext(context_)
|
|
|
|
, query_ptr(query_ptr_)
|
2012-06-18 07:49:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String InterpreterShowTablesQuery::getRewrittenQuery()
|
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & query = query_ptr->as<ASTShowTablesQuery &>();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
/// SHOW DATABASES
|
2019-03-15 16:14:13 +00:00
|
|
|
if (query.databases)
|
2020-09-07 02:54:59 +00:00
|
|
|
{
|
2020-11-10 18:22:26 +00:00
|
|
|
WriteBufferFromOwnString rewritten_query;
|
2020-09-07 02:54:59 +00:00
|
|
|
rewritten_query << "SELECT name FROM system.databases";
|
|
|
|
|
|
|
|
if (!query.like.empty())
|
|
|
|
{
|
2020-09-09 03:24:47 +00:00
|
|
|
rewritten_query
|
|
|
|
<< " WHERE name "
|
|
|
|
<< (query.not_like ? "NOT " : "")
|
|
|
|
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
|
2020-11-10 18:22:26 +00:00
|
|
|
<< DB::quote << query.like;
|
2020-09-07 02:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (query.limit_length)
|
|
|
|
rewritten_query << " LIMIT " << query.limit_length;
|
|
|
|
|
2023-03-28 20:55:28 +00:00
|
|
|
/// (*)
|
|
|
|
rewritten_query << " ORDER BY name";
|
|
|
|
|
2020-09-07 02:54:59 +00:00
|
|
|
return rewritten_query.str();
|
|
|
|
}
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2020-10-26 19:12:40 +00:00
|
|
|
/// SHOW CLUSTER/CLUSTERS
|
2020-06-05 09:13:13 +00:00
|
|
|
if (query.clusters)
|
|
|
|
{
|
2020-11-10 18:22:26 +00:00
|
|
|
WriteBufferFromOwnString rewritten_query;
|
2020-06-09 02:39:37 +00:00
|
|
|
rewritten_query << "SELECT DISTINCT cluster FROM system.clusters";
|
2020-06-05 09:13:13 +00:00
|
|
|
|
|
|
|
if (!query.like.empty())
|
2020-06-08 02:23:57 +00:00
|
|
|
{
|
2020-09-09 03:24:47 +00:00
|
|
|
rewritten_query
|
|
|
|
<< " WHERE cluster "
|
|
|
|
<< (query.not_like ? "NOT " : "")
|
|
|
|
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
|
2020-11-10 18:22:26 +00:00
|
|
|
<< DB::quote << query.like;
|
2020-06-08 02:23:57 +00:00
|
|
|
}
|
2020-06-05 09:13:13 +00:00
|
|
|
|
2023-03-28 20:55:28 +00:00
|
|
|
/// (*)
|
|
|
|
rewritten_query << " ORDER BY cluster";
|
|
|
|
|
2023-03-29 10:29:17 +00:00
|
|
|
if (query.limit_length)
|
|
|
|
rewritten_query << " LIMIT " << query.limit_length;
|
|
|
|
|
2020-06-08 02:23:57 +00:00
|
|
|
return rewritten_query.str();
|
2020-06-05 09:13:13 +00:00
|
|
|
}
|
|
|
|
else if (query.cluster)
|
|
|
|
{
|
2020-11-10 18:22:26 +00:00
|
|
|
WriteBufferFromOwnString rewritten_query;
|
2020-06-08 02:23:57 +00:00
|
|
|
rewritten_query << "SELECT * FROM system.clusters";
|
2020-06-05 09:13:13 +00:00
|
|
|
|
2020-11-10 18:22:26 +00:00
|
|
|
rewritten_query << " WHERE cluster = " << DB::quote << query.cluster_str;
|
2020-06-05 09:13:13 +00:00
|
|
|
|
2023-03-28 20:55:28 +00:00
|
|
|
/// (*)
|
2023-03-30 15:37:31 +00:00
|
|
|
rewritten_query << " ORDER BY cluster, shard_num, replica_num, host_name, host_address, port";
|
2023-03-28 20:55:28 +00:00
|
|
|
|
2020-06-08 02:23:57 +00:00
|
|
|
return rewritten_query.str();
|
2020-06-05 09:13:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 02:33:51 +00:00
|
|
|
/// SHOW SETTINGS
|
|
|
|
if (query.m_settings)
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString rewritten_query;
|
|
|
|
rewritten_query << "SELECT name, type, value FROM system.settings";
|
|
|
|
|
2020-12-15 06:44:39 +00:00
|
|
|
if (query.changed)
|
|
|
|
rewritten_query << " WHERE changed = 1";
|
|
|
|
|
2020-12-14 02:33:51 +00:00
|
|
|
if (!query.like.empty())
|
|
|
|
{
|
|
|
|
rewritten_query
|
2020-12-15 06:44:39 +00:00
|
|
|
<< (query.changed ? " AND name " : " WHERE name ")
|
2020-12-14 02:33:51 +00:00
|
|
|
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
|
|
|
|
<< DB::quote << query.like;
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:55:28 +00:00
|
|
|
/// (*)
|
|
|
|
rewritten_query << " ORDER BY name, type, value ";
|
|
|
|
|
2020-12-14 02:33:51 +00:00
|
|
|
return rewritten_query.str();
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
if (query.temporary && !query.from.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "The `FROM` and `TEMPORARY` cannot be used together in `SHOW TABLES`");
|
2018-02-02 14:45:36 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
String database = getContext()->resolveDatabase(query.from);
|
2020-02-10 13:10:17 +00:00
|
|
|
DatabaseCatalog::instance().assertDatabaseExists(database);
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2020-11-10 18:22:26 +00:00
|
|
|
WriteBufferFromOwnString rewritten_query;
|
2022-12-03 20:42:18 +00:00
|
|
|
|
|
|
|
if (query.full)
|
|
|
|
{
|
|
|
|
rewritten_query << "SELECT name, engine FROM system.";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rewritten_query << "SELECT name FROM system.";
|
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
|
|
|
|
if (query.dictionaries)
|
|
|
|
rewritten_query << "dictionaries ";
|
|
|
|
else
|
|
|
|
rewritten_query << "tables ";
|
|
|
|
|
|
|
|
rewritten_query << "WHERE ";
|
2018-02-02 13:17:45 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
if (query.temporary)
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
|
|
|
if (query.dictionaries)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::SYNTAX_ERROR, "Temporary dictionaries are not possible.");
|
2018-02-08 19:41:10 +00:00
|
|
|
rewritten_query << "is_temporary";
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2018-02-02 13:17:45 +00:00
|
|
|
else
|
2020-11-10 18:22:26 +00:00
|
|
|
rewritten_query << "database = " << DB::quote << database;
|
2012-06-18 07:49:19 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
if (!query.like.empty())
|
2020-09-09 03:24:47 +00:00
|
|
|
rewritten_query
|
|
|
|
<< " AND name "
|
|
|
|
<< (query.not_like ? "NOT " : "")
|
|
|
|
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
|
2020-11-10 18:22:26 +00:00
|
|
|
<< DB::quote << query.like;
|
2020-02-11 11:35:30 +00:00
|
|
|
else if (query.where_expression)
|
2020-02-13 13:46:35 +00:00
|
|
|
rewritten_query << " AND (" << query.where_expression << ")";
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2023-03-28 20:55:28 +00:00
|
|
|
/// (*)
|
|
|
|
rewritten_query << " ORDER BY name ";
|
|
|
|
|
2019-09-15 16:07:27 +00:00
|
|
|
if (query.limit_length)
|
|
|
|
rewritten_query << " LIMIT " << query.limit_length;
|
|
|
|
|
2012-06-18 07:49:19 +00:00
|
|
|
return rewritten_query.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterShowTablesQuery::execute()
|
|
|
|
{
|
2022-06-23 15:46:27 +00:00
|
|
|
const auto & query = query_ptr->as<ASTShowTablesQuery &>();
|
|
|
|
if (query.caches)
|
|
|
|
{
|
2022-09-19 12:02:51 +00:00
|
|
|
getContext()->checkAccess(AccessType::SHOW_FILESYSTEM_CACHES);
|
2022-06-23 15:46:27 +00:00
|
|
|
|
|
|
|
Block sample_block{ColumnWithTypeAndName(std::make_shared<DataTypeString>(), "Caches")};
|
|
|
|
MutableColumns res_columns = sample_block.cloneEmptyColumns();
|
2023-04-15 11:08:49 +00:00
|
|
|
auto caches = FileCacheFactory::instance().getAll();
|
2022-06-23 15:46:27 +00:00
|
|
|
for (const auto & [name, _] : caches)
|
|
|
|
res_columns[0]->insert(name);
|
|
|
|
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));
|
|
|
|
res.pipeline = QueryPipeline(std::move(source));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
return executeQuery(getRewrittenQuery(), getContext(), true);
|
2012-06-18 07:49:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:55:28 +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 <INFO> otherwise (SELECT * FROM (SHOW <INFO> ...) ORDER BY ...) is rejected) and 3. some
|
|
|
|
/// SQL tests can take advantage of this.
|
2012-06-18 07:49:19 +00:00
|
|
|
|
|
|
|
}
|