MySQL Forums
Forum List  »  UDF

UDF to aggregate float array stored in a Blob field
Posted by: Manish Patil
Date: December 31, 2012 06:35AM

I have a field in which we store an array of 26 floats identifying the frequency of occurrence. I want to write a function that takes multiple records and adds the corresponding float values. As a first step I tried writing an aggregate function that just returns the total count. The following is the code that I tried out:

#include "hist.h"
#include <string.h>

union inp_data
{
char buf[104];
float inp[26];
};
struct hist_data
{
float dist[26];
};

my_bool hist_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
{
if (args->arg_count != 1)
{
strcpy(
message,
"wrong number of arguments: pcsa() requires one arguments"
);
return 1;
}
if ((args->arg_type[0] != STRING_RESULT) )
{
strcpy(
message,
"wrong argument type: pcsa() requires a BLOB"
);
return 1;
}

initid->maybe_null = 0; /* The result may be null */
initid->decimals = 4; /* We want 4 decimals in the result */
initid->max_length = 20; /* 6 digits + . + 10 decimals */

struct hist_data* hist_buf;
for(int i=0; i<26; i++)
{
hist_buf->dist=0;
}
initid->ptr = (char*)hist_buf;

return 0;
}

void hist_deinit( UDF_INIT* initid )
{
free(initid->ptr);
}

void hist_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error )
{
hist_clear(initid, is_null, error);
hist_add(initid, args, is_null, error);
}

void hist_clear( UDF_INIT* initid, char* is_null, char *error )
{
struct hist_data* hist_buf = (struct hist_data*)initid->ptr;
for(int i=0; i<26; i++)
{
hist_buf->dist=0;
}
}

void hist_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error )
{
struct hist_data* hist_buf = (struct hist_data*)initid->ptr;
union inp_data* inp_buf = (union inp_data*)args->args[0];
for(int i=0; i<26; i++)
{
hist_buf->dist+=inp_buf->inp;
}
}

int hist( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error )
{
struct hist_data* hist_buf = (struct hist_data*)initid->ptr;
int result=0;
for(int i=0; i<26; i++)
{
result+=(int)(hist_buf->dist);
}
return result;
}



The header file just has mysql includes and function definitions.


#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>

my_bool hist_init( UDF_INIT* initid, UDF_ARGS* args, char* message );
void hist_deinit( UDF_INIT* initid );
void hist_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
void hist_clear( UDF_INIT* initid, char* is_null, char *error );
void hist_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
int hist( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );



I compiled this program with the following options:
gcc -I /usr/include/mysql/ -std=c99 -shared -fPIC -o hist.so hist.c

and added the function to MySQL with:
create function hist returns integer soname "hist.so";

However when I try to run any query with this UDF the MySQL server crashes. The trace for MySQL crash is:

121231 16:15:06 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.29' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
11:51:34 UTC - mysqld got signal 11 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.

key_buffer_size=8388608
read_buffer_size=131072
max_used_connections=1
max_threads=151
thread_count=1
connection_count=1
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 338492 K bytes of memory
Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0x1b52660
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 7fefac252e68 thread_stack 0x40000
/usr/sbin/mysqld(my_print_stacktrace+0x35)[0x79f445]
/usr/sbin/mysqld(handle_fatal_signal+0x403)[0x66ec03]
/lib64/libpthread.so.0[0x375ee0f500]

Trying to get some variables.
Some pointers may be invalid and cause the dump to abort.
Query (7fef90004b90): is an invalid pointer
Connection ID (thread ID): 1
Status: NOT_KILLED

The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
information that should help you find out what is causing the crash.
121231 17:21:34 mysqld_safe Number of processes running now: 0


Can somebody help me out with what I am doing incorrectly.

Regards,
Manish.

Options: ReplyQuote


Subject
Views
Written By
Posted
UDF to aggregate float array stored in a Blob field
3654
December 31, 2012 06:35AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.