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
2 changes: 1 addition & 1 deletion fuzz/fuzz_seeds/publication.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CREATE PUBLICATION p FOR TABLE ONLY public.items, other *, TABLE ONLY (last) WITH (publish = 'insert');
CREATE PUBLICATION p FOR TABLE ONLY public.items (id) WHERE ((id > 0)), other *, TABLE ONLY (last) WITH (publish = 'insert');
CREATE PUBLICATION a FOR ALL TABLES WITH (publish = 'insert', publish_via_partition_root);
DROP PUBLICATION IF EXISTS p, a CASCADE;
17 changes: 14 additions & 3 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5071,10 +5071,10 @@ impl fmt::Display for PublicationObject {
}
}

/// A published table.
/// A published table with an optional column list and row filter.
///
/// ```sql
/// [ ONLY ] table_name [ * ]
/// [ ONLY ] table_name [ * ] [ ( column_name [, ...] ) ] [ WHERE ( expression ) ]
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -5085,11 +5085,22 @@ pub struct PublicationTable {
pub name: ObjectName,
/// `ONLY` or `*` marker.
pub inheritance: Option<TableInheritance>,
/// Published columns, empty when all columns are published.
pub columns: Vec<Ident>,
/// Row filter of the `WHERE ( expression )` clause.
pub filter: Option<Expr>,
}

impl fmt::Display for PublicationTable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_publication_relation(f, &self.name, self.inheritance)
fmt_publication_relation(f, &self.name, self.inheritance)?;
if !self.columns.is_empty() {
write!(f, " ({})", display_comma_separated(&self.columns))?;
}
if let Some(filter) = &self.filter {
write!(f, " WHERE ({filter})")?;
}
Ok(())
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9156,22 +9156,40 @@ impl<'a> Parser<'a> {
}

fn parse_publication_table(&mut self) -> Result<PublicationTable, ParserError> {
let (name, inheritance) = self.parse_publication_relation()?;
let columns = self.parse_parenthesized_column_list(IsOptional::Optional, false)?;
let filter = if self.parse_keyword(Keyword::WHERE) {
self.expect_token(&Token::LParen)?;
let filter = self.parse_expr()?;
self.expect_token(&Token::RParen)?;
Some(filter)
} else {
None
};
Ok(PublicationTable {
name,
inheritance,
columns,
filter,
})
}

fn parse_publication_relation(
&mut self,
) -> Result<(ObjectName, Option<TableInheritance>), ParserError> {
if self.parse_keyword(Keyword::ONLY) {
let parenthesized = self.consume_token(&Token::LParen);
let name = self.parse_object_name(false)?;
if parenthesized {
self.expect_token(&Token::RParen)?;
}
Ok(PublicationTable {
name,
inheritance: Some(TableInheritance::Only { parenthesized }),
})
Ok((name, Some(TableInheritance::Only { parenthesized })))
} else {
let name = self.parse_object_name(false)?;
let inheritance = self
.consume_token(&Token::Mul)
.then_some(TableInheritance::IncludeDescendants);
Ok(PublicationTable { name, inheritance })
Ok((name, inheritance))
}
}

Expand Down
62 changes: 62 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13623,15 +13623,21 @@ fn parse_create_publication_for_table() {
inheritance: Some(TableInheritance::Only {
parenthesized: false
}),
columns: vec![],
filter: None,
},
PublicationTable {
name: ObjectName::from(vec![Ident::new("other")]),
inheritance: None,
columns: vec![],
filter: None,
},
]),
PublicationObject::Tables(vec![PublicationTable {
name: ObjectName::from(vec![Ident::new("last")]),
inheritance: Some(TableInheritance::IncludeDescendants),
columns: vec![],
filter: None,
}]),
]))
);
Expand All @@ -13658,3 +13664,59 @@ fn parse_create_publication_for_table() {
);
}
}

#[test]
fn parse_publication_table_columns_and_filter() {
for sql in [
"CREATE PUBLICATION p FOR TABLE ONLY public.items (id) WHERE (id > 0)",
"CREATE PUBLICATION p FOR TABLE ONLY public.items (id) WHERE ((id > 0)), other (a, b)",
"CREATE PUBLICATION p FOR TABLE items WHERE (id IS NOT NULL)",
] {
pg_and_generic().verified_stmt(sql);
}

let sql = r#"CREATE PUBLICATION p FOR TABLE items ("Id", name) WHERE ((id > 0))"#;
let Statement::CreatePublication(create) = pg_and_generic().verified_stmt(sql) else {
unreachable!()
};
assert_eq!(
create.target,
Some(PublicationTarget::Objects(vec![PublicationObject::Tables(
vec![PublicationTable {
name: ObjectName::from(vec![Ident::new("items")]),
inheritance: None,
columns: vec![Ident::with_quote('"', "Id"), Ident::new("name")],
filter: Some(Expr::Nested(Box::new(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("id"))),
op: BinaryOperator::Gt,
right: Box::new(Expr::value(number("0"))),
}))),
}]
)]))
);

for (sql, expected) in [
(
"CREATE PUBLICATION p FOR TABLE t WHERE id > 0",
"Expected: (, found: id",
),
(
"CREATE PUBLICATION p FOR TABLE t (id) WHERE (id > 0) AND id < 9",
"Expected: end of statement, found: AND",
),
(
"CREATE PUBLICATION p FOR TABLE t ()",
"Expected: identifier, found: )",
),
(
"CREATE PUBLICATION p FOR TABLE t WHERE (id > 0) (id)",
"Expected: end of statement, found: (",
),
] {
assert_eq!(
pg_and_generic().parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(expected.to_string()),
"{sql}"
);
}
}
Loading