2016-03-19 01:18:49 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
#include <boost/range/join.hpp>
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
#include <Poco/File.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
#include <Poco/FileStream.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
|
|
|
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <DataStreams/AddingDefaultBlockOutputStream.h>
|
|
|
|
#include <DataStreams/MaterializingBlockOutputStream.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/NullAndDoCopyBlockInputStream.h>
|
|
|
|
#include <DataStreams/ProhibitColumnsBlockOutputStream.h>
|
|
|
|
#include <DataStreams/PushingToViewsBlockOutputStream.h>
|
|
|
|
|
|
|
|
#include <Parsers/ASTColumnDeclaration.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/ASTNameTypePair.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
|
|
|
|
#include <Storages/StorageFactory.h>
|
|
|
|
#include <Storages/StorageLog.h>
|
|
|
|
|
2017-05-23 18:33:48 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-13 16:12:56 +00:00
|
|
|
#include <Interpreters/DDLWorker.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2017-12-25 18:58:39 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2017-12-20 07:39:52 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
|
|
|
#include <Databases/DatabaseFactory.h>
|
|
|
|
#include <Databases/IDatabase.h>
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-06-19 20:06:35 +00:00
|
|
|
#include <Common/ZooKeeper/ZooKeeper.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int DIRECTORY_DOESNT_EXIST;
|
|
|
|
extern const int DIRECTORY_ALREADY_EXISTS;
|
|
|
|
extern const int TABLE_ALREADY_EXISTS;
|
|
|
|
extern const int EMPTY_LIST_OF_COLUMNS_PASSED;
|
|
|
|
extern const int INCORRECT_QUERY;
|
|
|
|
extern const int ENGINE_REQUIRED;
|
|
|
|
extern const int TABLE_METADATA_ALREADY_EXISTS;
|
|
|
|
extern const int UNKNOWN_DATABASE_ENGINE;
|
|
|
|
extern const int DUPLICATE_COLUMN;
|
2017-12-20 07:39:52 +00:00
|
|
|
extern const int READONLY;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 20:33:20 +00:00
|
|
|
|
2017-12-22 19:20:18 +00:00
|
|
|
InterpreterCreateQuery::InterpreterCreateQuery(const ASTPtr & query_ptr_, Context & context_)
|
|
|
|
: query_ptr(query_ptr_), context(context_)
|
|
|
|
{
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2011-11-01 15:16:04 +00:00
|
|
|
|
2017-04-25 15:21:03 +00:00
|
|
|
BlockIO InterpreterCreateQuery::createDatabase(ASTCreateQuery & create)
|
2011-08-18 20:33:20 +00:00
|
|
|
{
|
2017-04-25 15:21:03 +00:00
|
|
|
if (!create.cluster.empty())
|
|
|
|
return executeDDLQueryOnCluster(query_ptr, context);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String database_name = create.database;
|
|
|
|
|
|
|
|
if (create.if_not_exists && context.isDatabaseExist(database_name))
|
2017-04-25 15:21:03 +00:00
|
|
|
return {};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String database_engine_name;
|
|
|
|
if (!create.storage)
|
|
|
|
{
|
2017-12-20 07:39:52 +00:00
|
|
|
database_engine_name = "Ordinary"; /// Default database engine.
|
2017-09-17 18:49:43 +00:00
|
|
|
auto engine = std::make_shared<ASTFunction>();
|
|
|
|
engine->name = database_engine_name;
|
|
|
|
auto storage = std::make_shared<ASTStorage>();
|
|
|
|
storage->set(storage->engine, engine);
|
|
|
|
create.set(create.storage, storage);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-18 14:18:29 +00:00
|
|
|
const ASTStorage & storage = *create.storage;
|
|
|
|
const ASTFunction & engine = *storage.engine;
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Currently, there are no database engines, that support any arguments.
|
2017-12-20 07:39:52 +00:00
|
|
|
if (engine.arguments || engine.parameters || storage.partition_by || storage.order_by || storage.sample_by || storage.settings)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
std::stringstream ostr;
|
2017-12-01 18:36:55 +00:00
|
|
|
formatAST(storage, ostr, false, false);
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Unknown database engine: " + ostr.str(), ErrorCodes::UNKNOWN_DATABASE_ENGINE);
|
|
|
|
}
|
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
database_engine_name = engine.name;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String database_name_escaped = escapeForFileName(database_name);
|
|
|
|
|
2017-11-03 19:53:10 +00:00
|
|
|
/// Create directories for tables metadata.
|
2017-04-01 07:20:54 +00:00
|
|
|
String path = context.getPath();
|
|
|
|
String metadata_path = path + "metadata/" + database_name_escaped + "/";
|
|
|
|
Poco::File(metadata_path).createDirectory();
|
|
|
|
|
|
|
|
DatabasePtr database = DatabaseFactory::get(database_engine_name, database_name, metadata_path, context);
|
|
|
|
|
|
|
|
/// Will write file with database metadata, if needed.
|
|
|
|
String metadata_file_tmp_path = path + "metadata/" + database_name_escaped + ".sql.tmp";
|
|
|
|
String metadata_file_path = path + "metadata/" + database_name_escaped + ".sql";
|
|
|
|
|
|
|
|
bool need_write_metadata = !create.attach;
|
|
|
|
|
|
|
|
if (need_write_metadata)
|
|
|
|
{
|
|
|
|
create.attach = true;
|
|
|
|
create.if_not_exists = false;
|
|
|
|
|
|
|
|
std::ostringstream statement_stream;
|
2017-12-01 18:36:55 +00:00
|
|
|
formatAST(create, statement_stream, false);
|
2017-04-01 07:20:54 +00:00
|
|
|
statement_stream << '\n';
|
|
|
|
String statement = statement_stream.str();
|
|
|
|
|
|
|
|
/// Exclusive flag guarantees, that database is not created right now in another thread.
|
|
|
|
WriteBufferFromFile out(metadata_file_tmp_path, statement.size(), O_WRONLY | O_CREAT | O_EXCL);
|
|
|
|
writeString(statement, out);
|
|
|
|
|
|
|
|
out.next();
|
|
|
|
if (context.getSettingsRef().fsync_metadata)
|
|
|
|
out.sync();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
context.addDatabase(database_name, database);
|
|
|
|
|
|
|
|
if (need_write_metadata)
|
|
|
|
Poco::File(metadata_file_tmp_path).renameTo(metadata_file_path);
|
|
|
|
|
|
|
|
database->loadTables(context, thread_pool, has_force_restore_data_flag);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (need_write_metadata)
|
|
|
|
Poco::File(metadata_file_tmp_path).remove();
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2017-04-25 15:21:03 +00:00
|
|
|
|
|
|
|
return {};
|
2011-08-18 20:33:20 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
using ColumnsAndDefaults = std::pair<NamesAndTypesList, ColumnDefaults>;
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// AST to the list of columns with types. Columns of Nested type are expanded into a list of real columns.
|
2017-09-17 18:49:43 +00:00
|
|
|
static ColumnsAndDefaults parseColumns(const ASTExpressionList & column_list_ast, const Context & context)
|
2014-07-10 11:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// list of table columns in correct order
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList columns{};
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnDefaults defaults{};
|
|
|
|
|
|
|
|
/// Columns requiring type-deduction or default_expression type-check
|
2017-12-25 21:57:29 +00:00
|
|
|
std::vector<std::pair<NameAndTypePair *, ASTColumnDeclaration *>> defaulted_columns{};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/** all default_expressions as a single expression list,
|
|
|
|
* mixed with conversion-columns for each explicitly specified type */
|
|
|
|
ASTPtr default_expr_list = std::make_shared<ASTExpressionList>();
|
|
|
|
default_expr_list->children.reserve(column_list_ast.children.size());
|
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
for (const auto & ast : column_list_ast.children)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
auto & col_decl = typeid_cast<ASTColumnDeclaration &>(*ast);
|
|
|
|
|
|
|
|
if (col_decl.type)
|
|
|
|
{
|
2017-07-16 03:05:40 +00:00
|
|
|
columns.emplace_back(col_decl.name, DataTypeFactory::instance().get(col_decl.type));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/// we're creating dummy DataTypeUInt8 in order to prevent the NullPointerException in ExpressionActions
|
|
|
|
columns.emplace_back(col_decl.name, std::make_shared<DataTypeUInt8>());
|
|
|
|
|
|
|
|
/// add column to postprocessing if there is a default_expression specified
|
|
|
|
if (col_decl.default_expression)
|
|
|
|
{
|
|
|
|
defaulted_columns.emplace_back(&columns.back(), &col_decl);
|
|
|
|
|
|
|
|
/** for columns with explicitly-specified type create two expressions:
|
|
|
|
* 1. default_expression aliased as column name with _tmp suffix
|
|
|
|
* 2. conversion of expression (1) to explicitly-specified type alias as column name */
|
|
|
|
if (col_decl.type)
|
|
|
|
{
|
|
|
|
const auto & final_column_name = col_decl.name;
|
|
|
|
const auto tmp_column_name = final_column_name + "_tmp";
|
|
|
|
const auto data_type_ptr = columns.back().type.get();
|
|
|
|
|
|
|
|
default_expr_list->children.emplace_back(setAlias(
|
|
|
|
makeASTFunction("CAST", std::make_shared<ASTIdentifier>(StringRange(), tmp_column_name),
|
|
|
|
std::make_shared<ASTLiteral>(StringRange(), Field(data_type_ptr->getName()))), final_column_name));
|
|
|
|
default_expr_list->children.emplace_back(setAlias(col_decl.default_expression->clone(), tmp_column_name));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
default_expr_list->children.emplace_back(setAlias(col_decl.default_expression->clone(), col_decl.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// set missing types and wrap default_expression's in a conversion-function if necessary
|
|
|
|
if (!defaulted_columns.empty())
|
|
|
|
{
|
|
|
|
const auto actions = ExpressionAnalyzer{default_expr_list, context, {}, columns}.getActions(true);
|
|
|
|
const auto block = actions->getSampleBlock();
|
|
|
|
|
|
|
|
for (auto & column : defaulted_columns)
|
|
|
|
{
|
|
|
|
const auto name_and_type_ptr = column.first;
|
|
|
|
const auto col_decl_ptr = column.second;
|
|
|
|
|
|
|
|
const auto & column_name = col_decl_ptr->name;
|
|
|
|
const auto has_explicit_type = nullptr != col_decl_ptr->type;
|
|
|
|
auto & explicit_type = name_and_type_ptr->type;
|
|
|
|
|
|
|
|
/// if column declaration contains explicit type, name_and_type_ptr->type is not null
|
|
|
|
if (has_explicit_type)
|
|
|
|
{
|
|
|
|
const auto & tmp_column = block.getByName(column_name + "_tmp");
|
|
|
|
const auto & deduced_type = tmp_column.type;
|
|
|
|
|
|
|
|
/// type mismatch between explicitly specified and deduced type, add conversion for non-array types
|
|
|
|
if (!explicit_type->equals(*deduced_type))
|
|
|
|
{
|
|
|
|
col_decl_ptr->default_expression = makeASTFunction("CAST", col_decl_ptr->default_expression,
|
|
|
|
std::make_shared<ASTLiteral>(StringRange(), explicit_type->getName()));
|
|
|
|
|
|
|
|
col_decl_ptr->children.clear();
|
|
|
|
col_decl_ptr->children.push_back(col_decl_ptr->type);
|
|
|
|
col_decl_ptr->children.push_back(col_decl_ptr->default_expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/// no explicit type, name_and_type_ptr->type is null, set to deduced type
|
|
|
|
explicit_type = block.getByName(column_name).type;
|
|
|
|
|
|
|
|
defaults.emplace(column_name, ColumnDefault{
|
|
|
|
columnDefaultTypeFromString(col_decl_ptr->default_specifier),
|
|
|
|
col_decl_ptr->default_expression
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
return {Nested::flatten(columns), defaults};
|
2014-07-10 11:13:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
static NamesAndTypesList removeAndReturnColumns(ColumnsAndDefaults & columns_and_defaults, const ColumnDefaultType type)
|
2014-07-10 11:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & columns = columns_and_defaults.first;
|
|
|
|
auto & defaults = columns_and_defaults.second;
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList removed{};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (auto it = std::begin(columns); it != std::end(columns);)
|
|
|
|
{
|
|
|
|
const auto jt = defaults.find(it->name);
|
|
|
|
if (jt != std::end(defaults) && jt->second.type == type)
|
|
|
|
{
|
|
|
|
removed.push_back(*it);
|
|
|
|
it = columns.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed;
|
2014-07-10 11:13:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
ASTPtr InterpreterCreateQuery::formatColumns(const NamesAndTypesList & columns)
|
2014-07-10 11:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto columns_list = std::make_shared<ASTExpressionList>();
|
2014-07-10 11:13:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & column : columns)
|
|
|
|
{
|
|
|
|
const auto column_declaration = std::make_shared<ASTColumnDeclaration>();
|
|
|
|
column_declaration->name = column.name;
|
2014-07-10 11:13:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
StringPtr type_name = std::make_shared<String>(column.type->getName());
|
|
|
|
auto pos = type_name->data();
|
|
|
|
const auto end = pos + type_name->size();
|
2014-07-10 11:13:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifierWithOptionalParameters storage_p;
|
|
|
|
column_declaration->type = parseQuery(storage_p, pos, end, "data type");
|
|
|
|
column_declaration->type->query_string = type_name;
|
|
|
|
columns_list->children.emplace_back(column_declaration);
|
|
|
|
}
|
2014-07-10 11:13:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return columns_list;
|
2014-07-10 11:13:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
ASTPtr InterpreterCreateQuery::formatColumns(
|
2017-12-25 21:57:29 +00:00
|
|
|
const NamesAndTypesList & columns,
|
|
|
|
const NamesAndTypesList & materialized_columns,
|
|
|
|
const NamesAndTypesList & alias_columns,
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnDefaults & column_defaults)
|
2014-09-25 15:01:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto columns_list = std::make_shared<ASTExpressionList>();
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
for (const auto & column : boost::join(columns, boost::join(materialized_columns, alias_columns)))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto column_declaration = std::make_shared<ASTColumnDeclaration>();
|
|
|
|
ASTPtr column_declaration_ptr{column_declaration};
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
column_declaration->name = column.name;
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
StringPtr type_name = std::make_shared<String>(column.type->getName());
|
|
|
|
auto pos = type_name->data();
|
|
|
|
const auto end = pos + type_name->size();
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserIdentifierWithOptionalParameters storage_p;
|
|
|
|
column_declaration->type = parseQuery(storage_p, pos, end, "data type");
|
|
|
|
column_declaration->type->query_string = type_name;
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto it = column_defaults.find(column.name);
|
|
|
|
if (it != std::end(column_defaults))
|
|
|
|
{
|
|
|
|
column_declaration->default_specifier = toString(it->second.type);
|
|
|
|
column_declaration->default_expression = it->second.expression->clone();
|
|
|
|
}
|
2014-09-25 15:01:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
columns_list->children.push_back(column_declaration_ptr);
|
|
|
|
}
|
2014-07-10 11:13:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return columns_list;
|
2014-07-10 11:13:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
InterpreterCreateQuery::ColumnsInfo InterpreterCreateQuery::getColumnsInfo(const ASTExpressionList & columns, const Context & context)
|
2016-03-19 01:18:49 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnsInfo res;
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto && columns_and_defaults = parseColumns(columns, context);
|
|
|
|
res.materialized_columns = removeAndReturnColumns(columns_and_defaults, ColumnDefaultType::Materialized);
|
|
|
|
res.alias_columns = removeAndReturnColumns(columns_and_defaults, ColumnDefaultType::Alias);
|
2017-12-25 21:10:46 +00:00
|
|
|
res.columns = std::move(columns_and_defaults.first);
|
2017-04-01 07:20:54 +00:00
|
|
|
res.column_defaults = std::move(columns_and_defaults.second);
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
if (res.columns.size() + res.materialized_columns.size() == 0)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception{"Cannot CREATE table without physical columns", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED};
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InterpreterCreateQuery::ColumnsInfo InterpreterCreateQuery::setColumns(
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTCreateQuery & create, const Block & as_select_sample, const StoragePtr & as_storage) const
|
2016-03-19 01:18:49 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnsInfo res;
|
|
|
|
|
|
|
|
if (create.columns)
|
|
|
|
{
|
2017-09-17 18:49:43 +00:00
|
|
|
res = getColumnsInfo(*create.columns, context);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (!create.as_table.empty())
|
|
|
|
{
|
2017-12-25 21:10:46 +00:00
|
|
|
res.columns = as_storage->getColumnsListNonMaterialized();
|
2017-04-01 07:20:54 +00:00
|
|
|
res.materialized_columns = as_storage->materialized_columns;
|
|
|
|
res.alias_columns = as_storage->alias_columns;
|
|
|
|
res.column_defaults = as_storage->column_defaults;
|
|
|
|
}
|
|
|
|
else if (create.select)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < as_select_sample.columns(); ++i)
|
2017-12-25 21:10:46 +00:00
|
|
|
res.columns.emplace_back(as_select_sample.safeGetByPosition(i).name, as_select_sample.safeGetByPosition(i).type);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Incorrect CREATE query: required list of column descriptions or AS section or SELECT.", ErrorCodes::INCORRECT_QUERY);
|
|
|
|
|
|
|
|
/// Even if query has list of columns, canonicalize it (unfold Nested columns).
|
2017-12-25 21:10:46 +00:00
|
|
|
ASTPtr new_columns = formatColumns(res.columns, res.materialized_columns, res.alias_columns, res.column_defaults);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (create.columns)
|
2017-09-17 18:49:43 +00:00
|
|
|
create.replace(create.columns, new_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-09-17 18:49:43 +00:00
|
|
|
create.set(create.columns, new_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Check for duplicates
|
|
|
|
std::set<String> all_columns;
|
2017-12-25 21:57:29 +00:00
|
|
|
auto check_column_already_exists = [&all_columns](const NameAndTypePair & column_name_and_type)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!all_columns.emplace(column_name_and_type.name).second)
|
|
|
|
throw Exception("Column " + backQuoteIfNeed(column_name_and_type.name) + " already exists", ErrorCodes::DUPLICATE_COLUMN);
|
|
|
|
};
|
|
|
|
|
2017-12-25 21:10:46 +00:00
|
|
|
for (const auto & elem : res.columns)
|
2017-04-01 07:20:54 +00:00
|
|
|
check_column_already_exists(elem);
|
|
|
|
for (const auto & elem : res.materialized_columns)
|
|
|
|
check_column_already_exists(elem);
|
|
|
|
for (const auto & elem : res.alias_columns)
|
|
|
|
check_column_already_exists(elem);
|
|
|
|
|
|
|
|
return res;
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const
|
2016-03-19 01:18:49 +00:00
|
|
|
{
|
2017-10-25 19:52:32 +00:00
|
|
|
if (create.storage)
|
2017-10-26 12:15:33 +00:00
|
|
|
{
|
|
|
|
if (create.is_temporary && create.storage->engine->name != "Memory")
|
|
|
|
throw Exception(
|
|
|
|
"Temporary tables can only be created with ENGINE = Memory, not " + create.storage->engine->name,
|
|
|
|
ErrorCodes::INCORRECT_QUERY);
|
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
return;
|
2017-10-26 12:15:33 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
if (create.is_temporary)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-09-17 18:49:43 +00:00
|
|
|
auto engine_ast = std::make_shared<ASTFunction>();
|
2017-10-25 19:52:32 +00:00
|
|
|
engine_ast->name = "Memory";
|
2017-09-17 18:49:43 +00:00
|
|
|
auto storage_ast = std::make_shared<ASTStorage>();
|
|
|
|
storage_ast->set(storage_ast->engine, engine_ast);
|
|
|
|
create.set(create.storage, storage_ast);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (!create.as_table.empty())
|
|
|
|
{
|
2017-04-02 17:37:49 +00:00
|
|
|
/// NOTE Getting the structure from the table specified in the AS is done not atomically with the creation of the table.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String as_database_name = create.as_database.empty() ? context.getCurrentDatabase() : create.as_database;
|
|
|
|
String as_table_name = create.as_table;
|
|
|
|
|
2017-09-17 18:49:43 +00:00
|
|
|
ASTPtr as_create_ptr = context.getCreateQuery(as_database_name, as_table_name);
|
|
|
|
const auto & as_create = typeid_cast<const ASTCreateQuery &>(*as_create_ptr);
|
2017-08-18 20:56:19 +00:00
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
if (as_create.is_view)
|
|
|
|
throw Exception(
|
2017-12-22 19:20:18 +00:00
|
|
|
"Cannot CREATE a table AS " + as_database_name + "." + as_table_name + ", it is a View",
|
2017-10-25 19:52:32 +00:00
|
|
|
ErrorCodes::INCORRECT_QUERY);
|
2017-08-18 20:56:19 +00:00
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
create.set(create.storage, as_create.storage->ptr());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create)
|
2016-03-19 01:18:49 +00:00
|
|
|
{
|
2017-04-21 12:39:28 +00:00
|
|
|
if (!create.cluster.empty())
|
2017-04-25 15:21:03 +00:00
|
|
|
return executeDDLQueryOnCluster(query_ptr, context);
|
2017-04-21 12:39:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String path = context.getPath();
|
|
|
|
String current_database = context.getCurrentDatabase();
|
|
|
|
|
|
|
|
String database_name = create.database.empty() ? current_database : create.database;
|
|
|
|
String table_name = create.table;
|
|
|
|
String table_name_escaped = escapeForFileName(table_name);
|
|
|
|
|
2017-10-21 20:38:39 +00:00
|
|
|
// If this is a stub ATTACH query, read the query definition from the database
|
|
|
|
if (create.attach && !create.storage && !create.columns)
|
|
|
|
{
|
2017-10-25 00:55:31 +00:00
|
|
|
// Table SQL definition is available even if the table is detached
|
2017-10-21 20:38:39 +00:00
|
|
|
auto query = context.getCreateQuery(database_name, table_name);
|
|
|
|
auto & as_create = typeid_cast<const ASTCreateQuery &>(*query);
|
|
|
|
create = as_create; // Copy the saved create query, but use ATTACH instead of CREATE
|
|
|
|
create.attach = true;
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:53:01 +00:00
|
|
|
if (create.to_database.empty())
|
|
|
|
create.to_database = current_database;
|
|
|
|
|
2017-11-21 13:30:45 +00:00
|
|
|
if (create.select && (create.is_view || create.is_materialized_view))
|
|
|
|
create.select->setDatabaseIfNeeded(current_database);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unique_ptr<InterpreterSelectQuery> interpreter_select;
|
|
|
|
Block as_select_sample;
|
2017-11-21 13:30:45 +00:00
|
|
|
if (create.select && (!create.attach || !create.columns))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-11-05 17:48:50 +00:00
|
|
|
interpreter_select = std::make_unique<InterpreterSelectQuery>(create.select->clone(), context);
|
2017-04-01 07:20:54 +00:00
|
|
|
as_select_sample = interpreter_select->getSampleBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
String as_database_name = create.as_database.empty() ? current_database : create.as_database;
|
|
|
|
String as_table_name = create.as_table;
|
|
|
|
|
|
|
|
StoragePtr as_storage;
|
|
|
|
TableStructureReadLockPtr as_storage_lock;
|
|
|
|
if (!as_table_name.empty())
|
|
|
|
{
|
|
|
|
as_storage = context.getTable(as_database_name, as_table_name);
|
2017-09-01 15:05:23 +00:00
|
|
|
as_storage_lock = as_storage->lockStructure(false, __PRETTY_FUNCTION__);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set and retrieve list of columns.
|
|
|
|
ColumnsInfo columns = setColumns(create, as_select_sample, as_storage);
|
|
|
|
|
2017-10-25 19:52:32 +00:00
|
|
|
/// Set the table engine if it was not specified explicitly.
|
|
|
|
setEngine(create);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
StoragePtr res;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::unique_ptr<DDLGuard> guard;
|
|
|
|
|
2017-11-03 19:53:10 +00:00
|
|
|
String data_path;
|
|
|
|
DatabasePtr database;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!create.is_temporary)
|
|
|
|
{
|
2017-11-03 19:53:10 +00:00
|
|
|
database = context.getDatabase(database_name);
|
|
|
|
data_path = database->getDataPath(context);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/** If the table already exists, and the request specifies IF NOT EXISTS,
|
|
|
|
* then we allow concurrent CREATE queries (which do nothing).
|
|
|
|
* Otherwise, concurrent queries for creating a table, if the table does not exist,
|
|
|
|
* can throw an exception, even if IF NOT EXISTS is specified.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2017-12-22 19:20:18 +00:00
|
|
|
guard = context.getDDLGuardIfTableDoesntExist(database_name, table_name,
|
2017-04-01 07:20:54 +00:00
|
|
|
"Table " + database_name + "." + table_name + " is creating or attaching right now");
|
|
|
|
|
|
|
|
if (!guard)
|
|
|
|
{
|
|
|
|
if (create.if_not_exists)
|
|
|
|
return {};
|
|
|
|
else
|
|
|
|
throw Exception("Table " + database_name + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
res = StorageFactory::instance().get(create,
|
|
|
|
data_path,
|
|
|
|
table_name,
|
|
|
|
database_name,
|
|
|
|
context,
|
|
|
|
context.getGlobalContext(),
|
|
|
|
columns.columns,
|
|
|
|
columns.materialized_columns,
|
|
|
|
columns.alias_columns,
|
|
|
|
columns.column_defaults,
|
|
|
|
create.attach,
|
|
|
|
false);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (create.is_temporary)
|
|
|
|
context.getSessionContext().addExternalTable(table_name, res);
|
|
|
|
else
|
2017-11-03 19:53:10 +00:00
|
|
|
database->createTable(context, table_name, res, query_ptr);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:06:14 +00:00
|
|
|
res->startup();
|
|
|
|
|
2017-11-21 13:30:45 +00:00
|
|
|
/// If the query is a CREATE SELECT, insert the data into the table.
|
|
|
|
if (create.select && !create.attach
|
|
|
|
&& !create.is_view && (!create.is_materialized_view || create.is_populate))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-09-01 15:05:23 +00:00
|
|
|
auto table_lock = res->lockStructure(true, __PRETTY_FUNCTION__);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-04-02 17:37:49 +00:00
|
|
|
/// Also see InterpreterInsertQuery.
|
2018-01-10 05:26:24 +00:00
|
|
|
BlockOutputStreamPtr out;
|
|
|
|
|
|
|
|
out = std::make_shared<PushingToViewsBlockOutputStream>(
|
|
|
|
create.database, create.table, res, create.is_temporary ? context.getSessionContext() : context, query_ptr);
|
|
|
|
|
|
|
|
out = std::make_shared<MaterializingBlockOutputStream>(out);
|
|
|
|
|
|
|
|
/// @note shouldn't these two contexts be session contexts in case of temporary table?
|
|
|
|
bool strict_insert_defaults = static_cast<bool>(context.getSettingsRef().strict_insert_defaults);
|
|
|
|
out = std::make_shared<AddingDefaultBlockOutputStream>(
|
|
|
|
out, columns.columns, columns.column_defaults, context, strict_insert_defaults);
|
|
|
|
|
2018-01-12 13:03:19 +00:00
|
|
|
if (!context.getSettingsRef().insert_allow_materialized_columns)
|
2018-01-10 05:26:24 +00:00
|
|
|
out = std::make_shared<ProhibitColumnsBlockOutputStream>(out, columns.materialized_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
BlockIO io;
|
|
|
|
io.in = std::make_shared<NullAndDoCopyBlockInputStream>(interpreter_select->execute().in, out);
|
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 13:42:29 +00:00
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
BlockIO InterpreterCreateQuery::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_ptr);
|
2017-12-22 19:20:18 +00:00
|
|
|
checkAccess(create);
|
2018-02-12 18:41:53 +00:00
|
|
|
ASTQueryWithOutput::resetOutputASTIfExist(create);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// CREATE|ATTACH DATABASE
|
|
|
|
if (!create.database.empty() && create.table.empty())
|
|
|
|
{
|
2017-04-25 15:21:03 +00:00
|
|
|
return createDatabase(create);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-21 12:39:28 +00:00
|
|
|
return createTable(create);
|
2016-03-19 01:18:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 19:20:18 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
void InterpreterCreateQuery::checkAccess(const ASTCreateQuery & create)
|
|
|
|
{
|
2018-01-18 23:40:32 +00:00
|
|
|
/// Internal queries (initiated by the server itself) always have access to everything.
|
|
|
|
if (internal)
|
|
|
|
return;
|
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
const Settings & settings = context.getSettingsRef();
|
|
|
|
auto readonly = settings.limits.readonly;
|
|
|
|
|
|
|
|
if (!readonly)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-03-19 01:18:49 +00:00
|
|
|
|
2017-12-20 07:39:52 +00:00
|
|
|
/// CREATE|ATTACH DATABASE
|
|
|
|
if (!create.database.empty() && create.table.empty())
|
|
|
|
{
|
|
|
|
throw Exception("Cannot create database in readonly mode", ErrorCodes::READONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create.is_temporary && readonly >= 2)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception("Cannot create table in readonly mode", ErrorCodes::READONLY);
|
|
|
|
}
|
2011-08-18 20:33:20 +00:00
|
|
|
}
|