Small progress on alter rename

This commit is contained in:
alesapin 2020-03-25 21:44:08 +03:00
parent 42703c727b
commit 41feef1901
13 changed files with 84 additions and 22 deletions

View File

@ -28,7 +28,6 @@ enum class AccessType
ADD_COLUMN,
DROP_COLUMN,
MODIFY_COLUMN,
RENAME_COLUMN,
COMMENT_COLUMN,
CLEAR_COLUMN,
ALTER_COLUMN, /// allow to execute ALTER {ADD|DROP|MODIFY...} COLUMN
@ -196,7 +195,6 @@ namespace impl
ACCESS_TYPE_TO_KEYWORD_CASE(ADD_COLUMN);
ACCESS_TYPE_TO_KEYWORD_CASE(DROP_COLUMN);
ACCESS_TYPE_TO_KEYWORD_CASE(MODIFY_COLUMN);
ACCESS_TYPE_TO_KEYWORD_CASE(RENAME_COLUMN);
ACCESS_TYPE_TO_KEYWORD_CASE(COMMENT_COLUMN);
ACCESS_TYPE_TO_KEYWORD_CASE(CLEAR_COLUMN);
ACCESS_TYPE_TO_KEYWORD_CASE(ALTER_COLUMN);

View File

@ -282,7 +282,6 @@ AccessRightsElements InterpreterAlterQuery::getRequiredAccessForCommand(const AS
}
case ASTAlterCommand::RENAME_COLUMN:
{
required_access.emplace_back(AccessType::RENAME_COLUMN, database, table);
break;
}
case ASTAlterCommand::NO_TYPE: break;

View File

@ -27,6 +27,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
ParserKeyword s_drop_column("DROP COLUMN");
ParserKeyword s_clear_column("CLEAR COLUMN");
ParserKeyword s_modify_column("MODIFY COLUMN");
ParserKeyword s_rename_column("RENAME COLUMN");
ParserKeyword s_comment_column("COMMENT COLUMN");
ParserKeyword s_modify_order_by("MODIFY ORDER BY");
ParserKeyword s_modify_ttl("MODIFY TTL");
@ -77,6 +78,7 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
ParserKeyword s_delete_where("DELETE WHERE");
ParserKeyword s_update("UPDATE");
ParserKeyword s_where("WHERE");
ParserKeyword s_to("TO");
ParserCompoundIdentifier parser_name;
ParserStringLiteral parser_string_literal;
@ -121,6 +123,22 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
command->type = ASTAlterCommand::ADD_COLUMN;
}
else if (s_rename_column.ignore(pos, expected))
{
if (s_if_not_exists.ignore(pos, expected))
command->if_not_exists = true;
if (!parser_name.parse(pos, command->column, expected))
return false;
if (!s_to.ignore(pos, expected))
return false;
if (!parser_name.parse(pos, command->rename_to, expected))
return false;
command->type = ASTAlterCommand::RENAME_COLUMN;
}
else if (s_drop_partition.ignore(pos, expected))
{
if (!parser_partition.parse(pos, command->partition, expected))

View File

@ -12,6 +12,7 @@ namespace DB
* [DROP COLUMN [IF EXISTS] col_to_drop, ...]
* [CLEAR COLUMN [IF EXISTS] col_to_clear [IN PARTITION partition],]
* [MODIFY COLUMN [IF EXISTS] col_to_modify type, ...]
* [RENAME COLUMN [IF EXISTS] col_name TO col_name]
* [MODIFY PRIMARY KEY (a, b, c...)]
* [MODIFY SETTING setting_name=setting_value, ...]
* [COMMENT COLUMN [IF EXISTS] col_name string]

View File

@ -533,7 +533,7 @@ bool AlterCommand::isRequireMutationStage(const StorageInMemoryMetadata & metada
if (ignore)
return false;
if (type == DROP_COLUMN || type == DROP_INDEX || type == RENAME_COLUMN)
if (type == DROP_COLUMN || type == DROP_INDEX)
return true;
if (type != MODIFY_COLUMN || data_type == nullptr)

View File

@ -199,23 +199,14 @@ void ColumnsDescription::remove(const String & column_name)
void ColumnsDescription::rename(const String & column_from, const String & column_to)
{
auto range = getNameRange(columns, column_from);
auto it = columns.get<1>().find(column_from);
if (it == columns.get<1>().end())
throw Exception("Cannot find column " + column_from + " in ColumnsDescription", ErrorCodes::LOGICAL_ERROR);
if (range.first == range.second)
throw Exception("There is no column " + column_from + " in table.", ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
std::vector<ColumnDescription> iterators;
for (auto list_it = range.first; list_it != range.second;)
columns.get<1>().modify_key(it, [&column_to] (String & old_name)
{
iterators.push_back(*list_it);
list_it = columns.get<0>().erase(list_it);
}
for (auto & col_desc : iterators)
{
boost::replace_all(col_desc.name, column_from, column_to);
add(col_desc);
}
old_name = column_to;
});
}

View File

@ -30,6 +30,7 @@ IMergeTreeReader::IMergeTreeReader(const MergeTreeData::DataPartPtr & data_part_
, columns(columns_), uncompressed_cache(uncompressed_cache_), mark_cache(mark_cache_)
, settings(settings_), storage(data_part_->storage)
, all_mark_ranges(all_mark_ranges_)
, alter_conversions(storage.getAlterConversionsForPart(data_part))
{
}
@ -62,6 +63,11 @@ static bool arrayHasNoElementsRead(const IColumn & column)
}
//void IMergeTreeReader::renameColumnsWithAlters(Columns & res_columns)
//{
//
//}
void IMergeTreeReader::fillMissingColumns(Columns & res_columns, bool & should_evaluate_missing_defaults, size_t num_rows)
{
try

View File

@ -48,6 +48,9 @@ public:
/// try to perform conversions of columns.
void performRequiredConversions(Columns & res_columns);
/// TODO(alesap)
void renameColumnsWithAlters(Columns & res_columns);
const NamesAndTypesList & getColumns() const { return columns; }
size_t numColumnsInResult() const { return columns.size(); }
@ -78,6 +81,7 @@ protected:
MarkRanges all_mark_ranges;
friend class MergeTreeRangeReader::DelayedStream;
MergeTreeData::AlterConversions alter_conversions;
};
}

View File

@ -3589,8 +3589,15 @@ bool MergeTreeData::canUsePolymorphicParts(const MergeTreeSettings & settings, S
return true;
}
MergeTreeData::AlterConversions MergeTreeData::getAlterConversionsForPart(const MergeTreeDataPartPtr /*part*/) const
MergeTreeData::AlterConversions MergeTreeData::getAlterConversionsForPart(const MergeTreeDataPartPtr part) const
{
return AlterConversions{};
MutationCommands commands = getFirtsAlterMutationCommandsForPart(part);
AlterConversions result{};
for (const auto & command : commands)
if (command.type == MutationCommand::Type::RENAME_COLUMN)
result.rename_map[command.column_name] = command.rename_to;
return result;
}
}

