Files
email-tracker/external/duckdb/test/sql/aggregate/aggregates/test_mad.test
2025-10-24 19:21:19 -05:00

136 lines
2.2 KiB
SQL

# name: test/sql/aggregate/aggregates/test_mad.test
# description: Test MAD (Moving Absolute Deviation) aggregate
# group: [aggregates]
statement ok
PRAGMA enable_verification
# scalar mad is zero
query II
SELECT mad(NULL), mad(1)
----
NULL 0
# constant mad is zero
query II
SELECT mad(NULL), mad(1) FROM range(2000)
----
NULL 0
#
# Small numerics
#
statement ok
create table tinys as
select range r, random()
from range(100)
union all values (NULL, 0.1), (NULL, 0.5), (NULL, 0.9)
order by 2;
foreach type tinyint decimal(4,1)
query I
SELECT mad(r::${type}) FROM tinys
----
25
query I
SELECT mad(NULL::${type}) FROM tinys
----
NULL
query I
SELECT mad(42::${type}) FROM tinys
----
0
endloop
#
# Large numerics
#
statement ok
create table numerics as
select range r, random()
from range(10000)
union all values (NULL, 0.1), (NULL, 0.5), (NULL, 0.9)
order by 2;
foreach type smallint integer bigint hugeint uhugeint float double decimal(8,1) decimal(12,1) decimal(18,1) decimal(24,1)
query I
SELECT mad(r::${type}) FROM numerics
----
2500
query I
SELECT mad(NULL::${type}) FROM numerics
----
NULL
query I
SELECT mad(42::${type}) FROM numerics
----
0
endloop
#
# Temporal
#
query I
SELECT mad(('2018-01-01'::DATE + INTERVAL (r) DAY)::DATE) FROM numerics
----
2500 days
query I
SELECT mad('2018-01-01'::TIMESTAMP + INTERVAL (r) HOUR) FROM numerics
----
104 days 04:00:00
query I
SELECT mad('00:00:00'::TIME + INTERVAL (r) SECOND) FROM numerics
----
00:41:40
#
# Extreme values
#
query I
select mad(x) from (values ('127'::DECIMAL(3,0)), ('-128'::DECIMAL(3,0))) tbl(x);
----
127
query I
select mad(x) from (values ('32767'::DECIMAL(5,0)), ('-32768'::DECIMAL(5,0))) tbl(x);
----
32767
query I
select mad(x) from (values ('2147483647'::DECIMAL(10,0)), ('-2147483648'::DECIMAL(10,0))) tbl(x);
----
2147483647
statement ok
select mad(x) from (values (-1e308), (1e308)) tbl(x);
query I
select mad(x) from (values ('294247-01-10'::date), ('290309-12-22 (BC)'::date)) tbl(x);
----
106751991 days
query I
select mad(x) from (values
('294247-01-10 04:00:54.775806'::timestamp),
('290309-12-22 (BC) 00:00:00'::timestamp)
) tbl(x);
----
106751991 days 02:00:27.387903
query I
select mad(x) from (values ('23:59:59.999999'::time), ('00:00:00'::time)) tbl(x);
----
12:00:00