|
11 | 11 | #include <Parsers/ASTSubquery.h> |
12 | 12 | #include <Parsers/ASTTablesInSelectQuery.h> |
13 | 13 | #include <Parsers/ASTWithElement.h> |
| 14 | +#include <Parsers/ASTLiteral.h> |
14 | 15 | #include <Common/checkStackSize.h> |
15 | 16 |
|
16 | 17 |
|
@@ -62,11 +63,16 @@ void ApplyWithSubqueryVisitor::visit(ASTSelectQuery & ast, const Data & data) |
62 | 63 | for (auto & child : with->children) |
63 | 64 | { |
64 | 65 | visit(child, new_data ? *new_data : data); |
65 | | - if (auto * ast_with_elem = child->as<ASTWithElement>()) |
| 66 | + auto * ast_with_elem = child->as<ASTWithElement>(); |
| 67 | + auto * ast_literal = child->as<ASTLiteral>(); |
| 68 | + if (ast_with_elem || ast_literal) |
66 | 69 | { |
67 | 70 | if (!new_data) |
68 | 71 | new_data = data; |
69 | | - new_data->subqueries[ast_with_elem->name] = ast_with_elem->subquery; |
| 72 | + if (ast_with_elem) |
| 73 | + new_data->subqueries[ast_with_elem->name] = ast_with_elem->subquery; |
| 74 | + else |
| 75 | + new_data->literals[ast_literal->alias] = child; |
70 | 76 | } |
71 | 77 | } |
72 | 78 | } |
@@ -120,15 +126,27 @@ void ApplyWithSubqueryVisitor::visit(ASTFunction & func, const Data & data) |
120 | 126 | { |
121 | 127 | /// Clang-tidy is wrong on this line, because `func.arguments->children.at(1)` gets replaced before last use of `name`. |
122 | 128 | auto name = identifier->shortName(); // NOLINT |
| 129 | + |
123 | 130 | auto subquery_it = data.subqueries.find(name); |
124 | 131 | if (subquery_it != data.subqueries.end()) |
125 | 132 | { |
126 | 133 | auto old_alias = func.arguments->children[1]->tryGetAlias(); |
127 | 134 | func.arguments->children[1] = subquery_it->second->clone(); |
128 | | - func.arguments->children[1]->as<ASTSubquery &>().cte_name = name; |
| 135 | + func.arguments->children[1]->as<ASTSubquery>()->cte_name = name; |
129 | 136 | if (!old_alias.empty()) |
130 | 137 | func.arguments->children[1]->setAlias(old_alias); |
131 | 138 | } |
| 139 | + else |
| 140 | + { |
| 141 | + auto literal_it = data.literals.find(name); |
| 142 | + if (literal_it != data.literals.end()) |
| 143 | + { |
| 144 | + auto old_alias = func.arguments->children[1]->tryGetAlias(); |
| 145 | + func.arguments->children[1] = literal_it->second->clone(); |
| 146 | + if (!old_alias.empty()) |
| 147 | + func.arguments->children[1]->setAlias(old_alias); |
| 148 | + } |
| 149 | + } |
132 | 150 | } |
133 | 151 | } |
134 | 152 | } |
|
0 commit comments