mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 08:02:02 +00:00
dbms: tiny improvements [#CONV-8383].
This commit is contained in:
parent
94f02c8efb
commit
c2fc882312
@ -33,7 +33,7 @@ public:
|
||||
ASTPtr column;
|
||||
|
||||
/// deep copy
|
||||
void clone( Parameters & p) const
|
||||
void clone(Parameters & p) const
|
||||
{
|
||||
p.type = type;
|
||||
p.column = column->clone();
|
||||
@ -54,7 +54,7 @@ public:
|
||||
ASTPtr clone() const
|
||||
{
|
||||
ASTAlterQuery * res = new ASTAlterQuery(*this);
|
||||
for(ParameterContainer::size_type i = 0; i < parameters.size(); ++i)
|
||||
for (ParameterContainer::size_type i = 0; i < parameters.size(); ++i)
|
||||
{
|
||||
parameters[i].clone(res->parameters[i]);
|
||||
}
|
||||
|
@ -123,7 +123,7 @@ public:
|
||||
/** ALTER таблицы в виде изменения столбцов, не затрагивающий изменение Storage или его параметров.
|
||||
* (ALTER, затрагивающий изменение движка, делается внешним кодом, путём копирования данных.)
|
||||
*/
|
||||
virtual void alter(const ASTAlterQuery::Parameters ¶ms)
|
||||
virtual void alter(const ASTAlterQuery::Parameters & params)
|
||||
{
|
||||
throw Exception("Method alter is not supported by storage " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ public:
|
||||
/// Метод ALTER позволяет добавлять и удалять столбцы.
|
||||
/// Метод ALTER нужно применять, когда обращения к базе приостановлены.
|
||||
/// Например если параллельно с INSERT выполнить ALTER, то ALTER выполниться, а INSERT бросит исключение
|
||||
void alter(const ASTAlterQuery::Parameters ¶ms);
|
||||
void alter(const ASTAlterQuery::Parameters & params);
|
||||
|
||||
private:
|
||||
String path;
|
||||
@ -332,7 +332,7 @@ private:
|
||||
void removeColumn(String column_name);
|
||||
|
||||
/// Возвращает true если имя директории совпадает с форматом имени директории кусочков
|
||||
bool isBlockDirectory(const String dir_name, Poco::RegularExpression::MatchVec & matches);
|
||||
bool isPartDirectory(const String & dir_name, Poco::RegularExpression::MatchVec & matches) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -14,17 +14,17 @@
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/bind/placeholders.hpp>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
InterpreterAlterQuery::InterpreterAlterQuery(ASTPtr query_ptr_, Context & context_)
|
||||
: query_ptr(query_ptr_), context(context_)
|
||||
: query_ptr(query_ptr_), context(context_)
|
||||
{
|
||||
}
|
||||
|
||||
bool namesEqual( const String &name, const ASTPtr & name_type_)
|
||||
bool namesEqual( const String &name, const ASTPtr & name_type_)
|
||||
{
|
||||
const ASTNameTypePair &name_type = dynamic_cast<const ASTNameTypePair &>(*name_type_);
|
||||
|
||||
const ASTNameTypePair & name_type = dynamic_cast<const ASTNameTypePair &>(*name_type_);
|
||||
return name_type.name == name;
|
||||
}
|
||||
|
||||
@ -33,21 +33,21 @@ void InterpreterAlterQuery::execute()
|
||||
/// Poco::Mutex является рекурсивным, т.е. взятие мьютекса дважды из одного потока не приводит к блокировке
|
||||
Poco::ScopedLock<Poco::Mutex> lock(context.getMutex());
|
||||
|
||||
ASTAlterQuery &alter = dynamic_cast<ASTAlterQuery &>(*query_ptr);
|
||||
ASTAlterQuery & alter = dynamic_cast<ASTAlterQuery &>(*query_ptr);
|
||||
|
||||
String database_name = alter.database.empty() ? context.getCurrentDatabase() : alter.database;
|
||||
String &table_name = alter.table;
|
||||
String & table_name = alter.table;
|
||||
String database_name_escaped = escapeForFileName(database_name);
|
||||
String table_name_escaped = escapeForFileName(table_name);
|
||||
|
||||
StoragePtr table = context.getTable(database_name, table_name);
|
||||
String path = context.getPath();
|
||||
String metadata_path = path + "metadata/" + database_name_escaped + "/" + (!table_name.empty() ? table_name_escaped + ".sql" : "");
|
||||
String metadata_path = path + "metadata/" + database_name_escaped + "/" + table_name_escaped + ".sql";
|
||||
|
||||
ASTPtr attach_ptr = context.getCreateQuery(database_name, table_name);
|
||||
ASTCreateQuery & attach = dynamic_cast< ASTCreateQuery &>(*attach_ptr);
|
||||
ASTPtr attach_ptr = context.getCreateQuery(database_name, table_name);
|
||||
ASTCreateQuery & attach = dynamic_cast< ASTCreateQuery &>(*attach_ptr);
|
||||
attach.attach = true;
|
||||
ASTs & columns = dynamic_cast<ASTExpressionList &>( *attach.columns).children;
|
||||
ASTs & columns = dynamic_cast<ASTExpressionList &>(*attach.columns).children;
|
||||
|
||||
const ASTFunction & storage = dynamic_cast<const ASTFunction &>(*attach.storage);
|
||||
|
||||
@ -81,7 +81,7 @@ void InterpreterAlterQuery::execute()
|
||||
throw DB::Exception("Wrong column name. Cannot find column to insert after", DB::ErrorCodes::ILLEGAL_COLUMN);
|
||||
else
|
||||
{
|
||||
// increase iterator because we want to insert after founded element not before
|
||||
/// increase iterator because we want to insert after found element not before
|
||||
++insert_it;
|
||||
columns.insert(insert_it, params.name_type);
|
||||
}
|
||||
@ -110,7 +110,6 @@ void InterpreterAlterQuery::execute()
|
||||
/// Перезаписываем файл метадата каждую итерацию
|
||||
Poco::FileOutputStream ostr(metadata_path);
|
||||
formatAST(attach, ostr, 0, false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -500,7 +500,7 @@ void StorageMergeTree::loadDataParts()
|
||||
{
|
||||
std::string file_name = it.name();
|
||||
|
||||
if (!isBlockDirectory(file_name, matches))
|
||||
if (!isPartDirectory(file_name, matches))
|
||||
continue;
|
||||
|
||||
DataPartPtr part = new DataPart(*this);
|
||||
@ -917,45 +917,51 @@ void StorageMergeTree::dropImpl()
|
||||
Poco::File(full_path).remove(true);
|
||||
}
|
||||
|
||||
bool namesEqual( const String &name, const DB::NameAndTypePair & name_type)
|
||||
|
||||
bool namesEqual(const String & name, const DB::NameAndTypePair & name_type)
|
||||
{
|
||||
return name_type.first == name;
|
||||
}
|
||||
|
||||
|
||||
void StorageMergeTree::removeColumn(String column_name)
|
||||
{
|
||||
Poco::ScopedLock<Poco::FastMutex> lock(data_parts_mutex);
|
||||
Poco::ScopedLock<Poco::FastMutex> lock_all(all_data_parts_mutex);
|
||||
|
||||
Poco::DirectoryIterator end;
|
||||
Poco::RegularExpression::MatchVec matches;
|
||||
|
||||
NamesAndTypesList::iterator column_it =std::find_if(columns->begin(), columns->end(), boost::bind(namesEqual, column_name, _1));
|
||||
NamesAndTypesList::iterator column_it = std::find_if(columns->begin(), columns->end(), boost::bind(namesEqual, column_name, _1));
|
||||
if (column_it == columns->end())
|
||||
throw DB::Exception("Wrong column name. Cannot find column to drop", DB::ErrorCodes::ILLEGAL_COLUMN);
|
||||
else
|
||||
columns->erase(column_it);
|
||||
|
||||
Poco::RegularExpression::MatchVec matches;
|
||||
Poco::DirectoryIterator end;
|
||||
for (Poco::DirectoryIterator it = Poco::DirectoryIterator(full_path); it != end; ++it)
|
||||
{
|
||||
std::string file_name = it.name();
|
||||
|
||||
if (!isBlockDirectory(file_name, matches))
|
||||
if (!isPartDirectory(file_name, matches))
|
||||
continue;
|
||||
|
||||
String full_dir_name = full_path + file_name + "/";
|
||||
|
||||
Poco::File file(full_dir_name + column_name + ".mrk");
|
||||
if (file.exists())
|
||||
file.remove();
|
||||
file = Poco::File(full_dir_name + column_name + ".bin");
|
||||
if (file.exists())
|
||||
file.remove();
|
||||
{
|
||||
Poco::File file(full_dir_name + column_name + ".mrk");
|
||||
if (file.exists())
|
||||
file.remove();
|
||||
}
|
||||
|
||||
{
|
||||
Poco::File file(full_dir_name + column_name + ".bin");
|
||||
if (file.exists())
|
||||
file.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void StorageMergeTree::alter(const ASTAlterQuery::Parameters ¶ms)
|
||||
void StorageMergeTree::alter(const ASTAlterQuery::Parameters & params)
|
||||
{
|
||||
if (params.type == ASTAlterQuery::ADD)
|
||||
{
|
||||
@ -974,26 +980,27 @@ void StorageMergeTree::alter(const ASTAlterQuery::Parameters ¶ms)
|
||||
++insert_it;
|
||||
}
|
||||
|
||||
const ASTNameTypePair & ast_name_type = dynamic_cast<const ASTNameTypePair&> (*params.name_type);
|
||||
const ASTNameTypePair & ast_name_type = dynamic_cast<const ASTNameTypePair &>(*params.name_type);
|
||||
StringRange type_range = ast_name_type.type->range;
|
||||
String type_string = String(type_range.first, type_range.second - type_range.first);
|
||||
NameAndTypePair pair(ast_name_type.name, context.getDataTypeFactory().get(type_string));
|
||||
columns->insert(insert_it, pair);
|
||||
return;
|
||||
}
|
||||
|
||||
if (params.type == ASTAlterQuery::DROP)
|
||||
else if (params.type == ASTAlterQuery::DROP)
|
||||
{
|
||||
String column_name = dynamic_cast<const ASTIdentifier &>(*params.column).name;
|
||||
String column_name = dynamic_cast<const ASTIdentifier &>(*params.column).name;
|
||||
removeColumn(column_name);
|
||||
}
|
||||
else
|
||||
throw Exception("Wrong parameter type in alter query");
|
||||
throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR);
|
||||
|
||||
}
|
||||
|
||||
bool StorageMergeTree::isBlockDirectory(const String dir_name, Poco::RegularExpression::MatchVec & matches)
|
||||
|
||||
bool StorageMergeTree::isPartDirectory(const String & dir_name, Poco::RegularExpression::MatchVec & matches) const
|
||||
{
|
||||
return (file_name_regexp.match(dir_name, 0, matches) && 6 == matches.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user