提交 41c30c1c authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Implement date-time subtraction properly

上级 02ef458f
......@@ -22,6 +22,7 @@ import org.h2.value.ValueDate;
import org.h2.value.ValueInterval;
import org.h2.value.ValueNull;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestampTimeZone;
/**
* A mathematical operation with intervals.
......@@ -57,7 +58,12 @@ public class IntervalOperation extends Expression {
/**
* Interval divided by numeric.
*/
INTERVAL_DIVIDE_NUMERIC
INTERVAL_DIVIDE_NUMERIC,
/**
* Date-time minus date-time.
*/
DATETIME_MINUS_DATETIME
}
private final IntervalOpType opType;
......@@ -68,16 +74,26 @@ public class IntervalOperation extends Expression {
this.opType = opType;
this.left = left;
this.right = right;
int l = left.getType(), r = right.getType();
switch (opType) {
case INTERVAL_PLUS_INTERVAL:
case INTERVAL_MINUS_INTERVAL:
dataType = Value.getHigherOrder(left.getType(), right.getType());
dataType = Value.getHigherOrder(l, r);
break;
case DATETIME_PLUS_INTERVAL:
case DATETIME_MINUS_INTERVAL:
case INTERVAL_MULTIPLY_NUMERIC:
case INTERVAL_DIVIDE_NUMERIC:
dataType = left.getType();
dataType = l;
break;
case DATETIME_MINUS_DATETIME:
if (l == Value.TIME && r == Value.TIME) {
dataType = Value.INTERVAL_HOUR_TO_SECOND;
} else if (l == Value.DATE && r == Value.DATE) {
dataType = Value.INTERVAL_DAY;
} else {
dataType = Value.INTERVAL_DAY_TO_SECOND;
}
}
}
......@@ -93,6 +109,7 @@ public class IntervalOperation extends Expression {
return '+';
case INTERVAL_MINUS_INTERVAL:
case DATETIME_MINUS_INTERVAL:
case DATETIME_MINUS_DATETIME:
return '-';
case INTERVAL_MULTIPLY_NUMERIC:
return '*';
......@@ -110,21 +127,22 @@ public class IntervalOperation extends Expression {
if (l == ValueNull.INSTANCE || r == ValueNull.INSTANCE) {
return ValueNull.INSTANCE;
}
int lType = l.getType(), rType = r.getType();
switch (opType) {
case INTERVAL_PLUS_INTERVAL:
case INTERVAL_MINUS_INTERVAL: {
BigInteger a1 = DateTimeUtils.intervalToAbsolute((ValueInterval) l);
BigInteger a2 = DateTimeUtils.intervalToAbsolute((ValueInterval) r);
return DateTimeUtils.intervalFromAbsolute(
IntervalQualifier.valueOf(Value.getHigherOrder(l.getType(), r.getType()) - Value.INTERVAL_YEAR),
IntervalQualifier.valueOf(Value.getHigherOrder(lType, rType) - Value.INTERVAL_YEAR),
opType == IntervalOpType.INTERVAL_PLUS_INTERVAL ? a1.add(a2) : a1.subtract(a2));
}
case DATETIME_PLUS_INTERVAL:
case DATETIME_MINUS_INTERVAL:
switch (l.getType()) {
switch (lType) {
case Value.TIME: {
if (DataType.isYearMonthIntervalType(r.getType())) {
throw DbException.throwInternalError("type=" + r.getType());
if (DataType.isYearMonthIntervalType(rType)) {
throw DbException.throwInternalError("type=" + rType);
}
BigInteger a1 = BigInteger.valueOf(((ValueTime) l).getNanos());
BigInteger a2 = DateTimeUtils.intervalToAbsolute((ValueInterval) r);
......@@ -137,7 +155,7 @@ public class IntervalOperation extends Expression {
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
if (DataType.isYearMonthIntervalType(r.getType())) {
if (DataType.isYearMonthIntervalType(rType)) {
long m = DateTimeUtils.intervalToAbsolute((ValueInterval) r).longValue();
if (opType == IntervalOpType.DATETIME_MINUS_INTERVAL) {
m = -m;
......@@ -145,7 +163,7 @@ public class IntervalOperation extends Expression {
return DateTimeFunctions.dateadd("MONTH", m, l);
} else {
BigInteger a2 = DateTimeUtils.intervalToAbsolute((ValueInterval) r);
if (l.getType() == Value.DATE) {
if (lType == Value.DATE) {
BigInteger a1 = BigInteger
.valueOf(DateTimeUtils.absoluteDayFromDateValue(((ValueDate) l).getDateValue()));
a2 = a2.divide(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY));
......@@ -180,10 +198,36 @@ public class IntervalOperation extends Expression {
case INTERVAL_DIVIDE_NUMERIC: {
BigDecimal a1 = new BigDecimal(DateTimeUtils.intervalToAbsolute((ValueInterval) l));
BigDecimal a2 = r.getBigDecimal();
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(l.getType() - Value.INTERVAL_YEAR),
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(lType - Value.INTERVAL_YEAR),
(opType == IntervalOpType.INTERVAL_MULTIPLY_NUMERIC ? a1.multiply(a2) : a1.divide(a2))
.toBigInteger());
}
case DATETIME_MINUS_DATETIME:
if (lType == Value.TIME && rType == Value.TIME) {
long diff = ((ValueTime) l).getNanos() - ((ValueTime) r).getNanos();
return ValueInterval.from(IntervalQualifier.HOUR_TO_SECOND, diff / 3_600_000_000_000L,
Math.abs(diff % 3_600_000_000_000L));
} else if (lType == Value.DATE && rType == Value.DATE) {
return ValueInterval.from(IntervalQualifier.DAY,
DateTimeUtils.absoluteDayFromDateValue(((ValueDate) l).getDateValue())
- DateTimeUtils.absoluteDayFromDateValue(((ValueDate) r).getDateValue()),
0L);
} else {
long[] a = DateTimeUtils.dateAndTimeFromValue(l);
long[] b = DateTimeUtils.dateAndTimeFromValue(r);
BigInteger bi1 = BigInteger.valueOf(a[0]).multiply(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY))
.add(BigInteger.valueOf(a[1]));
BigInteger bi2 = BigInteger.valueOf(b[0]).multiply(BigInteger.valueOf(DateTimeUtils.NANOS_PER_DAY))
.add(BigInteger.valueOf(b[1]));
BigInteger diff = bi1.subtract(bi2);
if (lType == Value.TIMESTAMP_TZ || rType == Value.TIMESTAMP_TZ) {
l = l.convertTo(Value.TIMESTAMP_TZ);
r = r.convertTo(Value.TIMESTAMP_TZ);
diff = diff.add(BigInteger.valueOf((((ValueTimestampTimeZone) r).getTimeZoneOffsetMins()
- ((ValueTimestampTimeZone) l).getTimeZoneOffsetMins()) * 60_000_000_000L));
}
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.DAY_TO_SECOND, diff);
}
}
throw DbException.throwInternalError("type=" + opType);
}
......
......@@ -388,21 +388,13 @@ public class Operation extends Expression {
return this;
case Value.DATE:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ: {
// Oracle date subtract
Function f = Function.getFunction(session.getDatabase(), "DATEDIFF");
f.setParameter(0, ValueExpression.get(ValueString.get("DAY")));
f.setParameter(1, right);
f.setParameter(2, left);
f.doneWithParameters();
return f.optimize(session);
}
case Value.TIMESTAMP_TZ:
return new IntervalOperation(IntervalOpType.DATETIME_MINUS_DATETIME, left, right);
}
break;
case Value.TIME:
if (r == Value.TIME) {
dataType = Value.TIME;
return this;
return new IntervalOperation(IntervalOpType.DATETIME_MINUS_DATETIME, left, right);
}
break;
}
......
......@@ -669,3 +669,26 @@ SELECT INTERVAL '1-2' YEAR TO MONTH + TIMESTAMP WITH TIME ZONE '2000-01-01 12:00
SELECT TIMESTAMP WITH TIME ZONE '2000-01-01 12:00:00+01' - INTERVAL '1-2' YEAR TO MONTH;
>> 1998-11-01 12:00:00+01
-- Date-time subtraction
SELECT TIME '10:30:15.123456789' - TIME '11:00:00';
>> INTERVAL '0:29:44.876543211' HOUR TO SECOND
SELECT DATE '2010-01-15' - DATE '2009-12-31';
>> INTERVAL '15' DAY
SELECT TIMESTAMP '2010-01-15 12:00:00.5' - TIMESTAMP '2010-01-13 01:30:00';
>> INTERVAL '2 10:30:00.5' DAY TO SECOND
SELECT TIMESTAMP WITH TIME ZONE '2010-01-15 12:00:00.5+01' - TIMESTAMP WITH TIME ZONE '2010-01-13 01:30:00+01';
>> INTERVAL '2 10:30:00.5' DAY TO SECOND
SELECT TIMESTAMP WITH TIME ZONE '2010-01-15 12:00:00.5+01' - TIMESTAMP WITH TIME ZONE '2010-01-13 01:30:00+02';
>> INTERVAL '2 11:30:00.5' DAY TO SECOND
SELECT TIMESTAMP '2010-01-15 12:00:00.5+01' - TIMESTAMP WITH TIME ZONE '2010-01-13 01:30:00+02';
>> INTERVAL '2 11:30:00.5' DAY TO SECOND
SELECT TIMESTAMP WITH TIME ZONE '2010-01-15 12:00:00.5+01' - TIMESTAMP '2010-01-13 01:30:00+02';
>> INTERVAL '2 11:30:00.5' DAY TO SECOND
......@@ -6132,11 +6132,11 @@ SELECT * FROM TEST;
> rows: 4
SELECT XD+1, XD-1, XD-XD FROM TEST;
> DATEADD('DAY', 1, XD) DATEADD('DAY', -1, XD) DATEDIFF('DAY', XD, XD)
> --------------------- ---------------------- -----------------------
> 0001-02-04 0001-02-02 0
> 0004-05-07 0004-05-05 0
> 2000-01-01 1999-12-30 0
> DATEADD('DAY', 1, XD) DATEADD('DAY', -1, XD) XD - XD
> --------------------- ---------------------- ----------------
> 0001-02-04 0001-02-02 INTERVAL '0' DAY
> 0004-05-07 0004-05-05 INTERVAL '0' DAY
> 2000-01-01 1999-12-30 INTERVAL '0' DAY
> null null null
> rows: 4
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论