Skip to content

Commit a1c9edd

Browse files
authored
fix vam cast of union to named type (#6675)
In the vector runtime, a cast of a union to a named type discards the union. Fix that.
1 parent 9929f68 commit a1c9edd

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

runtime/vam/expr/cast.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import (
99
"github.com/brimdata/super/vector"
1010
)
1111

12-
type literalCast struct {
13-
caster Evaluator
14-
expr Evaluator
15-
}
16-
1712
func NewLiteralCast(sctx *super.Context, expr Evaluator, literal *Literal) (Evaluator, error) {
18-
var c Evaluator
1913
typeVal := literal.val
2014
switch typeVal.Type().ID() {
2115
case super.IDType:
@@ -26,48 +20,49 @@ func NewLiteralCast(sctx *super.Context, expr Evaluator, literal *Literal) (Eval
2620
if typ.ID() >= super.IDTypeComplex {
2721
return nil, fmt.Errorf("cast: casting to type %s not currently supported in vector runtime", sup.FormatType(typ))
2822
}
29-
c = &casterPrimitive{sctx, typ}
23+
return &casterPrimitive{sctx, expr, typ}, nil
3024
case super.IDString:
3125
name := super.DecodeString(typeVal.Bytes())
3226
if _, err := super.NewContext().LookupTypeNamed(name, super.TypeNull); err != nil {
3327
return nil, err
3428
}
35-
c = &casterNamedType{sctx, name}
29+
return &casterNamedType{sctx, expr, name}, nil
3630
default:
3731
return nil, fmt.Errorf("cast type argument is not a type: %s", sup.FormatValue(typeVal))
3832
}
39-
return &literalCast{c, expr}, nil
40-
}
41-
42-
func (p *literalCast) Eval(vec vector.Any) vector.Any {
43-
return vector.Apply(true, func(vecs ...vector.Any) vector.Any {
44-
return p.caster.Eval(vecs[0])
45-
}, p.expr.Eval(vec))
4633
}
4734

4835
type casterPrimitive struct {
4936
sctx *super.Context
37+
expr Evaluator
5038
typ super.Type
5139
}
5240

5341
func (c *casterPrimitive) Eval(this vector.Any) vector.Any {
54-
return cast.To(c.sctx, this, c.typ)
42+
return vector.Apply(true, func(vecs ...vector.Any) vector.Any {
43+
return cast.To(c.sctx, vecs[0], c.typ)
44+
}, c.expr.Eval(this))
5545
}
5646

5747
type casterNamedType struct {
5848
sctx *super.Context
49+
expr Evaluator
5950
name string
6051
}
6152

6253
func (c *casterNamedType) Eval(this vector.Any) vector.Any {
63-
this = vector.Under(this)
64-
typ := this.Type()
65-
if typ.Kind() == super.ErrorKind {
66-
return this
54+
return vector.Apply(false, c.eval, c.expr.Eval(this))
55+
}
56+
57+
func (c *casterNamedType) eval(vecs ...vector.Any) vector.Any {
58+
vec := vecs[0]
59+
if vec.Kind() == vector.KindError {
60+
return vec
6761
}
68-
named, err := c.sctx.LookupTypeNamed(c.name, typ)
62+
vec = vector.Under(vec)
63+
named, err := c.sctx.LookupTypeNamed(c.name, vec.Type())
6964
if err != nil {
70-
return vector.NewStringError(c.sctx, err.Error(), this.Len())
65+
return vector.NewStringError(c.sctx, err.Error(), vec.Len())
7166
}
72-
return vector.NewNamed(named, this)
67+
return vector.NewNamed(named, vec)
7368
}

runtime/ztests/expr/cast/named.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ input: |
88
"foo"
99
"bar"
1010
"baz"::=a
11+
1::(int64|null)
1112
null
1213
error("missing")
1314
error("foo")
@@ -18,6 +19,7 @@ output: |
1819
"foo"::=named
1920
"bar"::=named
2021
"baz"::=named
22+
1::(named=int64|null)
2123
null::=named
2224
error("missing")
2325
error("foo")

0 commit comments

Comments
 (0)