Fix for recursive SQL returning extra level of descendants 14/134814/1
authordanielhanrahan <daniel.hanrahan@est.tech>
Mon, 12 Jun 2023 15:24:55 +0000 (16:24 +0100)
committerdanielhanrahan <daniel.hanrahan@est.tech>
Mon, 12 Jun 2023 17:02:53 +0000 (18:02 +0100)
Recursive SQL queries are incorrectly returning maxDepth + 1 levels
of fragments due to incorrect comparison in the WHERE clause.
This code is used by getDataNodes and queryDataNodes to
prefetch the descendants before converting to datanodes.
Prefetching extra descendants causes high memory usage.

Issue-ID: CPS-1716
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: Ie22215c0c7018cd293f73cf75adca2cd55f218a6

cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java

index 425a726..82c422f 100755 (executable)
@@ -119,7 +119,7 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>,
         + "   UNION "\r
         + "  SELECT c.id, depth + 1 "\r
         + "    FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
-        + "   WHERE depth <= (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
+        + "   WHERE depth < (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
         + ") "\r
         + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r
@@ -142,7 +142,7 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>,
         + "   UNION "\r
         + "  SELECT c.id, depth + 1 "\r
         + "    FROM fragment c INNER JOIN parent_search p ON c.parent_id = p.id"\r
-        + "   WHERE depth <= (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
+        + "   WHERE depth < (SELECT CASE WHEN :maxDepth = -1 THEN " + Integer.MAX_VALUE + " ELSE :maxDepth END) "\r
         + ") "\r
         + "SELECT f.id, anchor_id AS anchorId, xpath, f.parent_id AS parentId, CAST(attributes AS TEXT) AS attributes "\r
         + "FROM fragment f INNER JOIN parent_search p ON f.id = p.id",\r