Fw:how are user defined function arguments passed ?

integrity 357416268 at qq.com
Wed Jul 10 10:28:39 CEST 2013


Thanks very much for your instant reply,Lefteris!


I wrote a simple test UDF, but it doesnt work.


myudf.c
#include "monetdb_config.h"
#include "myudf.h"


str
addint(int *ret,int *one)
{


*ret=(*one)+10;


return  MAL_SUCCEED;


}

------------------------------------------------------------------------------------------------------------
myudf.h
/* In your own module, replace "UDF" & "udf" by your module's name */


#ifndef _SQL_MYUDF_H_
#define _SQL_MYUDF_H_
#include "sql.h"
#include <string.h>


/* This is required as-is (except from renaming "UDF" & "udf" as suggested
 * above) for all modules for correctly exporting function on Unix-like and
 * Windows systems. */


#ifdef WIN32
#ifndef LIBMYUDF
#define myudf_export extern __declspec(dllimport)
#else
#define myudf_export extern __declspec(dllexport)
#endif
#else
#define myudf_export extern
#endif


/* export MAL wrapper functions */


myudf_export str addint(int *ret, int *one);
#endif /* _SQL_MYUDF_H_ */
-----------------------------------------------------------------------------------------------------

myudf.mal
# scalar MAL signatures
module myudf;


pattern addint(one:int):int
address addint
comment "add one and two";

-----------------------------------------------------------------------------------------------------
82_myudf.sql
create function addint(one INT)
returns int external name myudf.addint;

-----------------------------------------------------------------------------------------------------
when i execute in sql:
sql>select addint(76777);
+---------------------+
| addint_single_value |
+=====================+
|                   0 |
+---------------------+
1 tuple (1.364ms)
sql>
sql>select addint(100);
+---------------------+
| addint_single_value |
+=====================+
|                   0 |
+---------------------+
1 tuple (0.295ms)
sql>
sql>
sql>
sql>select addint(101);
+---------------------+
| addint_single_value |
+=====================+
|                   0 |
+---------------------+
1 tuple (0.289ms)
sql>select addint(1010000000000);
overflow in conversion of 1010000000000 to int.
sql>
sql>select addint(1010000);
+---------------------+
| addint_single_value |
+=====================+
|                   0 |
+---------------------+
1 tuple (1.087ms)
sql>



i checked every format of above files many times, i cannot find what's wrong with my program...
it seems that the parameter didn't got passed to MAL....


Thanks again for your help!


Sincerely,


Meng
------------------ Original ------------------
From:  "Lefteris"<lsidir at gmail.com>;
Date:  Wed, Jul 10, 2013 04:13 PM
To:  "Communication channel for MonetDB users"<users-list at monetdb.org>; 

Subject:  Re: Fw:how are user defined function arguments passed ?



The str *ret is you return value as described in the MAL signature
reverse(ra1:str):str

the ra1 is the name of a str variable in MAL, which is mapped to the
str *src variable of the UDF. Naming here plays no role, just the
types and the order of definition. Similarly, the :str end of the mal
signature reverse(ra1:str):str means that it returns a str type, which
in turn is mapped to the str *ret variable of the UDF signature.
Naming plays no role again, just that the first variable of the
signature in the udf is the return variable. Ofcourse it does not need
to be always a str, it depends on the definition of the mal signature.

On Wed, Jul 10, 2013 at 9:59 AM, integrity <357416268 at qq.com> wrote:
> Could anyone explain this question to me, please?
>
> I am still wondering on it....
>
> Thanks!
>
> Meng
> ------------------ Original ------------------
> From:  "integrity"<357416268 at qq.com>;
> Date:  Fri, Jul 5, 2013 03:42 PM
> To:  "users-list"<users-list at monetdb.org>;
> Subject:  how are user defined function arguments passed ?
>
> Hi everyone,
>
> In
> http://www.monetdb.org/Documentation/Cookbooks/SQLrecipes/UserDefinedFunction
> In thesection about how to extend SQL with a simple scalar function to
> reverse a string
>
> step 3. Extension starts with a definitin of the MAL signatures. See the
> example given, or browse through the files in monetdb5/modules/mal/*.mal to
> get a glimpse on how to write them. The MonetDB kernel documentation
> provides more details.  The file contains the MAL snippet:
> command reverse(ra1:str):str
> address UDFreverse
> comment "Reverse a string";
>
> step 4. The signature says that it expects a command body implementation
> under the name UDFreverse, shown below. The C-signature is a direct mapping,
> where arguments are passed by reference and the return value(s)  references
> are the first in the arguments list. The body should return a (malloced)
> string to denote an exception being raised or MAL_SUCCEED upon access.
> #include "udf.h"
>
> static str
> reverse(const char *src)
> {
>     size_t len;
>     str ret, new;
>
>     /* The scalar function returns the new space */
>     len = strlen(src);
>     ret = new = GDKmalloc(len + 1);
>     if (new == NULL)
>         return NULL;
>     new[len] = 0;
>     while (len > 0)
>         *new++ = src[--len];
>     return ret;
> }
>
> str
> UDFreverse(str *ret, str *src)
> {
>     if (*src == 0 || strcmp(*src, str_nil) == 0)
>         *ret = GDKstrdup(str_nil);
>     else
>         *ret = reverse(*src);
>     return MAL_SUCCEED;
> }
>
>
> I don't know why the argument name of  "command reverse(ra1:str):str"
> different from function definition UDFreverse(str *ret, str *src)?
>
> how does ret come from? is it according to the naming rules that the first
> argument is the result, must it be a string type?
>
> Thanks!
>
> Meng
>
>
> _______________________________________________
> users-list mailing list
> users-list at monetdb.org
> http://mail.monetdb.org/mailman/listinfo/users-list
>
_______________________________________________
users-list mailing list
users-list at monetdb.org
http://mail.monetdb.org/mailman/listinfo/users-list
.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.monetdb.org/pipermail/users-list/attachments/20130710/f0511a44/attachment.html>


More information about the users-list mailing list