Commit b80b63bd authored by Sergei Golubchik's avatar Sergei Golubchik

MDEV-32885 VEC_DISTANCE() function

parent 8478a06c
......@@ -101,7 +101,7 @@ SET(SQL_EMBEDDED_SOURCES emb_qcache.cc libmysqld.c lib_sql.cc
../sql/sql_show.cc ../sql/sql_state.c
../sql/sql_statistics.cc ../sql/sql_string.cc
../sql/sql_table.cc ../sql/sql_test.cc
../sql/ddl_log.cc
../sql/ddl_log.cc ../sql/item_vectorfunc.cc
../sql/sql_trigger.cc ../sql/sql_udf.cc ../sql/sql_union.cc
../sql/sql_update.cc ../sql/sql_view.cc ../sql/sql_profile.cc
../sql/gcalc_tools.cc ../sql/gcalc_slicescan.cc
......
......@@ -103,7 +103,7 @@ SET (SQL_SOURCE
filesort_utils.cc
filesort.cc gstream.cc
signal_handler.cc
handler.cc
handler.cc item_vectorfunc.cc
hostname.cc init.cc item.cc item_buff.cc item_cmpfunc.cc
item_create.cc item_func.cc item_geofunc.cc item_row.cc
item_strfunc.cc item_subselect.cc item_sum.cc item_timefunc.cc
......
......@@ -6619,6 +6619,7 @@ class Item_int_with_ref :public Item_int
#include "item_subselect.h"
#include "item_xmlfunc.h"
#include "item_jsonfunc.h"
#include "item_vectorfunc.h"
#include "item_create.h"
#include "item_vers.h"
#endif
......
......@@ -6235,6 +6235,29 @@ Create_func_year_week::create_native(THD *thd, const LEX_CSTRING *name,
return func;
}
class Create_func_vec_distance: public Create_func_arg2
{
public:
Item *create_2_arg(THD *thd, Item *arg1, Item *arg2) override;
static Create_func_vec_distance s_singleton;
protected:
Create_func_vec_distance() = default;
virtual ~Create_func_vec_distance() = default;
};
Create_func_vec_distance Create_func_vec_distance::s_singleton;
Item*
Create_func_vec_distance::create_2_arg(THD *thd, Item *arg1, Item *arg2)
{
return new (thd->mem_root) Item_func_vec_distance(thd, arg1, arg2);
}
#define BUILDER(F) & F::s_singleton
/*
......@@ -6461,6 +6484,7 @@ const Native_func_registry func_array[] =
{ { STRING_WITH_LEN("UPDATEXML") }, BUILDER(Create_func_xml_update)},
{ { STRING_WITH_LEN("UPPER") }, BUILDER(Create_func_ucase)},
{ { STRING_WITH_LEN("UUID_SHORT") }, BUILDER(Create_func_uuid_short)},
{ { STRING_WITH_LEN("VEC_DISTANCE") }, BUILDER(Create_func_vec_distance)},
{ { STRING_WITH_LEN("VERSION") }, BUILDER(Create_func_version)},
{ { STRING_WITH_LEN("WEEK") }, BUILDER(Create_func_week)},
{ { STRING_WITH_LEN("WEEKDAY") }, BUILDER(Create_func_weekday)},
......
/* Copyright (c) 2023, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
/**
@file
@brief
This file defines all vector functions
*/
#include <my_global.h>
#include "item.h"
double Item_func_vec_distance::val_real()
{
String *r1= args[0]->val_str();
String *r2= args[1]->val_str();
null_value= !r1 || !r2 || r1->length() != r2->length() ||
r1->length() % sizeof(float);
if (null_value)
return 0;
float *v1= (float*)r1->ptr();
float *v2= (float*)r2->ptr();
double d= 0;
for (uint i=0; i < r1->length() / sizeof(float); i++)
d+= (v1[i] - v2[i])*(v1[i] - v2[i]);
return sqrt(d);
}
#ifndef ITEM_VECTORFUNC_INCLUDED
#define ITEM_VECTORFUNC_INCLUDED
/* Copyright (C) 2023, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
/* This file defines all vector functions */
#include "lex_string.h"
#include "item_func.h"
class Item_func_vec_distance: public Item_real_func
{
protected:
bool check_arguments() const override
{
return check_argument_types_or_binary(NULL, 0, arg_count);
}
public:
Item_func_vec_distance(THD *thd, Item *a, Item *b)
:Item_real_func(thd, a, b) {}
bool fix_length_and_dec(THD *thd) override
{
set_maybe_null();
return Item_real_func::fix_length_and_dec(thd);
}
double val_real() override;
LEX_CSTRING func_name_cstring() const override
{
static LEX_CSTRING name= {STRING_WITH_LEN("vec_distance") };
return name;
}
Item *do_get_copy(THD *thd) const override
{ return get_item_copy<Item_func_vec_distance>(thd, this); }
};
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment