提交 38bf010d authored 作者: Stéphane Eintrazi's avatar Stéphane Eintrazi

add first implementation of date_trunc function

上级 9f44bb4f
...@@ -107,7 +107,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -107,7 +107,7 @@ public class Function extends Expression implements FunctionCall {
SECOND = 114, WEEK = 115, YEAR = 116, CURRENT_DATE = 117, SECOND = 114, WEEK = 115, YEAR = 116, CURRENT_DATE = 117,
CURRENT_TIME = 118, CURRENT_TIMESTAMP = 119, EXTRACT = 120, CURRENT_TIME = 118, CURRENT_TIMESTAMP = 119, EXTRACT = 120,
FORMATDATETIME = 121, PARSEDATETIME = 122, ISO_YEAR = 123, FORMATDATETIME = 121, PARSEDATETIME = 122, ISO_YEAR = 123,
ISO_WEEK = 124, ISO_DAY_OF_WEEK = 125; ISO_WEEK = 124, ISO_DAY_OF_WEEK = 125, DATE_TRUNC = 132;
/** /**
* Pseudo functions for DATEADD, DATEDIFF, and EXTRACT. * Pseudo functions for DATEADD, DATEDIFF, and EXTRACT.
...@@ -419,6 +419,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -419,6 +419,7 @@ public class Function extends Expression implements FunctionCall {
1, Value.INT); 1, Value.INT);
addFunction("ISO_DAY_OF_WEEK", ISO_DAY_OF_WEEK, addFunction("ISO_DAY_OF_WEEK", ISO_DAY_OF_WEEK,
1, Value.INT); 1, Value.INT);
addFunction("DATE_TRUNC", DATE_TRUNC, 2, Value.NULL);
// system // system
addFunctionNotDeterministic("DATABASE", DATABASE, addFunctionNotDeterministic("DATABASE", DATABASE,
0, Value.STRING); 0, Value.STRING);
...@@ -1494,6 +1495,9 @@ public class Function extends Expression implements FunctionCall { ...@@ -1494,6 +1495,9 @@ public class Function extends Expression implements FunctionCall {
case DATE_DIFF: case DATE_DIFF:
result = ValueLong.get(datediff(v0.getString(), v1, v2)); result = ValueLong.get(datediff(v0.getString(), v1, v2));
break; break;
case DATE_TRUNC:
result = ValueTimestamp.fromDateValueAndNanos(((ValueTimestamp) v1).getDateValue(), 0);
break;
case EXTRACT: { case EXTRACT: {
int field = getDatePart(v0.getString()); int field = getDatePart(v0.getString());
if (field != EPOCH) { if (field != EPOCH) {
...@@ -2371,6 +2375,10 @@ public class Function extends Expression implements FunctionCall { ...@@ -2371,6 +2375,10 @@ public class Function extends Expression implements FunctionCall {
min = 1; min = 1;
max = 2; max = 2;
break; break;
case DATE_TRUNC:
min = 2;
max = 2;
break;
case TO_CHAR: case TO_CHAR:
case TO_DATE: case TO_DATE:
min = 1; min = 1;
...@@ -2579,6 +2587,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -2579,6 +2587,7 @@ public class Function extends Expression implements FunctionCall {
s = scale; s = scale;
d = displaySize; d = displaySize;
break; break;
case DATE_TRUNC:
case TRUNCATE: case TRUNCATE:
t = p0.getType(); t = p0.getType();
s = p0.getScale(); s = p0.getScale();
...@@ -2745,6 +2754,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -2745,6 +2754,7 @@ public class Function extends Expression implements FunctionCall {
case TRIM: case TRIM:
case STRINGDECODE: case STRINGDECODE:
case UTF8TOSTRING: case UTF8TOSTRING:
case DATE_TRUNC:
case TRUNCATE: case TRUNCATE:
precision = args[0].getPrecision(); precision = args[0].getPrecision();
displaySize = args[0].getDisplaySize(); displaySize = args[0].getDisplaySize();
......
...@@ -147,9 +147,10 @@ public class TestScript extends TestBase { ...@@ -147,9 +147,10 @@ public class TestScript extends TestBase {
"current-time", "dateadd", "datediff", "dayname", "current-time", "dateadd", "datediff", "dayname",
"day-of-month", "day-of-week", "day-of-year", "extract", "day-of-month", "day-of-week", "day-of-year", "extract",
"formatdatetime", "hour", "minute", "month", "monthname", "formatdatetime", "hour", "minute", "month", "monthname",
"parsedatetime", "quarter", "second", "truncate", "week", "year" }) { "parsedatetime", "quarter", "second", "truncate", "week", "year", "date_trunc" }) {
testScript("functions/timeanddate/" + s + ".sql"); testScript("functions/timeanddate/" + s + ".sql");
} }
deleteDb("script"); deleteDb("script");
System.out.flush(); System.out.flush();
} }
......
select DATE_TRUNC('day', timestamp '2015-05-29 15:00:00');
> 2015-05-29 00:00:00
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论