Fix constraints after column rename

This commit is contained in:
alesapin 2020-05-12 14:26:44 +03:00
parent c811e1f0d0
commit 979779e06f
8 changed files with 277 additions and 6 deletions

View File

@ -61,10 +61,11 @@ void CheckConstraintsBlockOutputStream::write(const Block & block)
std::stringstream exception_message;
exception_message << "Constraint " << backQuote(constraints.constraints[i]->name)
auto constraint_ptr = constraints.constraints[i]->as<ASTConstraintDeclaration>();
exception_message << "Constraint " << backQuote(constraint_ptr->name)
<< " for table " << table_id.getNameForLogs()
<< " is violated at row " << (rows_written + row_idx + 1)
<< ". Expression: (" << serializeAST(*(constraints.constraints[i]->expr), true) << ")"
<< ". Expression: (" << serializeAST(*(constraint_ptr->expr), true) << ")"
<< ". Column values";
bool first = true;

View File

@ -467,6 +467,9 @@ void AlterCommand::apply(StorageInMemoryMetadata & metadata) const
}
if (metadata.ttl_for_table_ast)
rename_visitor.visit(metadata.ttl_for_table_ast);
for (auto & constraint : metadata.constraints.constraints)
rename_visitor.visit(constraint);
}
else
throw Exception("Wrong parameter type in ALTER query", ErrorCodes::LOGICAL_ERROR);

View File

@ -33,7 +33,7 @@ ConstraintsDescription ConstraintsDescription::parse(const String & str)
ASTPtr list = parseQuery(parser, str, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
for (const auto & constraint : list->children)
res.constraints.push_back(std::dynamic_pointer_cast<ASTConstraintDeclaration>(constraint));
res.constraints.push_back(constraint);
return res;
}
@ -46,9 +46,10 @@ ConstraintsExpressions ConstraintsDescription::getExpressions(const DB::Context
for (const auto & constraint : constraints)
{
// SyntaxAnalyzer::analyze has query as non-const argument so to avoid accidental query changes we clone it
ASTPtr expr = constraint->expr->clone();
auto constraint_ptr = constraint->as<ASTConstraintDeclaration>();
ASTPtr expr = constraint_ptr->expr->clone();
auto syntax_result = SyntaxAnalyzer(context).analyze(expr, source_columns_);
res.push_back(ExpressionAnalyzer(constraint->expr->clone(), syntax_result, context).getActions(false));
res.push_back(ExpressionAnalyzer(constraint_ptr->expr->clone(), syntax_result, context).getActions(false));
}
return res;
}

View File

