Changeset: aa2aba3de7ea for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=aa2aba3de7ea
Modified Files:
        gdk/Makefile.ag
        gdk/gdk.h
        gdk/gdk_cast.c
        monetdb5/modules/kernel/Makefile.ag
        monetdb5/modules/kernel/colcast.c
        monetdb5/modules/kernel/colcast.c.mx
        monetdb5/modules/kernel/colcast.h
        monetdb5/modules/kernel/colcast.h.mx
        monetdb5/modules/kernel/colcast.mal.mx
Branch: headless
Log Message:

Moved "colcast" into gdk.
There is now a new function in gdk, COLconvert(COL *col, int type, int
overflow_is_error), which returns a new COL which is a copy of the
input COL, except it converts the type of the data.  It does overflow
checking during conversion and, depending on the last arg, returns an
error or fills in a nil.
M5 now uses this implementation.


diffs (truncated from 894 to 300 lines):

diff --git a/gdk/Makefile.ag b/gdk/Makefile.ag
--- a/gdk/Makefile.ag
+++ b/gdk/Makefile.ag
@@ -27,6 +27,7 @@
        VERSION = $(GDK_VERSION)
        NAME = bat
        SOURCES = \
+               gdk_cast.c \
                gdk_colop.c \
                gdk_search.c \
                gdk_setop.c gdk_utils.c gdk_atoms.c \
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -1650,6 +1650,7 @@
 gdk_export COL *COLcopy(COL *b, int tp, int writeable);
 gdk_export COL *COLmark_grp(COL *b, COL *g, COL *c, oid *base);
 gdk_export COL *COLgroup(COL *b, oid start, int incr, int grpsize);
