-
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.
Update the put operator to allow assignments to elements in an array or set. Closes #4798
- Loading branch information
Showing
15 changed files
with
579 additions
and
318 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 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,41 @@ | ||
package dynfield | ||
|
||
import ( | ||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/zson" | ||
) | ||
|
||
type Path []zed.Value | ||
|
||
func (p Path) Append(b []byte) []byte { | ||
for i, v := range p { | ||
if i > 0 { | ||
b = append(b, 0) | ||
} | ||
b = append(b, v.Bytes()...) | ||
} | ||
return b | ||
} | ||
|
||
func (p Path) String() string { | ||
var b []byte | ||
for i, v := range p { | ||
if i > 0 { | ||
b = append(b, '.') | ||
} | ||
b = append(b, zson.FormatValue(&v)...) | ||
} | ||
return string(b) | ||
} | ||
|
||
type List []Path | ||
|
||
func (l List) Append(b []byte) []byte { | ||
for i, path := range l { | ||
if i > 0 { | ||
b = append(b, ',') | ||
} | ||
b = path.Append(b) | ||
} | ||
return b | ||
} |
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,97 @@ | ||
package pathbuilder | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/runtime/expr/dynfield" | ||
) | ||
|
||
type builder struct { | ||
inputCount int | ||
base Step | ||
} | ||
|
||
func New(base zed.Type, paths []dynfield.Path, leafs []zed.Value) (Step, error) { | ||
if len(paths) != len(leafs) { | ||
return nil, errors.New("paths and leafs must be the same length") | ||
} | ||
b := &builder{base: newLeafStep(base, -1)} | ||
for i, p := range paths { | ||
if err := b.Put(p, leafs[i].Type); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return b.base, nil | ||
} | ||
|
||
func (m *builder) Put(p dynfield.Path, leaf zed.Type) error { | ||
defer func() { m.inputCount++ }() | ||
return m.put(&m.base, p, leaf) | ||
} | ||
|
||
func (m *builder) put(parent *Step, p dynfield.Path, typ zed.Type) error { | ||
// Actually let's do this differently. If current is a string then we are | ||
// putting to a record. When we support maps we'll need to check for that. | ||
if p[0].IsString() { | ||
return m.putRecord(parent, p, typ) | ||
} | ||
// This could be for a map or a set but keep it simple for now. | ||
if zed.IsInteger(p[0].Type.ID()) { | ||
return m.putVector(parent, p, typ) | ||
} | ||
// if zed.TypeUnder(parent.typeof()) | ||
return errors.New("unsupported types") | ||
} | ||
|
||
func (m *builder) putRecord(s *Step, p dynfield.Path, typ zed.Type) error { | ||
current, p := p[0], p[1:] | ||
rstep, ok := (*s).(*recordStep) | ||
if !ok { | ||
// If this is a leafStep with a type of record than we need to | ||
// initialize a recordStep with fields, otherwise just replace this will | ||
// a recordStep. | ||
var fields []zed.Field | ||
if lstep, ok := (*s).(*leafStep); ok && zed.TypeRecordOf(lstep.typ) != nil { | ||
fields = zed.TypeRecordOf(lstep.typ).Fields | ||
} | ||
rstep = newRecordStep(fields) | ||
if *s == m.base { | ||
rstep.isBase = true | ||
} | ||
*s = rstep | ||
} | ||
i := rstep.lookup(current.AsString()) | ||
field := &rstep.fields[i] | ||
if len(p) == 0 { | ||
field.step = newLeafStep(typ, m.inputCount) | ||
return nil | ||
} | ||
return m.put(&field.step, p, typ) | ||
} | ||
|
||
func (m *builder) putVector(s *Step, p dynfield.Path, typ zed.Type) error { | ||
current, p := p[0], p[1:] | ||
vstep, ok := (*s).(*vectorStep) | ||
if !ok { | ||
// If this is a leafStep with a type of array than we need to | ||
// initialize a arrayStep with fields, otherwise just replace this with | ||
// an arrayStep. | ||
vstep = &vectorStep{} | ||
if lstep, ok := (*s).(*leafStep); ok && zed.InnerType(lstep.typ) != nil { | ||
vstep.inner = zed.InnerType(lstep.typ) | ||
_, vstep.isSet = zed.TypeUnder(lstep.typ).(*zed.TypeSet) | ||
} | ||
if *s == m.base { | ||
vstep.isBase = true | ||
} | ||
*s = vstep | ||
} | ||
at := vstep.lookup(int(current.AsInt())) | ||
elem := &vstep.elems[at] | ||
if len(p) == 0 { | ||
elem.step = newLeafStep(typ, m.inputCount) | ||
return nil | ||
} | ||
return m.put(&elem.step, p, typ) | ||
} |
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,108 @@ | ||
package pathbuilder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/runtime/expr/dynfield" | ||
"github.com/brimdata/zed/zcode" | ||
"github.com/brimdata/zed/zson" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func parsePath(zctx *zed.Context, ss ...string) dynfield.Path { | ||
var path dynfield.Path | ||
for _, s := range ss { | ||
path = append(path, *zson.MustParseValue(zctx, s)) | ||
} | ||
return path | ||
} | ||
|
||
type testCase struct { | ||
describe string | ||
base string | ||
paths [][]string | ||
values []string | ||
expected string | ||
} | ||
|
||
func runTestCase(t *testing.T, c testCase) { | ||
zctx := zed.NewContext() | ||
var baseTyp zed.Type | ||
var baseBytes []byte | ||
if c.base != "" { | ||
base := zson.MustParseValue(zctx, c.base) | ||
baseTyp, baseBytes = base.Type, base.Bytes() | ||
} | ||
var paths []dynfield.Path | ||
for _, ss := range c.paths { | ||
paths = append(paths, parsePath(zctx, ss...)) | ||
} | ||
var values []zed.Value | ||
for _, s := range c.values { | ||
values = append(values, *zson.MustParseValue(zctx, s)) | ||
} | ||
step, err := New(baseTyp, paths, values) | ||
require.NoError(t, err) | ||
var b zcode.Builder | ||
typ, err := step.Build(zctx, &b, baseBytes, values) | ||
require.NoError(t, err) | ||
val := zed.NewValue(typ, b.Bytes()) | ||
require.Equal(t, c.expected, zson.FormatValue(val)) | ||
} | ||
|
||
func TestIt(t *testing.T) { | ||
runTestCase(t, testCase{ | ||
base: `{"a": 1, "b": 2}`, | ||
paths: [][]string{ | ||
{`"c"`, `"a"`, `"a"`}, | ||
{`"c"`, `"b"`}, | ||
{`"c"`, `"c"`}, | ||
}, | ||
values: []string{ | ||
`45`, | ||
`"string"`, | ||
"127.0.0.1", | ||
}, | ||
expected: `{a:1,b:2,c:{a:{a:45},b:"string",c:127.0.0.1}}`, | ||
}) | ||
runTestCase(t, testCase{ | ||
base: `{"a": [1,{foo:"bar"}]}`, | ||
paths: [][]string{ | ||
{`"a"`, `0`}, | ||
{`"a"`, `1`, `"foo"`}, | ||
}, | ||
values: []string{ | ||
`"hi"`, | ||
`"baz"`, | ||
}, | ||
expected: `{a:["hi",{foo:"baz"}]}`, | ||
}) | ||
runTestCase(t, testCase{ | ||
describe: "create from empty base", | ||
paths: [][]string{ | ||
{`"a"`}, | ||
{`"b"`}, | ||
}, | ||
values: []string{ | ||
`"foo"`, | ||
`"bar"`, | ||
}, | ||
expected: `{a:"foo",b:"bar"}`, | ||
}) | ||
runTestCase(t, testCase{ | ||
describe: "assign to base level array", | ||
base: `["a", "b", "c"]`, | ||
paths: [][]string{ | ||
{`0`}, | ||
{`1`}, | ||
{`2`}, | ||
}, | ||
values: []string{ | ||
`"foo"`, | ||
`"bar"`, | ||
`"baz"`, | ||
}, | ||
expected: `["foo","bar","baz"]`, | ||
}) | ||
} |
Oops, something went wrong.