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 (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;
25 changes: 25 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5059,6 +5059,8 @@ pub enum PublicationTarget {
pub enum PublicationObject {
/// `TABLE table [, ...]`
Tables(Vec<PublicationTable>),
/// `TABLES IN SCHEMA { schema_name | CURRENT_SCHEMA } [, ...]`
Schemas(Vec<PublicationSchema>),
}

impl fmt::Display for PublicationObject {
Expand All @@ -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"),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 23 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9128,10 +9128,18 @@ impl<'a> Parser<'a> {
fn parse_publication_objects(&mut self) -> Result<Vec<PublicationObject>, 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);
}
Expand All @@ -9146,7 +9154,9 @@ impl<'a> Parser<'a> {
) -> Result<Vec<Member>, 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;
}
Expand All @@ -9155,6 +9165,14 @@ impl<'a> Parser<'a> {
Ok(members)
}

fn parse_publication_schema(&mut self) -> Result<PublicationSchema, ParserError> {
if self.parse_keyword(Keyword::CURRENT_SCHEMA) {
Ok(PublicationSchema::CurrentSchema)
} else {
Ok(PublicationSchema::Name(self.parse_identifier()?))
}
}

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)?;
Expand Down
56 changes: 54 additions & 2 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()",
Expand Down Expand Up @@ -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: ,",
Expand Down Expand Up @@ -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}"
);
}
}
Loading