Skip to content
Open
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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/duckdb_2807_slice_stride.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT ([1, 2, 3, 4, 5, 6, 7, 8, 9])[1: :4];
7 changes: 5 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,8 +1514,11 @@ impl fmt::Display for Subscript {
write!(f, "{upper}")?;
}
if let Some(stride) = stride {
write!(f, ":")?;
write!(f, "{stride}")?;
// Two adjacent colons would tokenize as `::`.
if upper_bound.is_none() {
write!(f, " ")?;
}
write!(f, ":{stride}")?;
}
Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4435,13 +4435,15 @@ impl<'a> Parser<'a> {
self.expect_token(&Token::Colon)?;
}

// we are now at either `]`, `<upper>(rest)]`
// we are now at either `]`, `:stride]` or `<upper>(rest)]`
let upper_bound = if self.consume_token(&Token::RBracket) {
return Ok(Subscript::Slice {
lower_bound,
upper_bound: None,
stride: None,
});
} else if self.peek_token_ref().token == Token::Colon {
None
} else {
// parse expr until we hit a colon (or any token with lower precedence)
Some(self.parse_subexpr(self.dialect.prec_value(Precedence::Colon))?)
Expand Down
4 changes: 4 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21234,6 +21234,10 @@ fn parse_array_subscript() {

dialects.verified_stmt("SELECT arr[1][2]");
dialects.verified_stmt("SELECT arr[:][:]");

dialects.verified_stmt("SELECT arr[1: :4]");
dialects.verified_stmt("SELECT arr[: :4]");
dialects.verified_stmt("SELECT arr[:2:4]");
}

#[test]
Expand Down
18 changes: 18 additions & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,3 +977,21 @@ fn display_create_macro_propagates_argument_list_errors() {
}
}
}

#[test]
fn parse_slice_with_stride_and_no_upper_bound() {
let select = duckdb().verified_only_select("SELECT ([1, 2, 3, 4, 5, 6, 7, 8, 9])[1: :4]");
let Expr::CompoundFieldAccess { access_chain, .. } =
expr_from_projection(&select.projection[0])
else {
panic!("expected a compound field access");
};
assert_eq!(
access_chain,
&[AccessExpr::Subscript(Subscript::Slice {
lower_bound: Some(Expr::value(number("1"))),
upper_bound: None,
stride: Some(Expr::value(number("4"))),
})]
);
}
Loading