diff --git a/src/Parsers/ParserSelectQuery.cpp b/src/Parsers/ParserSelectQuery.cpp index f72259aad67..9fdf39ef50b 100644 --- a/src/Parsers/ParserSelectQuery.cpp +++ b/src/Parsers/ParserSelectQuery.cpp @@ -106,7 +106,7 @@ bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) { select_query->recursive_with = s_recursive.ignore(pos, expected); - if (!ParserList(std::make_unique(), std::make_unique(TokenType::Comma)) + if (!ParserList(std::make_unique(select_query->recursive_with), std::make_unique(TokenType::Comma)) .parse(pos, with_expression_list, expected)) return false; if (with_expression_list->children.empty()) diff --git a/src/Parsers/ParserWithElement.cpp b/src/Parsers/ParserWithElement.cpp index 5b31c929685..95eda721ef8 100644 --- a/src/Parsers/ParserWithElement.cpp +++ b/src/Parsers/ParserWithElement.cpp @@ -26,6 +26,10 @@ bool ParserWithElement::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) with_element->children.push_back(with_element->subquery); node = with_element; } + else if (with_recursive) + { + return false; + } else { pos = old_pos; diff --git a/src/Parsers/ParserWithElement.h b/src/Parsers/ParserWithElement.h index 75ad11f5deb..fe3628193b9 100644 --- a/src/Parsers/ParserWithElement.h +++ b/src/Parsers/ParserWithElement.h @@ -10,9 +10,14 @@ namespace DB */ class ParserWithElement : public IParserBase { +public: + explicit ParserWithElement(bool with_recursive_ = false) : with_recursive(with_recursive_) {} protected: const char * getName() const override { return "WITH element"; } bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; + + /// for `WITH RECURSIVE` CTE, we don't allow expression with alias. + bool with_recursive; }; } diff --git a/tests/queries/0_stateless/03237_create_table_select_as_with_recursive.reference b/tests/queries/0_stateless/03237_create_table_select_as_with_recursive.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/03237_create_table_select_as_with_recursive.sql b/tests/queries/0_stateless/03237_create_table_select_as_with_recursive.sql new file mode 100644 index 00000000000..1d7864f90ac --- /dev/null +++ b/tests/queries/0_stateless/03237_create_table_select_as_with_recursive.sql @@ -0,0 +1,2 @@ +drop table if exists t; +create view t AS (WITH RECURSIVE 42 as ttt SELECT ttt) -- { clientError SYNTAX_ERROR };