|
| 1 | +-- Takes a causal_id and returns a table of ALL hashes which are dependencies of that causal. |
| 2 | +CREATE OR REPLACE FUNCTION dependencies_of_causals(the_causal_ids INTEGER[]) RETURNS TABLE (hash TEXT) AS $$ |
| 3 | + WITH RECURSIVE all_causals(causal_id, causal_hash, causal_namespace_hash_id) AS ( |
| 4 | + -- Base causal |
| 5 | + SELECT causal.id, causal.hash, causal.namespace_hash_id |
| 6 | + FROM UNNEST(the_causal_ids) AS causal_id |
| 7 | + JOIN causals causal ON causal.id = causal_id |
| 8 | + UNION |
| 9 | + -- This nested CTE is required because RECURSIVE CTEs can't refer |
| 10 | + -- to the recursive table more than once. |
| 11 | + -- I don't fully understand why or how this works, but it does |
| 12 | + ( WITH rec AS ( |
| 13 | + SELECT tc.causal_id, tc.causal_namespace_hash_id |
| 14 | + FROM all_causals tc |
| 15 | + ) |
| 16 | + SELECT ancestor_causal.id, ancestor_causal.hash, ancestor_causal.namespace_hash_id |
| 17 | + FROM causal_ancestors ca |
| 18 | + JOIN rec tc ON ca.causal_id = tc.causal_id |
| 19 | + JOIN causals ancestor_causal ON ca.ancestor_id = ancestor_causal.id |
| 20 | + UNION |
| 21 | + SELECT child_causal.id, child_causal.hash, child_causal.namespace_hash_id |
| 22 | + FROM rec tc |
| 23 | + JOIN namespace_children nc ON tc.causal_namespace_hash_id = nc.parent_namespace_hash_id |
| 24 | + JOIN causals child_causal ON nc.child_causal_id = child_causal.id |
| 25 | + ) |
| 26 | + ), all_namespaces(namespace_hash_id, namespace_hash) AS ( |
| 27 | + SELECT DISTINCT tc.causal_namespace_hash_id AS namespace_hash_id, bh.base32 as namespace_hash |
| 28 | + FROM all_causals tc |
| 29 | + JOIN branch_hashes bh ON tc.causal_namespace_hash_id = bh.id |
| 30 | + ), all_patches(patch_id, patch_hash) AS ( |
| 31 | + SELECT DISTINCT patch.id, patch.hash |
| 32 | + FROM all_namespaces an |
| 33 | + JOIN namespace_patches np ON an.namespace_hash_id = np.namespace_hash_id |
| 34 | + JOIN patches patch ON np.patch_id = patch.id |
| 35 | + ), |
| 36 | + -- term components to start transitively joining dependencies to |
| 37 | + base_term_components(component_hash_id) AS ( |
| 38 | + SELECT DISTINCT term.component_hash_id |
| 39 | + FROM all_namespaces an |
| 40 | + JOIN namespace_terms nt ON an.namespace_hash_id = nt.namespace_hash_id |
| 41 | + JOIN terms term ON nt.term_id = term.id |
| 42 | + UNION |
| 43 | + SELECT DISTINCT term.component_hash_id |
| 44 | + FROM all_patches ap |
| 45 | + JOIN patch_term_mappings ptm ON ap.patch_id = ptm.patch_id |
| 46 | + JOIN terms term ON ptm.to_term_id = term.id |
| 47 | + UNION |
| 48 | + -- term metadata |
| 49 | + SELECT DISTINCT term.component_hash_id |
| 50 | + FROM all_namespaces an |
| 51 | + JOIN namespace_terms nt ON an.namespace_hash_id = nt.namespace_hash_id |
| 52 | + JOIN namespace_term_metadata meta ON nt.id = meta.named_term |
| 53 | + JOIN terms term ON meta.metadata_term_id = term.id |
| 54 | + UNION |
| 55 | + -- type metadata |
| 56 | + SELECT DISTINCT term.component_hash_id |
| 57 | + FROM all_namespaces an |
| 58 | + JOIN namespace_types nt ON an.namespace_hash_id = nt.namespace_hash_id |
| 59 | + JOIN namespace_type_metadata meta ON nt.id = meta.named_type |
| 60 | + JOIN terms term ON meta.metadata_term_id = term.id |
| 61 | + ), |
| 62 | + -- type components to start transitively joining dependencies to |
| 63 | + base_type_components(component_hash_id) AS ( |
| 64 | + SELECT DISTINCT typ.component_hash_id |
| 65 | + FROM all_namespaces an |
| 66 | + JOIN namespace_types nt ON an.namespace_hash_id = nt.namespace_hash_id |
| 67 | + JOIN types typ ON nt.type_id = typ.id |
| 68 | + UNION |
| 69 | + SELECT DISTINCT typ.component_hash_id |
| 70 | + FROM all_namespaces an |
| 71 | + JOIN namespace_terms nt ON an.namespace_hash_id = nt.namespace_hash_id |
| 72 | + JOIN constructors con ON nt.constructor_id = con.id |
| 73 | + JOIN types typ ON con.type_id = typ.id |
| 74 | + UNION |
| 75 | + SELECT DISTINCT typ.component_hash_id |
| 76 | + FROM all_patches ap |
| 77 | + JOIN patch_type_mappings ptm ON ap.patch_id = ptm.patch_id |
| 78 | + JOIN types typ ON ptm.to_type_id = typ.id |
| 79 | + UNION |
| 80 | + SELECT DISTINCT typ.component_hash_id |
| 81 | + FROM all_patches ap |
| 82 | + JOIN patch_constructor_mappings pcm ON ap.patch_id = pcm.patch_id |
| 83 | + JOIN constructors con ON pcm.to_constructor_id = con.id |
| 84 | + JOIN types typ ON con.type_id = typ.id |
| 85 | + ), |
| 86 | + -- All the dependencies we join in transitively from the known term & type components we depend on. |
| 87 | + all_components(component_hash_id) AS ( |
| 88 | + SELECT DISTINCT btc.component_hash_id |
| 89 | + FROM base_term_components btc |
| 90 | + UNION |
| 91 | + SELECT DISTINCT btc.component_hash_id |
| 92 | + FROM base_type_components btc |
| 93 | + UNION |
| 94 | + ( WITH rec AS ( |
| 95 | + SELECT DISTINCT ac.component_hash_id |
| 96 | + FROM all_components ac |
| 97 | + ) |
| 98 | + -- recursively union in term dependencies |
| 99 | + SELECT DISTINCT ref.component_hash_id |
| 100 | + FROM rec atc |
| 101 | + -- This joins in ALL the terms from the component, not just the one that caused the dependency on the |
| 102 | + -- component |
| 103 | + JOIN terms term ON atc.component_hash_id = term.component_hash_id |
| 104 | + JOIN term_local_component_references ref ON term.id = ref.term_id |
| 105 | + UNION |
| 106 | + -- recursively union in type dependencies |
| 107 | + SELECT DISTINCT ref.component_hash_id |
| 108 | + FROM rec atc |
| 109 | + -- This joins in ALL the types from the component, not just the one that caused the dependency on the |
| 110 | + -- component |
| 111 | + JOIN types typ ON atc.component_hash_id = typ.component_hash_id |
| 112 | + JOIN type_local_component_references ref ON typ.id = ref.type_id |
| 113 | + ) |
| 114 | + ) |
| 115 | + (SELECT ch.base32 AS hash |
| 116 | + FROM all_components ac |
| 117 | + JOIN component_hashes ch ON ac.component_hash_id = ch.id |
| 118 | + ) |
| 119 | + UNION ALL |
| 120 | + (SELECT ap.patch_hash AS hash |
| 121 | + FROM all_patches ap |
| 122 | + ) |
| 123 | + UNION ALL |
| 124 | + (SELECT an.namespace_hash AS hash |
| 125 | + FROM all_namespaces an |
| 126 | + ) |
| 127 | + UNION ALL |
| 128 | + (SELECT ac.causal_hash AS hash |
| 129 | + FROM all_causals ac |
| 130 | + ) |
| 131 | +$$ LANGUAGE SQL; |
0 commit comments