Skip to content

Commit

Permalink
builtins: fix window function for avg of intervals
Browse files Browse the repository at this point in the history
Release note: none
  • Loading branch information
otan committed Nov 18, 2019
1 parent c412a31 commit 8a727e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
75 changes: 45 additions & 30 deletions pkg/sql/logictest/testdata/logic_test/window
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ CREATE TABLE kv (
d DECIMAL,
s STRING,
b BOOL,
i INTERVAL,
FAMILY (k, v, w, f, b),
FAMILY (d),
FAMILY (s)
)

statement OK
INSERT INTO kv VALUES
(1, 2, 3, 1.0, 1, 'a', true),
(3, 4, 5, 2, 8, 'a', true),
(5, NULL, 5, 9.9, -321, NULL, false),
(6, 2, 3, 4.4, 4.4, 'b', true),
(7, 2, 2, 6, 7.9, 'b', true),
(8, 4, 2, 3, 3, 'A', false)
(1, 2, 3, 1.0, 1, 'a', true, '1min'),
(3, 4, 5, 2, 8, 'a', true, '2sec'),
(5, NULL, 5, 9.9, -321, NULL, false, NULL),
(6, 2, 3, 4.4, 4.4, 'b', true, '1ms'),
(7, 2, 2, 6, 7.9, 'b', true, '4 days'),
(8, 4, 2, 3, 3, 'A', false, '3 years')

query error window functions are not allowed in GROUP BY
SELECT * FROM kv GROUP BY v, count(w) OVER ()
Expand Down Expand Up @@ -208,38 +209,38 @@ SELECT avg(k) OVER (w ORDER BY w) FROM kv WINDOW w AS (PARTITION BY v) ORDER BY
7
8

query IIIRRTBR colnames
query IIIRRTBTR colnames
SELECT *, avg(k) OVER (w ORDER BY w) FROM kv WINDOW w AS (PARTITION BY v) ORDER BY 1
----
k v w f d s b avg
1 2 3 1 1 a true 4.6666666666666666667
3 4 5 2 8 a true 5.5
5 NULL 5 9.9 -321 NULL false 5
6 2 3 4.4 4.4 b true 4.6666666666666666667
7 2 2 6 7.9 b true 7
8 4 2 3 3 A false 8
k v w f d s b i avg
1 2 3 1 1 a true 00:01:00 4.6666666666666666667
3 4 5 2 8 a true 00:00:02 5.5
5 NULL 5 9.9 -321 NULL false NULL 5
6 2 3 4.4 4.4 b true 00:00:00.001 4.6666666666666666667
7 2 2 6 7.9 b true 4 days 7
8 4 2 3 3 A false 3 years 8

query IIIRRTBR colnames
query IIIRRTBTR colnames
SELECT *, avg(k) OVER w FROM kv WINDOW w AS (PARTITION BY v ORDER BY w) ORDER BY avg(k) OVER w, k
----
k v w f d s b avg
1 2 3 1 1 a true 4.6666666666666666667
6 2 3 4.4 4.4 b true 4.6666666666666666667
5 NULL 5 9.9 -321 NULL false 5
3 4 5 2 8 a true 5.5
7 2 2 6 7.9 b true 7
8 4 2 3 3 A false 8
k v w f d s b i avg
1 2 3 1 1 a true 00:01:00 4.6666666666666666667
6 2 3 4.4 4.4 b true 00:00:00.001 4.6666666666666666667
5 NULL 5 9.9 -321 NULL false NULL 5
3 4 5 2 8 a true 00:00:02 5.5
7 2 2 6 7.9 b true 4 days 7
8 4 2 3 3 A false 3 years 8

query IIIRRTB colnames
query IIIRRTBT colnames
SELECT * FROM kv WINDOW w AS (PARTITION BY v ORDER BY w) ORDER BY avg(k) OVER w DESC, k
----
k v w f d s b
8 4 2 3 3 A false
7 2 2 6 7.9 b true
3 4 5 2 8 a true
5 NULL 5 9.9 -321 NULL false
1 2 3 1 1 a true
6 2 3 4.4 4.4 b true
k v w f d s b i
8 4 2 3 3 A false 3 years
7 2 2 6 7.9 b true 4 days
3 4 5 2 8 a true 00:00:02
5 NULL 5 9.9 -321 NULL false NULL
1 2 3 1 1 a true 00:01:00
6 2 3 4.4 4.4 b true 00:00:00.001

query error window "w" is already defined
SELECT avg(k) OVER w FROM kv WINDOW w AS (), w AS ()
Expand Down Expand Up @@ -1898,6 +1899,20 @@ DELETE FROM kv WHERE k = 12
query error FILTER specified but rank\(\) is not an aggregate function
SELECT k, rank() FILTER (WHERE k=1) OVER () FROM kv

query TT
SELECT i, avg(i) OVER (ORDER BY i) FROM kv ORDER BY i
----
NULL NULL
NULL NULL
NULL NULL
NULL NULL
00:00:00.001 00:00:00.001
00:00:02 00:00:01.0005
00:01:00 00:00:20.667
4 days 1 day 00:00:15.50025
3 years 7 mons 6 days 19:12:12.4002


# Issue #14606: correctly handle aggregation functions above the windowing level
query I
SELECT max(i) * (row_number() OVER (ORDER BY max(i))) FROM (SELECT 1 AS i, 2 AS j) GROUP BY j
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/sem/builtins/window_frame_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ func (w *avgWindowFunc) Compute(
count := apd.New(int64(frameSize), 0)
_, err := tree.DecimalCtx.Quo(&avg.Decimal, &dd.Decimal, count)
return &avg, err
case *tree.DInterval:
return &tree.DInterval{Duration: t.Duration.Div(int64(frameSize))}, nil
default:
return nil, errors.AssertionFailedf("unexpected SUM result type: %s", t)
}
Expand Down

0 comments on commit 8a727e1

Please sign in to comment.