Hey Shmagi,
You are not using the correct function signature for writing a MonetDB UDF. The correct function signature is first the return value as pointer (this is where you write the return value to), and then a list of all the input parameters as pointers. For your function, the correct function signature is:
str UDFbitand(flt *ret, lng *a, lng *b);
Where (a,b) are the input parameters, and (ret) is where you write the return value. The actual return value of the function is a string that is either MAL_SUCCEED (if the function has completed correctly), or an error message (if it has not).
I notice in your function that in the end you write the result to a local variable:
float final = count/(float)length;
This variable is not exported from the function, and as such cannot be read by the database. Instead, the return value of the function should be written to the "ret" pointer, as such:
*ret = count / (float) length;
Also, always initialize your variables if you expect them to have a specific value. In your code, you have the following variables.
int length;
int count;
Which you then increment. As these variables are uninitialized, their value is undefined. Some compilers may set their value to 0, but you should not rely on this.
I have rewritten your UDF to operate correctly, here's the code.
str
UDFbitand(flt *ret, lng *a, lng *b)
{
long long int temp = weirdAnd(*a, *b);
long long int temp2 = weirdAnd(*a, *b);
int length = 0;
int count = 0;
while (temp != 0)
{
temp/=10;
length++;
}
while (temp2 != 0) {
if (temp2 % 10 == 1)
count++;
temp2 /= 10;
}
*ret = count/(float)length;
return MAL_SUCCEED;
}
This gives me the following result.
sql>SELECT bitand(1111, 1010);
+---------------------+
| bitand_single_value |
+=====================+
| 0.5 |
+---------------------+
Note that this is a single scalar UDF. While you can use this UDF to operate on entire columns, if you want the UDF to perform well you should implement a batch UDF that operates on BATs directly.
Hope that helps,
Mark
----- Original Message -----
From: "Shmagi Kavtaradze"