-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into vector-rename
- Loading branch information
Showing
18 changed files
with
241 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package expr | ||
|
||
import ( | ||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/vector" | ||
) | ||
|
||
// Index represents an index operator "container[index]" where container is | ||
// either an array or set (with index type integer), or a record | ||
// (with index type string), or a map (with any index type). | ||
type Index struct { | ||
zctx *zed.Context | ||
container Evaluator | ||
index Evaluator | ||
} | ||
|
||
func NewIndexExpr(zctx *zed.Context, container, index Evaluator) Evaluator { | ||
return &Index{zctx, container, index} | ||
} | ||
|
||
func (i *Index) Eval(this vector.Any) vector.Any { | ||
return vector.Apply(true, i.eval, this) | ||
} | ||
|
||
func (i *Index) eval(args ...vector.Any) vector.Any { | ||
this := args[0] | ||
container := i.container.Eval(this) | ||
index := i.index.Eval(this) | ||
switch val := vector.Under(container).(type) { | ||
case *vector.Array: | ||
return indexArrayOrSet(i.zctx, val.Offsets, val.Values, index, val.Nulls) | ||
case *vector.Set: | ||
return indexArrayOrSet(i.zctx, val.Offsets, val.Values, index, val.Nulls) | ||
case *vector.Record: | ||
return indexRecord(i.zctx, val, index) | ||
case *vector.Map: | ||
panic("vector index operations on maps not supported") | ||
default: | ||
return vector.NewMissing(i.zctx, this.Len()) | ||
} | ||
} | ||
|
||
func indexArrayOrSet(zctx *zed.Context, offsets []uint32, vals, index vector.Any, nulls *vector.Bool) vector.Any { | ||
if !zed.IsInteger(index.Type().ID()) { | ||
return vector.NewWrappedError(zctx, "index is not an integer", index) | ||
} | ||
index = promoteToSigned(index) | ||
var errs []uint32 | ||
var viewIndexes []uint32 | ||
for i, start := range offsets[:len(offsets)-1] { | ||
idx, idxNull := vector.IntValue(index, uint32(i)) | ||
if !nulls.Value(uint32(i)) && !idxNull { | ||
len := int64(offsets[i+1]) - int64(start) | ||
if idx < 0 { | ||
idx = len + idx | ||
} | ||
if idx >= 0 && idx < len { | ||
viewIndexes = append(viewIndexes, start+uint32(idx)) | ||
continue | ||
} | ||
} | ||
errs = append(errs, uint32(i)) | ||
} | ||
out := vector.Deunion(vector.NewView(viewIndexes, vals)) | ||
if len(errs) > 0 { | ||
return vector.Combine(out, errs, vector.NewMissing(zctx, uint32(len(errs)))) | ||
} | ||
return out | ||
} | ||
|
||
func indexRecord(zctx *zed.Context, record *vector.Record, index vector.Any) vector.Any { | ||
if index.Type().ID() != zed.IDString { | ||
return vector.NewWrappedError(zctx, "record index is not a string", index) | ||
} | ||
var errcnt uint32 | ||
tags := make([]uint32, record.Len()) | ||
n := len(record.Typ.Fields) | ||
viewIndexes := make([][]uint32, n) | ||
for i := uint32(0); i < record.Len(); i++ { | ||
field, _ := vector.StringValue(index, i) | ||
k, ok := record.Typ.IndexOfField(field) | ||
if !ok { | ||
tags[i] = uint32(n) | ||
errcnt++ | ||
continue | ||
} | ||
tags[i] = uint32(k) | ||
viewIndexes[k] = append(viewIndexes[k], i) | ||
} | ||
out := make([]vector.Any, n+1) | ||
out[n] = vector.NewMissing(zctx, errcnt) | ||
for i, field := range record.Fields { | ||
out[i] = vector.NewView(viewIndexes[i], field) | ||
} | ||
return vector.NewVariant(tags, out) | ||
} |
2 changes: 2 additions & 0 deletions
2
...sam/expr/ztests/index-deunion-vector.yaml → ...ime/ztests/expr/index-deunion-vector.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
zed: val[idx] | ||
|
||
vector: true | ||
|
||
input: | | ||
// array | ||
{val:[1,2,3,"foo"],idx:-1} | ||
{val:[1,2,3,"bar"],idx:1(uint8)} | ||
{val:[1,2,3,"foo"],idx:-4} | ||
{val:[1,2,3,"foo"],idx:-5} | ||
{val:null([(int64,string)]),idx:-5} | ||
{val:[1,2,3,"foo"],idx:null(int64)} | ||
{val:[1,2,3,"foo"],idx:"hi"} | ||
// set | ||
{val:|[1,2,3,"foo"]|,idx:-1} | ||
{val:|[1,2,3,"bar"]|,idx:1} | ||
{val:|[1,2,3,"foo"]|,idx:-4} | ||
{val:|[1,2,3,"foo"]|,idx:-5} | ||
{val:|[1,2,3,"foo"]|,idx:"hi"} | ||
// record | ||
{val:{a:"foo",b:"bar"},idx:"a"} | ||
{val:{a:"bar",b:"baz"},idx:"b"} | ||
{val:{a:"foo",b:"bar"},idx:1.} | ||
{val:{a:"bar",b:"baz"},idx:"doesnotexist"} | ||
output: | | ||
"foo" | ||
2 | ||
1 | ||
error("missing") | ||
error("missing") | ||
error("missing") | ||
error({message:"index is not an integer",on:"hi"}) | ||
"foo" | ||
2 | ||
1 | ||
error("missing") | ||
error({message:"index is not an integer",on:"hi"}) | ||
"foo" | ||
"baz" | ||
error({message:"record index is not a string",on:1.}) | ||
error("missing") |
2 changes: 2 additions & 0 deletions
2
...ime/sam/expr/ztests/cut-both-foo-bar.yaml → runtime/ztests/op/cut-both-foo-bar.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
zed: cut foo,bar | ||
|
||
vector: true | ||
|
||
input: | | ||
{foo:"foo1",bar:"bar1"} | ||
{foo:"foo2",bar:"bar2"} | ||
|
2 changes: 2 additions & 0 deletions
2
...ime/sam/expr/ztests/cut-foo-bar-only.yaml → runtime/ztests/op/cut-foo-bar-only.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
zed: cut foo | ||
|
||
vector: true | ||
|
||
input: | | ||
{bar:"bar1"} | ||
{bar:"bar2"} | ||
|
2 changes: 2 additions & 0 deletions
2
runtime/sam/expr/ztests/cut-foo-bar.yaml → runtime/ztests/op/cut-foo-bar.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
zed: cut foo | ||
|
||
vector: true | ||
|
||
input: | | ||
{foo:"foo1",bar:"bar1"} | ||
{foo:"foo2",bar:"bar2"} | ||
|
2 changes: 2 additions & 0 deletions
2
runtime/sam/expr/ztests/cut-foo-mixed.yaml → runtime/ztests/op/cut-foo-mixed.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
zed: cut foo | ||
|
||
vector: true | ||
|
||
input: | | ||
{bar:"bar1"} | ||
{bar:"bar2"} | ||
|
2 changes: 2 additions & 0 deletions
2
runtime/sam/expr/ztests/cut-foo.yaml → runtime/ztests/op/cut-foo.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
zed: cut foo | ||
|
||
vector: true | ||
|
||
input: &input | | ||
{foo:"foo1"} | ||
{foo:"foo2"} | ||
|
2 changes: 2 additions & 0 deletions
2
...me/sam/expr/ztests/cut-to-root-error.yaml → runtime/ztests/op/cut-to-root-error.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
zed: 'cut this:=a' | ||
|
||
vector: true | ||
|
||
input: | | ||
{a:1(int32)} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.