Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE OR REPLACE FUNCTION public.compute_arity_id(p_schema_id bigint)
RETURNS smallint
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT COALESCE(jsonb_array_length(literal_content->'roles'), 0) FROM public."Concept" WHERE id=p_schema_id;
$function$;

CREATE OR REPLACE FUNCTION public.compute_arity_lit(lit_content jsonb)
RETURNS smallint
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT COALESCE(jsonb_array_length(lit_content->'roles'), 0);
$function$;

CREATE OR REPLACE FUNCTION public.compute_arity_local(schema_id bigint, lit_content jsonb)
RETURNS smallint
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT CASE WHEN schema_id IS NULL THEN compute_arity_lit(lit_content) ELSE compute_arity_id(schema_id) END;
$function$;

ALTER TABLE "public"."Concept" DROP COLUMN "arity";

ALTER TABLE "public"."Concept" ADD COLUMN "arity" smallint GENERATED ALWAYS AS (compute_arity_local(schema_id, literal_content)) STORED;

CREATE OR REPLACE FUNCTION public.extract_references(refs jsonb)
RETURNS bigint []
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT COALESCE(array_agg(i::bigint), '{}') FROM (SELECT DISTINCT jsonb_array_elements(jsonb_path_query_array(refs, '$.*[*]')) i) exrefs;
$function$;
19 changes: 17 additions & 2 deletions packages/database/supabase/schemas/concept.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,24 @@ CREATE TYPE public."EpistemicStatus" AS ENUM (
ALTER TYPE public."EpistemicStatus" OWNER TO postgres;

CREATE OR REPLACE FUNCTION extract_references(refs JSONB) RETURNS BIGINT [] LANGUAGE sql IMMUTABLE AS $$
SELECT COALESCE(array_agg(i::bigint), '{}') FROM (SELECT jsonb_array_elements(jsonb_path_query_array(refs, '$.*[*]')) i) exrefs;
SELECT COALESCE(array_agg(i::bigint), '{}') FROM (SELECT DISTINCT jsonb_array_elements(jsonb_path_query_array(refs, '$.*[*]')) i) exrefs;
$$;

CREATE OR REPLACE FUNCTION compute_arity_lit(lit_content JSONB) RETURNS smallint language sql IMMUTABLE AS $$
SELECT COALESCE(jsonb_array_length(lit_content->'roles'), 0);
$$;

SET check_function_bodies = false;
CREATE OR REPLACE FUNCTION compute_arity_id(p_schema_id BIGINT) RETURNS smallint language sql IMMUTABLE AS $$
SELECT COALESCE(jsonb_array_length(literal_content->'roles'), 0) FROM public."Concept" WHERE id=p_schema_id;
$$;
SET check_function_bodies = true;

CREATE OR REPLACE FUNCTION compute_arity_local(schema_id BIGINT, lit_content JSONB) RETURNS smallint language sql IMMUTABLE AS $$
SELECT CASE WHEN schema_id IS NULL THEN compute_arity_lit(lit_content) ELSE compute_arity_id(schema_id) END;
$$;


CREATE TABLE IF NOT EXISTS public."Concept" (
id bigint DEFAULT nextval(
'public.entity_id_seq'::regclass
Expand All @@ -27,7 +42,7 @@ CREATE TABLE IF NOT EXISTS public."Concept" (
created timestamp without time zone NOT NULL,
last_modified timestamp without time zone NOT NULL,
space_id bigint NOT NULL,
arity smallint DEFAULT 0 NOT NULL,
arity smallint GENERATED ALWAYS AS (compute_arity_local(schema_id, literal_content)) STORED,
schema_id bigint,
literal_content jsonb NOT NULL DEFAULT '{}'::jsonb,
reference_content jsonb NOT NULL DEFAULT '{}'::jsonb,
Expand Down