Skip to content

Commit e020a3a

Browse files
committed
MDEV-35210 Vector type cannot store values which VEC_FromText produces and VEC_ToText accepts
let VEC_FromText validate that the vector l2squared isn't NaN. VEC_ToText still prints everything.
1 parent f336b10 commit e020a3a

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

mysql-test/main/vector_funcs.result

320 Bytes
Binary file not shown.

mysql-test/main/vector_funcs.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ drop table t1;
5858
--echo # MDEV-35212 Server crashes in Item_func_vec_fromtext::val_str upon query from empty table
5959
--echo #
6060
select vec_fromtext(NULL);
61+
62+
--echo #
63+
--echo # MDEV-35210 Vector type cannot store values which VEC_FromText produces and VEC_ToText accepts
64+
--echo #
65+
select vec_totext(0x77777777);
66+
select hex(vec_fromtext('[5.01922e33]'));

sql/item_vectorfunc.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "item_vectorfunc.h"
2424
#include "vector_mhnsw.h"
25+
#include "sql_type_vector.h"
2526

2627
key_map Item_func_vec_distance_common::part_of_sortkey() const
2728
{
@@ -191,18 +192,22 @@ String *Item_func_vec_fromtext::val_str(String *buf)
191192
if (!end_ok)
192193
goto error_format;
193194

194-
return buf;
195+
if (Type_handler_vector::is_valid(buf->ptr(), buf->length()))
196+
return buf;
197+
198+
null_value= true;
199+
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
200+
ER_TRUNCATED_WRONG_VALUE, ER(ER_TRUNCATED_WRONG_VALUE),
201+
"vector", value->ptr());
202+
return nullptr;
195203

196204
error_format:
197205
{
198206
int position= (int)((const char *) je.s.c_str - value->ptr());
199207
null_value= true;
200-
THD *thd= current_thd;
201-
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
202-
ER_VECTOR_FORMAT_INVALID,
203-
ER_THD(thd, ER_VECTOR_FORMAT_INVALID),
204-
position,
205-
value->ptr());
208+
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
209+
ER_VECTOR_FORMAT_INVALID, ER(ER_VECTOR_FORMAT_INVALID),
210+
position, value->ptr());
206211
return nullptr;
207212
}
208213

sql/sql_type_vector.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,7 @@ int Field_vector::store(const char *from, size_t length, CHARSET_INFO *cs)
311311
if (length != field_length)
312312
return report_wrong_value(ErrConvString(from, length, cs));
313313

314-
float abs2= 0.0f;
315-
for (const char *v= from, *end= from+length; v < end; v+= sizeof(float))
316-
{
317-
float val= get_float(v);
318-
abs2+= val*val;
319-
}
320-
if (!std::isfinite(abs2))
314+
if (!Type_handler_vector::is_valid(from, length))
321315
return report_wrong_value(ErrConvString(from, length, cs));
322316
}
323317

sql/sql_type_vector.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ class Type_handler_vector: public Type_handler_varchar
8787
override;
8888
bool Item_datetime_typecast_fix_length_and_dec(Item_datetime_typecast *) const
8989
override;
90+
91+
static bool is_valid(const char *from, size_t length)
92+
{
93+
float abs2= 0.0f;
94+
for (const char *v= from, *end= from+length; v < end; v+= sizeof(float))
95+
{
96+
float val= get_float(v);
97+
abs2+= val*val;
98+
}
99+
return std::isfinite(abs2);
100+
}
90101
};
91102

92103
extern Named_type_handler<Type_handler_vector> type_handler_vector;

0 commit comments

Comments
 (0)