Skip to content

Commit ca36de6

Browse files
Merge pull request ClickHouse#79505 from Algunenano/default_quick
Fix 2 cases of "Logical Error: Can't set alias of * of Asterisk on alias"
2 parents f3222bd + 52b2bc7 commit ca36de6

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/Interpreters/OptimizeIfWithConstantConditionVisitor.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,19 @@ void OptimizeIfWithConstantConditionVisitorData::visit(ASTFunction & function_no
9898
if (tryExtractConstValueFromCondition(condition_expr, condition))
9999
{
100100
ASTPtr replace_ast = condition ? then_expr : else_expr;
101+
bool replacement_supports_alias = dynamic_cast<ASTWithAlias *>(replace_ast.get());
102+
String if_alias = ast->tryGetAlias();
103+
/// We cannot set the resulting alias if the replace ast does not support it (e.g. ASTAsterisk), so it's better to do nothing
104+
if (!if_alias.empty() && !replacement_supports_alias)
105+
return;
106+
101107
ASTPtr child_copy = ast;
102108
String replace_alias = replace_ast->tryGetAlias();
103-
String if_alias = ast->tryGetAlias();
104109

105110
if (replace_alias.empty())
106111
{
107-
replace_ast->setAlias(if_alias);
112+
if (!if_alias.empty())
113+
replace_ast->setAlias(if_alias);
108114
ast = replace_ast;
109115
}
110116
else
@@ -113,12 +119,14 @@ void OptimizeIfWithConstantConditionVisitorData::visit(ASTFunction & function_no
113119
/// But IAST has only method for deep copy of subtree.
114120
/// This can be a reason of performance degradation in case of deep queries.
115121
ASTPtr replace_ast_deep_copy = replace_ast->clone();
116-
replace_ast_deep_copy->setAlias(if_alias);
122+
if (!if_alias.empty())
123+
replace_ast_deep_copy->setAlias(if_alias);
117124
ast = replace_ast_deep_copy;
118125
}
119126

120127
if (!if_alias.empty())
121128
{
129+
ast->setAlias(if_alias);
122130
auto alias_it = aliases.find(if_alias);
123131
if (alias_it != aliases.end() && alias_it->second.get() == child_copy.get())
124132
alias_it->second = ast;

src/Interpreters/TreeRewriter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,12 @@ void renameDuplicatedColumns(const ASTSelectQuery * select_query)
366366
for (auto & expr : elements)
367367
{
368368
auto name = expr->getAliasOrColumnName();
369-
370369
if (!assigned_column_names.insert(name).second)
371370
{
371+
/// We can't rename with aliases if it doesn't support alias (e.g. asterisk)
372+
if (!dynamic_cast<ASTWithAlias *>(expr.get()))
373+
continue;
374+
372375
size_t i = 1;
373376
while (all_column_names.end() != all_column_names.find(name + "_" + toString(i)))
374377
++i;

tests/queries/0_stateless/03443_alias_with_asterisk.reference

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE t0 (c0 Int ALIAS if(NULL, 1, *)) ENGINE = Memory; -- { serverError UNKNOWN_IDENTIFIER }
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
SELECT
2+
`_--t1.a` AS `t1.a`,
3+
`_--t1.b` AS `t1.b`,
4+
`_--t2.a` AS `t2.a`,
5+
`_--t2.b` AS `t2.b`,
6+
`toNullable(10)` AS `t3.toNullable(10)`,
7+
`_--t3.a` AS `t3.a`,
8+
`_--t3.b` AS `t3.b`,
9+
`isZeroOrNull(10)` AS `t3.isZeroOrNull(10)`,
10+
`_--t3.10` AS `t3.10`,
11+
`assumeNotNull(materialize(10))` AS `t3.assumeNotNull(materialize(10))`,
12+
`isNotNull(10)` AS `t3.isNotNull(10)`,
13+
x AS `t3.x`
14+
FROM
15+
(
16+
SELECT
17+
b AS `_--t1.b`,
18+
a AS `_--t1.a`,
19+
t2.b AS `_--t2.b`,
20+
t2.a AS `_--t2.a`
21+
FROM t1
22+
ALL INNER JOIN
23+
(
24+
SELECT
25+
a,
26+
b
27+
FROM t2
28+
) AS t2 ON if(`_--t2.b` > 0, `_--t2.a`, 0) = `_--t1.a`
29+
WHERE if(`_--t2.b` > 0, `_--t2.a`, 0) = `_--t1.a`
30+
) AS `--.s`
31+
CROSS JOIN
32+
(
33+
SELECT
34+
x,
35+
`assumeNotNull(materialize(10))`,
36+
`isNotNull(10)`,
37+
`isZeroOrNull(10)`,
38+
`toNullable(10)`,
39+
`10` AS `_--t3.10`,
40+
b AS `_--t3.b`,
41+
a AS `_--t3.a`
42+
FROM
43+
(
44+
SELECT
45+
toNullable(10),
46+
a,
47+
b,
48+
isZeroOrNull(10),
49+
10,
50+
10 AS `10_1`,
51+
10 AS `10_2`,
52+
assumeNotNull(materialize(10)),
53+
10 IS NOT NULL,
54+
a AS x
55+
FROM t3__fuzz_0
56+
WHERE (toNullable(toUInt256(1)) + a) = b
57+
) AS t3
58+
) AS `--.t`
59+
WHERE if(`_--t2.b` > 0, `_--t2.a`, 0) = `_--t1.a`
60+
ORDER BY
61+
x ASC NULLS FIRST,
62+
`_--t2.a` DESC NULLS LAST,
63+
`_--t1.a` DESC NULLS FIRST
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Fuzzing `Can't set alias of * of Asterisk on alias`
2+
DROP TABLE IF EXISTS t1, t2, t3__fuzz_0;
3+
CREATE TABLE t1 (`a` UInt64, `b` UInt64) ENGINE = Log;
4+
CREATE TABLE t2 (`a` UInt64, `b` UInt64) ENGINE = Log;
5+
CREATE TABLE t3__fuzz_0 (`a` LowCardinality(UInt64), `b` UInt64) ENGINE = Log SETTINGS allow_suspicious_low_cardinality_types=1;
6+
EXPLAIN SYNTAX SELECT * FROM t1, t2, (SELECT toNullable(10), *, isZeroOrNull(10), 10, *, 10, *, *, 10, *, *, *, assumeNotNull(materialize(10)), 10 IS NOT NULL, a AS x FROM t3__fuzz_0 WHERE (toNullable(toUInt256(1)) + a) = b) AS t3 WHERE if(t2.b > 0, t2.a, 0) = t1.a ORDER BY t3.x ASC NULLS FIRST, t2.a DESC NULLS LAST, t1.a DESC NULLS FIRST;

0 commit comments

Comments
 (0)