-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- Run this script from the parent directory, e.g. psql -f migrations/migrate-20240812.sql | ||
|
||
\i updates/NET-387-xofy-agg-accum-fix.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
solarnet-db-setup/postgres/updates/NET-387-xofy-agg-accum-fix.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
CREATE OR REPLACE FUNCTION solardatm.avg_agg_data_ffunc(agg_state solardatm.agg_data) | ||
RETURNS solardatm.agg_data LANGUAGE plpgsql STRICT IMMUTABLE AS | ||
$$ | ||
BEGIN | ||
WITH di AS ( | ||
SELECT | ||
p.idx | ||
, p.val / s.stat[1] AS val | ||
, s.stat[1] AS cnt | ||
, s.stat[2] AS val_min | ||
, s.stat[3] AS val_max | ||
FROM unnest(agg_state.data_i) WITH ORDINALITY AS p(val, idx) | ||
INNER JOIN solarcommon.reduce_dim(agg_state.stat_i) WITH ORDINALITY AS s(stat, idx) ON s.idx = p.idx | ||
) | ||
, di_ary AS ( | ||
SELECT | ||
vec_trim_scale(array_agg(val ORDER BY idx)) AS data_i | ||
, array_agg( | ||
vec_trim_scale(ARRAY[cnt, val_min, val_max]) ORDER BY idx | ||
) AS stat_i | ||
FROM di | ||
) | ||
, da AS ( | ||
SELECT | ||
p.idx | ||
, p.val / s.stat[1] AS val | ||
, s.stat[2] AS val_min | ||
, s.stat[3] AS val_max | ||
FROM unnest(agg_state.data_a) WITH ORDINALITY AS p(val, idx) | ||
INNER JOIN solarcommon.reduce_dim(agg_state.read_a) WITH ORDINALITY AS s(stat, idx) ON s.idx = p.idx | ||
) | ||
, da_ary AS ( | ||
SELECT | ||
vec_trim_scale(array_agg(val ORDER BY idx)) AS data_a | ||
, array_agg( | ||
vec_trim_scale(ARRAY[val, val_min, val_max]) ORDER BY idx | ||
) AS read_a | ||
FROM da | ||
) | ||
SELECT | ||
di_ary.data_i | ||
, da_ary.data_a | ||
, agg_state.data_s | ||
, agg_state.data_t | ||
, di_ary.stat_i | ||
, da_ary.read_a | ||
FROM di_ary, da_ary | ||
INTO agg_state; | ||
|
||
return agg_state; | ||
END; | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters