Support array indexing for duckdb (#1265)

This commit is contained in:
Jichao Sun 2024-05-09 03:50:15 -07:00 committed by GitHub
parent eb36bd7138
commit e3692f4681
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View file

@ -2547,7 +2547,7 @@ impl<'a> Parser<'a> {
expr: Box::new(expr),
})
} else if Token::LBracket == tok {
if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
if dialect_of!(self is PostgreSqlDialect | DuckDbDialect | GenericDialect) {
// parse index
self.parse_array_index(expr)
} else if dialect_of!(self is SnowflakeDialect) {

View file

@ -516,3 +516,29 @@ fn test_duckdb_named_argument_function_with_assignment_operator() {
expr_from_projection(only(&select.projection))
);
}
#[test]
fn test_array_index() {
let sql = r#"SELECT ['a', 'b', 'c'][3] AS three"#;
let select = duckdb().verified_only_select(sql);
let projection = &select.projection;
assert_eq!(1, projection.len());
let expr = match &projection[0] {
SelectItem::ExprWithAlias { expr, .. } => expr,
_ => panic!("Expected an expression with alias"),
};
assert_eq!(
&Expr::ArrayIndex {
obj: Box::new(Expr::Array(Array {
elem: vec![
Expr::Value(Value::SingleQuotedString("a".to_owned())),
Expr::Value(Value::SingleQuotedString("b".to_owned())),
Expr::Value(Value::SingleQuotedString("c".to_owned()))
],
named: false
})),
indexes: vec![Expr::Value(number("3"))]
},
expr
);
}