Skip to content

Commit

Permalink
expression: implement comparison between json opaque (#37316)
Browse files Browse the repository at this point in the history
close #37315
  • Loading branch information
YangKeao authored Aug 24, 2022
1 parent 64c30c0 commit cdf4bc9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
7 changes: 6 additions & 1 deletion types/json/binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,15 @@ func CompareBinary(left, right BinaryJSON) int {
}
}
case TypeCodeOpaque:
cmp = bytes.Compare(left.GetString(), right.GetString())
cmp = bytes.Compare(left.GetOpaque().Buf, right.GetOpaque().Buf)
}
} else {
cmp = precedence1 - precedence2
if cmp > 0 {
cmp = 1
} else if cmp < 0 {
cmp = -1
}
}
return cmp
}
Expand Down
54 changes: 54 additions & 0 deletions types/json/binary_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,57 @@ func BenchmarkMergeBinary(b *testing.B) {
_ = MergeBinary([]BinaryJSON{valueA, valueB})
}
}

func TestBinaryCompare(t *testing.T) {
tests := []struct {
left BinaryJSON
right BinaryJSON
result int
}{
{
CreateBinary("a"),
CreateBinary("b"),
-1,
},
{
CreateBinary(Opaque{
TypeCode: 0,
Buf: []byte{0, 1, 2, 3},
}),
CreateBinary(Opaque{
TypeCode: 0,
Buf: []byte{0, 1, 2},
}),
1,
},
{
CreateBinary(Opaque{
TypeCode: 0,
Buf: []byte{0, 1, 2, 3},
}),
CreateBinary(Opaque{
TypeCode: 0,
Buf: []byte{0, 2, 1},
}),
-1,
},
{
CreateBinary("test"),
CreateBinary(Opaque{
TypeCode: 0,
Buf: []byte{0, 2, 1},
}),
-1,
},
}

compareMsg := map[int]string{
1: "greater than",
0: "equal with",
-1: "smaller than",
}

for _, test := range tests {
require.Equal(t, test.result, CompareBinary(test.left, test.right), "%s should be %s %s", test.left.String(), compareMsg[test.result], test.right.String())
}
}

0 comments on commit cdf4bc9

Please sign in to comment.