Skip to content

Commit d4589a3

Browse files
committed
Unwrap wrapped values when computing hashes for keyless tables.
1 parent d8edd4c commit d4589a3

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

sql/expression/matchagainst.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (expr *MatchAgainst) inNaturalLanguageMode(ctx *sql.Context, row sql.Row) (
288288
}
289289

290290
accumulatedRelevancy := float32(0)
291-
hash, err := fulltext.HashRow(row)
291+
hash, err := fulltext.HashRow(ctx, row)
292292
if err != nil {
293293
return 0, err
294294
}

sql/fulltext/fulltext.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package fulltext
1616

1717
import (
18+
"context"
1819
"crypto/sha256"
1920
"encoding/binary"
2021
"encoding/hex"
@@ -95,14 +96,14 @@ const (
9596
)
9697

9798
// HashRow returns a 64 character lowercase hexadecimal hash of the given row. This is intended for use with keyless tables.
98-
func HashRow(row sql.Row) (string, error) {
99+
func HashRow(ctx context.Context, row sql.Row) (string, error) {
99100
h := sha256.New()
100101
// Since we can't represent a NULL value in binary, we instead append the NULL results to the end, which will
101102
// give us a unique representation for representing NULL values.
102103
valIsNull := make([]bool, len(row))
103104
for i, val := range row {
104105
var err error
105-
valIsNull[i], err = writeHashedValue(h, val)
106+
valIsNull[i], err = writeHashedValue(ctx, h, val)
106107
if err != nil {
107108
return "", err
108109
}
@@ -117,7 +118,11 @@ func HashRow(row sql.Row) (string, error) {
117118
}
118119

119120
// writeHashedValue writes the given value into the hash.
120-
func writeHashedValue(h hash.Hash, val interface{}) (valIsNull bool, err error) {
121+
func writeHashedValue(ctx context.Context, h hash.Hash, val interface{}) (valIsNull bool, err error) {
122+
val, err = sql.UnwrapAny(ctx, val)
123+
if err != nil {
124+
return false, err
125+
}
121126
switch val := val.(type) {
122127
case int:
123128
if err := binary.Write(h, binary.LittleEndian, int64(val)); err != nil {

sql/fulltext/fulltext_editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (editor TableEditor) StatementComplete(ctx *sql.Context) error {
256256

257257
// Insert implements the interface sql.TableEditor.
258258
func (editor TableEditor) Insert(ctx *sql.Context, row sql.Row) error {
259-
hash, err := HashRow(row)
259+
hash, err := HashRow(ctx, row)
260260
if err != nil {
261261
return err
262262
}
@@ -382,7 +382,7 @@ func (editor TableEditor) Update(ctx *sql.Context, old sql.Row, new sql.Row) err
382382

383383
// Delete implements the interface sql.TableEditor.
384384
func (editor TableEditor) Delete(ctx *sql.Context, row sql.Row) error {
385-
hash, err := HashRow(row)
385+
hash, err := HashRow(ctx, row)
386386
if err != nil {
387387
return err
388388
}

0 commit comments

Comments
 (0)