@ -6,7 +6,7 @@
namespace DB
{
using ConstraintsASTs = std::vector<std::shared_ptr<ASTConstraintDeclaration>>;
using ConstraintsASTs = std::vector<ASTPtr>;
using ConstraintsExpressions = std::vector<ExpressionActionsPtr>;
struct ConstraintsDescription

View File

@ -0,0 +1,90 @@
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
CREATE TABLE default.table_for_rename\n(\n `date` Date, \n `key` UInt64, \n `value4` String, \n `value5` String, \n `value3` String, \n CONSTRAINT cs_value1 CHECK toInt64(value4) < toInt64(value5), \n CONSTRAINT cs_value2 CHECK toInt64(value5) < toInt64(value3)\n)\nENGINE = MergeTree()\nPARTITION BY date\nORDER BY key\nSETTINGS index_granularity = 8192
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
-- insert after rename --
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
2019-10-02 10 10 11 12
2019-10-03 11 11 12 13
2019-10-01 12 12 13 14
2019-10-02 13 13 14 15
2019-10-03 14 14 15 16
2019-10-01 15 15 16 17
2019-10-02 16 16 17 18
2019-10-03 17 17 18 19
2019-10-01 18 18 19 20
2019-10-02 19 19 20 21
-- rename columns back --
CREATE TABLE default.table_for_rename\n(\n `date` Date, \n `key` UInt64, \n `value1` String, \n `value2` String, \n `value3` String, \n CONSTRAINT cs_value1 CHECK toInt64(value1) < toInt64(value2), \n CONSTRAINT cs_value2 CHECK toInt64(value2) < toInt64(value3)\n)\nENGINE = MergeTree()\nPARTITION BY date\nORDER BY key\nSETTINGS index_granularity = 8192
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
2019-10-02 10 10 11 12
2019-10-03 11 11 12 13
2019-10-01 12 12 13 14
2019-10-02 13 13 14 15
2019-10-03 14 14 15 16
2019-10-01 15 15 16 17
2019-10-02 16 16 17 18
2019-10-03 17 17 18 19
2019-10-01 18 18 19 20
2019-10-02 19 19 20 21
-- insert after rename column --
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
2019-10-02 10 10 11 12
2019-10-03 11 11 12 13
2019-10-01 12 12 13 14
2019-10-02 13 13 14 15
2019-10-03 14 14 15 16
2019-10-01 15 15 16 17
2019-10-02 16 16 17 18
2019-10-03 17 17 18 19
2019-10-01 18 18 19 20
2019-10-02 19 19 20 21
2019-10-03 20 20 21 22
2019-10-01 21 21 22 23
2019-10-02 22 22 23 24
2019-10-03 23 23 24 25
2019-10-01 24 24 25 26
2019-10-02 25 25 26 27
2019-10-03 26 26 27 28
2019-10-01 27 27 28 29
2019-10-02 28 28 29 30
2019-10-03 29 29 30 31

View File

@ -0,0 +1,43 @@
DROP TABLE IF EXISTS table_for_rename;
CREATE TABLE table_for_rename
(
date Date,
key UInt64,
value1 String,
value2 String,
value3 String,
CONSTRAINT cs_value1 CHECK toInt64(value1) < toInt64(value2),
CONSTRAINT cs_value2 CHECK toInt64(value2) < toInt64(value3)
)
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 + 1), toString(number + 2) from numbers(9);
INSERT INTO table_for_rename SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number) from numbers(9); ; --{serverError 469}
SELECT * FROM table_for_rename ORDER BY key;
ALTER TABLE table_for_rename RENAME COLUMN value1 TO value4;
ALTER TABLE table_for_rename RENAME COLUMN value2 TO value5;
SHOW CREATE TABLE table_for_rename;
SELECT * FROM table_for_rename ORDER BY key;
SELECT '-- insert after rename --';
INSERT INTO table_for_rename SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number + 2) from numbers(10, 10);
INSERT INTO table_for_rename SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number) from numbers(10, 10); ; --{serverError 469}
SELECT * FROM table_for_rename ORDER BY key;
SELECT '-- rename columns back --';
ALTER TABLE table_for_rename RENAME COLUMN value4 TO value1;
ALTER TABLE table_for_rename RENAME COLUMN value5 TO value2;
SHOW CREATE TABLE table_for_rename;
SELECT * FROM table_for_rename ORDER BY key;
SELECT '-- insert after rename column --';
INSERT INTO table_for_rename SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number + 2) from numbers(20,10);
INSERT INTO table_for_rename SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number), toString(number + 2) from numbers(20, 10); ; --{serverError 469}
SELECT * FROM table_for_rename ORDER BY key;
DROP TABLE IF EXISTS table_for_rename;

View File

@ -0,0 +1,90 @@
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
CREATE TABLE default.table_for_rename1\n(\n `date` Date, \n `key` UInt64, \n `value4` String, \n `value5` String, \n `value3` String, \n CONSTRAINT cs_value1 CHECK toInt64(value4) < toInt64(value5), \n CONSTRAINT cs_value2 CHECK toInt64(value5) < toInt64(value3)\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/tables/test_for_rename\', \'1\')\nPARTITION BY date\nORDER BY key\nSETTINGS index_granularity = 8192
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
-- insert after rename --
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
2019-10-02 10 10 11 12
2019-10-03 11 11 12 13
2019-10-01 12 12 13 14
2019-10-02 13 13 14 15
2019-10-03 14 14 15 16
2019-10-01 15 15 16 17
2019-10-02 16 16 17 18
2019-10-03 17 17 18 19
2019-10-01 18 18 19 20
2019-10-02 19 19 20 21
-- rename columns back --
CREATE TABLE default.table_for_rename1\n(\n `date` Date, \n `key` UInt64, \n `value1` String, \n `value2` String, \n `value3` String, \n CONSTRAINT cs_value1 CHECK toInt64(value1) < toInt64(value2), \n CONSTRAINT cs_value2 CHECK toInt64(value2) < toInt64(value3)\n)\nENGINE = ReplicatedMergeTree(\'/clickhouse/tables/test_for_rename\', \'1\')\nPARTITION BY date\nORDER BY key\nSETTINGS index_granularity = 8192
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
2019-10-02 10 10 11 12
2019-10-03 11 11 12 13
2019-10-01 12 12 13 14
2019-10-02 13 13 14 15
2019-10-03 14 14 15 16
2019-10-01 15 15 16 17
2019-10-02 16 16 17 18
2019-10-03 17 17 18 19
2019-10-01 18 18 19 20
2019-10-02 19 19 20 21
-- insert after rename column --
2019-10-01 0 0 1 2
2019-10-02 1 1 2 3
2019-10-03 2 2 3 4
2019-10-01 3 3 4 5
2019-10-02 4 4 5 6
2019-10-03 5 5 6 7
2019-10-01 6 6 7 8
2019-10-02 7 7 8 9
2019-10-03 8 8 9 10
2019-10-02 10 10 11 12
2019-10-03 11 11 12 13
2019-10-01 12 12 13 14
2019-10-02 13 13 14 15
2019-10-03 14 14 15 16
2019-10-01 15 15 16 17
2019-10-02 16 16 17 18
2019-10-03 17 17 18 19
2019-10-01 18 18 19 20
2019-10-02 19 19 20 21
2019-10-03 20 20 21 22
2019-10-01 21 21 22 23
2019-10-02 22 22 23 24
2019-10-03 23 23 24 25
2019-10-01 24 24 25 26
2019-10-02 25 25 26 27
2019-10-03 26 26 27 28
2019-10-01 27 27 28 29
2019-10-02 28 28 29 30
2019-10-03 29 29 30 31

View File

@ -0,0 +1,43 @@
DROP TABLE IF EXISTS table_for_rename1;
CREATE TABLE table_for_rename1
(
date Date,
key UInt64,
value1 String,
value2 String,
value3 String,
CONSTRAINT cs_value1 CHECK toInt64(value1) < toInt64(value2),
CONSTRAINT cs_value2 CHECK toInt64(value2) < toInt64(value3)
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/test_for_rename', '1')
PARTITION BY date
ORDER BY key;
INSERT INTO table_for_rename1 SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number + 2) from numbers(9);
INSERT INTO table_for_rename1 SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number) from numbers(9); ; --{serverError 469}
SELECT * FROM table_for_rename1 ORDER BY key;
ALTER TABLE table_for_rename1 RENAME COLUMN value1 TO value4;
ALTER TABLE table_for_rename1 RENAME COLUMN value2 TO value5;
SHOW CREATE TABLE table_for_rename1;
SELECT * FROM table_for_rename1 ORDER BY key;
SELECT '-- insert after rename --';
INSERT INTO table_for_rename1 SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number + 2) from numbers(10, 10);
INSERT INTO table_for_rename1 SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number) from numbers(10, 10); ; --{serverError 469}
SELECT * FROM table_for_rename1 ORDER BY key;
SELECT '-- rename columns back --';
ALTER TABLE table_for_rename1 RENAME COLUMN value4 TO value1;
ALTER TABLE table_for_rename1 RENAME COLUMN value5 TO value2;
SHOW CREATE TABLE table_for_rename1;
SELECT * FROM table_for_rename1 ORDER BY key;
SELECT '-- insert after rename column --';
INSERT INTO table_for_rename1 SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number + 1), toString(number + 2) from numbers(20,10);
INSERT INTO table_for_rename1 SELECT toDate('2019-10-01') + number % 3, number, toString(number), toString(number), toString(number + 2) from numbers(20, 10); ; --{serverError 469}
SELECT * FROM table_for_rename1 ORDER BY key;
DROP TABLE IF EXISTS table_for_rename1;