From d99e41b481e90c67045cefc03b2df27ecf4daaa3 Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Tue, 21 Jan 2025 08:14:45 -0500 Subject: [PATCH] Allow sequence index expression on record to return null (#5590) 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. --- runtime/ztests/expr/index.yaml | 2 ++ value.go | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/runtime/ztests/expr/index.yaml b/runtime/ztests/expr/index.yaml index 9cd88402ea..e208abde18 100644 --- a/runtime/ztests/expr/index.yaml +++ b/runtime/ztests/expr/index.yaml @@ -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"} @@ -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") diff --git a/value.go b/value.go index 3cc43fd4c5..51f713e64d 100644 --- a/value.go +++ b/value.go @@ -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 { @@ -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() } }