mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-23 08:02:02 +00:00
Fix "There is no supertype" error on ALTER UPDATE #7306
This commit is contained in:
parent
796acbc185
commit
fe5fb271dc
@ -367,15 +367,28 @@ ASTPtr MutationsInterpreter::prepare(bool dry_run)
|
||||
affected_materialized.emplace(mat_column);
|
||||
}
|
||||
|
||||
/// Just to be sure, that we don't change type
|
||||
/// after update expression execution.
|
||||
/// When doing UPDATE column = expression WHERE condition
|
||||
/// we will replace column to the result of the following expression:
|
||||
///
|
||||
/// CAST(if(condition, CAST(expression, type), column), type)
|
||||
///
|
||||
/// Inner CAST is needed to make 'if' work when branches have no common type,
|
||||
/// example: type is UInt64, UPDATE x = -1 or UPDATE x = x - 1.
|
||||
///
|
||||
/// Outer CAST is added just in case if we don't trust the returning type of 'if'.
|
||||
|
||||
auto type_literal = std::make_shared<ASTLiteral>(columns_desc.getPhysical(column).type->getName());
|
||||
|
||||
const auto & update_expr = kv.second;
|
||||
auto updated_column = makeASTFunction("CAST",
|
||||
makeASTFunction("if",
|
||||
command.predicate->clone(),
|
||||
update_expr->clone(),
|
||||
makeASTFunction("CAST",
|
||||
update_expr->clone(),
|
||||
type_literal),
|
||||
std::make_shared<ASTIdentifier>(column)),
|
||||
std::make_shared<ASTLiteral>(columns_desc.getPhysical(column).type->getName()));
|
||||
type_literal);
|
||||
|
||||
stages.back().column_to_updated.emplace(column, updated_column);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,21 @@
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
---
|
||||
0
|
||||
0
|
||||
2
|
||||
2
|
||||
4
|
||||
4
|
||||
6
|
||||
6
|
||||
8
|
||||
8
|
13
tests/queries/0_stateless/01413_alter_update_supertype.sql
Normal file
13
tests/queries/0_stateless/01413_alter_update_supertype.sql
Normal file
@ -0,0 +1,13 @@
|
||||
DROP TABLE IF EXISTS t;
|
||||
CREATE TABLE t (x UInt64) ENGINE = MergeTree ORDER BY tuple();
|
||||
INSERT INTO t SELECT number FROM numbers(10);
|
||||
|
||||
SELECT * FROM t;
|
||||
|
||||
SET mutations_sync = 1;
|
||||
ALTER TABLE t UPDATE x = x - 1 WHERE x % 2 = 1;
|
||||
|
||||
SELECT '---';
|
||||
SELECT * FROM t;
|
||||
|
||||
DROP TABLE t;
|
Loading…
Reference in New Issue
Block a user