Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix collations with recursive hash for HashInTuple expressions #2651

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions enginetest/queries/information_schema_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,26 @@ from information_schema.routines where routine_schema = 'mydb' and routine_type
},
},
},
{
Name: "information_schema.columns in expression uses info schema collation",
SetUpScript: []string{
"create table TEST (COL int);",
},
Assertions: []ScriptTestAssertion{
{
Query: "select table_schema, table_name, column_name table_comment from information_schema.columns where (table_name, column_name) in (('TEST', 'COL'));",
Expected: []sql.Row{
{"mydb", "TEST", "COL"},
},
},
{
Query: "select table_schema, table_name, column_name table_comment from information_schema.columns where (table_name, column_name) in (('test', 'col'));",
Expected: []sql.Row{
{"mydb", "TEST", "COL"},
},
},
},
},
}

var SkippedInfoSchemaScripts = []ScriptTest{
Expand Down
27 changes: 25 additions & 2 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7143,7 +7143,6 @@ where
},
},
},

{
Name: "unix_timestamp script tests",
SetUpScript: []string{
Expand Down Expand Up @@ -7174,7 +7173,6 @@ where
},
},
},

{
Name: "name_const queries",
SetUpScript: []string{
Expand Down Expand Up @@ -7258,6 +7256,31 @@ where
},
},
},
{
Name: "mismatched collation using hash in tuples",
SetUpScript: []string{
"create table t (t1 text collate utf8mb4_0900_bin, t2 text collate utf8mb4_0900_ai_ci)",
"insert into t values ('ABC', 'DEF')",
},
Assertions: []ScriptTestAssertion{
{
Query: "select * from t where (t1, t2) in (('ABC', 'DEF'));",
Expected: []sql.Row{
{"ABC", "DEF"},
},
},
{
Query: "select * from t where (t1, t2) in (('ABC', 'def'));",
Expected: []sql.Row{
{"ABC", "DEF"},
},
},
{
Query: "select * from t where (t1, t2) in (('abc', 'DEF'));",
Expected: []sql.Row{},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
14 changes: 13 additions & 1 deletion sql/expression/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,19 @@ func hashOfSimple(ctx *sql.Context, i interface{}, t sql.Type) (uint64, error) {

var str string
coll := sql.Collation_Default
if types.IsTextOnly(t) {
if types.IsTuple(t) {
tup := i.([]interface{})
tupType := t.(types.TupleType)
hashes := make([]uint64, len(tup))
for idx, v := range tup {
h, err := hashOfSimple(ctx, v, tupType[idx])
if err != nil {
return 0, err
}
hashes[idx] = h
}
str = fmt.Sprintf("%v", hashes)
} else if types.IsTextOnly(t) {
coll = t.(sql.StringType).Collation()
if s, ok := i.(string); ok {
str = s
Expand Down
17 changes: 17 additions & 0 deletions sql/expression/in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,23 @@ func TestHashInTuple(t *testing.T) {
nil,
nil,
},
{
"heterogeneous collations with nested",
expression.NewTuple(
expression.NewLiteral("ABC", types.MustCreateString(sqltypes.VarChar, 20, sql.Collation_Default)),
expression.NewLiteral("def", types.MustCreateString(sqltypes.VarChar, 20, sql.Collation_utf8mb4_0900_ai_ci)),
),
expression.NewTuple(
expression.NewTuple(
expression.NewLiteral("ABC", types.MustCreateString(sqltypes.VarChar, 20, sql.Collation_Default)),
expression.NewLiteral("DEF", types.MustCreateString(sqltypes.VarChar, 20, sql.Collation_Default)),
),
),
nil,
true,
nil,
nil,
},
{
"left get field tuple is in right",
expression.NewTuple(
Expand Down