|
| 1 | +// Copyright 2025 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package hash |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "sync" |
| 20 | + |
| 21 | + "github.com/cespare/xxhash/v2" |
| 22 | + |
| 23 | + "github.com/dolthub/go-mysql-server/sql" |
| 24 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 25 | +) |
| 26 | + |
| 27 | +var digestPool = sync.Pool{ |
| 28 | + New: func() any { |
| 29 | + return xxhash.New() |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +// HashOf returns a hash of the given value to be used as key in a cache. |
| 34 | +func HashOf(ctx *sql.Context, sch sql.Schema, row sql.Row) (uint64, error) { |
| 35 | + hash := digestPool.Get().(*xxhash.Digest) |
| 36 | + hash.Reset() |
| 37 | + defer digestPool.Put(hash) |
| 38 | + for i, v := range row { |
| 39 | + if i > 0 { |
| 40 | + // separate each value in the row with a nil byte |
| 41 | + if _, err := hash.Write([]byte{0}); err != nil { |
| 42 | + return 0, err |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + v, err := sql.UnwrapAny(ctx, v) |
| 47 | + if err != nil { |
| 48 | + return 0, fmt.Errorf("error unwrapping value: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + // TODO: we may not always have the type information available, so we check schema length. |
| 52 | + // Then, defer to original behavior |
| 53 | + if i >= len(sch) || v == nil { |
| 54 | + _, err := fmt.Fprintf(hash, "%v", v) |
| 55 | + if err != nil { |
| 56 | + return 0, err |
| 57 | + } |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + switch typ := sch[i].Type.(type) { |
| 62 | + case types.ExtendedType: |
| 63 | + // TODO: Doltgres follows Postgres conventions which don't align with the expectations of MySQL, |
| 64 | + // so we're using the old (probably incorrect) behavior for now |
| 65 | + _, err = fmt.Fprintf(hash, "%v", v) |
| 66 | + if err != nil { |
| 67 | + return 0, err |
| 68 | + } |
| 69 | + case types.StringType: |
| 70 | + var strVal string |
| 71 | + strVal, err = types.ConvertToString(ctx, v, typ, nil) |
| 72 | + if err != nil { |
| 73 | + return 0, err |
| 74 | + } |
| 75 | + err = typ.Collation().WriteWeightString(hash, strVal) |
| 76 | + if err != nil { |
| 77 | + return 0, err |
| 78 | + } |
| 79 | + default: |
| 80 | + // TODO: probably much faster to do this with a type switch |
| 81 | + _, err = fmt.Fprintf(hash, "%v", v) |
| 82 | + if err != nil { |
| 83 | + return 0, err |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return hash.Sum64(), nil |
| 88 | +} |
0 commit comments