Skip to content

Commit

Permalink
Add ability to slice sets
Browse files Browse the repository at this point in the history
Closes #4938
  • Loading branch information
mattnibs committed Dec 14, 2023
1 parent 59b72d8 commit dac2b5f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions runtime/expr/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func (s *Slice) Eval(ectx Context, this *zed.Value) *zed.Value {
length = len(elem.Bytes())
case *zed.TypeOfString:
length = utf8.RuneCount(elem.Bytes())
case *zed.TypeArray:
case *zed.TypeArray, *zed.TypeSet:
n, err := elem.ContainerLength()
if err != nil {
panic(err)
}
length = n
default:
return s.zctx.WrapError("sliced value is not array, bytes, or string", elem)
return s.zctx.WrapError("sliced value is not array, set, bytes, or string", elem)
}
if elem.IsNull() {
return elem
Expand All @@ -69,7 +69,7 @@ func (s *Slice) Eval(ectx Context, this *zed.Value) *zed.Value {
case *zed.TypeOfString:
bytes = bytes[utf8PrefixLen(bytes, from):]
bytes = bytes[:utf8PrefixLen(bytes, to-from)]
case *zed.TypeArray:
case *zed.TypeArray, *zed.TypeSet:
it := bytes.Iter()
for k := 0; k < to && !it.Done(); k++ {
if k == from {
Expand All @@ -81,6 +81,9 @@ func (s *Slice) Eval(ectx Context, this *zed.Value) *zed.Value {
default:
panic(elem.Type)
}
if _, ok := elem.Type.(*zed.TypeSet); ok {
bytes = zed.NormalizeSet(bytes)
}
return ectx.NewValue(elem.Type, bytes)
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/expr/ztests/slice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ input: |
{a:"0\u2071\u20723"}
{a:"\u2070\u2071\u2072\u2073"}
{a:[10(int32),11(int32),12(int32),13(int32)]}
{a:|[10(int32),11(int32),12(int32),13(int32)]|}
output: |
{a1:error({message:"sliced value is not array, bytes, or string",on:null}),a2:error({message:"sliced value is not array, bytes, or string",on:null}),a3:error({message:"sliced value is not array, bytes, or string",on:null}),a4:error({message:"sliced value is not array, bytes, or string",on:null}),a5:error({message:"sliced value is not array, bytes, or string",on:null}),a6:error({message:"sliced value is not array, bytes, or string",on:null}),a7:error({message:"sliced value is not array, bytes, or string",on:null}),a8:null}
Expand All @@ -27,3 +28,4 @@ output: |
{a1:"ⁱ⁲",a2:"ⁱ⁲3",a3:"0",a4:"0ⁱ⁲",a5:"",a6:"3",a7:"⁲",a8:error("slice index is not a number")}
{a1:"ⁱ⁲",a2:"ⁱ⁲⁳",a3:"⁰",a4:"⁰ⁱ⁲",a5:"",a6:"⁳",a7:"⁲",a8:error("slice index is not a number")}
{a1:[11(int32),12(int32)],a2:[11(int32),12(int32),13(int32)],a3:[10(int32)],a4:[10(int32),11(int32),12(int32)],a5:[]([int32]),a6:[13(int32)],a7:[12(int32)],a8:[10(int32),11(int32)]}
{a1:|[11(int32),12(int32)]|,a2:|[11(int32),12(int32),13(int32)]|,a3:|[10(int32)]|,a4:|[10(int32),11(int32),12(int32)]|,a5:|[]|(|[int32]|),a6:|[13(int32)]|,a7:|[12(int32)]|,a8:|[10(int32),11(int32)]|}

0 comments on commit dac2b5f

Please sign in to comment.