Fix primary.idx corruption after delete mutation

This commit is contained in:
alesapin 2020-02-07 16:54:17 +03:00
parent f4467aaa65
commit f3a35998e1
3 changed files with 84 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/formatAST.h>
#include <Parsers/ASTOrderByElement.h>
#include <IO/WriteHelpers.h>
@ -525,6 +526,25 @@ ASTPtr MutationsInterpreter::prepareInterpreterSelectQuery(std::vector<Stage> &
}
select->setExpression(ASTSelectQuery::Expression::WHERE, std::move(where_expression));
}
auto metadata = storage->getInMemoryMetadata();
/// We have to execute select in order of primary key
/// because we don't sort results additionaly and don't have
/// any guarantees on data order without ORDER BY. It's almost free, because we
/// have optimization for data read in primary key order.
if (metadata.order_by_ast)
{
ASTPtr dummy;
auto order_by_expr = std::make_shared<ASTOrderByElement>(1, 0, false, dummy, false, dummy, dummy, dummy);
if (metadata.primary_key_ast)
order_by_expr->children.push_back(metadata.primary_key_ast);
else
order_by_expr->children.push_back(metadata.order_by_ast);
auto res = std::make_shared<ASTExpressionList>();
res->children.push_back(order_by_expr);
select->setExpression(ASTSelectQuery::Expression::ORDER_BY, std::move(res));
}
return select;
}

View File

@ -0,0 +1,4 @@
43200
0
43200
43200

View File

@ -0,0 +1,60 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh
$CLICKHOUSE_CLIENT --query "DROP TABLE IF EXISTS movement"
$CLICKHOUSE_CLIENT -n --query "CREATE TABLE movement (date DateTime('Europe/Moscow')) Engine = MergeTree ORDER BY (toStartOfHour(date));"
$CLICKHOUSE_CLIENT --query "insert into movement select toDateTime('2020-01-22 00:00:00') + number%(23*3600) from numbers(1000000);"
$CLICKHOUSE_CLIENT --query "OPTIMIZE TABLE movement FINAL"
$CLICKHOUSE_CLIENT -n --query "
SELECT
count(),
toStartOfHour(date) AS Hour
FROM movement
WHERE (date >= toDateTime('2020-01-22T10:00:00')) AND (date <= toDateTime('2020-01-22T23:00:00'))
GROUP BY Hour
ORDER BY Hour DESC
" | grep "16:00:00" | cut -f1
$CLICKHOUSE_CLIENT --query "alter table movement delete where date >= toDateTime('2020-01-22T16:00:00') and date < toDateTime('2020-01-22T17:00:00') SETTINGS mutations_sync = 2"
$CLICKHOUSE_CLIENT -n --query "
SELECT
count(),
toStartOfHour(date) AS Hour
FROM movement
WHERE (date >= toDateTime('2020-01-22T10:00:00')) AND (date <= toDateTime('2020-01-22T23:00:00'))
GROUP BY Hour
ORDER BY Hour DESC
" | grep "16:00:00" | wc -l
$CLICKHOUSE_CLIENT -n --query "
SELECT
count(),
toStartOfHour(date) AS Hour
FROM movement
WHERE (date >= toDateTime('2020-01-22T10:00:00')) AND (date <= toDateTime('2020-01-22T23:00:00'))
GROUP BY Hour
ORDER BY Hour DESC
" | grep "22:00:00" | cut -f1
$CLICKHOUSE_CLIENT -n --query "
SELECT
count(),
toStartOfHour(date) AS Hour
FROM movement
GROUP BY Hour
ORDER BY Hour DESC
" | grep "22:00:00" | cut -f1
$CLICKHOUSE_CLIENT --query "DROP TABLE IF EXISTS movement"