expression: implement comparison between json opaque (#37316)

close pingcap/tidb#37315
This commit is contained in:
YangKeao
2022-08-23 23:08:20 -04:00
committed by GitHub
parent 64c30c0a24
commit cdf4bc9bea
2 changed files with 60 additions and 1 deletions

View File

@ -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
}

View File

@ -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())
}
}