1#include <Storages/IStorage.h>
2#include <Parsers/TablePropertiesQueriesASTs.h>
3#include <DataStreams/OneBlockInputStream.h>
4#include <DataStreams/BlockIO.h>
5#include <DataStreams/copyData.h>
6#include <DataTypes/DataTypesNumber.h>
7#include <Columns/ColumnsNumber.h>
8#include <Interpreters/Context.h>
9#include <Interpreters/InterpreterExistsQuery.h>
10#include <Common/typeid_cast.h>
11
12
13namespace DB
14{
15
16namespace ErrorCodes
17{
18 extern const int SYNTAX_ERROR;
19}
20
21BlockIO InterpreterExistsQuery::execute()
22{
23 BlockIO res;
24 res.in = executeImpl();
25 return res;
26}
27
28
29Block InterpreterExistsQuery::getSampleBlock()
30{
31 return Block{{
32 ColumnUInt8::create(),
33 std::make_shared<DataTypeUInt8>(),
34 "result" }};
35}
36
37
38BlockInputStreamPtr InterpreterExistsQuery::executeImpl()
39{
40 ASTQueryWithTableAndOutput * exists_query;
41 bool result = false;
42 if ((exists_query = query_ptr->as<ASTExistsTableQuery>()))
43 {
44 if (exists_query->temporary)
45 result = context.isExternalTableExist(exists_query->table);
46 else
47 result = context.isTableExist(exists_query->database, exists_query->table);
48 }
49 else if ((exists_query = query_ptr->as<ASTExistsDictionaryQuery>()))
50 {
51 if (exists_query->temporary)
52 throw Exception("Temporary dictionaries are not possible.", ErrorCodes::SYNTAX_ERROR);
53 result = context.isDictionaryExists(exists_query->database, exists_query->table);
54 }
55
56 return std::make_shared<OneBlockInputStream>(Block{{
57 ColumnUInt8::create(1, result),
58 std::make_shared<DataTypeUInt8>(),
59 "result" }});
60}
61
62}
63