<commit_before>--db:fhirb
--{{{

--TODO: handle publish date

CREATE OR REPLACE FUNCTION
insert_resource(_rsrs jsonb, _tags jsonb)
RETURNS uuid LANGUAGE plpgsql AS $$
DECLARE
  id uuid := gen_random_uuid();
BEGIN
  RETURN insert_resource(id, _rsrs, _tags);
END
$$;

CREATE OR REPLACE FUNCTION
numeric_to_sortable_varchar(i real)
RETURNS varchar LANGUAGE sql AS $$
  SELECT to_char(i, 'SG0000000000000000D99999');
$$;

--private
CREATE OR REPLACE FUNCTION
make_search_index(_id uuid, res_type varchar)
RETURNS text
LANGUAGE plpgsql AS $$
BEGIN
  EXECUTE
    eval_template($SQL$
      INSERT INTO {{tbl}}_sort
      (resource_id, param, lower, upper)

      SELECT resource_id, param, MIN(value), MAX(value)
        FROM {{tbl}}_search_string
       WHERE resource_id = $1
    GROUP BY param, resource_id

       UNION

      SELECT resource_id, param, MIN(code), MAX(code)
        FROM {{tbl}}_search_token
       WHERE resource_id = $1
    GROUP BY param, resource_id

       UNION<commit_msg>index date/time values for sorting<commit_after>--db:fhirb
--{{{

--TODO: handle publish date

CREATE OR REPLACE FUNCTION
insert_resource(_rsrs jsonb, _tags jsonb)
RETURNS uuid LANGUAGE plpgsql AS $$
DECLARE
  id uuid := gen_random_uuid();
BEGIN
  RETURN insert_resource(id, _rsrs, _tags);
END
$$;

CREATE OR REPLACE FUNCTION
numeric_to_sortable_varchar(i real)
RETURNS varchar LANGUAGE sql AS $$
  SELECT to_char(i, 'SG0000000000000000D99999');
$$;

--private
CREATE OR REPLACE FUNCTION
make_search_index(_id uuid, res_type varchar)
RETURNS text
LANGUAGE plpgsql AS $$
BEGIN
  EXECUTE
    eval_template($SQL$
      INSERT INTO {{tbl}}_sort
      (resource_id, param, lower, upper)

      SELECT resource_id, param, MIN(value), MAX(value)
        FROM {{tbl}}_search_string
       WHERE resource_id = $1
    GROUP BY param, resource_id

       UNION

      SELECT resource_id, param, MIN(code), MAX(code)
        FROM {{tbl}}_search_token
       WHERE resource_id = $1
    GROUP BY param, resource_id

       UNION

      SELECT resource_id, param, "start"::varchar, "end"::varchar
        FROM {{tbl}}_search_date
       WHERE resource_id = $1
    GROUP BY param, resource_id, "start", "end"

       UNION
{'fraction_non_alphanumeric': 0.09504320145520691, 'fraction_numerical': 0.021373351523419737, 'mean_word_length': 3.6413502109704643, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5363136', 'n_tokens_mistral': 809, 'n_tokens_neox': 734, 'n_words': 212}