Skip to content

Commit 1d0c33a

Browse files
authored
types: throw error when input exceeds the range of float32 in vector (#58841)
ref #58379
1 parent 837aec4 commit 1d0c33a

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

pkg/expression/integration_test/integration_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,12 @@ func TestVectorArithmatic(t *testing.T) {
961961
tk.MustQueryToErr(`SELECT VEC_FROM_TEXT('[1]') + 2;`)
962962
tk.MustQueryToErr(`SELECT VEC_FROM_TEXT('[1]') + '2';`)
963963

964+
// Input outside the float32 value range will result in an error.
964965
tk.MustQueryToErr(`SELECT VEC_FROM_TEXT('[3e38]') + '[3e38]';`)
965966
tk.MustQuery(`SELECT VEC_FROM_TEXT('[1,2,3]') * '[4,5,6]';`).Check(testkit.Rows("[4,10,18]"))
966967
tk.MustQueryToErr(`SELECT VEC_FROM_TEXT('[1e37]') * '[1e37]';`)
968+
tk.MustQueryToErr("select VEC_L2_NORM('[1e39]') + 1")
969+
tk.MustQueryToErr("select VEC_L2_NORM('[1e39]')*0 + 1")
967970
}
968971

969972
func TestVectorFunctions(t *testing.T) {

pkg/types/vector.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ func ParseVectorFloat32(s string) (VectorFloat32, error) {
233233
valueError = errors.Errorf("infinite value not allowed in vector")
234234
return false
235235
}
236+
// Check if the value can be safely converted to float32
237+
if v < -math.MaxFloat32 || v > math.MaxFloat32 {
238+
valueError = errors.Errorf("value %v out of range for float32", v)
239+
return false
240+
}
236241
values = append(values, float32(v))
237242
return true
238243
})

pkg/types/vector_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func TestVectorParse(t *testing.T) {
101101
require.False(t, v.IsZeroValue())
102102
require.Equal(t, 1, v.Compare(types.ZeroVectorFloat32))
103103
require.Equal(t, -1, types.ZeroVectorFloat32.Compare(v))
104+
105+
v, err = types.ParseVectorFloat32(`[-1e39, 1e39]`)
106+
require.EqualError(t, err, "value -1e+39 out of range for float32")
104107
}
105108

106109
func TestVectorDatum(t *testing.T) {

0 commit comments

Comments
 (0)