Changeset: b79677c1d2a9 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=b79677c1d2a9
Modified Files:
        sql/backends/monet5/sql.h
        sql/backends/monet5/sql.mal
        sql/backends/monet5/sql_bat2time.c
Branch: datetrunc
Log Message:

Temporary branch to realise date_trunc


diffs (151 lines):

diff --git a/sql/backends/monet5/sql.h b/sql/backends/monet5/sql.h
--- a/sql/backends/monet5/sql.h
+++ b/sql/backends/monet5/sql.h
@@ -248,6 +248,9 @@ sql5_export str batnil_2_timestamp(bat *
 sql5_export str str_2_timestamp(timestamp *res, const str *val);
 sql5_export str batstr_2_timestamp(bat *res, const bat *val);
 
+sql5_export str bat_date_trunc(bat *res, const str *scale, const bat *v);
+sql5_export str date_trunc(lng *res, const str *scale, const lng *v);
+
 sql5_export str nil_2_daytime(daytime *res, const void *val);
 sql5_export str batnil_2_daytime(bat *res, const bat *val);
 sql5_export str str_2_daytime(daytime *res, const str *val);
diff --git a/sql/backends/monet5/sql.mal b/sql/backends/monet5/sql.mal
--- a/sql/backends/monet5/sql.mal
+++ b/sql/backends/monet5/sql.mal
@@ -912,6 +912,18 @@ command batcalc.daytime( v:bat[:daytime]
 address batdaytime_2time_daytime
 comment "cast daytime to daytime and check for overflow";
 
+command sql.date_trunc( scale:str, v:bat[:daytime]):bat[:daytime] 
+address bat_date_trunc
+comment "Truncate a timestamp to (millenium, 
century,decade,year,quarter,month,week,day,hour,minute,second, 
milliseconds,microseconds)";
+command sql.date_trunc( scale:str, v:bat[:timestamp]):bat[:timestamp] 
+address bat_date_trunc;
+
+command sql.date_trunc( scale:str, v:daytime):daytime 
+address date_trunc
+comment "Truncate a timestamp to (millenium, 
century,decade,year,quarter,month,week,day,hour,minute,second, 
milliseconds,microseconds)";
+command sql.date_trunc( scale:str, v:timestamp):timestamp 
+address date_trunc;
+
 pattern current_time() :daytime
 address SQLcurrent_daytime
 comment "Get the clients current daytime";
diff --git a/sql/backends/monet5/sql_bat2time.c 
b/sql/backends/monet5/sql_bat2time.c
--- a/sql/backends/monet5/sql_bat2time.c
+++ b/sql/backends/monet5/sql_bat2time.c
@@ -276,3 +276,112 @@ batnil_2time_daytime(bat *res, const bat
        BBPunfix(b->batCacheid);
        return msg;
 }
+
+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;
+}
+
+#define date_trunc_loop(NAME, TYPE, DIVISOR)   \
+       if  ( strcmp(*scale, NAME) == 0){ \
+               for( ; lo < hi; lo++)           \
+                       if (is_lng_nil(bt[lo])) {               \
+                                       dt[lo] = lng_nil;               \
+                                       nils++;         \
+                       } else                          \
+                               dt[lo] = (bt[lo] / DIVISOR) * DIVISOR; \
+       }
+
+str
+bat_date_trunc(bat *res, const str *scale, const bat *bid)
+{
+       BAT *b, *bn;
+       oid lo, hi;
+       lng *bt;
+       lng *dt;
+       char *msg = NULL;
+       lng nils = 0;
+
+       if ( truncate_check(scale) == 0)
+               throw(SQL, "batcalc.truncate_timestamp", SQLSTATE(HY005) 
"Improper directive ");
+
+       if ((b = BATdescriptor(*bid)) == NULL) {
+               throw(SQL, "batcalc.truncate_timestamp", SQLSTATE(HY005) 
"Cannot access column descriptor");
+       }
+       bn = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT);
+       if (bn == NULL) {
+               BBPunfix(b->batCacheid);
+               throw(SQL, "sql.truncate", SQLSTATE(HY001) MAL_MALLOC_FAIL);
+       }
+
+       bt = (lng *) Tloc(b, 0);
+       dt = (lng *) Tloc(bn, 0);
+
+       lo = 0;
+       hi = lo + BATcount(b);
+
+       date_trunc_loop("microseconds", TIMESTAMP, 1)
+       date_trunc_loop("milliseconds", TIMESTAMP, 1000)
+       date_trunc_loop("seconds", TIMESTAMP, (1000 * 60))
+       date_trunc_loop("minute", TIMESTAMP, (1000 * 60 * 60))
+       date_trunc_loop("hour", TIMESTAMP, (1000 * 60 * 60 * 24))
+
+       // week
+       // quarter
+       // decade
+       // century
+       // millenium
+       if( nils){
+               bn->tnonil = false;  
+               bn->tnil = true;     
+               bn->tsorted = false;     
+               bn->trevsorted = false;  
+               bn->tkey = false;    
+       }
+       BATsetcount(bn, (BUN) lo);
+       BBPkeepref(*res = bn->batCacheid);
+       BBPunfix(b->batCacheid);
+       return msg;
+}
+
+#define date_trunc_single(NAME, TYPE, DIVISOR)         \
+       if  ( strcmp(*scale, NAME) == 0){ \
+               if (is_lng_nil(*bt)) {                  \
+                       *dt = lng_nil;                  \
+               } else                          \
+                       *dt = (*bt / DIVISOR) * DIVISOR; \
+       }
+
+str
+date_trunc(lng *dt, const str *scale, const lng *bt)
+{
+       str msg = MAL_SUCCEED;
+
+       if (truncate_check(scale) == 0)
+               throw(SQL, "sql.truncate", SQLSTATE(HY001) "NYI");      
+
+       date_trunc_single("microseconds", TIMESTAMP, 1)
+       date_trunc_single("milliseconds", TIMESTAMP, 1000)
+       date_trunc_single("seconds", TIMESTAMP, (1000 * 60))
+       date_trunc_single("minute", TIMESTAMP, (1000 * 60 * 60))
+       date_trunc_single("hour", TIMESTAMP, (1000 * 60 * 60 * 24))
+       // week
+       // quarter
+       // decade
+       // century
+       // millenium
+       return msg;
+}
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to