Fix: throw error when column transformer use non-exsit column

This commit is contained in:
hexiaoting 2020-10-20 15:38:56 +08:00
parent 9a9ad20c6d
commit 293d2f06fa
3 changed files with 11 additions and 8 deletions

View File

@ -12,6 +12,7 @@ namespace DB
namespace ErrorCodes namespace ErrorCodes
{ {
extern const int ILLEGAL_TYPE_OF_ARGUMENT; extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NO_SUCH_COLUMN_IN_TABLE;
} }
void IASTColumnsTransformer::transform(const ASTPtr & transformer, ASTs & nodes) 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); replace_map.emplace(replacement.name, replacement.expr);
} }
UInt8 replace_column_sucess = 0;
for (auto & column : nodes) for (auto & column : nodes)
{ {
if (const auto * id = column->as<ASTIdentifier>()) if (const auto * id = column->as<ASTIdentifier>())
@ -139,6 +141,7 @@ void ASTColumnsReplaceTransformer::transform(ASTs & nodes) const
{ {
column = replace_it->second; column = replace_it->second;
column->setAlias(replace_it->first); column->setAlias(replace_it->first);
++replace_column_sucess;
} }
} }
else if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(column.get())) else if (auto * ast_with_alias = dynamic_cast<ASTWithAlias *>(column.get()))
@ -151,9 +154,15 @@ void ASTColumnsReplaceTransformer::transform(ASTs & nodes) const
replaceChildren(new_ast, column, replace_it->first); replaceChildren(new_ast, column, replace_it->first);
column = new_ast; column = new_ast;
column->setAlias(replace_it->first); 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);
} }
} }

View File

@ -8,7 +8,6 @@
1970-04-11 1970-01-11 1970-11-21 1970-04-11 1970-01-11 1970-11-21
222 18 347 222 18 347
111 11 173.5 111 11 173.5
1970-04-11 1970-01-11 1970-11-21
SELECT SELECT
sum(i), sum(i),
sum(j), sum(j),
@ -51,11 +50,6 @@ SELECT
avg(j + 2 AS j), avg(j + 2 AS j),
avg(k) avg(k)
FROM columns_transformers FROM columns_transformers
SELECT
toDate(any(i)),
toDate(any(j)),
toDate(any(k))
FROM columns_transformers AS a
SELECT SELECT
(i + 1) + 1 AS i, (i + 1) + 1 AS i,
j, j,

View File

@ -13,11 +13,12 @@ SELECT columns_transformers.* EXCEPT(j) APPLY(avg) from columns_transformers;
-- EXCEPT after APPLY will not match anything -- EXCEPT after APPLY will not match anything
SELECT a.* APPLY(toDate) EXCEPT(i, j) APPLY(any) from columns_transformers a; 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 * 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 + 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 } 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 -- 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 * APPLY(sum) from columns_transformers;
EXPLAIN SYNTAX SELECT columns_transformers.* APPLY(avg) 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 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 * 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 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 -- Multiple REPLACE in a row
EXPLAIN SYNTAX SELECT * REPLACE(i + 1 AS i) REPLACE(i + 1 AS i) from columns_transformers; EXPLAIN SYNTAX SELECT * REPLACE(i + 1 AS i) REPLACE(i + 1 AS i) from columns_transformers;