View File

@ -94,6 +94,15 @@ std::optional<MutationCommand> MutationCommand::parse(ASTAlterCommand * command,
res.clear = true;
return res;
}
else if (parse_alter_commands && command->type == ASTAlterCommand::RENAME_COLUMN)
{
MutationCommand res;
res.ast = command->ptr();
res.type = MutationCommand::Type::RENAME_COLUMN;
res.column_name = command->column->as<ASTIdentifier &>().name;
res.rename_to = command->rename_to->as<ASTIdentifier &>().name;
return res;
}
else if (command->type == ASTAlterCommand::MATERIALIZE_TTL)
{
MutationCommand res;

View File

@ -31,7 +31,8 @@ struct MutationCommand
READ_COLUMN,
DROP_COLUMN,
DROP_INDEX,
MATERIALIZE_TTL
MATERIALIZE_TTL,
RENAME_COLUMN,
};
Type type = EMPTY;
@ -53,6 +54,9 @@ struct MutationCommand
/// We need just clear column, not drop from metadata.
bool clear = false;
/// Column rename_to
String rename_to;
/// If parse_alter_commands, than consider more Alter commands as mutation commands
static std::optional<MutationCommand> parse(ASTAlterCommand * command, bool parse_alter_commands = false);
};

View File

@ -0,0 +1,2 @@
1
1

View File

@ -0,0 +1,23 @@
DROP TABLE IF EXISTS table_for_rename;
CREATE TABLE table_for_rename
(
date Date,
key UInt64,
value1 String,
value2 String,
value3 String
)
ENGINE = MergeTree()
PARTITION BY date
ORDER BY key;
INSERT INTO table_for_rename SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number), toString(number) from numbers(9);
SELECT value1 FROM table_for_rename WHERE key = 1;
ALTER TABLE table_for_rename RENAME COLUMN value1 to renamed_value1;
SELECT renamed_value1 FROM table_for_rename WHERE key = 1;
DROP TABLE IF EXISTS table_for_rename;