2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DataStreams/BlockIO.h>
|
|
|
|
#include <DataStreams/copyData.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2017-05-23 18:01:50 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterExistsQuery.h>
|
2020-01-26 09:49:53 +00:00
|
|
|
#include <Access/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
|
|
|
|
{
|
|
|
|
|
2019-10-11 13:21:52 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-12 07:24:56 +00:00
|
|
|
BlockIO InterpreterExistsQuery::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO res;
|
|
|
|
res.in = executeImpl();
|
|
|
|
return res;
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block InterpreterExistsQuery::getSampleBlock()
|
|
|
|
{
|
2018-02-15 18:54:12 +00:00
|
|
|
return Block{{
|
|
|
|
ColumnUInt8::create(),
|
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
"result" }};
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr InterpreterExistsQuery::executeImpl()
|
|
|
|
{
|
2019-10-11 13:21:52 +00:00
|
|
|
ASTQueryWithTableAndOutput * exists_query;
|
|
|
|
bool result = false;
|
2021-01-14 11:54:03 +00:00
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
if ((exists_query = query_ptr->as<ASTExistsTableQuery>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
|
|
|
if (exists_query->temporary)
|
2020-01-24 16:20:36 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
result = getContext()->tryResolveStorageID({"", exists_query->table}, Context::ResolveExternal);
|
2020-01-24 16:20:36 +00:00
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
else
|
2020-01-24 16:20:36 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
String database = getContext()->resolveDatabase(exists_query->database);
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_TABLES, database, exists_query->table);
|
|
|
|
result = DatabaseCatalog::instance().isTableExist({database, exists_query->table}, getContext());
|
2020-01-24 16:20:36 +00:00
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2020-12-27 14:14:08 +00:00
|
|
|
else if ((exists_query = query_ptr->as<ASTExistsViewQuery>()))
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
String database = getContext()->resolveDatabase(exists_query->database);
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_TABLES, database, exists_query->table);
|
|
|
|
auto table = DatabaseCatalog::instance().tryGetTable({database, exists_query->table}, getContext());
|
2021-01-14 11:54:03 +00:00
|
|
|
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>()))
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
String database = getContext()->resolveDatabase(exists_query->database);
|
|
|
|
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>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
|
|
|
if (exists_query->temporary)
|
|
|
|
throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
|
2021-04-10 23:33:54 +00:00
|
|
|
String database = getContext()->resolveDatabase(exists_query->database);
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_DICTIONARIES, database, exists_query->table);
|
2020-03-13 15:41:36 +00:00
|
|
|
result = DatabaseCatalog::instance().isDictionaryExist({database, exists_query->table});
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<OneBlockInputStream>(Block{{
|
2019-10-11 13:21:52 +00:00
|
|
|
ColumnUInt8::create(1, result),
|
2017-04-01 07:20:54 +00:00
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
"result" }});
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|