mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 00:30:49 +00:00
Merge pull request #67587 from ClickHouse/fix-create-view-with-recursive
Fix: creation of view with recursive CTE
This commit is contained in:
commit
cbfc80a184
@ -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())
|
||||
|
@ -0,0 +1,2 @@
|
||||
5050
|
||||
8
|
43
tests/queries/0_stateless/03215_view_with_recursive.sql
Normal file
43
tests/queries/0_stateless/03215_view_with_recursive.sql
Normal 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;
|
Loading…
Reference in New Issue
Block a user