Skip to content

Commit 66038dc

Browse files
authored
use type switch instead of Fprintf for grouping key (#3296)
1 parent a28a597 commit 66038dc

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

sql/hash/hash.go

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,80 @@ func HashOf(ctx *sql.Context, sch sql.Schema, row sql.Row) (uint64, error) {
6060
return 0, fmt.Errorf("error unwrapping value: %w", err)
6161
}
6262

63-
// TODO: we may not always have the type information available, so we check schema length.
64-
// Then, defer to original behavior
65-
if i >= len(sch) || v == nil {
66-
_, err := fmt.Fprintf(hash, "%v", v)
67-
if err != nil {
63+
if v == nil {
64+
if _, err := hash.WriteString("<nil>"); err != nil {
6865
return 0, err
6966
}
7067
continue
7168
}
7269

73-
switch typ := sch[i].Type.(type) {
74-
case sql.ExtendedType:
75-
// TODO: Doltgres follows Postgres conventions which don't align with the expectations of MySQL,
76-
// so we're using the old (probably incorrect) behavior for now
77-
_, err = fmt.Fprintf(hash, "%v", v)
78-
if err != nil {
79-
return 0, err
70+
// TODO: we may not always have the type information available, so we check schema length.
71+
// Then, defer to original behavior
72+
if i < len(sch) {
73+
switch typ := sch[i].Type.(type) {
74+
case sql.ExtendedType:
75+
// TODO: Doltgres follows Postgres conventions which don't align with the expectations of MySQL,
76+
// so we're using the old (probably incorrect) behavior for now
77+
_, err := hash.WriteString(fmt.Sprintf("%v", v))
78+
if err != nil {
79+
return 0, err
80+
}
81+
continue
82+
case types.StringType:
83+
var strVal string
84+
strVal, err = types.ConvertToString(ctx, v, typ, nil)
85+
if err != nil {
86+
return 0, err
87+
}
88+
err = typ.Collation().WriteWeightString(hash, strVal)
89+
if err != nil {
90+
return 0, err
91+
}
92+
continue
8093
}
81-
case types.StringType:
82-
var strVal string
83-
strVal, err = types.ConvertToString(ctx, v, typ, nil)
84-
if err != nil {
85-
return 0, err
94+
}
95+
switch v := v.(type) {
96+
case int:
97+
_, err = hash.WriteString(strconv.FormatInt(int64(v), 10))
98+
case int8:
99+
_, err = hash.WriteString(strconv.FormatInt(int64(v), 10))
100+
case int16:
101+
_, err = hash.WriteString(strconv.FormatInt(int64(v), 10))
102+
case int32:
103+
_, err = hash.WriteString(strconv.FormatInt(int64(v), 10))
104+
case int64:
105+
_, err = hash.WriteString(strconv.FormatInt(v, 10))
106+
case uint:
107+
_, err = hash.WriteString(strconv.FormatUint(uint64(v), 10))
108+
case uint8:
109+
_, err = hash.WriteString(strconv.FormatUint(uint64(v), 10))
110+
case uint16:
111+
_, err = hash.WriteString(strconv.FormatUint(uint64(v), 10))
112+
case uint32:
113+
_, err = hash.WriteString(strconv.FormatUint(uint64(v), 10))
114+
case uint64:
115+
_, err = hash.WriteString(strconv.FormatUint(v, 10))
116+
case float32:
117+
str := strconv.FormatFloat(float64(v), 'f', -1, 32)
118+
if str == "-0" {
119+
str = "0"
86120
}
87-
err = typ.Collation().WriteWeightString(hash, strVal)
88-
if err != nil {
89-
return 0, err
121+
_, err = hash.WriteString(str)
122+
case float64:
123+
str := strconv.FormatFloat(v, 'f', -1, 64)
124+
if str == "-0" {
125+
str = "0"
90126
}
127+
_, err = hash.WriteString(str)
128+
case string:
129+
_, err = hash.WriteString(v)
130+
case []byte:
131+
_, err = hash.Write(v)
91132
default:
92-
// TODO: probably much faster to do this with a type switch
93-
_, err = fmt.Fprintf(hash, "%v", v)
94-
if err != nil {
95-
return 0, err
96-
}
133+
_, err = hash.WriteString(fmt.Sprintf("%v", v))
134+
}
135+
if err != nil {
136+
return 0, err
97137
}
98138
}
99139
return hash.Sum64(), nil

0 commit comments

Comments
 (0)