Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_cte_search.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WITH RECURSIVE r (id) AS (SELECT 1 UNION ALL SELECT id + 1 FROM r) SEARCH DEPTH FIRST BY id SET o SELECT id FROM r
39 changes: 20 additions & 19 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,26 @@ pub use self::dml::{
};
pub use self::operator::{BinaryOperator, UnaryOperator};
pub use self::query::{
AfterMatchSkip, ConnectByKind, Cte, CteAsMaterialized, Distinct, EmptyMatchesMode,
ExceptSelectItem, ExcludeSelectItem, ExprWithAlias, ExprWithAliasAndOrderBy, Fetch, ForClause,
ForJson, ForXml, FormatClause, GroupByExpr, GroupByWithModifier, IdentWithAlias,
IlikeSelectItem, InputFormatClause, Interpolate, InterpolateExpr, Join, JoinConstraint,
JoinOperator, JsonTableColumn, JsonTableColumnErrorHandling, JsonTableNamedColumn,
JsonTableNestedColumn, LateralView, LimitClause, LockClause, LockType, MatchRecognizePattern,
MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset,
OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr, OrderByKind, OrderByOptions,
OrderBySort, PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexDirective,
TableIndexHintForClause, TableIndexHintType, TableIndexHints, TableIndexType, TableInheritance,
TableSample, TableSampleBucket, TableSampleKind, TableSampleMethod, TableSampleModifier,
TableSampleQuantity, TableSampleSeed, TableSampleSeedModifier, TableSampleUnit, TableVersion,
TableWithJoins, Top, TopQuantity, UpdateTableFromKind, ValueTableMode, Values,
WildcardAdditionalOptions, With, WithFill, XmlNamespaceDefinition, XmlPassingArgument,
XmlPassingClause, XmlTableColumn, XmlTableColumnOption,
AfterMatchSkip, ConnectByKind, Cte, CteAsMaterialized, CteSearchClause, CteSearchOrder,
Distinct, EmptyMatchesMode, ExceptSelectItem, ExcludeSelectItem, ExprWithAlias,
ExprWithAliasAndOrderBy, Fetch, ForClause, ForJson, ForXml, FormatClause, GroupByExpr,
GroupByWithModifier, IdentWithAlias, IlikeSelectItem, InputFormatClause, Interpolate,
InterpolateExpr, Join, JoinConstraint, JoinOperator, JsonTableColumn,
JsonTableColumnErrorHandling, JsonTableNamedColumn, JsonTableNestedColumn, LateralView,
LimitClause, LockClause, LockType, MatchRecognizePattern, MatchRecognizeSymbol, Measure,
NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset, OffsetRows, OpenJsonTableColumn,
OrderBy, OrderByExpr, OrderByKind, OrderByOptions, OrderBySort, PipeOperator, PivotValueSource,
ProjectionSelect, Query, RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement,
ReplaceSelectItem, RowsPerMatch, Select, SelectFlavor, SelectInto, SelectItem,
SelectItemQualifiedWildcardKind, SelectModifiers, SetExpr, SetOperator, SetQuantifier, Setting,
SymbolDefinition, Table, TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs,
TableIndexDirective, TableIndexHintForClause, TableIndexHintType, TableIndexHints,
TableIndexType, TableInheritance, TableSample, TableSampleBucket, TableSampleKind,
TableSampleMethod, TableSampleModifier, TableSampleQuantity, TableSampleSeed,
TableSampleSeedModifier, TableSampleUnit, TableVersion, TableWithJoins, Top, TopQuantity,
UpdateTableFromKind, ValueTableMode, Values, WildcardAdditionalOptions, With, WithFill,
XmlNamespaceDefinition, XmlPassingArgument, XmlPassingClause, XmlTableColumn,
XmlTableColumnOption,
};

pub use self::trigger::{
Expand Down
56 changes: 56 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,8 @@ pub struct Cte {
pub materialized: Option<CteAsMaterialized>,
/// Token for the closing parenthesis of the CTE definition.
pub closing_paren_token: AttachedToken,
/// Optional PostgreSQL `SEARCH { BREADTH | DEPTH } FIRST BY ... SET ...` clause.
pub search: Option<CteSearchClause>,
}

impl fmt::Display for Cte {
Expand All @@ -835,13 +837,67 @@ impl fmt::Display for Cte {
f.write_str(")")?;
}
};
if let Some(search) = &self.search {
write!(f, " {search}")?;
}
if let Some(ref fr) = self.from {
write!(f, " FROM {fr}")?;
}
Ok(())
}
}

