|
| 1 | +/* |
| 2 | + * Copyright 2021 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +CREATE OR REPLACE FUNCTION runs.get_ancestors( |
| 18 | + IN i_id_partitioning BIGINT, |
| 19 | + IN i_limit INT DEFAULT 5, |
| 20 | + IN i_offset BIGINT DEFAULT 0, |
| 21 | + OUT status INTEGER, |
| 22 | + OUT status_text TEXT, |
| 23 | + OUT ancestorid BIGINT, |
| 24 | + OUT partitioning JSONB, |
| 25 | + OUT author TEXT, |
| 26 | + OUT has_more BOOLEAN |
| 27 | +) RETURNS SETOF record AS |
| 28 | +$$ |
| 29 | + ------------------------------------------------------------------------------- |
| 30 | +-- |
| 31 | +-- Function: runs.get_ancestors(3) |
| 32 | +-- Returns Ancestors' partition ID for the given id |
| 33 | +-- |
| 34 | +-- Parameters: |
| 35 | +-- i_id_partitioning - id that we asking the Ancestors for |
| 36 | +-- i_limit - (optional) maximum number of partitionings to return, default is 5 |
| 37 | +-- i_offset - (optional) offset to use for pagination, default is 0 |
| 38 | +-- |
| 39 | +-- Returns: |
| 40 | +-- status - Status code |
| 41 | +-- status_text - Status message |
| 42 | +-- ancestorid - ID of Ancestor partition |
| 43 | +-- partitioning - partitioning data of ancestor |
| 44 | +-- author - author of the Ancestor partitioning |
| 45 | +-- has_more - Flag indicating if there are more partitionings available |
| 46 | + |
| 47 | +-- Status codes: |
| 48 | +-- 11 - OK |
| 49 | +-- 41 - Partitioning not found |
| 50 | +-- 42 - Ancestor Partitioning not found |
| 51 | +-- |
| 52 | +------------------------------------------------------------------------------- |
| 53 | +DECLARE |
| 54 | + partitionCreateAt TIMESTAMP; |
| 55 | + _has_more BOOLEAN; |
| 56 | + |
| 57 | +BEGIN |
| 58 | + -- Check if the partitioning exists |
| 59 | + PERFORM 1 FROM runs.partitionings WHERE id_partitioning = i_id_partitioning; |
| 60 | + IF NOT FOUND THEN |
| 61 | + status := 41; |
| 62 | + status_text := 'Partitioning not found'; |
| 63 | + RETURN NEXT; |
| 64 | + RETURN; |
| 65 | + END IF; |
| 66 | + |
| 67 | + -- Get the creation timestamp of the partitioning |
| 68 | + SELECT created_at |
| 69 | + FROM runs.partitionings |
| 70 | + WHERE id_partitioning = i_id_partitioning |
| 71 | + INTO partitionCreateAt; |
| 72 | + |
| 73 | + -- Check if there are more partitionings than the limit |
| 74 | + SELECT count(*) > i_limit |
| 75 | + FROM flows.partitioning_to_flow PTF |
| 76 | + WHERE PTF.fk_flow IN ( |
| 77 | + SELECT fk_flow |
| 78 | + FROM flows.partitioning_to_flow |
| 79 | + WHERE fk_partitioning = i_id_partitioning |
| 80 | + ) |
| 81 | + LIMIT i_limit + 1 OFFSET i_offset |
| 82 | + INTO _has_more; |
| 83 | + |
| 84 | + -- Return the ancestors |
| 85 | + RETURN QUERY |
| 86 | + SELECT |
| 87 | + 11 AS status, |
| 88 | + 'OK' AS status_text, |
| 89 | + P.id_partitioning AS ancestorid, |
| 90 | + P.partitioning AS partitioning, |
| 91 | + P.created_by AS author, |
| 92 | + _has_more AS has_more |
| 93 | + FROM |
| 94 | + runs.partitionings P |
| 95 | + INNER JOIN flows.partitioning_to_flow PF ON PF.fk_partitioning = P.id_partitioning |
| 96 | + INNER JOIN flows.partitioning_to_flow PF2 ON PF2.fk_flow = PF.fk_flow |
| 97 | + WHERE |
| 98 | + PF2.fk_partitioning = i_id_partitioning |
| 99 | + AND |
| 100 | + P.created_at < partitionCreateAt |
| 101 | + GROUP BY P.id_partitioning |
| 102 | + ORDER BY P.id_partitioning, P.created_at DESC |
| 103 | + LIMIT i_limit |
| 104 | + OFFSET i_offset; |
| 105 | + |
| 106 | + IF FOUND THEN |
| 107 | + status := 11; |
| 108 | + status_text := 'OK'; |
| 109 | + ELSE |
| 110 | + status := 42; |
| 111 | + status_text := 'Ancestor Partitioning not found'; |
| 112 | + END IF; |
| 113 | + RETURN NEXT; |
| 114 | + RETURN; |
| 115 | + |
| 116 | +END; |
| 117 | +$$ |
| 118 | + LANGUAGE plpgsql VOLATILE SECURITY DEFINER; |
| 119 | + |
| 120 | +ALTER FUNCTION runs.get_ancestors(BIGINT, INT, BIGINT) OWNER TO atum_owner; |
| 121 | +GRANT EXECUTE ON FUNCTION runs.get_ancestors(BIGINT, INT, BIGINT) TO atum_user; |
0 commit comments