Changeset: 1572a42868fa for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1572a42868fa
Modified Files:
        sql/backends/monet5/sql_statistics.c
Branch: Jun2016
Log Message:

Create and use a variant of strToStr to convert value for inclusion in SQL 
query.
This fixes bug 4021.


diffs (67 lines):

diff --git a/sql/backends/monet5/sql_statistics.c 
b/sql/backends/monet5/sql_statistics.c
--- a/sql/backends/monet5/sql_statistics.c
+++ b/sql/backends/monet5/sql_statistics.c
@@ -21,6 +21,37 @@ analysis by optimizers.
 #include "sql_statistics.h"
 #include "sql_scenario.h"
 
+#define atommem(TYPE, size)                                    \
+       do {                                                    \
+               if (*dst == NULL || *len < (int) (size)) {      \
+                       GDKfree(*dst);                          \
+                       *len = (size);                          \
+                       *dst = (TYPE *) GDKmalloc(*len);        \
+                       if (*dst == NULL)                       \
+                               return -1;                      \
+               }                                               \
+       } while (0)
+
+static int
+strToStrSQuote(char **dst, int *len, const void *src)
+{
+       int l = 0;
+
+       if (GDK_STRNIL((str) src)) {
+               atommem(char, 4);
+
+               return snprintf(*dst, *len, "nil");
+       } else {
+               int sz = escapedStrlen(src, NULL, NULL, '\'');
+               atommem(char, sz + 3);
+               l = escapedStr((*dst) + 1, src, *len - 1, NULL, NULL, '\'');
+               l++;
+               (*dst)[0] = (*dst)[l++] = '"';
+               (*dst)[l] = 0;
+       }
+       return l;
+}
+
 str
 sql_analyze(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
@@ -86,10 +117,13 @@ sql_analyze(Client cntxt, MalBlkPtr mb, 
                                                BAT *bn = 
store_funcs.bind_col(tr, c, RDONLY), *br;
                                                BAT *bsample;
                                                lng sz = BATcount(bn);
-                                               int (*tostr)(str*,int*,const 
void*) = BATatoms[bn->ttype].atomToStr; \
+                                               int (*tostr)(str*,int*,const 
void*) = BATatoms[bn->ttype].atomToStr;
                                                int len = 0;
                                                void *val=0;
 
+                                               if (tostr == 
BATatoms[TYPE_str].atomToStr)
+                                                       tostr = strToStrSQuote;
+
                                                if (col && strcmp(bc->name, 
col))
                                                        continue;
                                                snprintf(dquery, 8192, "delete 
from sys.statistics where \"column_id\" = %d;", c->base.id);
@@ -132,8 +166,8 @@ sql_analyze(Client cntxt, MalBlkPtr mb, 
                                                        tostr(&minval, 
&len,val); 
                                                        GDKfree(val);
                                                } else {
-                                                       maxval = (char *) 
GDKzalloc(4);
-                                                       minval = (char *) 
GDKzalloc(4);
+                                                       maxval = GDKmalloc(4);
+                                                       minval = GDKmalloc(4);
                                                        snprintf(maxval, 4, 
"nil");
                                                        snprintf(minval, 4, 
"nil");
                                                }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to