Skip to content

add support for postgres composite types #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2022
Merged
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
8 changes: 8 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ pub enum Expr {
operator: JsonOperator,
right: Box<Expr>,
},
/// CompositeAccess (postgres) eg: SELECT (information_schema._pg_expandarray(array['i','i'])).n
CompositeAccess {
expr: Box<Expr>,
key: Ident,
},
/// `IS NULL` operator
IsNull(Box<Expr>),
/// `IS NOT NULL` operator
Expand Down Expand Up @@ -553,6 +558,9 @@ impl fmt::Display for Expr {
} => {
write!(f, "{} {} {}", left, operator, right)
}
Expr::CompositeAccess { expr, key } => {
write!(f, "{}.{}", expr, key)
}
}
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,18 @@ impl<'a> Parser<'a> {
}
};
self.expect_token(&Token::RParen)?;
Ok(expr)
if !self.consume_token(&Token::Period) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking if the CompositeAccess can only appear after a ) -- it seems like it should be possible in other places, though i suppose without a parenthesis .<name> will appear as a compound identifier.

return Ok(expr);
}
let tok = self.next_token();
let key = match tok {
Token::Word(word) => word.to_ident(),
_ => return parser_err!(format!("Expected identifier, found: {}", tok)),
};
Ok(Expr::CompositeAccess {
expr: Box::new(expr),
key,
})
}
Token::Placeholder(_) => {
self.prev_token();
Expand Down
61 changes: 61 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,67 @@ fn test_json() {
);
}

#[test]
fn test_composite_value() {
let sql = "SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price > 9";
let select = pg().verified_only_select(sql);
assert_eq!(
SelectItem::UnnamedExpr(Expr::CompositeAccess {
key: Ident::new("name"),
expr: Box::new(Expr::Nested(Box::new(Expr::CompoundIdentifier(vec![
Ident::new("on_hand"),
Ident::new("item")
]))))
}),
select.projection[0]
);

#[cfg(feature = "bigdecimal")]
let num: Expr = Expr::Value(Value::Number(bigdecimal::BigDecimal::from(9), false));
#[cfg(not(feature = "bigdecimal"))]
let num: Expr = Expr::Value(Value::Number("9".to_string(), false));
assert_eq!(
select.selection,
Some(Expr::BinaryOp {
left: Box::new(Expr::CompositeAccess {
key: Ident::new("price"),
expr: Box::new(Expr::Nested(Box::new(Expr::CompoundIdentifier(vec![
Ident::new("on_hand"),
Ident::new("item")
]))))
}),
op: BinaryOperator::Gt,
right: Box::new(num)
})
);

let sql = "SELECT (information_schema._pg_expandarray(ARRAY['i', 'i'])).n";
let select = pg().verified_only_select(sql);
assert_eq!(
SelectItem::UnnamedExpr(Expr::CompositeAccess {
key: Ident::new("n"),
expr: Box::new(Expr::Nested(Box::new(Expr::Function(Function {
name: ObjectName(vec![
Ident::new("information_schema"),
Ident::new("_pg_expandarray")
]),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Array(
Array {
elem: vec![
Expr::Value(Value::SingleQuotedString("i".to_string())),
Expr::Value(Value::SingleQuotedString("i".to_string())),
],
named: true
}
)))],
over: None,
distinct: false,
}))))
}),
select.projection[0]
);
}

#[test]
fn parse_comments() {
match pg().verified_stmt("COMMENT ON COLUMN tab.name IS 'comment'") {
Expand Down