From 979779e06ff298ad328b24bef527d77e4cb76c81 Mon Sep 17 00:00:00 2001 From: alesapin Date: Tue, 12 May 2020 14:26:44 +0300 Subject: [PATCH] Fix constraints after column rename --- .../CheckConstraintsBlockOutputStream.cpp | 5 +- src/Storages/AlterCommands.cpp | 3 + src/Storages/ConstraintsDescription.cpp | 7 +- src/Storages/ConstraintsDescription.h | 2 +- ...7_alter_rename_column_constraint.reference | 90 +++++++++++++++++++ .../01277_alter_rename_column_constraint.sql | 43 +++++++++ ...name_column_constraint_zookeeper.reference | 90 +++++++++++++++++++ ...ter_rename_column_constraint_zookeeper.sql | 43 +++++++++ 8 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 tests/queries/0_stateless/01277_alter_rename_column_constraint.reference create mode 100644 tests/queries/0_stateless/01277_alter_rename_column_constraint.sql create mode 100644 tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.reference create mode 100644 tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.sql diff --git a/src/DataStreams/CheckConstraintsBlockOutputStream.cpp b/src/DataStreams/CheckConstraintsBlockOutputStream.cpp index 25e4340f655..1b87cf29dd4 100644 --- a/src/DataStreams/CheckConstraintsBlockOutputStream.cpp +++ b/src/DataStreams/CheckConstraintsBlockOutputStream.cpp @@ -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(); + 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; diff --git a/src/Storages/AlterCommands.cpp b/src/Storages/AlterCommands.cpp index 121974c8b82..67bd88d10a8 100644 --- a/src/Storages/AlterCommands.cpp +++ b/src/Storages/AlterCommands.cpp @@ -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); diff --git a/src/Storages/ConstraintsDescription.cpp b/src/Storages/ConstraintsDescription.cpp index 8c38af0cd5e..023a94a5628 100644 --- a/src/Storages/ConstraintsDescription.cpp +++ b/src/Storages/ConstraintsDescription.cpp @@ -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(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(); + 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; } diff --git a/src/Storages/ConstraintsDescription.h b/src/Storages/ConstraintsDescription.h index 3ced0e8ddc9..b7bfc1a1acf 100644 --- a/src/Storages/ConstraintsDescription.h +++ b/src/Storages/ConstraintsDescription.h @@ -6,7 +6,7 @@ namespace DB { -using ConstraintsASTs = std::vector>; +using ConstraintsASTs = std::vector; using ConstraintsExpressions = std::vector; struct ConstraintsDescription diff --git a/tests/queries/0_stateless/01277_alter_rename_column_constraint.reference b/tests/queries/0_stateless/01277_alter_rename_column_constraint.reference new file mode 100644 index 00000000000..cb1842f95da --- /dev/null +++ b/tests/queries/0_stateless/01277_alter_rename_column_constraint.reference @@ -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 diff --git a/tests/queries/0_stateless/01277_alter_rename_column_constraint.sql b/tests/queries/0_stateless/01277_alter_rename_column_constraint.sql new file mode 100644 index 00000000000..72fbb045601 --- /dev/null +++ b/tests/queries/0_stateless/01277_alter_rename_column_constraint.sql @@ -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; diff --git a/tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.reference b/tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.reference new file mode 100644 index 00000000000..9ca17dbbc0a --- /dev/null +++ b/tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.reference @@ -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 diff --git a/tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.sql b/tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.sql new file mode 100644 index 00000000000..2d3a7e0198a --- /dev/null +++ b/tests/queries/0_stateless/01277_alter_rename_column_constraint_zookeeper.sql @@ -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;