LCOV - code coverage report
Current view: top level - monetdb5/mal - mal_type.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 75 75 100.0 %
Date: 2021-09-14 22:17:06 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * (c) M. Kersten
      11             :  * MAL Type System
      12             :  * The MAL type module overloads the atom structure managed in the GDK library.
      13             :  * For the time being, we assume GDK to support at most 127 different atomic types.
      14             :  * Type composition is limited to the  builtin scalar type and a column type.
      15             :  * Furthermore, the polymorphic MAL type :any can be qualified
      16             :  * with a type variable index :any_I, where I is a digit (1-9).
      17             :  * BEWARE, the TYPE_any is a speudo type known within MAL only.
      18             :  *
      19             :  * Within the MAL layer types are encoded in 32-bit integers using
      20             :  * bit stuffing to save some space.
      21             :  * The integer contains the following fields:
      22             :  * anyHeadIndex (bit 25-22), anyTypeIndex (bit 21-18),
      23             :  * batType (bit 17) headType (16-9) and tailType(8-0)
      24             :  * This encoding scheme permits a limited number of different bat types.
      25             :  * The headless case assumes all head types are TYPE_void/TYPE_oid
      26             :  */
      27             : #include "monetdb_config.h"
      28             : #include "mal_type.h"
      29             : 
      30             : /*
      31             :  * At any point we should be able to construct an ascii representation of
      32             :  * the type descriptor. Including the variable references.
      33             :  */
      34             : str
      35      532281 : getTypeName(malType tpe)
      36             : {
      37             :         char buf[FILENAME_MAX];
      38             :         int k;
      39             : 
      40      532281 :         if (tpe == TYPE_any)
      41        6318 :                 return GDKstrdup("any");
      42      525963 :         if (isaBatType(tpe)) {
      43      316012 :                 k = getTypeIndex(tpe);
      44      316012 :                 if (k)
      45       10030 :                         snprintf(buf, sizeof(buf), "bat[:any_%d]",  k);
      46      305982 :                 else if (getBatType(tpe) == TYPE_any)
      47         540 :                         snprintf(buf, sizeof(buf), "bat[:any]");
      48             :                 else
      49      305442 :                         snprintf(buf, sizeof(buf), "bat[:%s]", ATOMname(getBatType(tpe)));
      50      316016 :                 return GDKstrdup(buf);
      51             :         }
      52      209951 :         if (isAnyExpression(tpe)) {
      53        2770 :                 snprintf(buf, sizeof(buf), "any_%d", getTypeIndex(tpe));
      54        2770 :                 return GDKstrdup(buf);
      55             :         }
      56      207181 :         return GDKstrdup(ATOMname(tpe));
      57             : }
      58             : /*
      59             :  * It might be handy to encode the type information in an identifier
      60             :  * string for ease of comparison later.
      61             :  */
      62             : str
      63        1832 : getTypeIdentifier(malType tpe){
      64             :         str s,t,v;
      65        1832 :         s= getTypeName(tpe);
      66        1835 :         if (s == NULL)
      67             :                 return NULL;
      68        9653 :         for ( t=s; *t; t++)
      69        7818 :                 if ( !isalnum((unsigned char) *t) )
      70        1158 :                         *t='_';
      71             :         t--;
      72        1835 :         if (*t == '_')
      73         387 :                 *t = 0;
      74        7431 :         for (v=s, t=s+1; *t; t++){
      75        5596 :                 if (  !(*t == '_' && *v == '_' ) )
      76        5210 :                         *++v = *t;
      77             :         }
      78        1835 :         *++v =0;
      79        1835 :         return s;
      80             : }
      81             : 
      82             : 
      83             : /*
      84             :  * In many places we need a confirmed type identifier.
      85             :  * GDK returns the next available index when it can not find the type.
      86             :  * This is not sufficient here, an error message may have to be generated.
      87             :  * It is assumed that the type table does not change in the mean time.
      88             :  * Use the information that identifiers are at least one character
      89             :  * and are terminated by a null to speedup comparison
      90             :  */
      91             : 
      92             : /*
      93             :  * The ATOMindex routine is pretty slow, because it performs a
      94             :  * linear search through the type table. This code should actually
      95             :  * be integrated with the kernel.
      96             :  */
      97             : #define qt(x) (nme[1]==x[1] && nme[2]==x[2] )
      98             : 
      99             : int
     100     5045410 : getAtomIndex(const char *nme, size_t len, int deftype)
     101             : {
     102             :         int i;
     103             : 
     104     5045410 :         if (len >= IDLENGTH) {
     105             :                 /* name too long: cannot match any atom name */
     106             :                 return deftype;
     107             :         }
     108     5045406 :         if (len == 3)
     109     4797595 :                 switch (*nme) {
     110          50 :                 case 'a':
     111          50 :                         if (qt("any"))
     112             :                                 return TYPE_any;
     113             :                         break;
     114      878330 :                 case 'b':
     115      878330 :                         if (qt("bat"))
     116             :                                 return TYPE_bat;
     117      877656 :                         if (qt("bit"))
     118             :                                 return TYPE_bit;
     119      345542 :                         if (qt("bte"))
     120             :                                 return TYPE_bte;
     121             :                         break;
     122      474126 :                 case 'd':
     123      474126 :                         if (qt("dbl"))
     124             :                                 return TYPE_dbl;
     125             :                         break;
     126      869559 :                 case 'i':
     127      869559 :                         if (qt("int"))
     128             :                                 return TYPE_int;
     129             :                         break;
     130      321573 :                 case 'f':
     131      321573 :                         if (qt("flt"))
     132             :                                 return TYPE_flt;
     133             :                         break;
     134      456459 :                 case 'l':
     135      456459 :                         if (qt("lng"))
     136             :                                 return TYPE_lng;
     137             :                         break;
     138       17838 :                 case 'm':
     139       17838 :                         if (qt("msk"))
     140             :                                 return TYPE_msk;
     141             :                         break;
     142        3936 :                 case 'p':
     143        3936 :                         if (qt("ptr"))
     144             :                                 return TYPE_ptr;
     145             :                         break;
     146             : #ifdef HAVE_HGE
     147      324527 :                 case 'h':
     148      324527 :                         if (qt("hge"))
     149             :                                 return TYPE_hge;
     150             :                         break;
     151             : #endif
     152      497195 :                 case 'o':
     153      497195 :                         if (qt("oid"))
     154             :                                 return TYPE_oid;
     155             :                         break;
     156      859317 :                 case 's':
     157      859317 :                         if (qt("str"))
     158             :                                 return TYPE_str;
     159      320262 :                         if (qt("sht"))
     160             :                                 return TYPE_sht;
     161             :                         break;
     162             :                 }
     163      247811 :         else if (len == 4 && strncmp(nme, "void", len) == 0)
     164             :                 return TYPE_void;
     165      189890 :         else if (len == 4 && strncmp(nme, "date", len) == 0)
     166             :                 return TYPE_date;
     167      150521 :         else if (len == 7 && strncmp(nme, "daytime", len) == 0)
     168             :                 return TYPE_daytime;
     169      120993 :         else if (len == 9 && strncmp(nme, "timestamp", len) == 0)
     170             :                 return TYPE_timestamp;
     171       65575 :         else if (len == 4 && strncmp(nme, "uuid", len) == 0)
     172             :                 return TYPE_uuid;
     173     1626289 :         for (i = TYPE_str; i < GDKatomcnt; i++)
     174     1625911 :                 if (BATatoms[i].name[0] == nme[0] &&
     175      177190 :                         strncmp(nme, BATatoms[i].name, len) == 0 &&
     176      162823 :                         BATatoms[i].name[len] == 0)
     177      162823 :                         return i;
     178             :         return deftype;
     179             : }
     180             : 
     181             : inline int
     182    26447698 : findGDKtype(int type)
     183             : {
     184    26447698 :         if (type == TYPE_any || type== TYPE_void)
     185             :                 return TYPE_void;
     186    26447698 :         if (isaBatType(type))
     187             :                 return TYPE_bat;
     188        4519 :         return ATOMtype(type);
     189             : }
     190             : 
     191             : int
     192         196 : isIdentifier(str s)
     193             : {
     194         196 :         if (!isalpha((unsigned char) *s))
     195             :                 return -1;
     196        3779 :         for (; s && *s; s++)
     197        3583 :                 if (!isalnum((unsigned char) *s) && *s != '_')
     198             :                         return -1;
     199             :         return 0;
     200             : }

Generated by: LCOV version 1.14