expression: implement comparison between json opaque (#37316)
close pingcap/tidb#37315
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user