From 4185638a30d8db46068ce74ad749b61362bdb4ae Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sun, 15 Nov 2026 11:36:08 +0100 Subject: [PATCH] PostgreSQL: Support publication column lists and row filters --- fuzz/fuzz_seeds/publication.sql | 2 +- src/ast/ddl.rs | 17 +++++++-- src/parser/mod.rs | 28 ++++++++++++--- tests/sqlparser_postgres.rs | 62 +++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 9 deletions(-) diff --git a/fuzz/fuzz_seeds/publication.sql b/fuzz/fuzz_seeds/publication.sql index a2652d7634..a593bbbab3 100644 --- a/fuzz/fuzz_seeds/publication.sql +++ b/fuzz/fuzz_seeds/publication.sql @@ -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; diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 76edee0051..70a294a297 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -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))] @@ -5085,11 +5085,22 @@ pub struct PublicationTable { pub name: ObjectName, /// `ONLY` or `*` marker. pub inheritance: Option, + /// Published columns, empty when all columns are published. + pub columns: Vec, + /// Row filter of the `WHERE ( expression )` clause. + pub filter: Option, } 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(()) } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index d68d5993b2..8acd4fd33f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9156,22 +9156,40 @@ impl<'a> Parser<'a> { } fn parse_publication_table(&mut self) -> Result { + 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), 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)) } } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 0bdc904ad2..9243adc4ff 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -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, }]), ])) ); @@ -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}" + ); + } +}