 
 -- expand polimorphic types
 CREATE
-VIEW fhir.expanded_resource_elements as (
+VIEW fhir.polimorphic_expanded_resource_elements as (
   SELECT
     array_pop(path) || ARRAY[column_name(array_last(path), type)] as path,
     type,

      CASE WHEN array_length(t.path,1) = 1
        THEN e.max
        ELSE t.max
      END AS max
    FROM fhir.expanded_resource_elements e
    JOIN fhir.datatype_unified_elements t
    ON t.path[1] = e.type
);


-- TODO: insert procedure

--}}}
--{{{

select * from fhir.expanded_resource_elements
where path[1]='Patient' and path[2]='address'
order by path
--}}}
