Commit cbf6beba authored by Alexander Barkov's avatar Alexander Barkov

MDEV-20764 Add MariaDB_FUNCTION_COLLECTION_PLUGIN

parent 3616175f
......@@ -46,6 +46,7 @@ IF(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang" AND RUN_ABI_CHECK)
${CMAKE_SOURCE_DIR}/include/mysql/plugin_password_validation.h
${CMAKE_SOURCE_DIR}/include/mysql/plugin_encryption.h
${CMAKE_SOURCE_DIR}/include/mysql/plugin_data_type.h
${CMAKE_SOURCE_DIR}/include/mysql/plugin_function_collection.h
)
ADD_CUSTOM_TARGET(abi_check ALL
......
......@@ -90,12 +90,13 @@ typedef struct st_mysql_xid MYSQL_XID;
#define MYSQL_AUDIT_PLUGIN 5
#define MYSQL_REPLICATION_PLUGIN 6
#define MYSQL_AUTHENTICATION_PLUGIN 7
#define MYSQL_MAX_PLUGIN_TYPE_NUM 11 /* The number of plugin types */
#define MYSQL_MAX_PLUGIN_TYPE_NUM 12 /* The number of plugin types */
/* MariaDB plugin types */
#define MariaDB_PASSWORD_VALIDATION_PLUGIN 8
#define MariaDB_ENCRYPTION_PLUGIN 9
#define MariaDB_DATA_TYPE_PLUGIN 10
#define MariaDB_FUNCTION_COLLECTION_PLUGIN 11
/* We use the following strings to define licenses for plugins */
#define PLUGIN_LICENSE_PROPRIETARY 0
......
#ifndef MARIADB_PLUGIN_FUNCTION_COLLECTION_INCLUDED
#define MARIADB_PLUGIN_FUNCTION_COLLECTION_INCLUDED
/* Copyright (C) 2019, Alexander Barkov and 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
Data Type Plugin API.
This file defines the API for server plugins that manage function collections.
*/
#ifdef __cplusplus
#include <mysql/plugin.h>
/*
API for data type plugins. (MariaDB_FUNCTION_COLLECTION_PLUGIN)
*/
#define MariaDB_FUNCTION_COLLECTION_INTERFACE_VERSION (MYSQL_VERSION_ID << 8)
class Native_func_registry_array
{
const Native_func_registry *m_elements;
size_t m_count;
public:
Native_func_registry_array()
:m_elements(NULL),
m_count(0)
{ }
Native_func_registry_array(const Native_func_registry *elements, size_t count)
:m_elements(elements),
m_count(count)
{ }
const Native_func_registry& element(size_t i) const
{
DBUG_ASSERT(i < m_count);
return m_elements[i];
}
size_t count() const { return m_count; }
};
class Plugin_function_collection
{
int m_interface_version;
const Native_func_registry_array m_native_func_registry_array;
HASH m_hash;
public:
bool init();
void deinit()
{
my_hash_free(&m_hash);
}
static int init_plugin(st_plugin_int *plugin)
{
Plugin_function_collection *coll=
reinterpret_cast<Plugin_function_collection*>(plugin->plugin->info);
return coll->init();
}
static int deinit_plugin(st_plugin_int *plugin)
{
Plugin_function_collection *coll=
reinterpret_cast<Plugin_function_collection*>(plugin->plugin->info);
coll->deinit();
return 0;
}
public:
Plugin_function_collection(int interface_version,
const Native_func_registry_array &nfra)
:m_interface_version(interface_version),
m_native_func_registry_array(nfra)
{
bzero((void*) &m_hash, sizeof(m_hash));
}
Create_func *find_native_function_builder(THD *thd,
const LEX_CSTRING &name) const;
};
/**
Data type plugin descriptor
*/
#endif /* __cplusplus */
#endif /* MARIADB_PLUGIN_FUNCTION_COLLECTION_INCLUDED */
This diff is collapsed.
# Copyright (c) 2019, MariaDB corporation. All rights reserved.
#
# 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
MYSQL_ADD_PLUGIN(func_test plugin.cc RECOMPILE_FOR_EMBEDDED
MODULE_ONLY COMPONENT Test)
SELECT
PLUGIN_NAME,
PLUGIN_VERSION,
PLUGIN_STATUS,
PLUGIN_TYPE,
PLUGIN_AUTHOR,
PLUGIN_DESCRIPTION,
PLUGIN_LICENSE,
PLUGIN_MATURITY,
PLUGIN_AUTH_VERSION
FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='FUNCTION COLLECTION'
AND PLUGIN_NAME='func_test';
PLUGIN_NAME func_test
PLUGIN_VERSION 1.0
PLUGIN_STATUS ACTIVE
PLUGIN_TYPE FUNCTION COLLECTION
PLUGIN_AUTHOR MariaDB Corporation
PLUGIN_DESCRIPTION Function collection test
PLUGIN_LICENSE GPL
PLUGIN_MATURITY Experimental
PLUGIN_AUTH_VERSION 1.0
SELECT sysconst_test();
sysconst_test()
sysconst_test
SELECT sysconst_test();
sysconst_test()
sysconst_test
#--echo #
#--echo #
#--echo #
--vertical_results
SELECT
PLUGIN_NAME,
PLUGIN_VERSION,
PLUGIN_STATUS,
PLUGIN_TYPE,
PLUGIN_AUTHOR,
PLUGIN_DESCRIPTION,
PLUGIN_LICENSE,
PLUGIN_MATURITY,
PLUGIN_AUTH_VERSION
FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='FUNCTION COLLECTION'
AND PLUGIN_NAME='func_test';
--horizontal_results
SELECT sysconst_test();
SELECT sysconst_test();
package My::Suite::Func_test;
@ISA = qw(My::Suite);
return "No FUNC_TEST plugin" unless $ENV{FUNC_TEST_SO};
sub is_default { 1 }
bless { };
/*
Copyright (c) 2000, 2015, Oracle and/or its affiliates.
Copyright (c) 2009, 2019, 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-1301 USA */
#define MYSQL_SERVER
#include <my_global.h>
#include <sql_class.h>
#include <mysql/plugin_function_collection.h>
class Item_func_sysconst_test :public Item_func_sysconst
{
public:
Item_func_sysconst_test(THD *thd): Item_func_sysconst(thd) {}
String *val_str(String *str)
{
null_value= str->copy(STRING_WITH_LEN("sysconst_test"), system_charset_info);
return null_value ? NULL : str;
}
bool fix_length_and_dec()
{
max_length= MAX_FIELD_NAME * system_charset_info->mbmaxlen;
maybe_null= true;
return false;
}
const char *func_name() const { return "sysconst_test"; }
const char *fully_qualified_func_name() const { return "sysconst_test()"; }
Item *get_copy(THD *thd)
{ return get_item_copy<Item_func_sysconst_test>(thd, this); }
};
class Create_func_sysconst_test : public Create_func_arg0
{
public:
Item *create_builder(THD *thd) override;
static Create_func_sysconst_test s_singleton;
protected:
Create_func_sysconst_test() {}
};
Create_func_sysconst_test Create_func_sysconst_test::s_singleton;
Item* Create_func_sysconst_test::create_builder(THD *thd)
{
return new (thd->mem_root) Item_func_sysconst_test(thd);
}
#define BUILDER(F) & F::s_singleton
static Native_func_registry func_array[] =
{
{{STRING_WITH_LEN("SYSCONST_TEST")}, BUILDER(Create_func_sysconst_test)}
};
static Plugin_function_collection
plugin_descriptor_function_collection_test(
MariaDB_FUNCTION_COLLECTION_INTERFACE_VERSION,
Native_func_registry_array(func_array, array_elements(func_array)));
/*************************************************************************/
maria_declare_plugin(type_test)
{
MariaDB_FUNCTION_COLLECTION_PLUGIN, // the plugin type (see include/mysql/plugin.h)
&plugin_descriptor_function_collection_test, // pointer to type-specific plugin descriptor
"func_test", // plugin name
"MariaDB Corporation", // plugin author
"Function collection test", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license (see include/mysql/plugin.h)
0, // Pointer to plugin initialization function
0, // Pointer to plugin deinitialization function
0x0100, // Numeric version 0xAABB means AA.BB veriosn
NULL, // Status variables
NULL, // System variables
"1.0", // String version representation
MariaDB_PLUGIN_MATURITY_EXPERIMENTAL // Maturity(see include/mysql/plugin.h)*/
}
maria_declare_plugin_end;
......@@ -36,6 +36,94 @@
#include "item_inetfunc.h"
#include "sql_time.h"
#include "sql_type_geom.h"
#include <mysql/plugin_function_collection.h>
extern "C" uchar*
get_native_fct_hash_key(const uchar *buff, size_t *length,
my_bool /* unused */)
{
Native_func_registry *func= (Native_func_registry*) buff;
*length= func->name.length;
return (uchar*) func->name.str;
}
bool Plugin_function_collection::init()
{
DBUG_ENTER("Plugin_function_collection::init");
if (my_hash_init(&m_hash,
system_charset_info,
(ulong) m_native_func_registry_array.count(),
0,
0,
(my_hash_get_key) get_native_fct_hash_key,
NULL, /* Nothing to free */
MYF(0)))
DBUG_RETURN(true);
for (size_t i= 0; i < m_native_func_registry_array.count(); i++)
{
const Native_func_registry &func= m_native_func_registry_array.element(i);
DBUG_ASSERT(func.builder != NULL);
if (my_hash_insert(&m_hash, (uchar*) &func))
DBUG_RETURN(true);
}
DBUG_RETURN(false);
}
Create_func *
Plugin_function_collection::find_native_function_builder(THD *thd,
const LEX_CSTRING &name)
const
{
const Native_func_registry *func;
func= (const Native_func_registry*) my_hash_search(&m_hash,
(uchar*) name.str,
name.length);
return func ? func->builder : NULL;
}
class Plugin_find_native_func_builder_param
{
bool find_native_function_builder(THD *thd,
const Plugin_function_collection *pfc)
{
return ((builder= pfc->find_native_function_builder(thd, name))) == NULL;
}
static my_bool find_in_plugin(THD *thd, plugin_ref plugin, void *data)
{
Plugin_find_native_func_builder_param *param=
reinterpret_cast<Plugin_find_native_func_builder_param*>(data);
const Plugin_function_collection *fc=
reinterpret_cast<Plugin_function_collection*>
(plugin_decl(plugin)->info);
return param->find_native_function_builder(thd, fc);
}
public:
LEX_CSTRING name;
Create_func *builder;
Plugin_find_native_func_builder_param(const LEX_CSTRING &name_arg)
:name(name_arg), builder(NULL)
{ }
Create_func *find(THD *thd)
{
if (plugin_foreach(thd,
Plugin_find_native_func_builder_param::find_in_plugin,
MariaDB_FUNCTION_COLLECTION_PLUGIN, this))
return NULL;
return builder;
}
};
#ifdef HAVE_SPATIAL
extern Plugin_function_collection plugin_function_collection_geometry;
#endif
/*
=============================================================================
......@@ -5735,15 +5823,6 @@ static Native_func_registry func_array[] =
static HASH native_functions_hash;
extern "C" uchar*
get_native_fct_hash_key(const uchar *buff, size_t *length,
my_bool /* unused */)
{
Native_func_registry *func= (Native_func_registry*) buff;
*length= func->name.length;
return (uchar*) func->name.str;
}
/*
Load the hash table for native functions.
Note: this code is not thread safe, and is intended to be used at server
......@@ -5768,7 +5847,7 @@ int item_create_init()
DBUG_RETURN(1);
#ifdef HAVE_SPATIAL
if (function_collection_geometry.init())
if (plugin_function_collection_geometry.init())
DBUG_RETURN(1);
#endif
......@@ -5810,7 +5889,7 @@ void item_create_cleanup()
DBUG_ENTER("item_create_cleanup");
my_hash_free(& native_functions_hash);
#ifdef HAVE_SPATIAL
function_collection_geometry.cleanup();
plugin_function_collection_geometry.deinit();
#endif
DBUG_VOID_RETURN;
}
......@@ -5826,15 +5905,16 @@ find_native_function_builder(THD *thd, const LEX_CSTRING *name)
(uchar*) name->str,
name->length);
if (func)
{
builder= func->builder;
}
if (func && (builder= func->builder))
return builder;
if ((builder= Plugin_find_native_func_builder_param(*name).find(thd)))
return builder;
#ifdef HAVE_SPATIAL
if (!builder)
builder= function_collection_geometry.find_native_function_builder(thd,
*name);
builder= plugin_function_collection_geometry.
find_native_function_builder(thd, *name);
#endif
return builder;
......
......@@ -39,6 +39,7 @@
#include <m_ctype.h>
#include "opt_range.h"
#include "item_geofunc.h"
#include <mysql/plugin_function_collection.h>
bool Item_geometry_func::fix_length_and_dec()
......@@ -3906,62 +3907,10 @@ static Native_func_registry func_array_geom[] =
};
extern "C" uchar*
get_native_fct_hash_key_geom(const uchar *buff, size_t *length,
my_bool /* unused */)
{
Native_func_registry *func= (Native_func_registry*) buff;
*length= func->name.length;
return (uchar*) func->name.str;
}
Function_collection_geometry function_collection_geometry;
static FHash hash_funcn;
Create_func *
Function_collection_geometry::find_native_function_builder(THD *thd,
const LEX_CSTRING &f)
const
{
Native_func_registry *func;
func= (Native_func_registry*) my_hash_search(&hash_funcn,
(uchar*) f.str, f.length);
return func ? func->builder : NULL;
}
bool Function_collection_geometry::init()
{
DBUG_ENTER("Type_collection_geometry::init_functions");
if (my_hash_init(&hash_funcn,
system_charset_info,
array_elements(func_array_geom),
0,
0,
(my_hash_get_key) get_native_fct_hash_key_geom,
NULL, /* Nothing to free */
MYF(0)))
DBUG_RETURN(true);
for (uint i= 0; i < array_elements(func_array_geom); i++)
{
Native_func_registry *func= &func_array_geom[i];
DBUG_ASSERT(func->builder != NULL);
if (my_hash_insert(&hash_funcn, (uchar*) func))
DBUG_RETURN(1);
}
DBUG_RETURN(false);
}
void Function_collection_geometry::cleanup()
{
my_hash_free(&hash_funcn);
}
Plugin_function_collection
plugin_function_collection_geometry(
MariaDB_FUNCTION_COLLECTION_INTERFACE_VERSION,
Native_func_registry_array(func_array_geom,
array_elements(func_array_geom)));
#endif /*HAVE_SPATIAL*/
......@@ -39,6 +39,7 @@
#include <mysql/plugin_password_validation.h>
#include <mysql/plugin_encryption.h>
#include <mysql/plugin_data_type.h>
#include <mysql/plugin_function_collection.h>
#include "sql_plugin_compat.h"
#ifdef HAVE_LINK_H
......@@ -92,7 +93,8 @@ const LEX_CSTRING plugin_type_names[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{ STRING_WITH_LEN("AUTHENTICATION") },
{ STRING_WITH_LEN("PASSWORD VALIDATION") },
{ STRING_WITH_LEN("ENCRYPTION") },
{ STRING_WITH_LEN("DATA TYPE") }
{ STRING_WITH_LEN("DATA TYPE") },
{ STRING_WITH_LEN("FUNCTION COLLECTION") }
};
extern int initialize_schema_table(st_plugin_int *plugin);
......@@ -112,13 +114,15 @@ extern int finalize_encryption_plugin(st_plugin_int *plugin);
plugin_type_init plugin_type_initialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
0, ha_initialize_handlerton, 0, 0,initialize_schema_table,
initialize_audit_plugin, 0, 0, 0, initialize_encryption_plugin, 0
initialize_audit_plugin, 0, 0, 0, initialize_encryption_plugin, 0,
Plugin_function_collection::init_plugin
};
plugin_type_init plugin_type_deinitialize[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
0, ha_finalize_handlerton, 0, 0, finalize_schema_table,
finalize_audit_plugin, 0, 0, 0, finalize_encryption_plugin, 0
finalize_audit_plugin, 0, 0, 0, finalize_encryption_plugin, 0,
Plugin_function_collection::deinit_plugin
};
/*
......@@ -131,6 +135,7 @@ static int plugin_type_initialization_order[MYSQL_MAX_PLUGIN_TYPE_NUM]=
MYSQL_DAEMON_PLUGIN,
MariaDB_ENCRYPTION_PLUGIN,
MariaDB_DATA_TYPE_PLUGIN,
MariaDB_FUNCTION_COLLECTION_PLUGIN,
MYSQL_STORAGE_ENGINE_PLUGIN,
MYSQL_INFORMATION_SCHEMA_PLUGIN,
MYSQL_FTPARSER_PLUGIN,
......@@ -173,7 +178,8 @@ static int min_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
MIN_AUTHENTICATION_INTERFACE_VERSION,
MariaDB_PASSWORD_VALIDATION_INTERFACE_VERSION,
MariaDB_ENCRYPTION_INTERFACE_VERSION,
MariaDB_DATA_TYPE_INTERFACE_VERSION
MariaDB_DATA_TYPE_INTERFACE_VERSION,
MariaDB_FUNCTION_COLLECTION_INTERFACE_VERSION
};
static int cur_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
{
......@@ -187,7 +193,8 @@ static int cur_plugin_info_interface_version[MYSQL_MAX_PLUGIN_TYPE_NUM]=
MYSQL_AUTHENTICATION_INTERFACE_VERSION,
MariaDB_PASSWORD_VALIDATION_INTERFACE_VERSION,
MariaDB_ENCRYPTION_INTERFACE_VERSION,
MariaDB_DATA_TYPE_INTERFACE_VERSION
MariaDB_DATA_TYPE_INTERFACE_VERSION,
MariaDB_FUNCTION_COLLECTION_INTERFACE_VERSION
};
static struct
......
......@@ -263,17 +263,6 @@ extern MYSQL_PLUGIN_IMPORT Type_handler_multipolygon type_handler_multipolygo
extern MYSQL_PLUGIN_IMPORT Type_handler_geometrycollection type_handler_geometrycollection;
class Function_collection_geometry: public Function_collection
{
public:
bool init() override;
void cleanup() override;
Create_func *find_native_function_builder(THD *thd,
const LEX_CSTRING &name)
const override;
};
class Type_collection_geometry: public Type_collection
{
const Type_handler *aggregate_common(const Type_handler *a,
......@@ -326,9 +315,6 @@ class Type_collection_geometry: public Type_collection
};
extern MYSQL_PLUGIN_IMPORT
Function_collection_geometry function_collection_geometry;
extern MYSQL_PLUGIN_IMPORT Type_collection_geometry type_collection_geometry;
......
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