diff --git a/fuzz/fuzz_seeds/publication.sql b/fuzz/fuzz_seeds/publication.sql index a593bbbab3..a23ae892a1 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 (id) WHERE ((id > 0)), other *, TABLE ONLY (last) WITH (publish = 'insert'); +CREATE PUBLICATION p FOR TABLE ONLY public.items (id) WHERE ((id > 0)), other *, TABLES IN SCHEMA feed, CURRENT_SCHEMA, 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 70a294a297..05f8fb7086 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -5059,6 +5059,8 @@ pub enum PublicationTarget { pub enum PublicationObject { /// `TABLE table [, ...]` Tables(Vec), + /// `TABLES IN SCHEMA { schema_name | CURRENT_SCHEMA } [, ...]` + Schemas(Vec), } impl fmt::Display for PublicationObject { @@ -5067,6 +5069,29 @@ impl fmt::Display for PublicationObject { PublicationObject::Tables(tables) => { write!(f, "TABLE {}", display_comma_separated(tables)) } + PublicationObject::Schemas(schemas) => { + write!(f, "TABLES IN SCHEMA {}", display_comma_separated(schemas)) + } + } + } +} + +/// A schema whose tables are published. +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] +pub enum PublicationSchema { + /// A named schema. + Name(Ident), + /// `CURRENT_SCHEMA` + CurrentSchema, +} + +impl fmt::Display for PublicationSchema { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + PublicationSchema::Name(name) => write!(f, "{name}"), + PublicationSchema::CurrentSchema => write!(f, "CURRENT_SCHEMA"), } } } diff --git a/src/ast/mod.rs b/src/ast/mod.rs index f6a1bd8f68..9ee8ded5fc 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -88,9 +88,9 @@ pub use self::ddl::{ IdentityPropertyKind, IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition, - PartitionBoundValue, ProcedureParam, PublicationObject, PublicationTable, PublicationTarget, - ReferentialAction, RenameTableNameKind, ReplicaIdentity, SqliteTableOption, SqliteTableOptions, - TagsColumnOption, TextSearchObjectType, TriggerObjectKind, Truncate, + PartitionBoundValue, ProcedureParam, PublicationObject, PublicationSchema, PublicationTable, + PublicationTarget, ReferentialAction, RenameTableNameKind, ReplicaIdentity, SqliteTableOption, + SqliteTableOptions, TagsColumnOption, TextSearchObjectType, TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, UserMappingUser, ViewColumnDef, WithData, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8acd4fd33f..6a1245f9a3 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9128,10 +9128,18 @@ impl<'a> Parser<'a> { fn parse_publication_objects(&mut self) -> Result, ParserError> { let mut objects = vec![]; loop { - self.expect_keyword_is(Keyword::TABLE)?; - objects.push(PublicationObject::Tables( - self.parse_publication_group_members(Parser::parse_publication_table)?, - )); + let object = if self.parse_keyword(Keyword::TABLE) { + PublicationObject::Tables( + self.parse_publication_group_members(Parser::parse_publication_table)?, + ) + } else if self.parse_keywords(&[Keyword::TABLES, Keyword::IN, Keyword::SCHEMA]) { + PublicationObject::Schemas( + self.parse_publication_group_members(Parser::parse_publication_schema)?, + ) + } else { + return self.expected_ref("TABLE or TABLES IN SCHEMA", self.peek_token_ref()); + }; + objects.push(object); if !self.consume_token(&Token::Comma) { return Ok(objects); } @@ -9146,7 +9154,9 @@ impl<'a> Parser<'a> { ) -> Result, ParserError> { let mut members = vec![parse_member(self)?]; while self.consume_token(&Token::Comma) { - if self.peek_keyword(Keyword::TABLE) { + if self.peek_keyword(Keyword::TABLE) + || self.peek_keywords(&[Keyword::TABLES, Keyword::IN, Keyword::SCHEMA]) + { self.prev_token(); break; } @@ -9155,6 +9165,14 @@ impl<'a> Parser<'a> { Ok(members) } + fn parse_publication_schema(&mut self) -> Result { + if self.parse_keyword(Keyword::CURRENT_SCHEMA) { + Ok(PublicationSchema::CurrentSchema) + } else { + Ok(PublicationSchema::Name(self.parse_identifier()?)) + } + } + fn parse_publication_table(&mut self) -> Result { let (name, inheritance) = self.parse_publication_relation()?; let columns = self.parse_parenthesized_column_list(IsOptional::Optional, false)?; diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 9243adc4ff..090c0a2298 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -13578,7 +13578,7 @@ fn parse_create_publication() { for (sql, expected) in [ ( "CREATE PUBLICATION p FOR ALL", - "Expected: TABLE, found: ALL", + "Expected: TABLE or TABLES IN SCHEMA, found: ALL", ), ( "CREATE PUBLICATION p WITH ()", @@ -13643,7 +13643,10 @@ fn parse_create_publication_for_table() { ); for (sql, expected) in [ - ("CREATE PUBLICATION p FOR t", "Expected: TABLE, found: t"), + ( + "CREATE PUBLICATION p FOR t", + "Expected: TABLE or TABLES IN SCHEMA, found: t", + ), ( "CREATE PUBLICATION p FOR ALL TABLES, TABLE t", "Expected: end of statement, found: ,", @@ -13720,3 +13723,52 @@ fn parse_publication_table_columns_and_filter() { ); } } + +#[test] +fn parse_publication_schemas() { + for sql in [ + "CREATE PUBLICATION p FOR TABLES IN SCHEMA feed, CURRENT_SCHEMA", + "CREATE PUBLICATION p FOR TABLE public.items, TABLES IN SCHEMA feed", + "CREATE PUBLICATION p FOR TABLES IN SCHEMA feed, TABLE a (id), b", + ] { + pg_and_generic().verified_stmt(sql); + } + + let sql = + r#"CREATE PUBLICATION p FOR TABLE a, TABLES IN SCHEMA "CURRENT_SCHEMA", CURRENT_SCHEMA"#; + 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("a")]), + inheritance: None, + columns: vec![], + filter: None, + }]), + PublicationObject::Schemas(vec![ + PublicationSchema::Name(Ident::with_quote('"', "CURRENT_SCHEMA")), + PublicationSchema::CurrentSchema, + ]), + ])) + ); + + for (sql, expected) in [ + ( + "CREATE PUBLICATION p FOR TABLES IN SCHEMA s.t", + "Expected: end of statement, found: .", + ), + ( + "CREATE PUBLICATION p FOR TABLES IN feed", + "Expected: TABLE or TABLES IN SCHEMA, found: TABLES", + ), + ] { + assert_eq!( + pg_and_generic().parse_sql_statements(sql).unwrap_err(), + ParserError::ParserError(expected.to_string()), + "{sql}" + ); + } +}