prevent normalization of WITH RECURSIVE alias

This commit is contained in:
Yakov Olkhovskiy 2024-08-01 07:36:53 +00:00
parent f947f91b10
commit d83c0c1b3b
3 changed files with 22 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 String with_alias;
bool only_replace_current_database_function = false;
bool only_replace_in_join = false;
@ -117,6 +119,9 @@ private:
void visit(ASTSelectQuery & select, ASTPtr &) const
{
if (auto with = select.with())
with_alias = with->children[0]->as<ASTWithElement>()->name;
if (select.tables())
tryVisit<ASTTablesInSelectQuery>(select.refTables());
@ -165,6 +170,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 (identifier.name() == with_alias)
return;
auto qualified_identifier = std::make_shared<ASTTableIdentifier>(database_name, identifier.name());
if (!identifier.alias.empty())

View File

@ -0,0 +1 @@
5050

View File

@ -0,0 +1,13 @@
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;