Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MATERIALIZE TTL IN PARTITION #9581

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions dbms/src/Parsers/ASTAlterQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ void ASTAlterCommand::formatImpl(
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "MATERIALIZE TTL"
<< (settings.hilite ? hilite_none : "");
if (partition)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str<< " IN PARTITION " << (settings.hilite ? hilite_none : "");
partition->formatImpl(settings, state, frame);
}
}
else if (type == ASTAlterCommand::MODIFY_SETTING)
{
Expand Down
6 changes: 6 additions & 0 deletions dbms/src/Parsers/ParserAlterQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,12 @@ bool ParserAlterCommand::parseImpl(Pos & pos, ASTPtr & node, Expected & expected
else if (s_materialize_ttl.ignore(pos, expected))
{
command->type = ASTAlterCommand::MATERIALIZE_TTL;

if (s_in_partition.ignore(pos, expected))
{
if (!parser_partition.parse(pos, command->partition, expected))
return false;
}
}
else if (s_modify_setting.ignore(pos, expected))
{
Expand Down
1 change: 1 addition & 0 deletions dbms/src/Storages/MutationCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ std::optional<MutationCommand> MutationCommand::parse(ASTAlterCommand * command,
MutationCommand res;
res.ast = command->ptr();
res.type = MATERIALIZE_TTL;
res.partition = command->partition;
return res;
}
return {};
Expand Down
75 changes: 73 additions & 2 deletions dbms/tests/integration/test_ttl_move/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ def get_random_string(length):
return str(result_list)


def get_used_disks_for_table(node, table_name):
return node.query("select disk_name from system.parts where table == '{}' and active=1 order by modification_time".format(table_name)).strip().split('\n')
def get_used_disks_for_table(node, table_name, partition=None):
if partition is None:
suffix = ""
else:
suffix = "and partition='{}'".format(partition)
return node.query("""
SELECT disk_name
FROM system.parts
WHERE table == '{name}' AND active=1 {suffix}
ORDER BY modification_time
""".format(name=table_name, suffix=suffix)).strip().split('\n')


@pytest.mark.skip(reason="Flappy test")
Expand Down Expand Up @@ -587,6 +596,68 @@ def test_ttls_do_not_work_after_alter(started_cluster, name, engine, positive, b
node1.query("DROP TABLE IF EXISTS {}".format(name))


@pytest.mark.parametrize("name,engine", [
("mt_test_materialize_ttl_in_partition","MergeTree()"),
("replicated_mt_test_materialize_ttl_in_partition","ReplicatedMergeTree('/clickhouse/test_materialize_ttl_in_partition', '1')"),
])
def test_materialize_ttl_in_partition(started_cluster, name, engine):
try:
node1.query("""
CREATE TABLE {name} (
p1 Int8,
s1 String,
d1 DateTime
) ENGINE = {engine}
ORDER BY p1
PARTITION BY p1
SETTINGS storage_policy='small_jbod_with_external'
""".format(name=name, engine=engine))

data = [] # 5MB in total
for i in range(5):
data.append((str(i), "'{}'".format(get_random_string(1024 * 1024)), "toDateTime({})".format(time.time()-1))) # 1MB row
node1.query("INSERT INTO {} (p1, s1, d1) VALUES {}".format(name, ",".join(["(" + ",".join(x) + ")" for x in data])))

time.sleep(0.5)

used_disks = get_used_disks_for_table(node1, name)
assert set(used_disks) == {"jbod1"}

node1.query("""
ALTER TABLE {name}
MODIFY TTL
d1 TO DISK 'external'
""".format(name=name))

time.sleep(0.5)

used_disks = get_used_disks_for_table(node1, name)
assert set(used_disks) == {"jbod1"}

node1.query("""
ALTER TABLE {name}
MATERIALIZE TTL IN PARTITION 2
""".format(name=name))

node1.query("""
ALTER TABLE {name}
MATERIALIZE TTL IN PARTITION 4
""".format(name=name))

time.sleep(0.5)

used_disks_sets = []
for i in range(len(data)):
used_disks_sets.append(set(get_used_disks_for_table(node1, name, partition=i)))

assert used_disks_sets == [{"jbod1"}, {"jbod1"}, {"external"}, {"jbod1"}, {"external"}]

assert node1.query("SELECT count() FROM {name}".format(name=name)).strip() == str(len(data))

finally:
node1.query("DROP TABLE IF EXISTS {}".format(name))


@pytest.mark.skip(reason="Flappy test")
@pytest.mark.parametrize("name,engine,positive", [
("mt_test_alter_multiple_ttls_positive", "MergeTree()", True),
Expand Down