Changeset: 32a3577c9a37 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=32a3577c9a37
Modified Files:
        sql/backends/monet5/UDF/capi/Tests/capi04.sql
        sql/backends/monet5/UDF/capi/Tests/capi08.sql
        sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
        sql/backends/monet5/UDF/capi/capi.c
Branch: jitudf
Log Message:

Properly handle changing parameter names with function caching as well.


diffs (152 lines):

diff --git a/sql/backends/monet5/UDF/capi/Tests/capi04.sql 
b/sql/backends/monet5/UDF/capi/Tests/capi04.sql
--- a/sql/backends/monet5/UDF/capi/Tests/capi04.sql
+++ b/sql/backends/monet5/UDF/capi/Tests/capi04.sql
@@ -45,8 +45,6 @@ CREATE FUNCTION capi04(inp STRING) RETUR
                if (inp.is_null(inp.data[i])) {
                        result->data[i] = result->null_value;
                } else {
-                       result->data[i] = malloc(strlen(inp.data[i]) + 2);
-                       strcpy(result->data[i] + 1, inp.data[i]);
                        result->data[i] = "hello";
                }
        }
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi08.sql 
b/sql/backends/monet5/UDF/capi/Tests/capi08.sql
--- a/sql/backends/monet5/UDF/capi/Tests/capi08.sql
+++ b/sql/backends/monet5/UDF/capi/Tests/capi08.sql
@@ -31,4 +31,28 @@ SELECT capi08(i) FROM integers;
 SELECT capi08(i) FROM integers;
 
 
+DROP FUNCTION capi08;
+CREATE FUNCTION capi08(inp1 INTEGER, inp2 INTEGER) RETURNS INTEGER LANGUAGE C {
+    result->initialize(result, inp1.count);
+    for(size_t i = 0; i < inp1.count; i++) {
+        result->data[i] = inp1.data[i] / inp2.data[i];
+    }
+};
+
+SELECT capi08(i * 2, i) FROM integers;
+
+DROP FUNCTION capi08;
+CREATE FUNCTION capi08(inp2 INTEGER, inp1 INTEGER) RETURNS INTEGER LANGUAGE C {
+    result->initialize(result, inp1.count);
+    for(size_t i = 0; i < inp1.count; i++) {
+        result->data[i] = inp1.data[i] / inp2.data[i];
+    }
+};
+
+# same function body and parameter types, but switched parameter names
+# should still give the same result
+SELECT capi08(i, i * 2) FROM integers;
+
+
+
 ROLLBACK;
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out 
b/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
--- a/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
+++ b/sql/backends/monet5/UDF/capi/Tests/capi08.stable.out
@@ -116,6 +116,40 @@ Ready.
 [ 6    ]
 [ 8    ]
 [ 10   ]
+#DROP FUNCTION capi08;
+#CREATE FUNCTION capi08(inp1 INTEGER, inp2 INTEGER) RETURNS INTEGER LANGUAGE C 
{
+#    result->initialize(result, inp1.count);
+#    for(size_t i = 0; i < inp1.count; i++) {
+#        result->data[i] = inp1.data[i] / inp2.data[i];
+#    }
+#};
+#SELECT capi08(i * 2, i) FROM integers;
+% sys.L2 # table_name
+% L2 # name
+% int # type
+% 1 # length
+[ 2    ]
+[ 2    ]
+[ 2    ]
+[ 2    ]
+[ 2    ]
+#DROP FUNCTION capi08;
+#CREATE FUNCTION capi08(inp2 INTEGER, inp1 INTEGER) RETURNS INTEGER LANGUAGE C 
{
+#    result->initialize(result, inp1.count);
+#    for(size_t i = 0; i < inp1.count; i++) {
+#        result->data[i] = inp1.data[i] / inp2.data[i];
+#    }
+#};
+#SELECT capi08(i, i * 2) FROM integers;
+% sys.L2 # table_name
+% L2 # name
+% int # type
+% 1 # length
+[ 2    ]
+[ 2    ]
+[ 2    ]
+[ 2    ]
+[ 2    ]
 #ROLLBACK;
 
 # 16:28:18 >  
diff --git a/sql/backends/monet5/UDF/capi/capi.c 
b/sql/backends/monet5/UDF/capi/capi.c
--- a/sql/backends/monet5/UDF/capi/capi.c
+++ b/sql/backends/monet5/UDF/capi/capi.c
@@ -151,8 +151,10 @@ static char *mprotect_region(void *addr,
        size_t actual_len = len;
        if (len == 0)
                return NULL;
-       // check if the region is page-aligned
 
+       // we must mprotect an entire page
+       // thus here we check if the region is page-aligned
+       // and if it is not, we page-align it
        pagesize = getpagesize();
        page_begin = (void *)((size_t)addr - (size_t)addr % pagesize);
        if (page_begin != addr) {
@@ -420,8 +422,7 @@ static str CUDFeval(Client cntxt, MalBlk
        funcname = sqlfun ? sqlfun->base.name : "yet_another_c_function";
 
        args = (str *)GDKzalloc(sizeof(str) * pci->argc);
-       output_names =
-               pci->retc > 0 ? (str *)GDKzalloc(sizeof(str) * pci->retc) : 
NULL;
+       output_names = (str *)GDKzalloc(sizeof(str) * pci->argc);
        if (!args || !output_names) {
                throw(MAL, "cudf.eval", MAL_MALLOC_FAIL);
        }
@@ -496,7 +497,17 @@ static str CUDFeval(Client cntxt, MalBlk
        GDK_STRHASH(exprStr, expression_hash);
        GDK_STRHASH(funcname, funcname_hash);
        funcname_hash = funcname_hash % FUNCTION_CACHE_SIZE;
-       function_parameters = GDKzalloc((input_count + output_count + 1) * 
sizeof(char));
+       j = 0;
+       for (i = 0; i < (size_t)pci->argc; i++) {
+               if (args[i]) {
+                       j += strlen(args[i]);
+               }
+               if (output_names[i]) {
+                       j += strlen(output_names[i]);
+               }
+       }
+
+       function_parameters = GDKzalloc((j + input_count + output_count + 1) * 
sizeof(char));
        if (!function_parameters) {
                msg = createException(MAL, "cudf.eval", MAL_MALLOC_FAIL);
                goto wrapup;
@@ -515,6 +526,19 @@ static str CUDFeval(Client cntxt, MalBlk
                        function_parameters[input_count + i] = 
getBatType(getArgType(mb, pci, i));
                }
        }
+       j = input_count + output_count;
+       for (i = 0; i < (size_t)pci->argc; i++) {
+               if (args[i]) {
+                       size_t len = strlen(args[i]);
+                       memcpy(function_parameters + j, args[i], len);
+                       j += len;
+               }
+               if (output_names[i]) {
+                       size_t len = strlen(output_names[i]);
+                       memcpy(function_parameters + j, output_names[i], len);
+                       j += len;
+               }
+       }
 
        MT_lock_set(&cache_lock);
        cached_function = function_cache[funcname_hash];
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to