mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-24 16:04:04 +00:00
feat: support more Postgres index syntax (#943)
This commit is contained in:
parent
a49ea1908d
commit
9a39afbe07
5 changed files with 279 additions and 10 deletions
|
@ -1348,13 +1348,17 @@ pub enum Statement {
|
||||||
/// CREATE INDEX
|
/// CREATE INDEX
|
||||||
CreateIndex {
|
CreateIndex {
|
||||||
/// index name
|
/// index name
|
||||||
name: ObjectName,
|
name: Option<ObjectName>,
|
||||||
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
|
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
|
||||||
table_name: ObjectName,
|
table_name: ObjectName,
|
||||||
using: Option<Ident>,
|
using: Option<Ident>,
|
||||||
columns: Vec<OrderByExpr>,
|
columns: Vec<OrderByExpr>,
|
||||||
unique: bool,
|
unique: bool,
|
||||||
|
concurrently: bool,
|
||||||
if_not_exists: bool,
|
if_not_exists: bool,
|
||||||
|
include: Vec<Ident>,
|
||||||
|
nulls_distinct: Option<bool>,
|
||||||
|
predicate: Option<Expr>,
|
||||||
},
|
},
|
||||||
/// CREATE ROLE
|
/// CREATE ROLE
|
||||||
/// See [postgres](https://www.postgresql.org/docs/current/sql-createrole.html)
|
/// See [postgres](https://www.postgresql.org/docs/current/sql-createrole.html)
|
||||||
|
@ -2464,20 +2468,41 @@ impl fmt::Display for Statement {
|
||||||
using,
|
using,
|
||||||
columns,
|
columns,
|
||||||
unique,
|
unique,
|
||||||
|
concurrently,
|
||||||
if_not_exists,
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct,
|
||||||
|
predicate,
|
||||||
} => {
|
} => {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}",
|
"CREATE {unique}INDEX {concurrently}{if_not_exists}",
|
||||||
unique = if *unique { "UNIQUE " } else { "" },
|
unique = if *unique { "UNIQUE " } else { "" },
|
||||||
|
concurrently = if *concurrently { "CONCURRENTLY " } else { "" },
|
||||||
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
|
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
|
||||||
name = name,
|
|
||||||
table_name = table_name
|
|
||||||
)?;
|
)?;
|
||||||
|
if let Some(value) = name {
|
||||||
|
write!(f, "{value} ")?;
|
||||||
|
}
|
||||||
|
write!(f, "ON {table_name}")?;
|
||||||
if let Some(value) = using {
|
if let Some(value) = using {
|
||||||
write!(f, " USING {value} ")?;
|
write!(f, " USING {value} ")?;
|
||||||
}
|
}
|
||||||
write!(f, "({})", display_separated(columns, ","))
|
write!(f, "({})", display_separated(columns, ","))?;
|
||||||
|
if !include.is_empty() {
|
||||||
|
write!(f, " INCLUDE ({})", display_separated(include, ","))?;
|
||||||
|
}
|
||||||
|
if let Some(value) = nulls_distinct {
|
||||||
|
if *value {
|
||||||
|
write!(f, " NULLS DISTINCT")?;
|
||||||
|
} else {
|
||||||
|
write!(f, " NULLS NOT DISTINCT")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(predicate) = predicate {
|
||||||
|
write!(f, " WHERE {predicate}")?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
Statement::CreateRole {
|
Statement::CreateRole {
|
||||||
names,
|
names,
|
||||||
|
|
|
@ -153,6 +153,7 @@ define_keywords!(
|
||||||
COMMITTED,
|
COMMITTED,
|
||||||
COMPRESSION,
|
COMPRESSION,
|
||||||
COMPUTE,
|
COMPUTE,
|
||||||
|
CONCURRENTLY,
|
||||||
CONDITION,
|
CONDITION,
|
||||||
CONFLICT,
|
CONFLICT,
|
||||||
CONNECT,
|
CONNECT,
|
||||||
|
@ -310,6 +311,7 @@ define_keywords!(
|
||||||
ILIKE,
|
ILIKE,
|
||||||
IMMUTABLE,
|
IMMUTABLE,
|
||||||
IN,
|
IN,
|
||||||
|
INCLUDE,
|
||||||
INCREMENT,
|
INCREMENT,
|
||||||
INDEX,
|
INDEX,
|
||||||
INDICATOR,
|
INDICATOR,
|
||||||
|
|
|
@ -3370,9 +3370,15 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
|
pub fn parse_create_index(&mut self, unique: bool) -> Result<Statement, ParserError> {
|
||||||
|
let concurrently = self.parse_keyword(Keyword::CONCURRENTLY);
|
||||||
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
|
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
|
||||||
|
let index_name = if if_not_exists || !self.parse_keyword(Keyword::ON) {
|
||||||
let index_name = self.parse_object_name()?;
|
let index_name = self.parse_object_name()?;
|
||||||
self.expect_keyword(Keyword::ON)?;
|
self.expect_keyword(Keyword::ON)?;
|
||||||
|
Some(index_name)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
let table_name = self.parse_object_name()?;
|
let table_name = self.parse_object_name()?;
|
||||||
let using = if self.parse_keyword(Keyword::USING) {
|
let using = if self.parse_keyword(Keyword::USING) {
|
||||||
Some(self.parse_identifier()?)
|
Some(self.parse_identifier()?)
|
||||||
|
@ -3382,13 +3388,41 @@ impl<'a> Parser<'a> {
|
||||||
self.expect_token(&Token::LParen)?;
|
self.expect_token(&Token::LParen)?;
|
||||||
let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
|
let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
|
||||||
self.expect_token(&Token::RParen)?;
|
self.expect_token(&Token::RParen)?;
|
||||||
|
|
||||||
|
let include = if self.parse_keyword(Keyword::INCLUDE) {
|
||||||
|
self.expect_token(&Token::LParen)?;
|
||||||
|
let columns = self.parse_comma_separated(Parser::parse_identifier)?;
|
||||||
|
self.expect_token(&Token::RParen)?;
|
||||||
|
columns
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
};
|
||||||
|
|
||||||
|
let nulls_distinct = if self.parse_keyword(Keyword::NULLS) {
|
||||||
|
let not = self.parse_keyword(Keyword::NOT);
|
||||||
|
self.expect_keyword(Keyword::DISTINCT)?;
|
||||||
|
Some(!not)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let predicate = if self.parse_keyword(Keyword::WHERE) {
|
||||||
|
Some(self.parse_expr()?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Statement::CreateIndex {
|
Ok(Statement::CreateIndex {
|
||||||
name: index_name,
|
name: index_name,
|
||||||
table_name,
|
table_name,
|
||||||
using,
|
using,
|
||||||
columns,
|
columns,
|
||||||
unique,
|
unique,
|
||||||
|
concurrently,
|
||||||
if_not_exists,
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct,
|
||||||
|
predicate,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5981,7 +5981,7 @@ fn parse_create_index() {
|
||||||
];
|
];
|
||||||
match verified_stmt(sql) {
|
match verified_stmt(sql) {
|
||||||
Statement::CreateIndex {
|
Statement::CreateIndex {
|
||||||
name,
|
name: Some(name),
|
||||||
table_name,
|
table_name,
|
||||||
columns,
|
columns,
|
||||||
unique,
|
unique,
|
||||||
|
@ -6015,19 +6015,25 @@ fn test_create_index_with_using_function() {
|
||||||
];
|
];
|
||||||
match verified_stmt(sql) {
|
match verified_stmt(sql) {
|
||||||
Statement::CreateIndex {
|
Statement::CreateIndex {
|
||||||
name,
|
name: Some(name),
|
||||||
table_name,
|
table_name,
|
||||||
using,
|
using,
|
||||||
columns,
|
columns,
|
||||||
unique,
|
unique,
|
||||||
|
concurrently,
|
||||||
if_not_exists,
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: None,
|
||||||
|
predicate: None,
|
||||||
} => {
|
} => {
|
||||||
assert_eq!("idx_name", name.to_string());
|
assert_eq!("idx_name", name.to_string());
|
||||||
assert_eq!("test", table_name.to_string());
|
assert_eq!("test", table_name.to_string());
|
||||||
assert_eq!("btree", using.unwrap().to_string());
|
assert_eq!("btree", using.unwrap().to_string());
|
||||||
assert_eq!(indexed_columns, columns);
|
assert_eq!(indexed_columns, columns);
|
||||||
assert!(unique);
|
assert!(unique);
|
||||||
assert!(if_not_exists)
|
assert!(!concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert!(include.is_empty());
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1761,6 +1761,208 @@ fn parse_array_index_expr() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_index() {
|
||||||
|
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2)";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name: Some(ObjectName(name)),
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
nulls_distinct: None,
|
||||||
|
include,
|
||||||
|
predicate: None,
|
||||||
|
} => {
|
||||||
|
assert_eq_vec(&["my_index"], &name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(!concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert!(include.is_empty());
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_anonymous_index() {
|
||||||
|
let sql = "CREATE INDEX ON my_table(col1,col2)";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name,
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: None,
|
||||||
|
predicate: None,
|
||||||
|
} => {
|
||||||
|
assert_eq!(None, name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(!concurrently);
|
||||||
|
assert!(!if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert!(include.is_empty());
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_index_concurrently() {
|
||||||
|
let sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS my_index ON my_table(col1,col2)";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name: Some(ObjectName(name)),
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: None,
|
||||||
|
predicate: None,
|
||||||
|
} => {
|
||||||
|
assert_eq_vec(&["my_index"], &name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert!(include.is_empty());
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_index_with_predicate() {
|
||||||
|
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) WHERE col3 IS NULL";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name: Some(ObjectName(name)),
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: None,
|
||||||
|
predicate: Some(_),
|
||||||
|
} => {
|
||||||
|
assert_eq_vec(&["my_index"], &name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(!concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert!(include.is_empty());
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_index_with_include() {
|
||||||
|
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) INCLUDE (col3)";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name: Some(ObjectName(name)),
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: None,
|
||||||
|
predicate: None,
|
||||||
|
} => {
|
||||||
|
assert_eq_vec(&["my_index"], &name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(!concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert_eq_vec(&["col3"], &include);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_index_with_nulls_distinct() {
|
||||||
|
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS NOT DISTINCT";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name: Some(ObjectName(name)),
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: Some(nulls_distinct),
|
||||||
|
predicate: None,
|
||||||
|
} => {
|
||||||
|
assert_eq_vec(&["my_index"], &name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(!concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert!(include.is_empty());
|
||||||
|
assert!(!nulls_distinct);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
|
||||||
|
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS DISTINCT";
|
||||||
|
match pg().verified_stmt(sql) {
|
||||||
|
Statement::CreateIndex {
|
||||||
|
name: Some(ObjectName(name)),
|
||||||
|
table_name: ObjectName(table_name),
|
||||||
|
using,
|
||||||
|
columns,
|
||||||
|
unique,
|
||||||
|
concurrently,
|
||||||
|
if_not_exists,
|
||||||
|
include,
|
||||||
|
nulls_distinct: Some(nulls_distinct),
|
||||||
|
predicate: None,
|
||||||
|
} => {
|
||||||
|
assert_eq_vec(&["my_index"], &name);
|
||||||
|
assert_eq_vec(&["my_table"], &table_name);
|
||||||
|
assert_eq!(None, using);
|
||||||
|
assert!(!unique);
|
||||||
|
assert!(!concurrently);
|
||||||
|
assert!(if_not_exists);
|
||||||
|
assert_eq_vec(&["col1", "col2"], &columns);
|
||||||
|
assert!(include.is_empty());
|
||||||
|
assert!(nulls_distinct);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_array_subquery_expr() {
|
fn parse_array_subquery_expr() {
|
||||||
let sql = "SELECT ARRAY(SELECT 1 UNION SELECT 2)";
|
let sql = "SELECT ARRAY(SELECT 1 UNION SELECT 2)";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue