Changeset: 0e25db35dd85 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=0e25db35dd85
Modified Files:
        sql/backends/monet5/sql_datetrunc.c
Branch: Apr2019
Log Message:

Various improvements.


diffs (truncated from 498 to 300 lines):

diff --git a/sql/backends/monet5/sql_datetrunc.c 
b/sql/backends/monet5/sql_datetrunc.c
--- a/sql/backends/monet5/sql_datetrunc.c
+++ b/sql/backends/monet5/sql_datetrunc.c
@@ -10,34 +10,37 @@
 #include "sql.h"
 #include "mal_instruction.h"
 
-#define date_trunc_time_loop(NAME, TYPE, DIVISOR)      \
-       if  ( strcmp(*scale, NAME) == 0){ \
-               for( ; lo < hi; lo++)           \
-                       if (is_timestamp_nil(bt[lo])) {                 \
-                                       dt[lo] = *timestamp_nil;                
\
-                                       nils++;         \
-                       } else {                                \
-                               ts = bt[0];                                     
\
-                               ts.msecs = (int) ((lng)ts.msecs / (lng)DIVISOR) 
* (lng)DIVISOR; \
-                               dt[lo] = ts;                                    
\
-       }               }
+#define date_trunc_time_loop(NAME, DIVISOR)                            \
+       do {                                                            \
+               if  ( strcmp(*scale, NAME) == 0){                       \
+                       for( ; lo < hi; lo++)                           \
+                               if (is_timestamp_nil(bt[lo])) {         \
+                                       dt[lo] = *timestamp_nil;        \
+                               } else {                                \
+                                       ts = bt[0];                     \
+                                       ts.msecs = (ts.msecs / (DIVISOR)) * 
(DIVISOR); \
+                                       dt[lo] = ts;                    \
+                               }                                       \
+               }                                                       \
+       } while (0)
 
-static int truncate_check(const str *scale){
-       (void) scale;
-       return 
-               strcmp(*scale, "millenium") == 0 ||
-               strcmp(*scale, "century") == 0  ||
-               strcmp(*scale, "decade") == 0 ||
-               strcmp(*scale, "year") == 0 ||
-               strcmp(*scale, "quarter" ) == 0 ||
-               strcmp(*scale, "month") == 0 ||
-               strcmp(*scale, "week") == 0 ||
-               strcmp(*scale, "day") == 0  ||
-               strcmp(*scale, "hour") == 0 ||
-               strcmp(*scale, "minute") == 0 ||
-               strcmp(*scale, "second") == 0 ||
-               strcmp(*scale, "milliseconds") == 0 ||
-               strcmp(*scale, "microseconds") == 0;
+static inline bool
+truncate_check(const char *scale)
+{
+       return
+               strcmp(scale, "millenium") == 0 ||
+               strcmp(scale, "century") == 0  ||
+               strcmp(scale, "decade") == 0 ||
+               strcmp(scale, "year") == 0 ||
+               strcmp(scale, "quarter" ) == 0 ||
+               strcmp(scale, "month") == 0 ||
+               strcmp(scale, "week") == 0 ||
+               strcmp(scale, "day") == 0  ||
+               strcmp(scale, "hour") == 0 ||
+               strcmp(scale, "minute") == 0 ||
+               strcmp(scale, "second") == 0 ||
+               strcmp(scale, "milliseconds") == 0 ||
+               strcmp(scale, "microseconds") == 0;
 }
 
 str
@@ -45,14 +48,13 @@ bat_date_trunc(bat *res, const str *scal
 {
        BAT *b, *bn;
        oid lo, hi;
-       timestamp *bt;
+       const timestamp *bt;
        timestamp *dt;
        char *msg = NULL;
-       lng nils = 0;
        timestamp ts;
        int dow, y, m, d, one = 1;
 
-       if ( truncate_check(scale) == 0)
+       if ( truncate_check(*scale) == 0)
                throw(SQL, "batcalc.truncate_timestamp", SQLSTATE(HY005) 
"Improper directive ");
 
        if ((b = BATdescriptor(*bid)) == NULL) {
@@ -64,140 +66,146 @@ bat_date_trunc(bat *res, const str *scal
                throw(SQL, "sql.truncate", SQLSTATE(HY001) MAL_MALLOC_FAIL);
        }
 
-       bt = (timestamp *) Tloc(b, 0);
+       bt = (const timestamp *) Tloc(b, 0);
        dt = (timestamp *) Tloc(bn, 0);
 
        lo = 0;
        hi = lo + BATcount(b);
 
-       date_trunc_time_loop("microseconds", TIMESTAMP, 1)
-       date_trunc_time_loop("milliseconds", TIMESTAMP, 1)
-       date_trunc_time_loop("second", TIMESTAMP, (1000 ))
-       date_trunc_time_loop("minute", TIMESTAMP, (1000 * 60))
-       date_trunc_time_loop("hour", TIMESTAMP, (1000 * 60 * 24))
+       date_trunc_time_loop("microseconds", 1);
+       date_trunc_time_loop("milliseconds", 1);
+       date_trunc_time_loop("second", 1000);
+       date_trunc_time_loop("minute", 1000 * 60);
+       date_trunc_time_loop("hour", 1000 * 60 * 24);
 
-       if  ( strcmp(*scale, "day") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "day") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "week") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "week") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                MTIMEdate_extract_dayofweek(&dow, &ts.days);
                                d =  d - dow - 1;
                                MTIMEdate_create(&ts.days, &y, &m, &d);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "month") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "month") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                MTIMEdate_create(&ts.days, &y, &m, &one);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "quarter") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "quarter") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                m = m/4 + 1;
                                MTIMEdate_create(&ts.days, &y, &one, &one);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "year") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "year") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                MTIMEdate_create(&ts.days, &y, &one, &one);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "decade") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "decade") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                y = (y /10 ) *10;
                                MTIMEdate_create(&ts.days, &y, &one, &one);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "century") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "century") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                y = (y /100 ) *100;
                                MTIMEdate_create(&ts.days, &y, &one, &one);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if  ( strcmp(*scale, "millenium") == 0){ 
-               for( ; lo < hi; lo++)           
-                       if (is_timestamp_nil(bt[lo])) {                 
-                               dt[lo] = *timestamp_nil;                
-                       } else {                                
-                               ts = bt[lo];                                    
+       if  ( strcmp(*scale, "millenium") == 0){
+               for( ; lo < hi; lo++)
+                       if (is_timestamp_nil(bt[lo])) {
+                               dt[lo] = *timestamp_nil;
+                       } else {
+                               ts = bt[lo];
                                ts.msecs = 0;
                                MTIMEdate_extract_ymd(&y, &m, &d, &ts.days);
                                y = (y /1000 ) *1000;
                                MTIMEdate_create(&ts.days, &y, &one, &one);
-                               dt[lo] = ts;                                    
-       }               }
+                               dt[lo] = ts;
+                       }
+       }
 
-       if( nils){
-               bn->tnonil = false;  
-               bn->tnil = true;     
-               bn->tsorted = false;     
-               bn->trevsorted = false;  
-               bn->tkey = false;    
-       }
        BATsetcount(bn, (BUN) lo);
+       /* we can inherit most properties */
+       bn->tnonil = b->tnonil;
+       bn->tnil = b->tnil;
+       bn->tsorted = b->tsorted;
+       bn->trevsorted = b->trevsorted;
+       bn->tkey = false;       /* can't be sure */
        BBPkeepref(*res = bn->batCacheid);
        BBPunfix(b->batCacheid);
        return msg;
 }
 
-#define date_trunc_single_time(NAME, TYPE, DIVISOR)    \
-       if  ( strcmp(*scale, NAME) == 0){ \
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to