Merge pull request #67587 from ClickHouse/fix-create-view-with-recursive

Fix: creation of view with recursive CTE
This commit is contained in:
Yakov Olkhovskiy 2024-08-08 12:16:48 +00:00 committed by GitHub
commit cbfc80a184
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <Common/typeid_cast.h>
#include <Parsers/ASTWithElement.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTQueryWithTableAndOutput.h>
#include <Parsers/ASTRenameQuery.h>
@ -100,6 +101,7 @@ private:
const String database_name;
std::set<String> external_tables;
mutable std::unordered_set<String> with_aliases;
bool only_replace_current_database_function = false;
bool only_replace_in_join = false;
@ -117,6 +119,10 @@ private:
void visit(ASTSelectQuery & select, ASTPtr &) const
{
if (select.recursive_with)
for (const auto & child : select.with()->children)
with_aliases.insert(child->as<ASTWithElement>()->name);
if (select.tables())
tryVisit<ASTTablesInSelectQuery>(select.refTables());
@ -165,6 +171,9 @@ private:
/// There is temporary table with such name, should not be rewritten.
if (external_tables.contains(identifier.shortName()))
return;
/// This is WITH RECURSIVE alias.
if (with_aliases.contains(identifier.name()))
return;
auto qualified_identifier = std::make_shared<ASTTableIdentifier>(database_name, identifier.name());
if (!identifier.alias.empty())

View File

@ -0,0 +1,2 @@
5050
8

View File

@ -0,0 +1,43 @@
SET allow_experimental_analyzer = 1;
CREATE VIEW 03215_test_v
AS WITH RECURSIVE test_table AS
(
SELECT 1 AS number
UNION ALL
SELECT number + 1
FROM test_table
WHERE number < 100
)
SELECT sum(number)
FROM test_table;
SELECT * FROM 03215_test_v;
CREATE VIEW 03215_multi_v
AS WITH RECURSIVE
task AS
(
SELECT
number AS task_id,
number - 1 AS parent_id
FROM numbers(10)
),
rtq AS
(
SELECT
task_id,
parent_id
FROM task AS t
WHERE t.parent_id = 1
UNION ALL
SELECT
t.task_id,
t.parent_id
FROM task AS t, rtq AS r
WHERE t.parent_id = r.task_id
)
SELECT count()
FROM rtq;
SELECT * FROM 03215_multi_v;