Skip to content

Commit e3692f4

Browse files
authored
Support array indexing for duckdb (apache#1265)
1 parent eb36bd7 commit e3692f4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2547,7 +2547,7 @@ impl<'a> Parser<'a> {
25472547
expr: Box::new(expr),
25482548
})
25492549
} else if Token::LBracket == tok {
2550-
if dialect_of!(self is PostgreSqlDialect | GenericDialect) {
2550+
if dialect_of!(self is PostgreSqlDialect | DuckDbDialect | GenericDialect) {
25512551
// parse index
25522552
self.parse_array_index(expr)
25532553
} else if dialect_of!(self is SnowflakeDialect) {

tests/sqlparser_duckdb.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,29 @@ fn test_duckdb_named_argument_function_with_assignment_operator() {
516516
expr_from_projection(only(&select.projection))
517517
);
518518
}
519+
520+
#[test]
521+
fn test_array_index() {
522+
let sql = r#"SELECT ['a', 'b', 'c'][3] AS three"#;
523+
let select = duckdb().verified_only_select(sql);
524+
let projection = &select.projection;
525+
assert_eq!(1, projection.len());
526+
let expr = match &projection[0] {
527+
SelectItem::ExprWithAlias { expr, .. } => expr,
528+
_ => panic!("Expected an expression with alias"),
529+
};
530+
assert_eq!(
531+
&Expr::ArrayIndex {
532+
obj: Box::new(Expr::Array(Array {
533+
elem: vec![
534+
Expr::Value(Value::SingleQuotedString("a".to_owned())),
535+
Expr::Value(Value::SingleQuotedString("b".to_owned())),
536+
Expr::Value(Value::SingleQuotedString("c".to_owned()))
537+
],
538+
named: false
539+
})),
540+
indexes: vec![Expr::Value(number("3"))]
541+
},
542+
expr
543+
);
544+
}

0 commit comments

Comments
 (0)