Hey Mark,

Thanks a lot! There was no way for me to get that rigth! :)
About getting a pointer, you are right, that seems like the easiest solution to get the metadata needed from the SQL layer. So this means that one can write any C code directly in the MAL layer without having to define a C-UDF (going through the SQL layer), which is also pretty awesome!
Just a final question, how can I return a BAT in MAL? like in my example I would like to return tuples of type (bat[:int], bat[:dbl]) , in the code I am setting a sql.resultset which I also don't know what to do with it. My end goal is to return the results of the function to an external application. Any pointers are appreciated.
Again, many thanks for your help!


Best,

Renato M.

El dom., 4 nov. 2018 a las 21:35, Mark Raasveldt (<m.raasveldt@cwi.nl>) escribió:
Hey Renato,

Apparently the arguments start with 2 (i.e. arg2, arg3, etc…). The unnamed outputs are named output0, output1, output2… (This is a result of an off-by-one error in the naming and it never being tested in pure MAL). The following code works for me:

g:= bat.new(:int);
bat.append(g,5);
(r:bat[:int],s:bat[:int]) := batcapi.eval(nil:ptr, false:bit, "{\n#include <math.h>\n    size_t j;\n size_t count = arg2.data[0];\n\n    output0->initialize(output0, count);\n    output1->initialize(output1, count);\n  for(j = 0; j < count; j++) {\n        output0->data[j] = j;\n        output1->data[j] = round(j > 0 ? 42.0 / j : 42.0);\n    }\n};",g);

I don’t know about the pointer, I would think there is no easy way to get it as it is an internal memory address that is not supposed to be exposed. Using the pointer to the SQL structure is a bit hacky in the first place, but it was the easiest way of transferring this information from the SQL layer to the MAL layer.

Regards,

Mark

On 4 Nov 2018, at 18:16, Renato Marroquín Mogrovejo <renatoj.marroquin@gmail.com> wrote:

Hey Mark,

Thanks for the reply, however I am still having trouble with it.
So I am trying to replicate the example of creating C UDF from [https://www.monetdb.org/blog/JIT_C_C%252B%252B_UDFs_in_MonetDB], the one that returns a table. My MAL function is as follows:

function user.s8_1():void;
X_5:int := sql.mvc();
(X_8:bat[:int], X_12:bat[:dbl]) := capi.eval(nil:ptr, false:bit, "{\n#include <math.h>\n    size_t j;\n size_t count = arg1.data[0];\n\n    i->initialize(i, count);\n    d->initialize(d, count);\n  for(j = 0; j < count; j++) {\n        i->data[j] = j;\n        d->data[j] = round(j > 0 ? 42.0 / j : 42.0);\n    }\n};", 5);
barrier X_87:bit := language.dataflow();
X_14:bat[:str] := bat.new(nil:str);
X_20:bat[:int] := bat.new(nil:int);
X_18:bat[:int] := bat.new(nil:int);
X_17:bat[:str] := bat.new(nil:str);
X_16:bat[:str] := bat.new(nil:str);
X_21:bat[:str] := bat.append(X_14, ".L2");
X_23:bat[:str] := bat.append(X_16, "i");
X_25:bat[:str] := bat.append(X_17, "int");
X_27:bat[:int] := bat.append(X_18, 32);
X_29:bat[:int] := bat.append(X_20, 0);
X_31:bat[:str] := bat.append(X_21, ".L2");
X_32:bat[:str] := bat.append(X_23, "d");
X_34:bat[:str] := bat.append(X_25, "double");
X_36:bat[:int] := bat.append(X_27, 53);
X_38:bat[:int] := bat.append(X_29, 0);
exit X_87:bit;
sql.resultSet(X_31, X_32, X_34, X_36, X_38, X_8, X_12);
end user.s8_1;

But I still get: error: ‘arg1’ undeclared (first use in this function)
Other related question is regarding output parameters names? In the C code, they are named <i> and <d> and this also throws an undeclared error.
Btw, is there a way to get the UDF definition, i.e., the pointer as expected? I maybe that would be an easier solution?
Thanks again!


Best,

Renato M.

El dom., 4 nov. 2018 a las 15:42, Mark Raasveldt (<m.raasveldt@cwi.nl>) escribió:
Hey Renato,

Just like the pyapi and rapi functions, the first argument is a pointer to the SQL function definition. This pointer is used to retrieve the argument names that were defined in the SQL layer. On the MAL layer you can simply pass the argument "nil:ptr”. This will result in the variables being called “arg1”, “arg2”, etc… See the pyapi MAL tests for more information (sql/backends/monet5/UDF/pyapi/Tests/*.malC).

Regards,

Mark

On 4 Nov 2018, at 11:53, Renato Marroquín Mogrovejo <renatoj.marroquin@gmail.com> wrote:

Hi all,

I am trying to use the following patten from MAL[1] 

pattern eval(fptr:ptr,cpp:bit,expr:str,arg:any...):any...

However, I am not sure how I should create the first parameter <fptr:ptr> from MAL. I am just trying to be able to call a C-UDF from MAL. From capi.c, it would seem that it is the client's context but I don't know how to get a handle of that.
Any pointers are highly appreciated.

Thanks!

Renato M.
_______________________________________________
users-list mailing list
users-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/users-list

_______________________________________________
users-list mailing list
users-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/users-list
_______________________________________________
users-list mailing list
users-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/users-list

_______________________________________________
users-list mailing list
users-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/users-list