Skip to content

Commit

Permalink
[Array] Fix ordinal boundary indexing (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaibbq authored Mar 9, 2024
1 parent 4a17cdc commit 4505d46
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func ARRAY_ORDINAL(v Value, idx int) (Value, error) {
if err != nil {
return nil, err
}
if idx < 1 || len(array.values) <= idx {
if idx < 1 || len(array.values) < idx {
return nil, fmt.Errorf("ORDINAL(%d) is out of range", idx)
}
return array.values[idx-1], nil
Expand All @@ -199,7 +199,7 @@ func ARRAY_SAFE_ORDINAL(v Value, idx int) (Value, error) {
if err != nil {
return nil, err
}
if idx < 1 || len(array.values) <= idx {
if idx < 1 || len(array.values) < idx {
return nil, nil
}
return array.values[idx-1], nil
Expand Down
6 changes: 6 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@ FROM Items`,
nil,
}},
},
{
name: "array boundary indexing test",
query: `WITH toks AS (SELECT ['one', 'two'] AS digits)
SELECT digits[SAFE_ORDINAL(2)], digits[ORDINAL(2)], digits[OFFSET(1)], digits[SAFE_OFFSET(1)] FROM toks`,
expectedRows: [][]interface{}{{"two", "two", "two", "two"}},
},
{
name: "create function",
query: `
Expand Down

0 comments on commit 4505d46

Please sign in to comment.