Skip to content

Commit 1d56334

Browse files
author
James Cor
committed
use type switch instead of fprintf for grouping key
1 parent e8f63ac commit 1d56334

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

sql/hash/hash.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,41 @@ func HashOf(ctx *sql.Context, sch sql.Schema, row sql.Row) (uint64, error) {
7070
// TODO: we may not always have the type information available, so we check schema length.
7171
// Then, defer to original behavior
7272
if i >= len(sch) {
73-
_, err := fmt.Fprintf(hash, "%v", v)
74-
if err != nil {
75-
return 0, err
73+
switch v := v.(type) {
74+
case int:
75+
hash.WriteString(strconv.FormatInt(int64(v), 10))
76+
case int8:
77+
hash.WriteString(strconv.FormatInt(int64(v), 10))
78+
case int16:
79+
hash.WriteString(strconv.FormatInt(int64(v), 10))
80+
case int32:
81+
hash.WriteString(strconv.FormatInt(int64(v), 10))
82+
case int64:
83+
hash.WriteString(strconv.FormatInt(v, 10))
84+
case uint:
85+
hash.WriteString(strconv.FormatUint(uint64(v), 10))
86+
case uint8:
87+
hash.WriteString(strconv.FormatUint(uint64(v), 10))
88+
case uint16:
89+
hash.WriteString(strconv.FormatUint(uint64(v), 10))
90+
case uint32:
91+
hash.WriteString(strconv.FormatUint(uint64(v), 10))
92+
case uint64:
93+
hash.WriteString(strconv.FormatUint(v, 10))
94+
case float32:
95+
str := strconv.FormatFloat(float64(v), 'f', -1, 32)
96+
if str == "-0" {
97+
str = "0"
98+
}
99+
hash.WriteString(str)
100+
case float64:
101+
str := strconv.FormatFloat(v, 'f', -1, 64)
102+
if str == "-0" {
103+
str = "0"
104+
}
105+
hash.WriteString(str)
106+
default:
107+
hash.WriteString(fmt.Sprintf("%v", v))
76108
}
77109
continue
78110
}

0 commit comments

Comments
 (0)