Skip to content

Commit

Permalink
refactor: API update collations Module
Browse files Browse the repository at this point in the history
Signed-off-by: Lakshya Singh <[email protected]>
  • Loading branch information
king-11 committed Nov 2, 2021
1 parent 9e83dec commit ea23efe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
9 changes: 6 additions & 3 deletions go/vt/vtgate/engine/ordered_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,12 +1073,13 @@ func TestOrderedAggregateCollate(t *testing.T) {
)},
}

collationID, _ := collations.IDFromName("utf8mb4_0900_ai_ci")
oa := &OrderedAggregate{
Aggregates: []*AggregateParams{{
Opcode: AggregateCount,
Col: 1,
}},
GroupByKeys: []*GroupByParams{{KeyCol: 0, CollationID: collations.LookupIDByName("utf8mb4_0900_ai_ci")}},
GroupByKeys: []*GroupByParams{{KeyCol: 0, CollationID: collationID}},
Input: fp,
}

Expand Down Expand Up @@ -1114,12 +1115,13 @@ func TestOrderedAggregateCollateAS(t *testing.T) {
)},
}

collationID, _ := collations.IDFromName("utf8mb4_0900_as_ci")
oa := &OrderedAggregate{
Aggregates: []*AggregateParams{{
Opcode: AggregateCount,
Col: 1,
}},
GroupByKeys: []*GroupByParams{{KeyCol: 0, CollationID: collations.LookupIDByName("utf8mb4_0900_as_ci")}},
GroupByKeys: []*GroupByParams{{KeyCol: 0, CollationID: collationID}},
Input: fp,
}

Expand Down Expand Up @@ -1157,12 +1159,13 @@ func TestOrderedAggregateCollateKS(t *testing.T) {
)},
}

