提交 ea6d213b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Support INTERVAL interpolation in MEDIAN aggregate

上级 1289d67c
...@@ -3389,7 +3389,7 @@ MEDIAN( [ DISTINCT ] value ) [ FILTER ( WHERE expression ) ] ...@@ -3389,7 +3389,7 @@ MEDIAN( [ DISTINCT ] value ) [ FILTER ( WHERE expression ) ]
"," ","
The value separating the higher half of a values from the lower half. The value separating the higher half of a values from the lower half.
Returns the middle value or an interpolated value between two middle values if number of values is even. Returns the middle value or an interpolated value between two middle values if number of values is even.
Interpolation is only supported for numeric, date, and time data types. Interpolation is only supported for numeric, date-time, and interval data types.
NULL values are ignored in the calculation. NULL values are ignored in the calculation.
If no rows are selected, the result is NULL. If no rows are selected, the result is NULL.
Aggregates are only allowed in select statements. Aggregates are only allowed in select statements.
......
...@@ -9,6 +9,7 @@ import java.math.BigDecimal; ...@@ -9,6 +9,7 @@ import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import org.h2.api.IntervalQualifier;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.Mode; import org.h2.engine.Mode;
import org.h2.engine.Session; import org.h2.engine.Session;
...@@ -29,6 +30,7 @@ import org.h2.value.ValueDecimal; ...@@ -29,6 +30,7 @@ import org.h2.value.ValueDecimal;
import org.h2.value.ValueDouble; import org.h2.value.ValueDouble;
import org.h2.value.ValueFloat; import org.h2.value.ValueFloat;
import org.h2.value.ValueInt; import org.h2.value.ValueInt;
import org.h2.value.ValueInterval;
import org.h2.value.ValueLong; import org.h2.value.ValueLong;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
import org.h2.value.ValueTime; import org.h2.value.ValueTime;
...@@ -246,6 +248,22 @@ class AggregateDataMedian extends AggregateDataCollecting { ...@@ -246,6 +248,22 @@ class AggregateDataMedian extends AggregateDataCollecting {
return ValueTimestampTimeZone.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(dateSum / 2), return ValueTimestampTimeZone.fromDateValueAndNanos(DateTimeUtils.dateValueFromAbsoluteDay(dateSum / 2),
nanos, (short) (offset / 2)); nanos, (short) (offset / 2));
} }
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
return DateTimeUtils.intervalFromAbsolute(IntervalQualifier.valueOf(dataType - Value.INTERVAL_YEAR),
DateTimeUtils.intervalToAbsolute((ValueInterval) v0)
.add(DateTimeUtils.intervalToAbsolute((ValueInterval) v1)).shiftRight(1));
default: default:
// Just return first // Just return first
return v0.convertTo(dataType); return v0.convertTo(dataType);
......
...@@ -504,6 +504,18 @@ select median(v) from test; ...@@ -504,6 +504,18 @@ select median(v) from test;
drop table test; drop table test;
> ok > ok
create table test(v interval day to second);
> ok
insert into test values ('0 1'), ('0 2'), ('0 2'), ('0 2'), ('-0 1'), ('-0 1');
> update count: 6
select median (v) from test;
>> INTERVAL '0 01:30:00' DAY TO SECOND
drop table test;
> ok
-- with group by -- with group by
create table test(name varchar, value int); create table test(name varchar, value int);
> ok > ok
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论