Skip to content

Commit

Permalink
expr.Coerce(): Deref union values
Browse files Browse the repository at this point in the history
Closes #4955
  • Loading branch information
mattnibs committed Jan 18, 2024
1 parent 3aab9fd commit bd6d1cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions runtime/expr/coerce/coerce.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (c *Pair) Equal() bool {
}

func (c *Pair) Coerce(a, b zed.Value) (int, error) {
a, b = a.Under(), b.Under()
c.A = a.Bytes()
c.B = b.Bytes()
aid := a.Type().ID()
Expand Down
8 changes: 8 additions & 0 deletions runtime/expr/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ func TestArithmetic(t *testing.T) {
testSuccessful(t, "f / 1.25", record, zed.NewFloat64(2.0))
testSuccessful(t, "5.0 / f", record, zed.NewFloat64(2.0))

// Union values are dereferenced when doing arithmetic on them.
val := "10((int64,string))"
testSuccessful(t, "this + 1", val, zed.NewInt64(11))
testSuccessful(t, "this - 1", val, zed.NewInt64(9))
testSuccessful(t, "this * 2", val, zed.NewInt64(20))
testSuccessful(t, "this / 2", val, zed.NewInt64(5))
testSuccessful(t, "this % 3", val, zed.NewInt64(1))

// Difference of two times is a duration
testSuccessful(t, "a - b", "{a:2022-09-22T00:00:01Z,b:2022-09-22T00:00:00Z}",
zed.NewDuration(nano.Second))
Expand Down

0 comments on commit bd6d1cd

Please sign in to comment.