/// The `SEARCH` clause of a recursive CTE.
///
/// ```sql
/// SEARCH DEPTH FIRST BY id SET ordercol
/// ```
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-SEARCH)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct CteSearchClause {
/// Breadth-first or depth-first ordering.
pub order: CteSearchOrder,
/// The columns after `BY`.
pub columns: Vec<Ident>,
/// The sequence column after `SET`.
pub sequence_column: Ident,
}

impl fmt::Display for CteSearchClause {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"SEARCH {} BY {} SET {}",
self.order,
display_comma_separated(&self.columns),
self.sequence_column
)
}
}

/// The traversal order of a [`CteSearchClause`].
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CteSearchOrder {
/// `BREADTH FIRST`
BreadthFirst,
/// `DEPTH FIRST`
DepthFirst,
}

impl fmt::Display for CteSearchOrder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CteSearchOrder::BreadthFirst => f.write_str("BREADTH FIRST"),
CteSearchOrder::DepthFirst => f.write_str("DEPTH FIRST"),
}
}
}

/// Represents an expression behind a wildcard expansion in a projection.
/// `SELECT T.* FROM T;
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down
23 changes: 21 additions & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::{
ClusteredIndex, ColumnDef, ColumnOption, ColumnOptionDef, ConditionalStatementBlock,
ConditionalStatements, ConflictTarget, ConnectByKind, ConstraintCharacteristics, CopySource,
CreateIndex, CreateRule, CreateRuleAction, CreateTable, CreateTableOptions, CreateUserMapping,
Cte, Delete, DoOption, DoStatement, DoUpdate, DropRule, ExceptSelectItem,
Cte, CteSearchClause, Delete, DoOption, DoStatement, DoUpdate, DropRule, ExceptSelectItem,
ExcludeConstraintElement, ExcludeSelectItem, Expr, ExprWithAlias, Fetch, ForValues, FromTable,
Function, FunctionArg, FunctionArgExpr, FunctionArgumentClause, FunctionArgumentList,
FunctionArguments, GroupByExpr, HavingBound, IfStatement, IlikeSelectItem, InTarget,
Expand Down Expand Up @@ -205,13 +205,32 @@ impl Spanned for Cte {
from,
materialized: _, // enum
closing_paren_token,
search,
} = self;

union_spans(
core::iter::once(alias.span())
.chain(core::iter::once(query.span()))
.chain(from.iter().map(|item| item.span))
.chain(core::iter::once(closing_paren_token.0.span)),
.chain(core::iter::once(closing_paren_token.0.span))
.chain(search.iter().map(|item| item.span())),
)
}
}

