Skip to content

Commit 0eb0331

Browse files
author
James Cor
committed
feedback
1 parent c8940d7 commit 0eb0331

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

enginetest/queries/join_queries.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,20 +1162,34 @@ var JoinScriptTests = []ScriptTest{
11621162
},
11631163
},
11641164
{
1165-
// Since hash.HashOf takes in a sql.Schema to convert and hash keys,
1166-
// we need to pass in the right keys.
1167-
Name: "HashLookups regression test",
1165+
// After this change: https://github.com/dolthub/go-mysql-server/pull/3038
1166+
// hash.HashOf takes in a sql.Schema to convert and hash keys, so
1167+
// we need to pass in the schema of the join key.
1168+
// This tests a bug introduced in that same PR where we incorrectly pass in the entire schema,
1169+
// resulting in incorrect conversions.
1170+
Name: "HashLookup on multiple columns with tables with different schemas",
11681171
SetUpScript: []string{
1169-
"create table t1 (i int primary key, j varchar(1), k int);",
1170-
"create table t2 (i int primary key, k int);",
1171-
"insert into t1 values (111111, 'a', 111111);",
1172-
"insert into t2 values (111111, 111111);",
1172+
"create table t1 (i int primary key, k int);",
1173+
"create table t2 (i int primary key, j varchar(1), k int);",
1174+
"insert into t1 values (111111, 111111);",
1175+
"insert into t2 values (111111, 'a', 111111);",
1176+
1177+
"create table tt2 (i int primary key, j varchar(128) collate utf8mb4_0900_ai_ci);",
1178+
"create table tt1 (i int primary key, j varchar(128) collate utf8mb4_0900_ai_ci);",
1179+
"insert into tt1 values (1, 'ABCDE');",
1180+
"insert into tt2 values (1, 'abcde');",
11731181
},
11741182
Assertions: []ScriptTestAssertion{
11751183
{
11761184
Query: "select /*+ HASH_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.i and t1.k = t2.k;",
11771185
Expected: []sql.Row{
1178-
{111111, "a", 111111, 111111, 111111},
1186+
{111111, 111111, 111111, "a", 111111},
1187+
},
1188+
},
1189+
{
1190+
Query: "select /*+ HASH_JOIN(tt1, tt2) */ * from tt1 join tt2 on tt1.i = tt2.i and tt1.j = tt2.j;",
1191+
Expected: []sql.Row{
1192+
{1, "ABCDE", 1, "abcde"},
11791193
},
11801194
},
11811195
},

sql/hash/hash.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ var digestPool = sync.Pool{
3030
},
3131
}
3232

33+
// ExprsToSchema converts a list of sql.Expression to a sql.Schema.
34+
// This is used for functions that use HashOf, but don't already have a schema.
35+
// The generated schema ONLY contains the types of the expressions without any column names or any other info.
36+
func ExprsToSchema(exprs... sql.Expression) sql.Schema {
37+
var sch sql.Schema
38+
for _, expr := range exprs {
39+
sch = append(sch, &sql.Column{Type: expr.Type()})
40+
}
41+
return sch
42+
}
43+
3344
// HashOf returns a hash of the given value to be used as key in a cache.
3445
func HashOf(ctx *sql.Context, sch sql.Schema, row sql.Row) (uint64, error) {
3546
hash := digestPool.Get().(*xxhash.Digest)

sql/plan/hash_lookup.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import (
1919
"sync"
2020

2121
"github.com/dolthub/go-mysql-server/sql"
22-
"github.com/dolthub/go-mysql-server/sql/expression"
23-
"github.com/dolthub/go-mysql-server/sql/hash"
22+
"github.com/dolthub/go-mysql-server/sql/hash"
2423
"github.com/dolthub/go-mysql-server/sql/types"
2524
)
2625

@@ -34,12 +33,14 @@ import (
3433
// on the projected results. If cached results are not available, it
3534
// simply delegates to the child.
3635
func NewHashLookup(n sql.Node, rightEntryKey sql.Expression, leftProbeKey sql.Expression, joinType JoinType) *HashLookup {
36+
leftKeySch := hash.ExprsToSchema(leftProbeKey)
3737
return &HashLookup{
3838
UnaryNode: UnaryNode{n},
3939
RightEntryKey: rightEntryKey,
4040
LeftProbeKey: leftProbeKey,
4141
Mutex: new(sync.Mutex),
4242
JoinType: joinType,
43+
leftKeySch: leftKeySch,
4344
}
4445
}
4546

@@ -50,6 +51,7 @@ type HashLookup struct {
5051
Mutex *sync.Mutex
5152
Lookup *map[interface{}][]sql.Row
5253
JoinType JoinType
54+
leftKeySch sql.Schema
5355
}
5456

5557
var _ sql.Node = (*HashLookup)(nil)
@@ -71,6 +73,7 @@ func (n *HashLookup) WithExpressions(exprs ...sql.Expression) (sql.Node, error)
7173
ret := *n
7274
ret.RightEntryKey = exprs[0]
7375
ret.LeftProbeKey = exprs[1]
76+
ret.leftKeySch = hash.ExprsToSchema(ret.LeftProbeKey)
7477
return &ret, nil
7578
}
7679

@@ -128,13 +131,7 @@ func (n *HashLookup) GetHashKey(ctx *sql.Context, e sql.Expression, row sql.Row)
128131
return nil, err
129132
}
130133
if s, ok := key.([]interface{}); ok {
131-
var sch sql.Schema
132-
if tup, isTup := e.(*expression.Tuple); isTup {
133-
for _, expr := range tup.Children() {
134-
sch = append(sch, &sql.Column{Type: expr.Type()})
135-
}
136-
}
137-
return hash.HashOf(ctx, sch, s)
134+
return hash.HashOf(ctx, n.leftKeySch, s)
138135
}
139136
// byte slices are not hashable
140137
if k, ok := key.([]byte); ok {
@@ -146,3 +143,4 @@ func (n *HashLookup) GetHashKey(ctx *sql.Context, e sql.Expression, row sql.Row)
146143
func (n *HashLookup) Dispose() {
147144
n.Lookup = nil
148145
}
146+

0 commit comments

Comments
 (0)