Skip to content

Commit

Permalink
Allow sequence index expression on record to return null (#5590)
Browse files Browse the repository at this point in the history
In the sequence runtime, indexing a null value inside a non-null record
returns error("missing").  Return the null value instead both for
consistency with the vector runtime and because it's less surprising.
  • Loading branch information
nwt authored Jan 21, 2025
1 parent b50f2ba commit d99e41b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions runtime/ztests/expr/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ input: |
// record
{val:{a:"foo",b:"bar"},idx:"a"}
{val:{a:"bar",b:"baz"},idx:"b"}
{val:{a:"bar",b:null},idx:"b"}
{val:{a:"foo",b:"bar"},idx:1.}
{val:{a:"bar",b:"baz"},idx:"doesnotexist"}
Expand All @@ -42,5 +43,6 @@ output: |
error({message:"index is not an integer",on:"hi"})
"foo"
"baz"
null
error({message:"record index is not a string",on:1.})
error("missing")
8 changes: 4 additions & 4 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,15 @@ func (r Value) Walk(rv Visitor) error {
return Walk(r.Type(), r.Bytes(), rv)
}

func (r Value) nth(n int) zcode.Bytes {
func (r Value) nth(n int) (zcode.Bytes, bool) {
var zv zcode.Bytes
for i, it := 0, r.Bytes().Iter(); i <= n; i++ {
if it.Done() {
return nil
return nil, false
}
zv = it.Next()
}
return zv
return zv, true
}

func (r Value) Fields() []Field {
Expand All @@ -356,7 +356,7 @@ func (r Value) Fields() []Field {

func (v *Value) DerefByColumn(col int) *Value {
if v != nil {
if bytes := v.nth(col); bytes != nil {
if bytes, ok := v.nth(col); ok {
return NewValue(v.Fields()[col].Type, bytes).Ptr()
}
}
Expand Down

0 comments on commit d99e41b

Please sign in to comment.