Support create index with clause (#1389)

Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
张林伟 2024-09-01 19:38:20 +08:00 committed by GitHub
parent d64beea4d4
commit 4d52ee7280
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 92 additions and 0 deletions

View file

@ -45,6 +45,8 @@ pub struct CreateIndex {
pub if_not_exists: bool,
pub include: Vec<Ident>,
pub nulls_distinct: Option<bool>,
/// WITH clause: <https://www.postgresql.org/docs/current/sql-createindex.html>
pub with: Vec<Expr>,
pub predicate: Option<Expr>,
}
@ -83,6 +85,9 @@ impl Display for CreateIndex {
write!(f, " NULLS NOT DISTINCT")?;
}
}
if !self.with.is_empty() {
write!(f, " WITH ({})", display_comma_separated(&self.with))?;
}
if let Some(predicate) = &self.predicate {
write!(f, " WHERE {predicate}")?;
}

View file

@ -86,4 +86,8 @@ impl Dialect for GenericDialect {
fn allow_extract_single_quotes(&self) -> bool {
true
}
fn supports_create_index_with_clause(&self) -> bool {
true
}
}

View file

@ -509,6 +509,12 @@ pub trait Dialect: Debug + Any {
fn allow_extract_single_quotes(&self) -> bool {
false
}
/// Does the dialect support with clause in create index statement?
/// e.g. `CREATE INDEX idx ON t WITH (key = value, key2)`
fn supports_create_index_with_clause(&self) -> bool {
false
}
}
/// This represents the operators for which precedence must be defined

View file

@ -162,6 +162,10 @@ impl Dialect for PostgreSqlDialect {
fn allow_extract_single_quotes(&self) -> bool {
true
}
fn supports_create_index_with_clause(&self) -> bool {
true
}
}
pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {

View file

@ -5324,6 +5324,17 @@ impl<'a> Parser<'a> {
None
};
let with = if self.dialect.supports_create_index_with_clause()
&& self.parse_keyword(Keyword::WITH)
{
self.expect_token(&Token::LParen)?;
let with_params = self.parse_comma_separated(Parser::parse_expr)?;
self.expect_token(&Token::RParen)?;
with_params
} else {
Vec::new()
};
let predicate = if self.parse_keyword(Keyword::WHERE) {
Some(self.parse_expr()?)
} else {
@ -5340,6 +5351,7 @@ impl<'a> Parser<'a> {
if_not_exists,
include,
nulls_distinct,
with,
predicate,
}))
}