Changeset: be3ba6cdfe45 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=be3ba6cdfe45
Added Files:
sql/test/BugTracker-2019/Tests/date_trunc.sql
sql/test/BugTracker-2019/Tests/date_trunc.stable.err
sql/test/BugTracker-2019/Tests/date_trunc.stable.out
Modified Files:
sql/backends/monet5/sql.h
sql/backends/monet5/sql_bat2time.c
sql/scripts/17_temporal.sql
sql/test/BugTracker-2019/Tests/All
Branch: datetrunc
Log Message:
Phase II. get the semantics of date_trunc correct.
To be done, the day part of timestamp
diffs (truncated from 309 to 300 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
@@ -249,7 +249,7 @@ sql5_export str str_2_timestamp(timestam
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 date_trunc(timestamp *res, const str *scale, const timestamp
*v);
sql5_export str nil_2_daytime(daytime *res, const void *val);
sql5_export str batnil_2_daytime(bat *res, const bat *val);
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
@@ -295,25 +295,28 @@ static int truncate_check(const str *sca
strcmp(*scale, "microseconds") == 0;
}
-#define date_trunc_loop(NAME, TYPE, DIVISOR) \
+#define date_trunc_time_loop(NAME, TYPE, DIVISOR) \
if ( strcmp(*scale, NAME) == 0){ \
for( ; lo < hi; lo++) \
- if (is_lng_nil(bt[lo])) { \
- dt[lo] = lng_nil; \
+ if (timestamp_isnil(bt[lo])) { \
+ dt[lo] = *timestamp_nil;
\
nils++; \
- } else \
- dt[lo] = (bt[lo] / DIVISOR) * DIVISOR; \
- }
+ } else { \
+ ts = bt[0];
\
+ ts.msecs = (ts.msecs / DIVISOR) * DIVISOR; \
+ dt[lo] = ts;
\
+ } }
str
bat_date_trunc(bat *res, const str *scale, const bat *bid)
{
BAT *b, *bn;
oid lo, hi;
- lng *bt;
- lng *dt;
+ timestamp *bt;
+ timestamp *dt;
char *msg = NULL;
lng nils = 0;
+ timestamp ts;
if ( truncate_check(scale) == 0)
throw(SQL, "batcalc.truncate_timestamp", SQLSTATE(HY005)
"Improper directive ");
@@ -327,17 +330,17 @@ bat_date_trunc(bat *res, const str *scal
throw(SQL, "sql.truncate", SQLSTATE(HY001) MAL_MALLOC_FAIL);
}
- bt = (lng *) Tloc(b, 0);
- dt = (lng *) Tloc(bn, 0);
+ bt = (timestamp *) Tloc(b, 0);
+ dt = (timestamp *) 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))
+ date_trunc_time_loop("microseconds", TIMESTAMP, 1)
+ date_trunc_time_loop("milliseconds", TIMESTAMP, 1000)
+ date_trunc_time_loop("seconds", TIMESTAMP, (1000 * 60))
+ date_trunc_time_loop("minute", TIMESTAMP, (1000 * 60 * 60))
+ date_trunc_time_loop("hour", TIMESTAMP, (1000 * 60 * 60 * 24))
// week
// quarter
@@ -357,27 +360,30 @@ bat_date_trunc(bat *res, const str *scal
return msg;
}
-#define date_trunc_single(NAME, TYPE, DIVISOR) \
+#define date_trunc_single_time(NAME, TYPE, DIVISOR) \
if ( strcmp(*scale, NAME) == 0){ \
- if (is_lng_nil(*bt)) { \
- *dt = lng_nil; \
- } else \
- *dt = (*bt / DIVISOR) * DIVISOR; \
- }
+ if (timestamp_isnil(*bt)) { \
+ *dt = *timestamp_nil; \
+ } else { \
+ ts = *bt; \
+ ts.msecs = (ts.msecs / DIVISOR) * DIVISOR; \
+ *dt = ts; \
+ } }
str
-date_trunc(lng *dt, const str *scale, const lng *bt)
+date_trunc(timestamp *dt, const str *scale, const timestamp *bt)
{
str msg = MAL_SUCCEED;
+ timestamp ts;
if (truncate_check(scale) == 0)
- throw(SQL, "sql.truncate", SQLSTATE(HY001) "NYI");
+ throw(SQL, "sql.truncate", SQLSTATE(HY001) "Improper directive
");
- 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))
+ date_trunc_single_time("microseconds", TIMESTAMP, 1)
+ date_trunc_single_time("milliseconds", TIMESTAMP, 1000)
+ date_trunc_single_time("seconds", TIMESTAMP, (1000 * 60))
+ date_trunc_single_time("minute", TIMESTAMP, (1000 * 60 * 60))
+ date_trunc_single_time("hour", TIMESTAMP, (1000 * 60 * 60 * 24))
// week
// quarter
// decade
diff --git a/sql/scripts/17_temporal.sql b/sql/scripts/17_temporal.sql
--- a/sql/scripts/17_temporal.sql
+++ b/sql/scripts/17_temporal.sql
@@ -21,3 +21,7 @@ grant execute on function sys."epoch" (B
grant execute on function sys."epoch" (INT) to public;
grant execute on function sys."epoch" (TIMESTAMP) to public;
grant execute on function sys."epoch" (TIMESTAMP WITH TIME ZONE) to public;
+
+create function sys.date_trunc(txt string, t timestamp)
+returns timestamp
+external name sql.date_trunc;
diff --git a/sql/test/BugTracker-2019/Tests/All
b/sql/test/BugTracker-2019/Tests/All
--- a/sql/test/BugTracker-2019/Tests/All
+++ b/sql/test/BugTracker-2019/Tests/All
@@ -1,1 +1,2 @@
copy-into-from-stdin-empty-line.Bug-67999
+date_trunc
diff --git a/sql/test/BugTracker-2019/Tests/date_trunc.sql
b/sql/test/BugTracker-2019/Tests/date_trunc.sql
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2019/Tests/date_trunc.sql
@@ -0,0 +1,14 @@
+select date_trunc('microseconds', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('milliseconds', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('second', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('minute', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('hour', timestamp '2019-02-17 02:08:12.345678');
+
+select date_trunc('day', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('week', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('month', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('quarter', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('year', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('decade', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('century', timestamp '2019-02-17 02:08:12.345678');
+select date_trunc('millennium', timestamp '2019-02-17 02:08:12.345678');
diff --git a/sql/test/BugTracker-2019/Tests/date_trunc.stable.err
b/sql/test/BugTracker-2019/Tests/date_trunc.stable.err
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2019/Tests/date_trunc.stable.err
@@ -0,0 +1,39 @@
+stderr of test 'date_trunc` in directory 'sql/test/BugTracker-2019` itself:
+
+
+# 08:07:24 >
+# 08:07:24 > "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set"
"mapi_open=true" "--set" "mapi_port=33395" "--set"
"mapi_usock=/var/tmp/mtest-23934/.s.monetdb.33395" "--set" "monet_prompt="
"--forcemito"
"--dbpath=/export/scratch1/mk/branches/default//Linux/var/MonetDB/mTests_sql_test_BugTracker-2019"
"--set" "embedded_c=true"
+# 08:07:24 >
+
+# builtin opt gdk_dbpath =
/export/scratch1/mk/branches/default//Linux/var/monetdb5/dbfarm/demo
+# builtin opt gdk_debug = 0
+# builtin opt gdk_vmtrim = no
+# builtin opt monet_prompt = >
+# builtin opt monet_daemon = no
+# builtin opt mapi_port = 50000
+# builtin opt mapi_open = false
+# builtin opt mapi_autosense = false
+# builtin opt sql_optimizer = default_pipe
+# builtin opt sql_debug = 0
+# cmdline opt gdk_nr_threads = 0
+# cmdline opt mapi_open = true
+# cmdline opt mapi_port = 33395
+# cmdline opt mapi_usock = /var/tmp/mtest-23934/.s.monetdb.33395
+# cmdline opt monet_prompt =
+# cmdline opt gdk_dbpath =
/export/scratch1/mk/branches/default//Linux/var/MonetDB/mTests_sql_test_BugTracker-2019
+# cmdline opt embedded_c = true
+# cmdline opt gdk_debug = 553648138
+
+# 08:07:24 >
+# 08:07:24 > "mclient" "-lsql" "-ftest" "-tnone" "-Eutf-8" "-i" "-e"
"--host=/var/tmp/mtest-23934" "--port=33395"
+# 08:07:24 >
+
+MAPI = (monetdb) /var/tmp/mtest-24278/.s.monetdb.39992
+QUERY = select date_trunc('millennium', timestamp '2019-02-17
02:08:12.345678');
+
+CODE = HY001
+
+# 08:07:24 >
+# 08:07:24 > "Done."
+# 08:07:24 >
+
diff --git a/sql/test/BugTracker-2019/Tests/date_trunc.stable.out
b/sql/test/BugTracker-2019/Tests/date_trunc.stable.out
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2019/Tests/date_trunc.stable.out
@@ -0,0 +1,103 @@
+stdout of test 'date_trunc` in directory 'sql/test/BugTracker-2019` itself:
+
+
+# 08:07:24 >
+# 08:07:24 > "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set"
"mapi_open=true" "--set" "mapi_port=33395" "--set"
"mapi_usock=/var/tmp/mtest-23934/.s.monetdb.33395" "--set" "monet_prompt="
"--forcemito"
"--dbpath=/export/scratch1/mk/branches/default//Linux/var/MonetDB/mTests_sql_test_BugTracker-2019"
"--set" "embedded_c=true"
+# 08:07:24 >
+
+# MonetDB 5 server v11.31.14
+# This is an unreleased version
+# Serving database 'mTests_sql_test_BugTracker-2019', using 4 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 128bit integers
+# Found 31.309 GiB available main-memory.
+# Copyright (c) 1993 - July 2008 CWI.
+# Copyright (c) August 2008 - 2019 MonetDB B.V., all rights reserved
+# Visit https://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://toke.da.cwi.nl:33395/
+# Listening for UNIX domain connection requests on
mapi:monetdb:///var/tmp/mtest-23934/.s.monetdb.33395
+# MonetDB/GIS module loaded
+# MonetDB/SQL module loaded
+
+Ready.
+
+# 08:07:24 >
+# 08:07:24 > "mclient" "-lsql" "-ftest" "-tnone" "-Eutf-8" "-i" "-e"
"--host=/var/tmp/mtest-23934" "--port=33395"
+# 08:07:24 >
+
+#select date_trunc('microseconds', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-17 02:08:12.345678 ]
+#select date_trunc('milliseconds', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-17 02:08:12.345000 ]
+#select date_trunc('second', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-17 02:08:12.000000 ]
+#select date_trunc('minute', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-17 02:08:00.000000 ]
+#select date_trunc('hour', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-17 02:00:00.000000 ]
+#select date_trunc('day', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-17 00:00:00.000000 ]
+#select date_trunc('week', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ ??? ]
+#select date_trunc('month', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-02-00 00:00:00.000000 ]
+#select date_trunc('quarter', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ ??? 00:00:00.000000 ]
+#select date_trunc('year', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2019-00-00 00:00:00.000000 ]
+#select date_trunc('decade', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
+% L2 # name
+% timestamp # type
+% 26 # length
+[ 2000-00-00 00:00:00.000000 ]
+#select date_trunc('century', timestamp '2019-02-17 02:08:12.345678');
+% .L2 # table_name
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list