From 293d2f06fac5d1daeca5e24b5c0c43910195a307 Mon Sep 17 00:00:00 2001 From: hexiaoting Date: Tue, 20 Oct 2020 15:38:56 +0800 Subject: [PATCH] Fix: throw error when column transformer use non-exsit column --- src/Parsers/ASTColumnsTransformers.cpp | 9 +++++++++ .../0_stateless/01470_columns_transformers.reference | 6 ------ tests/queries/0_stateless/01470_columns_transformers.sql | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Parsers/ASTColumnsTransformers.cpp b/src/Parsers/ASTColumnsTransformers.cpp index 43d54f07ab8..a204a409926 100644 --- a/src/Parsers/ASTColumnsTransformers.cpp +++ b/src/Parsers/ASTColumnsTransformers.cpp @@ -12,6 +12,7 @@ namespace DB namespace ErrorCodes { extern const int ILLEGAL_TYPE_OF_ARGUMENT; + extern const int NO_SUCH_COLUMN_IN_TABLE; } void IASTColumnsTransformer::transform(const ASTPtr & transformer, ASTs & nodes) @@ -130,6 +131,7 @@ void ASTColumnsReplaceTransformer::transform(ASTs & nodes) const replace_map.emplace(replacement.name, replacement.expr); } + UInt8 replace_column_sucess = 0; for (auto & column : nodes) { if (const auto * id = column->as()) @@ -139,6 +141,7 @@ void ASTColumnsReplaceTransformer::transform(ASTs & nodes) const { column = replace_it->second; column->setAlias(replace_it->first); + ++replace_column_sucess; } } else if (auto * ast_with_alias = dynamic_cast(column.get())) @@ -151,9 +154,15 @@ void ASTColumnsReplaceTransformer::transform(ASTs & nodes) const replaceChildren(new_ast, column, replace_it->first); column = new_ast; column->setAlias(replace_it->first); + ++replace_column_sucess; } } } + + if (replace_column_sucess < replace_map.size()) + throw Exception( + "Expressions in columns transformer REPLACE should use same column name as original column", + ErrorCodes::NO_SUCH_COLUMN_IN_TABLE); } } diff --git a/tests/queries/0_stateless/01470_columns_transformers.reference b/tests/queries/0_stateless/01470_columns_transformers.reference index 2d8a1802289..397499f990f 100644 --- a/tests/queries/0_stateless/01470_columns_transformers.reference +++ b/tests/queries/0_stateless/01470_columns_transformers.reference @@ -8,7 +8,6 @@ 1970-04-11 1970-01-11 1970-11-21 222 18 347 111 11 173.5 -1970-04-11 1970-01-11 1970-11-21 SELECT sum(i), sum(j), @@ -51,11 +50,6 @@ SELECT avg(j + 2 AS j), avg(k) FROM columns_transformers -SELECT - toDate(any(i)), - toDate(any(j)), - toDate(any(k)) -FROM columns_transformers AS a SELECT (i + 1) + 1 AS i, j, diff --git a/tests/queries/0_stateless/01470_columns_transformers.sql b/tests/queries/0_stateless/01470_columns_transformers.sql index f95cee51fb0..755978e82c4 100644 --- a/tests/queries/0_stateless/01470_columns_transformers.sql +++ b/tests/queries/0_stateless/01470_columns_transformers.sql @@ -13,11 +13,12 @@ SELECT columns_transformers.* EXCEPT(j) APPLY(avg) from columns_transformers; -- EXCEPT after APPLY will not match anything SELECT a.* APPLY(toDate) EXCEPT(i, j) APPLY(any) from columns_transformers a; +SELECT * REPLACE(i + 1 AS col) from columns_transformers; -- { serverError 16 } SELECT * REPLACE(i + 1 AS i) APPLY(sum) from columns_transformers; SELECT columns_transformers.* REPLACE(j + 2 AS j, i + 1 AS i) APPLY(avg) from columns_transformers; SELECT columns_transformers.* REPLACE(j + 1 AS j, j + 2 AS j) APPLY(avg) from columns_transformers; -- { serverError 43 } -- REPLACE after APPLY will not match anything -SELECT a.* APPLY(toDate) REPLACE(i + 1 AS i) APPLY(any) from columns_transformers a; +SELECT a.* APPLY(toDate) REPLACE(i + 1 AS i) APPLY(any) from columns_transformers a; -- { serverError 16 } EXPLAIN SYNTAX SELECT * APPLY(sum) from columns_transformers; EXPLAIN SYNTAX SELECT columns_transformers.* APPLY(avg) from columns_transformers; @@ -28,7 +29,6 @@ EXPLAIN SYNTAX SELECT columns_transformers.* EXCEPT(j) APPLY(avg) from columns_t EXPLAIN SYNTAX SELECT a.* APPLY(toDate) EXCEPT(i, j) APPLY(any) from columns_transformers a; EXPLAIN SYNTAX SELECT * REPLACE(i + 1 AS i) APPLY(sum) from columns_transformers; EXPLAIN SYNTAX SELECT columns_transformers.* REPLACE(j + 2 AS j, i + 1 AS i) APPLY(avg) from columns_transformers; -EXPLAIN SYNTAX SELECT a.* APPLY(toDate) REPLACE(i + 1 AS i) APPLY(any) from columns_transformers a; -- Multiple REPLACE in a row EXPLAIN SYNTAX SELECT * REPLACE(i + 1 AS i) REPLACE(i + 1 AS i) from columns_transformers;