Skip to content

Commit 8ab1e24

Browse files
authored
abundance search [more fixes]
This commit addresses an issue in the explorer search database function, which was returning datasets without respecting user-defined abundance threshold parameters. This commit also accounts for situations in which the sum of values attempts to be divided by zero in generating the average abundance value for a site, which would result in a server error, failed api call, and unresponsive explorer application.
1 parent 68020ae commit 8ab1e24

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

function/ap/explorersearch.sql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ BEGIN
8282
FROM ndb.ecolgroups eg
8383
JOIN ap.pollensumgroups sg ON eg.ecolgroupid = sg.ecolgroupid
8484
WHERE
85-
eg.taxonid IN (_taxonids[0])
85+
eg.taxonid IN (_taxonids[1])
8686
);
8787
IF sumGroupId > 0 THEN
8888
doAbund := true;
@@ -108,7 +108,12 @@ BEGIN
108108
BEGIN
109109
cteBaseSelect := cteBaseSelect || ',
110110
v.taxonid,
111-
CAST(d.value / SUM(d.value) OVER(PARTITION BY s.sampleid) * 100 AS DECIMAL(5,2)) AS abundance';
111+
(CASE WHEN (SUM(d.value) OVER(PARTITION BY s.sampleid)) IS NOT NULL AND
112+
(SUM(d.value) OVER(PARTITION BY s.sampleid)) <>0 THEN
113+
(CAST(d.value / SUM(d.value) OVER (PARTITION BY s.sampleid) * 100 AS DECIMAL(5,2)))
114+
ELSE
115+
NULL END
116+
) AS abundance';
112117
cteBaseFrom := cteBaseFrom || '
113118
JOIN ndb.taxa t ON v.taxonid = t.taxonid
114119
JOIN ndb.ecolgroups e ON t.taxonid = e.taxonid
@@ -177,7 +182,7 @@ BEGIN
177182
IF doAbund THEN
178183
cteAgesWhere := cteAgesWhere || '
179184
AND base.abundance > ' || _abundpct ||
180-
' AND base.taxonid IN (' || array_to_string( _taxonids ,',') || '))';
185+
' AND base.taxonid IN (' || array_to_string( _taxonids ,',') || ')';
181186
END IF;
182187

183188
IF NOT (_ageold IS NULL AND _ageyoung IS NULL AND noTaxa = false) THEN

0 commit comments

Comments
 (0)