2011-11-05 23:31:19 +00:00
|
|
|
|
#include <Poco/File.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
#include <Poco/FileStream.h>
|
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
#include <DB/Common/escapeForFileName.h>
|
|
|
|
|
|
2011-11-01 17:57:37 +00:00
|
|
|
|
#include <DB/IO/WriteBufferFromString.h>
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
|
|
2011-11-06 06:22:52 +00:00
|
|
|
|
#include <DB/DataStreams/MaterializingBlockInputStream.h>
|
2011-11-01 15:16:04 +00:00
|
|
|
|
#include <DB/DataStreams/copyData.h>
|
|
|
|
|
|
2011-08-18 20:33:20 +00:00
|
|
|
|
#include <DB/Parsers/ASTCreateQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTNameTypePair.h>
|
2014-09-25 13:40:26 +00:00
|
|
|
|
#include <DB/Parsers/ASTColumnDeclaration.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Storages/StorageLog.h>
|
|
|
|
|
#include <DB/Storages/StorageSystemNumbers.h>
|
|
|
|
|
|
2012-08-20 19:21:04 +00:00
|
|
|
|
#include <DB/Parsers/ParserCreateQuery.h>
|
2012-05-22 20:18:45 +00:00
|
|
|
|
#include <DB/Parsers/formatAST.h>
|
|
|
|
|
|
2011-11-01 15:16:04 +00:00
|
|
|
|
#include <DB/Interpreters/InterpreterSelectQuery.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
#include <DB/Interpreters/InterpreterCreateQuery.h>
|
2014-09-25 15:01:09 +00:00
|
|
|
|
#include <DB/Interpreters/ExpressionAnalyzer.h>
|
2014-07-10 11:13:45 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeNested.h>
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 00:09:41 +00:00
|
|
|
|
InterpreterCreateQuery::InterpreterCreateQuery(ASTPtr query_ptr_, Context & context_)
|
|
|
|
|
: query_ptr(query_ptr_), context(context_)
|
2011-11-01 15:16:04 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2011-11-01 15:16:04 +00:00
|
|
|
|
|
2013-01-17 20:31:44 +00:00
|
|
|
|
StoragePtr InterpreterCreateQuery::execute(bool assume_metadata_exists)
|
2011-08-18 20:33:20 +00:00
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
String path = context.getPath();
|
|
|
|
|
String current_database = context.getCurrentDatabase();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
ASTCreateQuery & create = typeid_cast<ASTCreateQuery &>(*query_ptr);
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
String database_name = create.database.empty() ? current_database : create.database;
|
2011-11-05 23:31:19 +00:00
|
|
|
|
String database_name_escaped = escapeForFileName(database_name);
|
2011-10-30 05:19:41 +00:00
|
|
|
|
String table_name = create.table;
|
2011-11-05 23:31:19 +00:00
|
|
|
|
String table_name_escaped = escapeForFileName(table_name);
|
2012-08-02 17:33:31 +00:00
|
|
|
|
String as_database_name = create.as_database.empty() ? current_database : create.as_database;
|
2011-10-31 17:30:44 +00:00
|
|
|
|
String as_table_name = create.as_table;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
String data_path = path + "data/" + database_name_escaped + "/";
|
|
|
|
|
String metadata_path = path + "metadata/" + database_name_escaped + "/" + (!table_name.empty() ? table_name_escaped + ".sql" : "");
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
/// CREATE|ATTACH DATABASE
|
|
|
|
|
if (!database_name.empty() && table_name.empty())
|
2011-08-19 18:31:14 +00:00
|
|
|
|
{
|
2011-11-05 23:31:19 +00:00
|
|
|
|
if (create.attach)
|
|
|
|
|
{
|
|
|
|
|
if (!Poco::File(data_path).exists())
|
|
|
|
|
throw Exception("Directory " + data_path + " doesn't exist.", ErrorCodes::DIRECTORY_DOESNT_EXIST);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!create.if_not_exists && Poco::File(metadata_path).exists())
|
|
|
|
|
throw Exception("Directory " + metadata_path + " already exists.", ErrorCodes::DIRECTORY_ALREADY_EXISTS);
|
|
|
|
|
if (!create.if_not_exists && Poco::File(data_path).exists())
|
|
|
|
|
throw Exception("Directory " + data_path + " already exists.", ErrorCodes::DIRECTORY_ALREADY_EXISTS);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
Poco::File(metadata_path).createDirectory();
|
|
|
|
|
Poco::File(data_path).createDirectory();
|
2011-08-19 18:31:14 +00:00
|
|
|
|
}
|
2011-11-05 23:31:19 +00:00
|
|
|
|
|
2013-06-25 12:03:36 +00:00
|
|
|
|
if (!create.if_not_exists || !context.isDatabaseExist(database_name))
|
|
|
|
|
context.addDatabase(database_name);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-01-23 17:38:03 +00:00
|
|
|
|
return StoragePtr();
|
2011-11-05 23:31:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
SharedPtr<InterpreterSelectQuery> interpreter_select;
|
2014-03-20 10:59:45 +00:00
|
|
|
|
Block select_sample;
|
2014-05-16 11:06:31 +00:00
|
|
|
|
/// Для таблиц типа вью, чтобы получить столбцы, может понадобиться sample block.
|
|
|
|
|
if (create.select && (!create.attach || (!create.columns && (create.is_view || create.is_materialized_view))))
|
2014-03-20 10:59:45 +00:00
|
|
|
|
{
|
|
|
|
|
interpreter_select = new InterpreterSelectQuery(create.select, context);
|
|
|
|
|
select_sample = interpreter_select->getSampleBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StoragePtr res;
|
2013-10-30 13:52:02 +00:00
|
|
|
|
String storage_name;
|
2013-11-13 14:39:48 +00:00
|
|
|
|
NamesAndTypesListPtr columns = new NamesAndTypesList;
|
2014-10-03 15:30:10 +00:00
|
|
|
|
NamesAndTypesList materialized_columns{};
|
2014-09-30 03:08:47 +00:00
|
|
|
|
NamesAndTypesList alias_columns{};
|
2014-09-29 14:58:48 +00:00
|
|
|
|
ColumnDefaults column_defaults{};
|
2012-08-02 17:33:31 +00:00
|
|
|
|
|
2014-03-20 10:59:45 +00:00
|
|
|
|
StoragePtr as_storage;
|
|
|
|
|
IStorage::TableStructureReadLockPtr as_storage_lock;
|
|
|
|
|
if (!as_table_name.empty())
|
|
|
|
|
{
|
|
|
|
|
as_storage = context.getTable(as_database_name, as_table_name);
|
|
|
|
|
as_storage_lock = as_storage->lockStructure(false);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-05 23:31:19 +00:00
|
|
|
|
{
|
2012-08-02 17:33:31 +00:00
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
2014-03-12 13:14:16 +00:00
|
|
|
|
if (!create.is_temporary)
|
2012-08-02 17:33:31 +00:00
|
|
|
|
{
|
2014-03-12 13:14:16 +00:00
|
|
|
|
context.assertDatabaseExists(database_name);
|
|
|
|
|
|
|
|
|
|
if (context.isTableExist(database_name, table_name))
|
|
|
|
|
{
|
|
|
|
|
if (create.if_not_exists)
|
|
|
|
|
return context.getTable(database_name, table_name);
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Table " + database_name + "." + table_name + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS);
|
|
|
|
|
}
|
2012-08-02 17:33:31 +00:00
|
|
|
|
}
|
2011-11-01 15:16:04 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
/// Получаем список столбцов
|
|
|
|
|
if (create.columns)
|
2011-10-31 17:30:44 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
auto && columns_and_defaults = parseColumns(create.columns);
|
2014-10-03 15:30:10 +00:00
|
|
|
|
materialized_columns = removeAndReturnColumns(columns_and_defaults, ColumnDefaultType::Materialized);
|
|
|
|
|
alias_columns = removeAndReturnColumns(columns_and_defaults, ColumnDefaultType::Alias);
|
2014-09-29 14:58:48 +00:00
|
|
|
|
columns = new NamesAndTypesList{std::move(columns_and_defaults.first)};
|
|
|
|
|
column_defaults = std::move(columns_and_defaults.second);
|
2011-10-31 17:30:44 +00:00
|
|
|
|
}
|
2012-08-02 17:33:31 +00:00
|
|
|
|
else if (!create.as_table.empty())
|
2014-10-10 15:45:43 +00:00
|
|
|
|
{
|
2014-10-16 13:37:01 +00:00
|
|
|
|
columns = new NamesAndTypesList(as_storage->getColumnsListNonMaterialized());
|
2014-10-10 15:45:43 +00:00
|
|
|
|
materialized_columns = as_storage->materialized_columns;
|
|
|
|
|
alias_columns = as_storage->alias_columns;
|
|
|
|
|
column_defaults = as_storage->column_defaults;
|
|
|
|
|
}
|
2012-08-02 17:33:31 +00:00
|
|
|
|
else if (create.select)
|
|
|
|
|
{
|
|
|
|
|
columns = new NamesAndTypesList;
|
2014-03-20 10:59:45 +00:00
|
|
|
|
for (size_t i = 0; i < select_sample.columns(); ++i)
|
|
|
|
|
columns->push_back(NameAndTypePair(select_sample.getByPosition(i).name, select_sample.getByPosition(i).type));
|
2012-08-02 17:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Incorrect CREATE query: required list of column descriptions or AS section or SELECT.", ErrorCodes::INCORRECT_QUERY);
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
2014-07-10 11:13:45 +00:00
|
|
|
|
/// Даже если в запросе был список столбцов, на всякий случай приведем его к стандартному виду (развернем Nested).
|
2014-10-03 15:30:10 +00:00
|
|
|
|
ASTPtr new_columns = formatColumns(*columns, materialized_columns, alias_columns, column_defaults);
|
2014-07-10 11:13:45 +00:00
|
|
|
|
if (create.columns)
|
2014-07-14 14:26:11 +00:00
|
|
|
|
{
|
|
|
|
|
auto it = std::find(create.children.begin(), create.children.end(), create.columns);
|
|
|
|
|
if (it != create.children.end())
|
|
|
|
|
*it = new_columns;
|
|
|
|
|
else
|
|
|
|
|
create.children.push_back(new_columns);
|
|
|
|
|
}
|
2014-07-10 11:13:45 +00:00
|
|
|
|
else
|
|
|
|
|
create.children.push_back(new_columns);
|
|
|
|
|
create.columns = new_columns;
|
2012-08-20 19:21:04 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
/// Выбор нужного движка таблицы
|
|
|
|
|
if (create.storage)
|
2014-02-26 19:50:04 +00:00
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
storage_name = typeid_cast<ASTFunction &>(*create.storage).name;
|
2014-02-26 19:50:04 +00:00
|
|
|
|
}
|
2012-08-02 17:33:31 +00:00
|
|
|
|
else if (!create.as_table.empty())
|
2012-08-20 19:21:04 +00:00
|
|
|
|
{
|
2014-03-20 10:59:45 +00:00
|
|
|
|
storage_name = as_storage->getName();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
create.storage = typeid_cast<const ASTCreateQuery &>(*context.getCreateQuery(as_database_name, as_table_name)).storage;
|
2014-02-26 19:50:04 +00:00
|
|
|
|
}
|
2014-03-12 13:14:16 +00:00
|
|
|
|
else if (create.is_temporary)
|
|
|
|
|
{
|
|
|
|
|
storage_name = "Memory";
|
|
|
|
|
ASTFunction * func = new ASTFunction();
|
|
|
|
|
func->name = storage_name;
|
|
|
|
|
create.storage = func;
|
|
|
|
|
}
|
2014-02-26 19:50:04 +00:00
|
|
|
|
else if (create.is_view)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
|
|
|
|
storage_name = "View";
|
|
|
|
|
ASTFunction * func = new ASTFunction();
|
|
|
|
|
func->name = storage_name;
|
|
|
|
|
create.storage = func;
|
2014-02-26 19:50:04 +00:00
|
|
|
|
}
|
|
|
|
|
else if (create.is_materialized_view)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
|
|
|
|
storage_name = "MaterializedView";
|
|
|
|
|
ASTFunction * func = new ASTFunction();
|
|
|
|
|
func->name = storage_name;
|
|
|
|
|
create.storage = func;
|
2014-02-26 19:50:04 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2012-08-20 19:21:04 +00:00
|
|
|
|
throw Exception("Incorrect CREATE query: required ENGINE.", ErrorCodes::ENGINE_REQUIRED);
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
2013-05-06 11:28:45 +00:00
|
|
|
|
res = context.getStorageFactory().get(
|
2014-09-23 15:28:21 +00:00
|
|
|
|
storage_name, data_path, table_name, database_name, context,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
context.getGlobalContext(), query_ptr, columns,
|
|
|
|
|
materialized_columns, alias_columns, column_defaults, create.attach);
|
2011-08-18 20:33:20 +00:00
|
|
|
|
|
2012-08-02 17:33:31 +00:00
|
|
|
|
/// Проверка наличия метаданных таблицы на диске и создание метаданных
|
2014-03-12 13:14:16 +00:00
|
|
|
|
if (!assume_metadata_exists && !create.is_temporary)
|
2012-08-02 17:33:31 +00:00
|
|
|
|
{
|
2013-01-17 20:31:44 +00:00
|
|
|
|
if (Poco::File(metadata_path).exists())
|
|
|
|
|
{
|
|
|
|
|
/** Запрос ATTACH TABLE может использоваться, чтобы создать в оперативке ссылку на уже существующую таблицу.
|
|
|
|
|
* Это используется, например, при загрузке сервера.
|
|
|
|
|
*/
|
|
|
|
|
if (!create.attach)
|
|
|
|
|
throw Exception("Metadata for table " + database_name + "." + table_name + " already exists.",
|
|
|
|
|
ErrorCodes::TABLE_METADATA_ALREADY_EXISTS);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/// Меняем CREATE на ATTACH и пишем запрос в файл.
|
|
|
|
|
ASTPtr attach_ptr = query_ptr->clone();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ASTCreateQuery & attach = typeid_cast<ASTCreateQuery &>(*attach_ptr);
|
2013-01-17 20:31:44 +00:00
|
|
|
|
|
|
|
|
|
attach.attach = true;
|
|
|
|
|
attach.database.clear();
|
|
|
|
|
attach.as_database.clear();
|
|
|
|
|
attach.as_table.clear();
|
|
|
|
|
attach.if_not_exists = false;
|
2013-12-24 17:01:50 +00:00
|
|
|
|
attach.is_populate = false;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-10-30 13:52:02 +00:00
|
|
|
|
/// Для engine VIEW необходимо сохранить сам селект запрос, для остальных - наоборот
|
2013-11-08 17:43:03 +00:00
|
|
|
|
if (storage_name != "View" && storage_name != "MaterializedView")
|
2014-04-08 07:31:51 +00:00
|
|
|
|
attach.select = nullptr;
|
2013-01-17 20:31:44 +00:00
|
|
|
|
|
|
|
|
|
Poco::FileOutputStream metadata_file(metadata_path);
|
|
|
|
|
formatAST(attach, metadata_file, 0, false);
|
|
|
|
|
metadata_file << "\n";
|
|
|
|
|
}
|
2012-08-02 17:33:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 13:14:16 +00:00
|
|
|
|
if (create.is_temporary)
|
|
|
|
|
{
|
2014-03-13 15:00:06 +00:00
|
|
|
|
context.getSessionContext().addExternalTable(table_name, res);
|
2014-03-12 13:14:16 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
context.addTable(database_name, table_name, res);
|
2012-08-02 17:33:31 +00:00
|
|
|
|
}
|
2011-11-01 15:16:04 +00:00
|
|
|
|
|
|
|
|
|
/// Если запрос CREATE SELECT, то вставим в таблицу данные
|
2013-12-10 12:23:43 +00:00
|
|
|
|
if (create.select && storage_name != "View" && (storage_name != "MaterializedView" || create.is_populate))
|
2011-11-06 06:22:52 +00:00
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr from = new MaterializingBlockInputStream(interpreter_select->execute());
|
|
|
|
|
copyData(*from, *res->write(query_ptr));
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2011-08-18 20:33:20 +00:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
InterpreterCreateQuery::ColumnsAndDefaults InterpreterCreateQuery::parseColumns(ASTPtr expression_list)
|
2014-07-10 11:13:45 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
auto & column_list_ast = typeid_cast<ASTExpressionList &>(*expression_list);
|
|
|
|
|
|
2014-09-25 15:01:09 +00:00
|
|
|
|
/// list of table columns in correct order
|
2014-09-30 03:08:47 +00:00
|
|
|
|
NamesAndTypesList columns{}, known_type_columns{};
|
2014-09-29 14:58:48 +00:00
|
|
|
|
ColumnDefaults defaults{};
|
2014-09-25 15:01:09 +00:00
|
|
|
|
|
2014-09-29 13:32:16 +00:00
|
|
|
|
/// Columns requiring type-deduction or default_expression type-check
|
2014-09-30 03:08:47 +00:00
|
|
|
|
std::vector<std::pair<NameAndTypePair *, ASTColumnDeclaration *>> defaulted_columns{};
|
2014-09-29 13:32:16 +00:00
|
|
|
|
|
|
|
|
|
/** all default_expressions as a single expression list,
|
|
|
|
|
* mixed with conversion-columns for each explicitly specified type */
|
|
|
|
|
ASTPtr default_expr_list{new ASTExpressionList};
|
2014-09-30 03:08:47 +00:00
|
|
|
|
default_expr_list->children.reserve(column_list_ast.children.size());
|
2014-09-29 13:32:16 +00:00
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
for (auto & ast : column_list_ast.children)
|
2014-07-10 11:13:45 +00:00
|
|
|
|
{
|
2014-09-25 15:01:09 +00:00
|
|
|
|
auto & col_decl = typeid_cast<ASTColumnDeclaration &>(*ast);
|
2014-09-29 13:32:16 +00:00
|
|
|
|
|
2014-09-25 13:40:26 +00:00
|
|
|
|
if (col_decl.type)
|
|
|
|
|
{
|
|
|
|
|
const auto & type_range = col_decl.type->range;
|
2014-09-29 13:32:16 +00:00
|
|
|
|
columns.emplace_back(col_decl.name,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
context.getDataTypeFactory().get({ type_range.first, type_range.second }));
|
|
|
|
|
known_type_columns.emplace_back(columns.back());
|
2014-09-25 13:40:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
columns.emplace_back(col_decl.name, nullptr);
|
|
|
|
|
|
2014-09-29 13:32:16 +00:00
|
|
|
|
/// add column to postprocessing if there is a default_expression specified
|
|
|
|
|
if (col_decl.default_expression)
|
2014-09-25 13:40:26 +00:00
|
|
|
|
{
|
2014-09-29 13:32:16 +00:00
|
|
|
|
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 tmp_column_name = col_decl.name + "_tmp";
|
|
|
|
|
const auto & final_column_name = col_decl.name;
|
|
|
|
|
const auto conversion_function_name = "to" + columns.back().type->getName();
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
default_expr_list->children.emplace_back(setAlias(
|
2014-09-29 13:32:16 +00:00
|
|
|
|
makeASTFunction(conversion_function_name, ASTPtr{new ASTIdentifier{{}, tmp_column_name}}),
|
|
|
|
|
final_column_name));
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
default_expr_list->children.emplace_back(setAlias(col_decl.default_expression->clone(), tmp_column_name));
|
2014-09-29 13:32:16 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2014-09-29 14:58:48 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
default_expr_list->children.emplace_back(setAlias(col_decl.default_expression->clone(), col_decl.name));
|
2014-09-29 14:58:48 +00:00
|
|
|
|
}
|
2014-09-25 13:40:26 +00:00
|
|
|
|
}
|
2014-07-10 11:13:45 +00:00
|
|
|
|
}
|
2014-09-25 13:40:26 +00:00
|
|
|
|
|
2014-09-29 13:32:16 +00:00
|
|
|
|
/// set missing types and wrap default_expression's in a conversion-function if necessary
|
|
|
|
|
if (!defaulted_columns.empty())
|
2014-09-25 13:40:26 +00:00
|
|
|
|
{
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const auto actions = ExpressionAnalyzer{default_expr_list, context, known_type_columns}.getActions(true);
|
2014-09-29 13:32:16 +00:00
|
|
|
|
const auto block = actions->getSampleBlock();
|
2014-09-25 13:40:26 +00:00
|
|
|
|
|
2014-09-29 13:32:16 +00:00
|
|
|
|
for (auto & column : defaulted_columns)
|
2014-09-25 15:01:09 +00:00
|
|
|
|
{
|
2014-09-29 13:32:16 +00:00
|
|
|
|
const auto name_and_type_ptr = column.first;
|
|
|
|
|
const auto col_decl_ptr = column.second;
|
2014-09-25 13:40:26 +00:00
|
|
|
|
|
2014-09-29 13:32:16 +00:00
|
|
|
|
if (name_and_type_ptr->type)
|
|
|
|
|
{
|
|
|
|
|
const auto & tmp_column = block.getByName(col_decl_ptr->name + "_tmp");
|
|
|
|
|
|
|
|
|
|
/// type mismatch between explicitly specified and deduced type, add conversion
|
|
|
|
|
if (typeid(*name_and_type_ptr->type) != typeid(*tmp_column.type))
|
|
|
|
|
{
|
|
|
|
|
col_decl_ptr->default_expression = makeASTFunction(
|
|
|
|
|
"to" + name_and_type_ptr->type->getName(),
|
|
|
|
|
col_decl_ptr->default_expression);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
name_and_type_ptr->type = block.getByName(name_and_type_ptr->name).type;
|
2014-09-29 14:58:48 +00:00
|
|
|
|
|
|
|
|
|
defaults.emplace(col_decl_ptr->name, ColumnDefault{
|
|
|
|
|
columnDefaultTypeFromString(col_decl_ptr->default_specifier),
|
2014-10-21 12:11:20 +00:00
|
|
|
|
col_decl_ptr->default_expression
|
2014-09-29 14:58:48 +00:00
|
|
|
|
});
|
2014-09-29 13:32:16 +00:00
|
|
|
|
}
|
2014-09-25 13:40:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 14:58:48 +00:00
|
|
|
|
return { *DataTypeNested::expandNestedColumns(columns), defaults };
|
2014-07-10 11:13:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 15:30:10 +00:00
|
|
|
|
NamesAndTypesList InterpreterCreateQuery::removeAndReturnColumns(ColumnsAndDefaults & columns_and_defaults,
|
|
|
|
|
const ColumnDefaultType type)
|
2014-09-30 03:08:47 +00:00
|
|
|
|
{
|
|
|
|
|
auto & columns = columns_and_defaults.first;
|
|
|
|
|
auto & defaults = columns_and_defaults.second;
|
|
|
|
|
|
2014-10-03 15:30:10 +00:00
|
|
|
|
NamesAndTypesList removed{};
|
2014-09-30 03:08:47 +00:00
|
|
|
|
|
|
|
|
|
for (auto it = std::begin(columns); it != std::end(columns);)
|
|
|
|
|
{
|
|
|
|
|
const auto jt = defaults.find(it->name);
|
2014-10-03 15:30:10 +00:00
|
|
|
|
if (jt != std::end(defaults) && jt->second.type == type)
|
2014-09-30 03:08:47 +00:00
|
|
|
|
{
|
2014-10-03 15:30:10 +00:00
|
|
|
|
removed.push_back(*it);
|
2014-09-30 03:08:47 +00:00
|
|
|
|
it = columns.erase(it);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 15:30:10 +00:00
|
|
|
|
return removed;
|
2014-09-30 03:08:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-10 11:13:45 +00:00
|
|
|
|
ASTPtr InterpreterCreateQuery::formatColumns(const NamesAndTypesList & columns)
|
|
|
|
|
{
|
2014-09-25 13:40:26 +00:00
|
|
|
|
ASTPtr columns_list_ptr{new ASTExpressionList};
|
2014-07-10 11:13:45 +00:00
|
|
|
|
ASTExpressionList & columns_list = typeid_cast<ASTExpressionList &>(*columns_list_ptr);
|
|
|
|
|
|
2014-09-25 13:40:26 +00:00
|
|
|
|
for (const auto & column : columns)
|
2014-07-10 11:13:45 +00:00
|
|
|
|
{
|
2014-09-25 13:40:26 +00:00
|
|
|
|
const auto column_declaration = new ASTColumnDeclaration;
|
|
|
|
|
ASTPtr column_declaration_ptr{column_declaration};
|
2014-07-10 11:13:45 +00:00
|
|
|
|
|
2014-09-25 13:40:26 +00:00
|
|
|
|
column_declaration->name = column.name;
|
2014-07-10 11:13:45 +00:00
|
|
|
|
|
2014-09-25 13:40:26 +00:00
|
|
|
|
StringPtr type_name{new String(column.type->getName())};
|
|
|
|
|
auto pos = type_name->data();
|
|
|
|
|
const auto end = pos + type_name->size();
|
|
|
|
|
|
|
|
|
|
ParserIdentifierWithOptionalParameters storage_p;
|
|
|
|
|
Expected expected{""};
|
|
|
|
|
if (!storage_p.parse(pos, end, column_declaration->type, expected))
|
2014-07-10 11:13:45 +00:00
|
|
|
|
throw Exception("Cannot parse data type.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
2014-09-25 13:40:26 +00:00
|
|
|
|
column_declaration->type->query_string = type_name;
|
|
|
|
|
columns_list.children.push_back(column_declaration_ptr);
|
2014-07-10 11:13:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return columns_list_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
ASTPtr InterpreterCreateQuery::formatColumns(NamesAndTypesList columns,
|
2014-10-03 15:30:10 +00:00
|
|
|
|
const NamesAndTypesList & materialized_columns,
|
2014-09-30 03:08:47 +00:00
|
|
|
|
const NamesAndTypesList & alias_columns,
|
|
|
|
|
const ColumnDefaults & column_defaults)
|
2014-09-25 15:01:09 +00:00
|
|
|
|
{
|
2014-10-03 15:30:10 +00:00
|
|
|
|
columns.insert(std::end(columns), std::begin(materialized_columns), std::end(materialized_columns));
|
2014-09-30 03:08:47 +00:00
|
|
|
|
columns.insert(std::end(columns), std::begin(alias_columns), std::end(alias_columns));
|
|
|
|
|
|
|
|
|
|
ASTPtr columns_list_ptr{new ASTExpressionList};
|
|
|
|
|
ASTExpressionList & columns_list = typeid_cast<ASTExpressionList &>(*columns_list_ptr);
|
|
|
|
|
|
|
|
|
|
for (const auto & column : columns)
|
|
|
|
|
{
|
|
|
|
|
const auto column_declaration = new ASTColumnDeclaration;
|
|
|
|
|
ASTPtr column_declaration_ptr{column_declaration};
|
|
|
|
|
|
|
|
|
|
column_declaration->name = column.name;
|
|
|
|
|
|
|
|
|
|
StringPtr type_name{new String(column.type->getName())};
|
|
|
|
|
auto pos = type_name->data();
|
|
|
|
|
const auto end = pos + type_name->size();
|
|
|
|
|
|
|
|
|
|
ParserIdentifierWithOptionalParameters storage_p;
|
|
|
|
|
Expected expected{""};
|
|
|
|
|
if (!storage_p.parse(pos, end, column_declaration->type, expected))
|
|
|
|
|
throw Exception("Cannot parse data type.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
|
|
column_declaration->type->query_string = type_name;
|
|
|
|
|
|
|
|
|
|
const auto it = column_defaults.find(column.name);
|
|
|
|
|
if (it != std::end(column_defaults))
|
|
|
|
|
{
|
|
|
|
|
column_declaration->default_specifier = toString(it->second.type);
|
2014-10-21 12:11:20 +00:00
|
|
|
|
column_declaration->default_expression = it->second.expression->clone();
|
2014-09-30 03:08:47 +00:00
|
|
|
|
}
|
2014-09-25 15:01:09 +00:00
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
columns_list.children.push_back(column_declaration_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return columns_list_ptr;
|
2014-09-25 15:01:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
|
2011-08-18 20:33:20 +00:00
|
|
|
|
}
|