collationID, _ := collations.IDFromName("utf8mb4_ja_0900_as_cs_ks")
oa := &OrderedAggregate{
Aggregates: []*AggregateParams{{
Opcode: AggregateCount,
Col: 1,
}},
GroupByKeys: []*GroupByParams{{KeyCol: 0, CollationID: collations.LookupIDByName("utf8mb4_ja_0900_as_cs_ks")}},
GroupByKeys: []*GroupByParams{{KeyCol: 0, CollationID: collationID}},
Input: fp,
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/evalengine/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func NullsafeCompare(v1, v2 sqltypes.Value, collationID collations.ID) (int, err
return bytes.Compare(v1.ToBytes(), v2.ToBytes()), nil
}
if v1.IsText() && v2.IsText() && collationID != collations.Unknown {
collation := collations.LookupById(collationID)
collation := collations.FromID(collationID)
if collation == nil {
return 0, UnsupportedCollationError{
ID: collationID,
Expand Down
41 changes: 23 additions & 18 deletions go/vt/vtgate/evalengine/arithmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ func TestNullsafeCompare(t *testing.T) {
}
}

func getCollationID(collation string) collations.ID {
id, _ := collations.IDFromName(collation)
return id
}

func TestNullsafeCompareCollate(t *testing.T) {
tcases := []struct {
v1, v2 string
Expand All @@ -611,42 +616,42 @@ func TestNullsafeCompareCollate(t *testing.T) {
v1: "abCd",
v2: "aBcd",
out: 0,
collation: collations.LookupIDByName("utf8mb4_0900_as_ci"),
collation: getCollationID("utf8mb4_0900_as_ci"),
},
{
// accent sensitive
v1: "ǍḄÇ",
v2: "ÁḆĈ",
out: 1,
collation: collations.LookupIDByName("utf8mb4_0900_as_ci"),
collation: getCollationID("utf8mb4_0900_as_ci"),
},
{
// hangul decomposition
v1: "\uAC00",
v2: "\u326E",
out: 0,
collation: collations.LookupIDByName("utf8mb4_0900_as_ci"),
collation: getCollationID("utf8mb4_0900_as_ci"),
},
{
// kana sensitive
v1: "\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94",
v2: "\xE3\x83\x8B\xE3\x83\x9B\xE3\x83\xB3\xE3\x82\xB4",
out: -1,
collation: collations.LookupIDByName("utf8mb4_ja_0900_as_cs_ks"),
collation: getCollationID("utf8mb4_ja_0900_as_cs_ks"),
},
{
// non breaking space
v1: "abc ",
v2: "abc\u00a0",
out: -1,
collation: collations.LookupIDByName("utf8mb4_0900_as_cs"),
collation: getCollationID("utf8mb4_0900_as_cs"),
},
{
// "cs" counts as a separate letter, where c < cs < d
v1: "c",
v2: "cs",
out: -1,
collation: collations.LookupIDByName("utf8mb4_hu_0900_ai_ci"),
collation: getCollationID("utf8mb4_hu_0900_ai_ci"),
},
{
v1: "abcd",
Expand All @@ -661,8 +666,8 @@ func TestNullsafeCompareCollate(t *testing.T) {
err: vterrors.New(vtrpcpb.Code_UNKNOWN, "comparison using collation 1111 isn't possible"),
},
{
v1: "abcd",
v2: "abcd",
v1: "abcd",
v2: "abcd",
// unsupported collation gb18030_bin
collation: 249,
err: vterrors.New(vtrpcpb.Code_UNKNOWN, "comparison using collation 249 isn't possible"),
Expand Down Expand Up @@ -1379,35 +1384,35 @@ func TestMinCollate(t *testing.T) {
v1: "ǍḄÇ",
v2: "ÁḆĈ",
out: "ǍḄÇ",
collation: collations.LookupIDByName("utf8mb4_0900_as_ci"),
collation: getCollationID("utf8mb4_0900_as_ci"),
},
{
// kana sensitive
v1: "\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94",
v2: "\xE3\x83\x8B\xE3\x83\x9B\xE3\x83\xB3\xE3\x82\xB4",
out: "\xE3\x83\x8B\xE3\x83\x9B\xE3\x83\xB3\xE3\x82\xB4",
collation: collations.LookupIDByName("utf8mb4_ja_0900_as_cs_ks"),
collation: getCollationID("utf8mb4_ja_0900_as_cs_ks"),
},
{
// non breaking space
v1: "abc ",
v2: "abc\u00a0",
out: "abc\u00a0",
collation: collations.LookupIDByName("utf8mb4_0900_as_cs"),
collation: getCollationID("utf8mb4_0900_as_cs"),
},
{
// "cs" counts as a separate letter, where c < cs < d
v1: "c",
v2: "cs",
out: "cs",
collation: collations.LookupIDByName("utf8mb4_hu_0900_ai_ci"),
collation: getCollationID("utf8mb4_hu_0900_ai_ci"),
},
{
// "cs" counts as a separate letter, where c < cs < d
v1: "cukor",
v2: "csak",
out: "csak",
collation: collations.LookupIDByName("utf8mb4_hu_0900_ai_ci"),
collation: getCollationID("utf8mb4_hu_0900_ai_ci"),
},
}
for _, tcase := range tcases {
Expand Down Expand Up @@ -1486,35 +1491,35 @@ func TestMaxCollate(t *testing.T) {
v1: "ǍḄÇ",
v2: "ÁḆĈ",
out: "ǍḄÇ",
collation: collations.LookupIDByName("utf8mb4_0900_as_ci"),
collation: getCollationID("utf8mb4_0900_as_ci"),
},
{
// kana sensitive
v1: "\xE3\x81\xAB\xE3\x81\xBB\xE3\x82\x93\xE3\x81\x94",
v2: "\xE3\x83\x8B\xE3\x83\x9B\xE3\x83\xB3\xE3\x82\xB4",
out: "\xE3\x83\x8B\xE3\x83\x9B\xE3\x83\xB3\xE3\x82\xB4",
collation: collations.LookupIDByName("utf8mb4_ja_0900_as_cs_ks"),
collation: getCollationID("utf8mb4_ja_0900_as_cs_ks"),
},
{
// non breaking space
v1: "abc ",
v2: "abc\u00a0",
out: "abc\u00a0",
collation: collations.LookupIDByName("utf8mb4_0900_as_cs"),
collation: getCollationID("utf8mb4_0900_as_cs"),
},
{
// "cs" counts as a separate letter, where c < cs < d
v1: "c",
v2: "cs",
out: "cs",
collation: collations.LookupIDByName("utf8mb4_hu_0900_ai_ci"),
collation: getCollationID("utf8mb4_hu_0900_ai_ci"),
},
{
// "cs" counts as a separate letter, where c < cs < d
v1: "cukor",
v2: "csak",
out: "csak",
collation: collations.LookupIDByName("utf8mb4_hu_0900_ai_ci"),
collation: getCollationID("utf8mb4_hu_0900_ai_ci"),
},
}
for _, tcase := range tcases {
Expand Down

0 comments on commit ea23efe

Please sign in to comment.