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

Fixed type hashing for Full-Text #1963

Merged
merged 1 commit into from
Aug 23, 2023
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
22 changes: 22 additions & 0 deletions enginetest/enginetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3529,6 +3529,28 @@ func TestFulltextIndexes(t *testing.T, harness Harness) {
for _, script := range queries.FulltextTests {
TestScript(t, harness, script)
}
t.Run("Type Hashing", func(t *testing.T) {
for _, script := range queries.TypeWireTests {
t.Run(script.Name, func(t *testing.T) {
e := mustNewEngine(t, harness)
defer e.Close()

for _, statement := range script.SetUpScript {
if sh, ok := harness.(SkippingHarness); ok {
if sh.SkipQueryTest(statement) {
t.Skip()
}
}
ctx := NewContext(harness).WithQuery(statement)
RunQueryWithContext(t, e, harness, ctx, statement)
}

ctx := NewContext(harness)
RunQueryWithContext(t, e, harness, ctx, "ALTER TABLE test ADD COLUMN extracol VARCHAR(200) DEFAULT '';")
RunQueryWithContext(t, e, harness, ctx, "CREATE FULLTEXT INDEX idx ON test (extracol);")
})
}
})
}

// todo(max): rewrite this using info schema and []QueryTest
Expand Down
118 changes: 75 additions & 43 deletions sql/fulltext/fulltext.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"hash"
"io"
"strings"
"time"

"github.com/shopspring/decimal"

Expand Down Expand Up @@ -99,49 +101,10 @@ func HashRow(row sql.Row) (string, error) {
// give us a unique representation for representing NULL values.
valIsNull := make([]bool, len(row))
for i, val := range row {
switch val := val.(type) {
case int:
if err := binary.Write(h, binary.LittleEndian, int64(val)); err != nil {
return "", err
}
case uint:
if err := binary.Write(h, binary.LittleEndian, uint64(val)); err != nil {
return "", err
}
case string:
if _, err := h.Write([]byte(val)); err != nil {
return "", err
}
case []byte:
if _, err := h.Write(val); err != nil {
return "", err
}
case decimal.Decimal:
bytes, err := val.GobEncode()
if err != nil {
return "", err
}
if _, err := h.Write(bytes); err != nil {
return "", err
}
case decimal.NullDecimal:
if !val.Valid {
valIsNull[i] = true
} else {
bytes, err := val.Decimal.GobEncode()
if err != nil {
return "", err
}
if _, err := h.Write(bytes); err != nil {
return "", err
}
}
case nil:
valIsNull[i] = true
default:
if err := binary.Write(h, binary.LittleEndian, val); err != nil {
return "", err
}
var err error
valIsNull[i], err = writeHashedValue(h, val)
if err != nil {
return "", err
}
}

Expand All @@ -153,6 +116,75 @@ func HashRow(row sql.Row) (string, error) {
return strings.ToLower(hex.EncodeToString(h.Sum(nil))), nil
}

// writeHashedValue writes the given value into the hash.
func writeHashedValue(h hash.Hash, val interface{}) (valIsNull bool, err error) {
switch val := val.(type) {
case int:
if err := binary.Write(h, binary.LittleEndian, int64(val)); err != nil {
return false, err
}
case uint:
if err := binary.Write(h, binary.LittleEndian, uint64(val)); err != nil {
return false, err
}
case string:
if _, err := h.Write([]byte(val)); err != nil {
return false, err
}
case []byte:
if _, err := h.Write(val); err != nil {
return false, err
}
case decimal.Decimal:
bytes, err := val.GobEncode()
if err != nil {
return false, err
}
if _, err := h.Write(bytes); err != nil {
return false, err
}
case decimal.NullDecimal:
if !val.Valid {
return true, nil
} else {
bytes, err := val.Decimal.GobEncode()
if err != nil {
return false, err
}
if _, err := h.Write(bytes); err != nil {
return false, err
}
}
case time.Time:
bytes, err := val.MarshalBinary()
if err != nil {
return false, err
}
if _, err := h.Write(bytes); err != nil {
return false, err
}
case types.GeometryValue:
if _, err := h.Write(val.Serialize()); err != nil {
return false, err
}
case types.JSONDocument:
str, err := val.ToString(sql.NewEmptyContext())
if err != nil {
return false, err
}
if _, err := h.Write([]byte(str)); err != nil {
return false, err
}
case nil:
return true, nil
default:
if err := binary.Write(h, binary.LittleEndian, val); err != nil {
return false, err
}
}
return false, nil
}

// GetKeyColumns returns the key columns from the parent table that will be used to uniquely reference any given row on
// the parent table. For many tables, this will be the primary key. For tables that do not have a valid key, the columns
// will be much more important.
Expand Down