Skip to content

Commit 3788818

Browse files
Implement sum for durations (#9097)
Closes #7403 --------- Co-authored-by: Aljaž Mur Eržen <aljaz@erzen.si>
1 parent 9c9594f commit 3788818

File tree

6 files changed

+100
-7
lines changed

6 files changed

+100
-7
lines changed

docs/reference/stdlib/set.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,13 @@ Sets
638638
std::sum(s: set of float64) -> float64
639639
std::sum(s: set of bigint) -> bigint
640640
std::sum(s: set of decimal) -> decimal
641+
std::sum(s: set of duration) -> duration
642+
std::sum(s: set of cal::relative_duration) -> cal::relative_duration
643+
std::sum(s: set of cal::date_duration) -> cal::date_duration
641644
642645
.. index:: aggregate
643646

644-
Returns the sum of the set of numbers.
647+
Return the arithmetic sum of values in a set.
645648

646649
The result type depends on the input set type. The general rule of thumb
647650
is that the type of the input set is preserved (as if a simple

edb/lib/cal.edgeql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,31 @@ std::max(vals: SET OF array<std::cal::date_duration>) -> OPTIONAL array<std::cal
19641964
};
19651965

19661966

1967+
CREATE FUNCTION
1968+
std::sum(s: SET OF std::cal::relative_duration) -> std::cal::relative_duration
1969+
{
1970+
CREATE ANNOTATION std::description :=
1971+
'Return the arithmetic sum of values in a set.';
1972+
SET volatility := 'Immutable';
1973+
SET initial_value := <std::cal::relative_duration>"PT0S";
1974+
SET force_return_cast := true;
1975+
USING SQL FUNCTION 'sum';
1976+
};
1977+
1978+
1979+
CREATE FUNCTION
1980+
std::sum(s: SET OF std::cal::date_duration) -> std::cal::date_duration
1981+
{
1982+
CREATE ANNOTATION std::description :=
1983+
'Return the arithmetic sum of values in a set.';
1984+
SET volatility := 'Immutable';
1985+
SET initial_value := <std::cal::date_duration>"PT0S";
1986+
SET force_return_cast := true;
1987+
USING SQL FUNCTION 'sum';
1988+
};
1989+
1990+
1991+
19671992
## Range functions
19681993

19691994

edb/lib/std/20-genericfuncs.edgeql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ CREATE FUNCTION
157157
std::sum(s: SET OF std::bigint) -> std::bigint
158158
{
159159
CREATE ANNOTATION std::description :=
160-
'Return the sum of the set of numbers.';
160+
'Return the arithmetic sum of values in a set.';
161161
SET volatility := 'Immutable';
162162
SET initial_value := 0;
163163
SET force_return_cast := true;
@@ -169,7 +169,7 @@ CREATE FUNCTION
169169
std::sum(s: SET OF std::decimal) -> std::decimal
170170
{
171171
CREATE ANNOTATION std::description :=
172-
'Return the sum of the set of numbers.';
172+
'Return the arithmetic sum of values in a set.';
173173
SET volatility := 'Immutable';
174174
SET initial_value := 0;
175175
USING SQL FUNCTION 'sum';
@@ -180,7 +180,7 @@ CREATE FUNCTION
180180
std::sum(s: SET OF std::int32) -> std::int64
181181
{
182182
CREATE ANNOTATION std::description :=
183-
'Return the sum of the set of numbers.';
183+
'Return the arithmetic sum of values in a set.';
184184
SET volatility := 'Immutable';
185185
SET initial_value := 0;
186186
SET force_return_cast := true;
@@ -192,7 +192,7 @@ CREATE FUNCTION
192192
std::sum(s: SET OF std::int64) -> std::int64
193193
{
194194
CREATE ANNOTATION std::description :=
195-
'Return the sum of the set of numbers.';
195+
'Return the arithmetic sum of values in a set.';
196196
SET volatility := 'Immutable';
197197
SET initial_value := 0;
198198
SET force_return_cast := true;
@@ -204,7 +204,7 @@ CREATE FUNCTION
204204
std::sum(s: SET OF std::float32) -> std::float32
205205
{
206206
CREATE ANNOTATION std::description :=
207-
'Return the sum of the set of numbers.';
207+
'Return the arithmetic sum of values in a set.';
208208
SET volatility := 'Immutable';
209209
SET initial_value := 0;
210210
USING SQL FUNCTION 'sum';
@@ -215,7 +215,7 @@ CREATE FUNCTION
215215
std::sum(s: SET OF std::float64) -> std::float64
216216
{
217217
CREATE ANNOTATION std::description :=
218-
'Return the sum of the set of numbers.';
218+
'Return the arithmetic sum of values in a set.';
219219
SET volatility := 'Immutable';
220220
SET initial_value := 0;
221221
USING SQL FUNCTION 'sum';

edb/lib/std/30-datetimefuncs.edgeql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,17 @@ CREATE CAST FROM std::duration TO std::str {
507507
SELECT regexp_replace(val::text, '[[:<:]]mon(?=s?[[:>:]])', 'month');
508508
$$;
509509
};
510+
511+
512+
# std::sum
513+
514+
CREATE FUNCTION
515+
std::sum(s: SET OF std::duration) -> std::duration
516+
{
517+
CREATE ANNOTATION std::description :=
518+
'Return the arithmetic sum of values in a set.';
519+
SET volatility := 'Immutable';
520+
SET initial_value := <std::duration>"PT0S";
521+
SET force_return_cast := true;
522+
USING SQL FUNCTION 'sum';
523+
};

edb/testbase/serutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _stringify(o):
7373
@serialize.register(decimal.Decimal)
7474
@serialize.register(datetime.timedelta)
7575
@serialize.register(edgedb.RelativeDuration)
76+
@serialize.register(edgedb.DateDuration)
7677
def _scalar(o):
7778
return o
7879

tests/test_edgeql_functions.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import os.path
2525
import random
2626
import uuid
27+
from datetime import timedelta
2728

2829
import edgedb
30+
import gel
2931

3032
from edb.testbase import server as tb
3133
from edb.tools import test
@@ -1819,6 +1821,54 @@ async def test_edgeql_functions_sum_04(self):
18191821
[True],
18201822
)
18211823

1824+
await self.assert_query_result(
1825+
r'''SELECT sum(<duration>"PT5S") IS duration;''',
1826+
[True],
1827+
)
1828+
1829+
await self.assert_query_result(
1830+
r'''
1831+
SELECT sum(<std::cal::relative_duration>"PT5S")
1832+
IS std::cal::relative_duration;
1833+
''',
1834+
[True],
1835+
)
1836+
1837+
await self.assert_query_result(
1838+
r'''
1839+
SELECT sum(<cal::date_duration>"PT5S")
1840+
IS std::cal::date_duration;
1841+
''',
1842+
[True],
1843+
)
1844+
1845+
async def test_edgeql_functions_sum_05(self):
1846+
await self.assert_query_result(
1847+
r'''SELECT sum({<duration>"PT5S", <duration>"PT10S"})''',
1848+
["PT15S"],
1849+
[timedelta(seconds=15)],
1850+
)
1851+
1852+
async def test_edgeql_functions_sum_07(self):
1853+
await self.assert_query_result(
1854+
r'''
1855+
SELECT sum({<cal::relative_duration>"PT5S",
1856+
<cal::relative_duration>"PT10S"})
1857+
''',
1858+
["PT15S"],
1859+
[gel.RelativeDuration(microseconds=15_000_000)],
1860+
)
1861+
1862+
async def test_edgeql_functions_sum_08(self):
1863+
await self.assert_query_result(
1864+
r'''
1865+
SELECT sum({<cal::date_duration>"5 days",
1866+
<cal::date_duration>"10 days"})
1867+
''',
1868+
["P15D"],
1869+
[gel.DateDuration(days=15)]
1870+
)
1871+
18221872
async def test_edgeql_functions_unix_to_datetime_01(self):
18231873
dt = await self.con.query_single(
18241874
'SELECT <str>to_datetime(1590595184.584);'

0 commit comments

Comments
 (0)