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

Always create aggr_group input and add test case for grouped/ungrouped 
aggregates.


diffs (truncated from 326 to 300 lines):

diff --git a/sql/backends/monet5/UDF/capi/Tests/capi13.sql 
b/sql/backends/monet5/UDF/capi/Tests/capi13.sql
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi13.sql
@@ -0,0 +1,28 @@
+
+START TRANSACTION;
+
+# grouped aggregate: sum
+
+CREATE AGGREGATE capi13(inp INTEGER) RETURNS BIGINT LANGUAGE C {
+#include <string.h>
+       // initialize one aggregate per group
+       result->initialize(result, aggr_group.count);
+       // zero initialize the sums
+       memset(result->data, 0, result->count * sizeof(result->null_value));
+       // gather the sums for each of the groups
+       for(size_t i = 0; i < inp.count; i++) {
+               result->data[aggr_group.data[i]] += inp.data[i];
+       }
+};
+
+CREATE TABLE vals(grp INTEGER, value INTEGER);
+INSERT INTO vals VALUES (1, 100), (2, 200), (1, 50), (2, 300);
+
+SELECT grp, capi13(value) FROM vals GROUP BY grp;
+
+SELECT capi13(value) FROM vals;
+
+DROP AGGREGATE capi13;
+DROP TABLE vals;
+
+ROLLBACK;
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi13.stable.err 
b/sql/backends/monet5/UDF/capi/Tests/capi13.stable.err
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi13.stable.err
@@ -0,0 +1,34 @@
+stderr of test 'capi13` in directory 'sql/backends/monet5/UDF/capi` itself:
+
+
+# 14:43:45 >  
+# 14:43:45 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=35341" "--set" 
"mapi_usock=/var/tmp/mtest-91643/.s.monetdb.35341" "--set" "monet_prompt=" 
"--forcemito" "--dbpath=NONE/var/MonetDB/mTests_sql_backends_monet5_UDF_capi"
+# 14:43:45 >  
+
+# builtin opt  gdk_dbpath = /Users/myth/opt/var/monetdb5/dbfarm/demo
+# builtin opt  gdk_debug = 0
+# builtin opt  gdk_vmtrim = no
+# builtin opt  monet_prompt = >
+# builtin opt  monet_daemon = no
+# builtin opt  mapi_port = 50000
+# builtin opt  mapi_open = false
+# builtin opt  mapi_autosense = false
+# builtin opt  sql_optimizer = default_pipe
+# builtin opt  sql_debug = 0
+# cmdline opt  gdk_nr_threads = 0
+# cmdline opt  mapi_open = true
+# cmdline opt  mapi_port = 35341
+# cmdline opt  mapi_usock = /var/tmp/mtest-91643/.s.monetdb.35341
+# cmdline opt  monet_prompt = 
+# cmdline opt  gdk_dbpath = 
/Users/myth/opt/mTests/sql/backends/monet5/UDF/capi/NONE/var/MonetDB/mTests_sql_backends_monet5_UDF_capi
+# cmdline opt  gdk_debug = 536870922
+
+# 14:43:46 >  
+# 14:43:46 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-91643" "--port=35341"
+# 14:43:46 >  
+
+
+# 14:43:47 >  
+# 14:43:47 >  "Done."
+# 14:43:47 >  
+
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi13.stable.out 
b/sql/backends/monet5/UDF/capi/Tests/capi13.stable.out
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/capi/Tests/capi13.stable.out
@@ -0,0 +1,97 @@
+stdout of test 'capi13` in directory 'sql/backends/monet5/UDF/capi` itself:
+
+
+# 14:43:45 >  
+# 14:43:45 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=35341" "--set" 
"mapi_usock=/var/tmp/mtest-91643/.s.monetdb.35341" "--set" "monet_prompt=" 
"--forcemito" "--dbpath=NONE/var/MonetDB/mTests_sql_backends_monet5_UDF_capi"
+# 14:43:45 >  
+
+# MonetDB 5 server v11.28.0
+# This is an unreleased version
+# Serving database 'mTests_sql_backends_monet5_UDF_capi', using 4 threads
+# Compiled for x86_64-apple-darwin15.6.0/64bit with 128bit integers
+# Found 8.000 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2017 MonetDB B.V., all rights reserved
+# Visit https://www.monetdb.org/ for further information
+# Listening for connection requests on 
mapi:monetdb://dhcp-21.eduroam.cwi.nl:35341/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-91643/.s.monetdb.35341
+# MonetDB/SQL module loaded
+
+Ready.
+# SQL catalog created, loading sql scripts once
+# loading sql script: 09_like.sql
+# loading sql script: 10_math.sql
+# loading sql script: 11_times.sql
+# loading sql script: 12_url.sql
+# loading sql script: 13_date.sql
+# loading sql script: 14_inet.sql
+# loading sql script: 15_querylog.sql
+# loading sql script: 16_tracelog.sql
+# loading sql script: 17_temporal.sql
+# loading sql script: 18_index.sql
+# loading sql script: 20_vacuum.sql
+# loading sql script: 21_dependency_functions.sql
+# loading sql script: 22_clients.sql
+# loading sql script: 23_skyserver.sql
+# loading sql script: 25_debug.sql
+# loading sql script: 26_sysmon.sql
+# loading sql script: 27_rejects.sql
+# loading sql script: 39_analytics.sql
+# loading sql script: 39_analytics_hge.sql
+# loading sql script: 40_json.sql
+# loading sql script: 40_json_hge.sql
+# loading sql script: 41_md5sum.sql
+# loading sql script: 45_uuid.sql
+# loading sql script: 46_profiler.sql
+# loading sql script: 51_sys_schema_extension.sql
+# loading sql script: 60_wlcr.sql
+# loading sql script: 72_fits.sql
+# loading sql script: 74_netcdf.sql
+# loading sql script: 75_storagemodel.sql
+# loading sql script: 80_statistics.sql
+# loading sql script: 80_udf.sql
+# loading sql script: 80_udf_hge.sql
+# loading sql script: 90_generator.sql
+# loading sql script: 90_generator_hge.sql
+# loading sql script: 99_system.sql
+
+# 14:43:46 >  
+# 14:43:46 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-91643" "--port=35341"
+# 14:43:46 >  
+
+#START TRANSACTION;
+#CREATE AGGREGATE capi13(inp INTEGER) RETURNS BIGINT LANGUAGE C {
+##include <string.h>
+#      // initialize one aggregate per group
+#      result->initialize(result, aggr_group.count);
+#      // zero initialize the sums
+#      memset(result->data, 0, result->count * sizeof(result->null_value));
+#      // gather the sums for each of the groups
+#      for(size_t i = 0; i < inp.count; i++) {
+#              result->data[aggr_group.data[i]] += inp.data[i];
+#      }
+#};
+#CREATE TABLE vals(grp INTEGER, value INTEGER);
+#INSERT INTO vals VALUES (1, 100), (2, 200), (1, 50), (2, 300);
+[ 4    ]
+#SELECT grp, capi13(value) FROM vals GROUP BY grp;
+% sys.vals,    sys.L4 # table_name
+% grp, L3 # name
+% int, bigint # type
+% 1,   3 # length
+[ 1,   150     ]
+[ 2,   500     ]
+#SELECT capi13(value) FROM vals;
+% sys.L4 # table_name
+% L3 # name
+% bigint # type
+% 3 # length
+[ 650  ]
+#DROP AGGREGATE capi13;
+#DROP TABLE vals;
+#ROLLBACK;
+
+# 14:43:47 >  
+# 14:43:47 >  "Done."
+# 14:43:47 >  
+
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
@@ -270,7 +270,7 @@ GENERATE_BASE_HEADERS(cudf_data_time, ti
 GENERATE_BASE_HEADERS(cudf_data_timestamp, timestamp);
 GENERATE_BASE_HEADERS(cudf_data_blob, blob);
 
-#define GENERATE_BAT_INPUT_BASE(b, tpe)                                        
\
+#define GENERATE_BAT_INPUT_BASE(tpe)                                           
\
        struct cudf_data_struct_##tpe *bat_data =                               
   \
                GDKmalloc(sizeof(struct cudf_data_struct_##tpe));               
       \
        if (!bat_data) {                                                        
   \
@@ -285,7 +285,7 @@ GENERATE_BASE_HEADERS(cudf_data_blob, bl
 #define GENERATE_BAT_INPUT(b, tpe)                                             
\
        {                                                                       
   \
                char *mprotect_retval;                                          
       \
-               GENERATE_BAT_INPUT_BASE(b, tpe);                                
       \
+               GENERATE_BAT_INPUT_BASE(tpe);                                   
       \
                bat_data->count = BATcount(b);                                  
       \
                bat_data->null_value = tpe##_nil;                               
       \
                if (b->tdense && !b->tnodense) { \
@@ -421,6 +421,8 @@ static str CUDFeval(Client cntxt, MalBlk
        cached_functions* cached_function;
        char* function_parameters = NULL;
        int tid = THRgettid();
+       size_t input_size = 0;
+       bit non_grouped_aggregate = 0;
 
        (void)cntxt;
 
@@ -517,6 +519,10 @@ static str CUDFeval(Client cntxt, MalBlk
                        }
                }
        }
+       // non-grouped aggregates don't have the group list
+       // to allow users to write code for both grouped and non-grouped 
aggregates
+       // we create an "aggr_group" BAT for non-grouped aggregates
+       non_grouped_aggregate = grouped && !seengrp;
 
        input_count = pci->argc - (pci->retc + ARG_OFFSET);
        output_count = pci->retc;
@@ -741,6 +747,19 @@ static str CUDFeval(Client cntxt, MalBlk
                                }
                        }
                }
+               if (non_grouped_aggregate) {
+                       // manually add "aggr_group" for non-grouped aggregates
+                       int bat_type = TYPE_oid;
+                       const char *tpe = GetTypeName(bat_type);
+                       assert(tpe);
+                       if (tpe) {
+                               snprintf(buf, sizeof(buf),
+                                                "%s%s %s = 
*((%s%s*)__inputs[%zu]);\n", struct_prefix,
+                                                tpe, "aggr_group", 
struct_prefix, tpe,
+                                                input_count);
+                               ATTEMPT_TO_WRITE_TO_FILE(f, buf);
+                       }
+               }
                // output types
                for (i = 0; i < (size_t)pci->retc; i++) {
                        int bat_type = getBatType(getArgType(mb, pci, i));
@@ -849,11 +868,11 @@ static str CUDFeval(Client cntxt, MalBlk
                        MT_lock_unset(&cache_lock);
                }
        }
-
-       // now create the actual input/output parameters from the input bats
        if (input_count > 0) {
-               input_bats = GDKzalloc(sizeof(BAT *) * input_count);
-               inputs = GDKzalloc(sizeof(void *) * input_count);
+               // add "aggr_group" for non-grouped aggregates
+               size_t extra_inputs = non_grouped_aggregate ? 1 : 0;
+               input_bats = GDKzalloc(sizeof(BAT *) * (input_count + 
extra_inputs));
+               inputs = GDKzalloc(sizeof(void *) * (input_count + 
extra_inputs));
                if (!inputs || !input_bats) {
                        msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
                        goto wrapup;
@@ -934,7 +953,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                BATiter li;
                                BUN p = 0, q = 0;
                                str mprotect_retval;
-                               GENERATE_BAT_INPUT_BASE(input_bats[index], str);
+                               GENERATE_BAT_INPUT_BASE(str);
                                bat_data->count = BATcount(input_bats[index]);
                                bat_data->data = GDKmalloc(sizeof(char *) * 
bat_data->count);
                                bat_data->null_value = NULL;
@@ -968,7 +987,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                }
                        } else if (bat_type == TYPE_date) {
                                date *baseptr;
-                               GENERATE_BAT_INPUT_BASE(input_bats[index], 
date);
+                               GENERATE_BAT_INPUT_BASE(date);
                                bat_data->count = BATcount(input_bats[index]);
                                bat_data->data =
                                        GDKmalloc(sizeof(bat_data->null_value) 
* bat_data->count);
@@ -984,7 +1003,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                data_from_date(date_nil, &bat_data->null_value);
                        } else if (bat_type == TYPE_daytime) {
                                daytime *baseptr;
-                               GENERATE_BAT_INPUT_BASE(input_bats[index], 
time);
+                               GENERATE_BAT_INPUT_BASE(time);
                                bat_data->count = BATcount(input_bats[index]);
                                bat_data->data =
                                        GDKmalloc(sizeof(bat_data->null_value) 
* bat_data->count);
@@ -1000,7 +1019,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                data_from_time(daytime_nil, 
&bat_data->null_value);
                        } else if (bat_type == TYPE_timestamp) {
                                timestamp *baseptr;
-                               GENERATE_BAT_INPUT_BASE(input_bats[index], 
timestamp);
+                               GENERATE_BAT_INPUT_BASE(timestamp);
                                bat_data->count = BATcount(input_bats[index]);
                                bat_data->data =
                                        GDKmalloc(sizeof(bat_data->null_value) 
* bat_data->count);
@@ -1018,7 +1037,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                BATiter li;
                                BUN p = 0, q = 0;
                                str mprotect_retval;
-                               GENERATE_BAT_INPUT_BASE(input_bats[index], 
blob);
+                               GENERATE_BAT_INPUT_BASE(blob);
                                bat_data->count = BATcount(input_bats[index]);
                                bat_data->data = 
GDKmalloc(sizeof(cudf_data_blob) * bat_data->count);
                                if (!bat_data->data) {
@@ -1058,7 +1077,7 @@ static str CUDFeval(Client cntxt, MalBlk
                                BATiter li;
                                BUN p = 0, q = 0;
                                str mprotect_retval;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to