Skip to content

Commit

Permalink
coerce.Pair: Deref union values (#4995)
Browse files Browse the repository at this point in the history
Closes #4955
  • Loading branch information
mattnibs authored Jan 18, 2024
1 parent 6875f96 commit 144cc28
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, "2.")
testSuccessful(t, "5.0 / f", record, "2.")

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

// Difference of two times is a duration
testSuccessful(t, "a - b", "{a:2022-09-22T00:00:01Z,b:2022-09-22T00:00:00Z}", "1s")

Expand Down

0 comments on commit 144cc28

Please sign in to comment.