+gdk_export COL *COLconvert(COL *b, int tp, int return_null_on_error);
 
 /*
  *
diff --git a/gdk/gdk_cast.c b/gdk/gdk_cast.c
new file mode 100644
--- /dev/null
+++ b/gdk/gdk_cast.c
@@ -0,0 +1,399 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2011 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+#include "monetdb_config.h"
+#include "gdk.h"
+#include "gdk_private.h"
+
+#define convertimpl_enlarge(TYPE1, TYPE2)                      \
+static oid                                                     \
+convert_##TYPE1##_##TYPE2(TYPE1 *src, TYPE2 *dst, oid cnt)     \
+{                                                              \
+       oid nils = 0;                                           \
+                                                               \
+       while (cnt-- > 0) {                                     \
+               if (*src == TYPE1##_nil) {                      \
+                       *dst = TYPE2##_nil;                     \
+                       nils++;                                 \
+               } else                                          \
+                       *dst = (TYPE2) *src;                    \
+               src++;                                          \
+               dst++;                                          \
+       }                                                       \
+       return nils;                                            \
+}
+
+#define convertimpl_reduce(TYPE1, TYPE2)                       \
+static oid                                                     \
+convert_##TYPE1##_##TYPE2(TYPE1 *src, TYPE2 *dst, oid cnt,     \
+                         int overflow_is_error)                \
+{                                                              \
+       oid nils = 0;                                           \
+                                                               \
+       while (cnt-- > 0) {                                     \
+               if (*src == TYPE1##_nil) {                      \
+                       *dst = TYPE2##_nil;                     \
+                       nils++;                                 \
+               } else if (*src < (TYPE1) GDK_##TYPE2##_min ||  \
+                          *src > (TYPE1) GDK_##TYPE2##_max) {  \
+                       if (overflow_is_error)                  \
+                               return BUN_NONE;                \
+                       *dst = TYPE2##_nil;                     \
+                       nils++;                                 \
+               } else                                          \
+                       *dst = (TYPE2) *src;                    \
+               src++;                                          \
+               dst++;                                          \
+       }                                                       \
+       return nils;                                            \
+}
+
+#define convert2bit_impl(TYPE)                         \
+static oid                                             \
+convert_##TYPE##_bit(TYPE *src, bte *dst, oid cnt)     \
+{                                                      \
+       oid nils = 0;                                   \
+                                                       \
+       while (cnt-- > 0) {                             \
+               if (*src == TYPE##_nil) {               \
+                       *dst = bit_nil;                 \
+                       nils++;                         \
+               } else                                  \
+                       *dst = (bte) (*src != 0);       \
+               src++;                                  \
+               dst++;                                  \
+       }                                               \
+       return nils;                                    \
+}
+
+convertimpl_enlarge(bte, sht)
+convertimpl_enlarge(bte, int)
+convertimpl_enlarge(bte, lng)
+convertimpl_enlarge(bte, flt)
+convertimpl_enlarge(bte, dbl)
+
+convertimpl_reduce(sht, bte)
+convertimpl_enlarge(sht, int)
+convertimpl_enlarge(sht, lng)
+convertimpl_enlarge(sht, flt)
+convertimpl_enlarge(sht, dbl)
+
+convertimpl_reduce(int, bte)
+convertimpl_reduce(int, sht)
+convertimpl_enlarge(int, lng)
+convertimpl_enlarge(int, flt)
+convertimpl_enlarge(int, dbl)
+
+convertimpl_reduce(lng, bte)
+convertimpl_reduce(lng, sht)
+convertimpl_reduce(lng, int)
+convertimpl_enlarge(lng, flt)
+convertimpl_enlarge(lng, dbl)
+
+convertimpl_reduce(flt, bte)
+convertimpl_reduce(flt, sht)
+convertimpl_reduce(flt, int)
+convertimpl_reduce(flt, lng)
+convertimpl_enlarge(flt, dbl)
+
+convertimpl_reduce(dbl, bte)
+convertimpl_reduce(dbl, sht)
+convertimpl_reduce(dbl, int)
+convertimpl_reduce(dbl, lng)
+convertimpl_reduce(dbl, flt)
+
+convert2bit_impl(bte)
+convert2bit_impl(sht)
+convert2bit_impl(int)
+convert2bit_impl(lng)
+convert2bit_impl(flt)
+convert2bit_impl(dbl)
+
+static oid
+convert_any_str(int tp, void *src, COL *bn, oid cnt)
+{
+       str dst = 0;
+       int len = 0;
+       oid nils = 0;
+       void *nil = ATOMnilptr(tp);
+       int (*atomtostr)(str *, int *, ptr) = COLatoms[tp].atomToStr;
+
+       while (cnt-- > 0) {
+               (*atomtostr)(&dst, &len, src);
+               if (ATOMcmp(tp, src, nil) == 0)
+                       nils++;
+               bunfastins_nocheck_str(bn, bn->count, dst);
+       }
+       if (dst)
+               GDKfree(dst);
+       return nils;
+  bunins_failed:
+       if (dst)
+               GDKfree(dst);
+       return BUN_NONE;
+}
+
+static oid
+convert_str_any(COL *b, int tp, void *dst, int overflow_is_error)
+{
+       oid i;
+       oid nils = 0;
+       void *nil = ATOMnilptr(tp);
+       char *s;
+       ptr d;
+       int len = ATOMsize(tp);
+       int (*atomfromstr)(str, int *, ptr *) = COLatoms[tp].atomFromStr;
+
+       COLforloop(b, i) {
+               s = COLget_str(b, i);
+               d = dst;
+               if ((*atomfromstr)(s, &len, &d) == 0) {
+                       if (overflow_is_error)
+                               return BUN_NONE;
+                       memcpy(dst, nil, len);
+               }
+               assert(len == ATOMsize(tp));
+               if (ATOMcmp(tp, dst, nil) == 0)
+                       nils++;
+               dst = (void *) ((char *) dst + len);
+       }
+       return nils;
+}
+
+COL *
+COLconvert(COL *b, int tp, int overflow_is_error)
+{
+       COL *bn;
+       void *src, *dst;
+       oid nils = BUN_NONE;    /* in case no conversion defined */
+
+       assert(ATOMstorage(TYPE_wrd) == ATOMstorage(TYPE_int) ||
+              ATOMstorage(TYPE_wrd) == ATOMstorage(TYPE_lng));
+       assert(ATOMstorage(TYPE_oid) == ATOMstorage(TYPE_int) ||
+              ATOMstorage(TYPE_oid) == ATOMstorage(TYPE_lng));
+
+       COLcheck(b, "COLconvert");
+       if (tp == TYPE_void)
+               tp = TYPE_oid;
+
+       if (ATOMstorage(b->type) == ATOMstorage(tp) ||
+           (ATOMstorage(b->type) == TYPE_chr && ATOMstorage(tp) == TYPE_bte) ||
+           (ATOMstorage(b->type) == TYPE_bte && ATOMstorage(tp) == TYPE_chr && 
tp != TYPE_bit))
+               return COLcopy(b, tp, 0);
+
+       bn = COLnew(tp, b->count);
+       if (bn == NULL)
+               return NULL;
+
+       src = COLelement(b, b->first);
+       dst = COLelement(bn, bn->first);
+
+       switch (ATOMstorage(b->type)) {
+       case TYPE_chr:
+       case TYPE_bte:
+               switch (ATOMstorage(tp)) {
+               case TYPE_chr:
+                       assert(tp == TYPE_bit);
+                       nils = convert_bte_bit(src, dst, b->count);
+                       break;
+               case TYPE_sht:
+                       nils = convert_bte_sht(src, dst, b->count);
+                       break;
+               case TYPE_int:
+                       nils = convert_bte_int(src, dst, b->count);
+                       break;
+               case TYPE_lng:
+                       nils = convert_bte_lng(src, dst, b->count);
+                       break;
+               case TYPE_flt:
+                       nils = convert_bte_flt(src, dst, b->count);
+                       break;
+               case TYPE_dbl:
+                       nils = convert_bte_dbl(src, dst, b->count);
+                       break;
+               case TYPE_str:
+                       nils = convert_any_str(b->type, src, bn, b->count);
+                       break;
+               }
+               break;
+       case TYPE_sht:
+               switch (ATOMstorage(tp)) {
+               case TYPE_chr:
+               case TYPE_bte:
+                       if (tp == TYPE_bit)
+                               nils = convert_sht_bit(src, dst, b->count);
+                       else
+                               nils = convert_sht_bte(src, dst, b->count,
+                                                      overflow_is_error);
+                       break;
+               case TYPE_int:
+                       nils = convert_sht_int(src, dst, b->count);
+                       break;
+               case TYPE_lng:
+                       nils = convert_sht_lng(src, dst, b->count);
+                       break;
+               case TYPE_flt:
+                       nils = convert_sht_flt(src, dst, b->count);
+                       break;
+               case TYPE_dbl:
+                       nils = convert_sht_dbl(src, dst, b->count);
+                       break;
+               case TYPE_str:
+                       nils = convert_any_str(b->type, src, bn, b->count);
+                       break;
+               }
+               break;
+       case TYPE_int:
+               switch (ATOMstorage(tp)) {
+               case TYPE_chr:
+               case TYPE_bte:
+                       if (tp == TYPE_bit)
+                               nils = convert_int_bit(src, dst, b->count);
+                       else
+                               nils = convert_int_bte(src, dst, b->count,
+                                                      overflow_is_error);
+                       break;
+               case TYPE_sht:
+                       nils = convert_int_sht(src, dst, b->count,
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to