ClickHouse/src/Interpreters/InterpreterExistsQuery.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.9 KiB
C++
Raw Normal View History

#include <Storages/IStorage.h>
#include <Parsers/TablePropertiesQueriesASTs.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>
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnsNumber.h>
2017-05-23 18:01:50 +00:00
#include <Interpreters/Context.h>
#include <Interpreters/InterpreterExistsQuery.h>
#include <Access/Common/AccessFlags.h>
2017-07-13 20:58:19 +00:00
#include <Common/typeid_cast.h>
2016-12-12 07:24:56 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int SYNTAX_ERROR;
}
2016-12-12 07:24:56 +00:00
BlockIO InterpreterExistsQuery::execute()
{
BlockIO res;
2021-09-15 19:35:48 +00:00
res.pipeline = executeImpl();
2016-12-12 07:24:56 +00:00
return res;
}
Block InterpreterExistsQuery::getSampleBlock()
{
return Block{{
ColumnUInt8::create(),
std::make_shared<DataTypeUInt8>(),
"result" }};
2016-12-12 07:24:56 +00:00
}
2021-09-15 19:35:48 +00:00
QueryPipeline InterpreterExistsQuery::executeImpl()
2016-12-12 07:24:56 +00:00
{
ASTQueryWithTableAndOutput * exists_query;
bool result = false;
2019-12-15 06:34:43 +00:00
if ((exists_query = query_ptr->as<ASTExistsTableQuery>()))
{
if (exists_query->temporary)
2020-01-24 16:20:36 +00:00
{
result = static_cast<bool>(getContext()->tryResolveStorageID(
{"", exists_query->getTable()}, Context::ResolveExternal));
2020-01-24 16:20:36 +00:00
}
else
2020-01-24 16:20:36 +00:00
{
String database = getContext()->resolveDatabase(exists_query->getDatabase());
getContext()->checkAccess(AccessType::SHOW_TABLES, database, exists_query->getTable());
result = DatabaseCatalog::instance().isTableExist({database, exists_query->getTable()}, getContext());
2020-01-24 16:20:36 +00:00
}
}
2020-12-27 14:14:08 +00:00
else if ((exists_query = query_ptr->as<ASTExistsViewQuery>()))
{
String database = getContext()->resolveDatabase(exists_query->getDatabase());
getContext()->checkAccess(AccessType::SHOW_TABLES, database, exists_query->getTable());
auto table = DatabaseCatalog::instance().tryGetTable({database, exists_query->getTable()}, getContext());
result = table && table->isView();
2020-12-27 14:14:08 +00:00
}
2020-12-24 09:16:47 +00:00
else if ((exists_query = query_ptr->as<ASTExistsDatabaseQuery>()))
{
String database = getContext()->resolveDatabase(exists_query->getDatabase());
getContext()->checkAccess(AccessType::SHOW_DATABASES, database);
2020-12-24 09:16:47 +00:00
result = DatabaseCatalog::instance().isDatabaseExist(database);
}
2019-12-15 06:34:43 +00:00
else if ((exists_query = query_ptr->as<ASTExistsDictionaryQuery>()))
{
if (exists_query->temporary)
throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
String database = getContext()->resolveDatabase(exists_query->getDatabase());
getContext()->checkAccess(AccessType::SHOW_DICTIONARIES, database, exists_query->getTable());
result = DatabaseCatalog::instance().isDictionaryExist({database, exists_query->getTable()});
}
2016-12-12 07:24:56 +00:00
2021-09-15 19:35:48 +00:00
return QueryPipeline(std::make_shared<SourceFromSingleChunk>(Block{{
ColumnUInt8::create(1, result),
2016-12-12 07:24:56 +00:00
std::make_shared<DataTypeUInt8>(),
2021-09-15 19:35:48 +00:00
"result" }}));
2016-12-12 07:24:56 +00:00
}
}