impl Spanned for CteSearchClause {
fn span(&self) -> Span {
let CteSearchClause {
order: _, // enum
columns,
sequence_column,
} = self;

union_spans(
columns
.iter()
.map(|item| item.span)
.chain(core::iter::once(sequence_column.span)),
)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_cte_search_clause(&self) -> bool {
true
}

fn supports_select_item_multi_column_alias(&self) -> bool {
true
}
Expand Down
14 changes: 14 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,20 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `SEARCH` clause after a common
/// table expression.
///
/// ```sql
/// WITH RECURSIVE t(id) AS (SELECT 1 UNION ALL SELECT id + 1 FROM t)
/// SEARCH DEPTH FIRST BY id SET ordercol
/// SELECT * FROM t
/// ```
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-SEARCH)
fn supports_cte_search_clause(&self) -> bool {
false
}

/// Returns true if the dialect supports `CREATE GROUP`.
///
/// <https://www.postgresql.org/docs/current/sql-creategroup.html>
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ impl Dialect for PostgreSqlDialect {
true
}

fn supports_cte_search_clause(&self) -> bool {
true
}

fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
Some('"')
}
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ define_keywords!(
BOOST,
BOTH,
BOX,
BREADTH,
BRIN,
BROWSE,
BTREE,
Expand Down Expand Up @@ -336,6 +337,7 @@ define_keywords!(
DENSE_RANK,
DENY,
DEPENDS,
DEPTH,
DEREF,
DESC,
DESCRIBE,
Expand Down
27 changes: 27 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16085,6 +16085,7 @@ impl<'a> Parser<'a> {
from: None,
materialized: None,
closing_paren_token: closing_paren_token.into(),
search: self.parse_cte_search_clause()?,
};
if self.parse_keyword(Keyword::FROM) {
cte.from = Some(self.parse_identifier()?);
Expand Down Expand Up @@ -16130,13 +16131,39 @@ impl<'a> Parser<'a> {
from: None,
materialized: is_materialized,
closing_paren_token: closing_paren_token.into(),
search: self.parse_cte_search_clause()?,
};
if self.dialect.supports_from_first_insert() && self.parse_keyword(Keyword::FROM) {
cte.from = Some(self.parse_identifier()?);
}
Ok(cte)
}

/// Parse the optional `SEARCH` clause after a CTE's closing parenthesis.
fn parse_cte_search_clause(&mut self) -> Result<Option<CteSearchClause>, ParserError> {
if !self.dialect.supports_cte_search_clause() || !self.parse_keyword(Keyword::SEARCH) {
return Ok(None);
}
let order = match self.expect_one_of_keywords(&[Keyword::BREADTH, Keyword::DEPTH])? {
Keyword::BREADTH => CteSearchOrder::BreadthFirst,
Keyword::DEPTH => CteSearchOrder::DepthFirst,
unexpected_keyword => {
return Err(ParserError::ParserError(format!(
"Internal parser error: unexpected keyword `{unexpected_keyword}` in BREADTH/DEPTH"
)))
}
};
self.expect_keywords(&[Keyword::FIRST, Keyword::BY])?;
let columns = self.parse_comma_separated(|parser| parser.parse_identifier())?;
self.expect_keyword_is(Keyword::SET)?;
let sequence_column = self.parse_identifier()?;
Ok(Some(CteSearchClause {
order,
columns,
sequence_column,
}))
}

/// Parse a "query body", which is an expression with roughly the
/// following grammar:
/// ```sql
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8908,6 +8908,7 @@ fn parse_recursive_cte() {
from: None,
materialized: None,
closing_paren_token: AttachedToken::empty(),
search: None,
};
assert_eq!(with.cte_tables.first().unwrap(), &expected);
}
Expand Down
60 changes: 60 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13518,3 +13518,63 @@ fn parse_case_ending_in_end_keyword_is_not_silently_dropped() {
ParserError::ParserError("Expected: end of statement, found: END".to_string())
);
}

const RECURSIVE_GROUPS_CTE: &str = "WITH RECURSIVE r (id, parent_id) AS (SELECT id, parent_id FROM groups WHERE id = 1 UNION ALL SELECT g.id, g.parent_id FROM groups AS g JOIN r ON g.parent_id = r.id)";

/// Asserts that `RECURSIVE_GROUPS_CTE`, each set of clauses and `SELECT id FROM r` fail to parse with the paired error.
fn assert_recursive_cte_errors(dialects: &TestedDialects, cases: &[(&str, &str)]) {
for (clauses, error) in cases {
let sql = format!("{RECURSIVE_GROUPS_CTE} {clauses} SELECT id FROM r");
let actual = dialects.parse_sql_statements(&sql).unwrap_err().to_string();
assert!(
actual.starts_with(&format!("sql parser error: {error}")),
"{sql}: {actual}"
);
}
}

#[test]
fn parse_cte_search_clause() {
for clauses in [
"SEARCH BREADTH FIRST BY id SET ordercol",
"SEARCH DEPTH FIRST BY id, parent_id SET ordercol",
"SEARCH DEPTH FIRST BY id SET ordercol, s AS (SELECT 1)",
] {
pg_and_generic().verified_stmt(&format!(
"{RECURSIVE_GROUPS_CTE} {clauses} SELECT id FROM r"
));
}

let query = pg().verified_query(&format!(
"{RECURSIVE_GROUPS_CTE} SEARCH DEPTH FIRST BY id SET ordercol SELECT id FROM r"
));
assert_eq!(
query.with.unwrap().cte_tables[0].search,
Some(CteSearchClause {
order: CteSearchOrder::DepthFirst,
columns: vec![Ident::new("id")],
sequence_column: Ident::new("ordercol"),
})
);

assert_recursive_cte_errors(
&pg(),
&[
(
"SEARCH FIRST BY id SET ordercol",
"Expected: one of BREADTH or DEPTH, found: FIRST",
),
(
"SEARCH DEPTH FIRST BY id USING ordercol",
"Expected: SET, found: USING",
),
],
);
assert_recursive_cte_errors(
&all_dialects_where(|d| !d.supports_cte_search_clause()),
&[(
"SEARCH DEPTH FIRST BY id SET ordercol",
"Expected: SELECT, VALUES, or a subquery in the query body, found: SEARCH",
)],
);
}
Loading