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

add verification on the value type before truncating the date

上级 7bae69de
......@@ -15,6 +15,7 @@ import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.GregorianCalendar;
......@@ -30,6 +31,7 @@ import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.engine.Mode;
import org.h2.engine.Session;
import org.h2.jdbc.JdbcSQLException;
import org.h2.message.DbException;
import org.h2.mvstore.DataUtils;
import org.h2.schema.Schema;
......@@ -1496,7 +1498,39 @@ public class Function extends Expression implements FunctionCall {
result = ValueLong.get(datediff(v0.getString(), v1, v2));
break;
case DATE_TRUNC:
result = ValueTimestamp.fromDateValueAndNanos(((ValueTimestamp) v1).getDateValue(), 0);
// Retrieve the field value (e.g. 'day', 'microseconds', etc.)
String fieldValue = v0.getString().toLowerCase();
long[] fieldDateAndTime = DateTimeUtils.dateAndTimeFromValue(v1);
long fieldDateValue = fieldDateAndTime[0];
if (fieldValue.equals("day")) {
// Case where we truncate the date to the day if the value is of
// type time e.g. '10:00:00'
if (v1 instanceof ValueTime) {
result = v1.subtract(v1);
} else if (v1 instanceof ValueDate) {
result = v1;
} else if (v1 instanceof ValueTimestampTimeZone) {
ValueTimestampTimeZone v = (ValueTimestampTimeZone) v1;
result = ValueTimestampTimeZone.fromDateValueAndNanos(v.getDateValue(), 0,
v.getTimeZoneOffsetMins());
} else {
result = ValueTimestamp.fromDateValueAndNanos(fieldDateValue, 0);
}
} else {
throw DbException.getUnsupportedException(v0.getString());
}
break;
case EXTRACT: {
int field = getDatePart(v0.getString());
......
select DATE_TRUNC('day', timestamp '2015-05-29 15:00:00');
>> 2015-05-29 00:00:00
\ No newline at end of file
select DATE_TRUNC('day', time '00:00:00');
>> 00:00:00
select DATE_TRUNC('day', time '15:14:13');
>> 00:00:00
select DATE_TRUNC('day', date '2015-05-29');
>> 2015-05-29
select DATE_TRUNC('day', timestamp '2015-05-29 15:14:13');
>> 2015-05-29 00:00:00
select DATE_TRUNC('day', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-05-29 00:00:00+00
select DATE_TRUNC('day', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-05-29 00:00:00-06
select DATE_TRUNC('day', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-05-29 00:00:00+10
select DATE_TRUNC('day', '2015-05-29 15:14:13');
>> 2015-05-29 00:00:00
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论