ClickHouse/src/Interpreters/InterpreterShowTablesQuery.cpp

131 lines
3.5 KiB
C++
Raw Normal View History

#include <IO/ReadBufferFromString.h>
#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>
#include <Interpreters/executeQuery.h>
#include <Interpreters/InterpreterShowTablesQuery.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
2017-04-16 05:40:17 +00:00
#include <iomanip>
#include <sstream>
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
2017-05-23 18:24:43 +00:00
InterpreterShowTablesQuery::InterpreterShowTablesQuery(const ASTPtr & query_ptr_, Context & context_)
: query_ptr(query_ptr_), context(context_)
2012-06-18 07:49:19 +00:00
{
}
String InterpreterShowTablesQuery::getRewrittenQuery()
{
const auto & query = query_ptr->as<ASTShowTablesQuery &>();
/// SHOW DATABASES
if (query.databases)
2020-09-07 02:54:59 +00:00
{
std::stringstream rewritten_query;
rewritten_query << "SELECT name FROM system.databases";
if (!query.like.empty())
{
rewritten_query
<< " WHERE name "
<< (query.not_like ? "NOT " : "")
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
<< std::quoted(query.like, '\'');
2020-09-07 02:54:59 +00:00
}
if (query.limit_length)
rewritten_query << " LIMIT " << query.limit_length;
return rewritten_query.str();
}
2012-06-18 07:49:19 +00:00
2020-06-05 09:13:13 +00:00
/// SHOW CLUSTER/CLUSTERS
if (query.clusters)
{
std::stringstream rewritten_query;
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
{
rewritten_query
<< " WHERE cluster "
<< (query.not_like ? "NOT " : "")
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
<< std::quoted(query.like, '\'');
2020-06-08 02:23:57 +00:00
}
2020-06-05 09:13:13 +00:00
2020-06-08 02:23:57 +00:00
if (query.limit_length)
2020-06-05 09:13:13 +00:00
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)
{
std::stringstream rewritten_query;
2020-06-08 02:23:57 +00:00
rewritten_query << "SELECT * FROM system.clusters";
2020-06-05 09:13:13 +00:00
rewritten_query << " WHERE cluster = " << std::quoted(query.cluster_str, '\'');
2020-06-08 02:23:57 +00:00
return rewritten_query.str();
2020-06-05 09:13:13 +00:00
}
if (query.temporary && !query.from.empty())
2018-02-02 14:45:36 +00:00
throw Exception("The `FROM` and `TEMPORARY` cannot be used together in `SHOW TABLES`", ErrorCodes::SYNTAX_ERROR);
String database = context.resolveDatabase(query.from);
DatabaseCatalog::instance().assertDatabaseExists(database);
2012-06-18 07:49:19 +00:00
std::stringstream rewritten_query;
rewritten_query << "SELECT name FROM system.";
if (query.dictionaries)
rewritten_query << "dictionaries ";
else
rewritten_query << "tables ";
rewritten_query << "WHERE ";
2018-02-02 13:17:45 +00:00
if (query.temporary)
{
if (query.dictionaries)
throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
2018-02-08 19:41:10 +00:00
rewritten_query << "is_temporary";
}
2018-02-02 13:17:45 +00:00
else
rewritten_query << "database = " << std::quoted(database, '\'');
2012-06-18 07:49:19 +00:00
if (!query.like.empty())
rewritten_query
<< " AND name "
<< (query.not_like ? "NOT " : "")
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
<< std::quoted(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 << ")";
2019-09-15 16:07:27 +00:00
if (query.limit_length)
rewritten_query << " LIMIT " << query.limit_length;
return rewritten_query.str();
2012-06-18 07:49:19 +00:00
}
BlockIO InterpreterShowTablesQuery::execute()
{
return executeQuery(getRewrittenQuery(), context, true);
2012-06-18 07:49:19 +00:00
}
}