--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;

--
-- Name: functions; Type: SCHEMA; Schema: -; Owner: -
--

CREATE SCHEMA functions;


SET search_path = functions, pg_catalog;

--
-- Name: datatype; Type: TYPE; Schema: functions; Owner: -
--

CREATE TYPE datatype AS ENUM (
    'str',
    'float'
);


--
-- Name: _alt(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION _alt() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := coalesce(new."0", new."1", new."2", new."3", new."4", new."5",
        new."6", new."7", new."8", new."9");
    RETURN new;
END;
$$;


--
-- Name: _label(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION _label() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := (SELECT new.label||': '||new.value);
    RETURN new;
END;
$$;


--
-- Name: _merge(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION _merge() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    new.result := (
        SELECT functions.join_strs('; ', value)
        FROM
        (
            SELECT *
            FROM
            (
                SELECT
                DISTINCT ON (value)
                *
                FROM
                (VALUES
                      (0, new."0")
                    , (1, new."1")
                    , (2, new."2")
                    , (3, new."3")
                    , (4, new."4")
                    , (5, new."5")
                    , (6, new."6")
                    , (7, new."7")
                    , (8, new."8")
                    , (9, new."9")
                )
                AS v (sort_order, value)
            )
            AS v
            ORDER BY sort_order
        )
        AS v
    );
    RETURN new;
END;
$$;


--
-- Name: _nullIf(); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION "_nullIf"() RETURNS trigger
    LANGUAGE plpgsql IMMUTABLE
    AS $$
BEGIN
    IF new.type = 'str' THEN -- no cast needed
        new.result := (SELECT nullif(new.value, new."null"));
    -- Invalid value is ignored, but invalid null value generates error
    ELSIF new.type = 'float' THEN
        DECLARE
            -- Outside the try block so that invalid null value generates error
            "null" double precision := new."null"::double precision;
        BEGIN
            new.result := (SELECT nullif(new.value::double precision, "null"));
        EXCEPTION
            WHEN data_exception THEN NULL; -- ignore invalid value
        END;
    END IF;
    
    RETURN new;
END;
$$;


--
-- Name: ensure_not_null(text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION ensure_not_null(value text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT COALESCE($1, E'\\N');
$_$;


--
-- Name: join_strs_(text, text, text); Type: FUNCTION; Schema: functions; Owner: -
--

CREATE FUNCTION join_strs_(state text, delim text, value text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT $1 || (CASE
WHEN $1 = '' OR $3 IS NULL OR $3 = ''
THEN ''
ELSE $2
END) || coalesce($3, '');
$_$;


--
-- Name: join_strs(text, text); Type: AGGREGATE; Schema: functions; Owner: -
--

CREATE AGGREGATE join_strs(text, text) (
    SFUNC = join_strs_,
    STYPE = text,
    INITCOND = ''
);


SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: _alt; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE _alt (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    "0" text,
    "1" text,
    "2" text,
    "3" text,
    "4" text,
    "5" text,
    "6" text,
    "7" text,
    "8" text,
    "9" text
);


--
-- Name: _label; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE _label (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    label text NOT NULL,
    value text
);


--
-- Name: _merge; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE _merge (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    "0" text,
    "1" text,
    "2" text,
    "3" text,
    "4" text,
    "5" text,
    "6" text,
    "7" text,
    "8" text,
    "9" text
);


--
-- Name: _nullIf; Type: TABLE; Schema: functions; Owner: -; Tablespace: 
--

CREATE TABLE "_nullIf" (
    result text,
    not_null_col boolean DEFAULT true NOT NULL,
    "null" text NOT NULL,
    type datatype DEFAULT 'str'::datatype NOT NULL,
    value text
);


--
-- Name: _alt_0; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_0 ON _alt USING btree ((COALESCE("0", '\\N'::text)));


--
-- Name: _alt_1; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_1 ON _alt USING btree ((COALESCE("1", '\\N'::text)));


--
-- Name: _alt_2; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_2 ON _alt USING btree ((COALESCE("2", '\\N'::text)));


--
-- Name: _alt_3; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_3 ON _alt USING btree ((COALESCE("3", '\\N'::text)));


--
-- Name: _alt_4; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_4 ON _alt USING btree ((COALESCE("4", '\\N'::text)));


--
-- Name: _alt_5; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_5 ON _alt USING btree ((COALESCE("5", '\\N'::text)));


--
-- Name: _alt_6; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_6 ON _alt USING btree ((COALESCE("6", '\\N'::text)));


--
-- Name: _alt_7; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_7 ON _alt USING btree ((COALESCE("7", '\\N'::text)));


--
-- Name: _alt_8; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_8 ON _alt USING btree ((COALESCE("8", '\\N'::text)));


--
-- Name: _alt_9; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _alt_9 ON _alt USING btree ((COALESCE("9", '\\N'::text)));


--
-- Name: _alt_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX _alt_unique ON _alt USING btree ((COALESCE("0", '\\N'::text)), (COALESCE("1", '\\N'::text)), (COALESCE("2", '\\N'::text)), (COALESCE("3", '\\N'::text)), (COALESCE("4", '\\N'::text)), (COALESCE("5", '\\N'::text)), (COALESCE("6", '\\N'::text)), (COALESCE("7", '\\N'::text)), (COALESCE("8", '\\N'::text)), (COALESCE("9", '\\N'::text)));


--
-- Name: _label_label; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _label_label ON _label USING btree ((COALESCE(label, '\\N'::text)));


--
-- Name: _label_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX _label_unique ON _label USING btree ((COALESCE(label, '\\N'::text)), (COALESCE(value, '\\N'::text)));


--
-- Name: _label_value; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _label_value ON _label USING btree (value);


--
-- Name: _merge_0; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_0 ON _merge USING btree ((COALESCE("0", '\\N'::text)));


--
-- Name: _merge_1; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_1 ON _merge USING btree ((COALESCE("1", '\\N'::text)));


--
-- Name: _merge_2; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_2 ON _merge USING btree ((COALESCE("2", '\\N'::text)));


--
-- Name: _merge_3; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_3 ON _merge USING btree ((COALESCE("3", '\\N'::text)));


--
-- Name: _merge_4; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_4 ON _merge USING btree ((COALESCE("4", '\\N'::text)));


--
-- Name: _merge_5; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_5 ON _merge USING btree ((COALESCE("5", '\\N'::text)));


--
-- Name: _merge_6; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_6 ON _merge USING btree ((COALESCE("6", '\\N'::text)));


--
-- Name: _merge_7; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_7 ON _merge USING btree ((COALESCE("7", '\\N'::text)));


--
-- Name: _merge_8; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_8 ON _merge USING btree ((COALESCE("8", '\\N'::text)));


--
-- Name: _merge_9; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX _merge_9 ON _merge USING btree ((COALESCE("9", '\\N'::text)));


--
-- Name: _merge_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX _merge_unique ON _merge USING btree ((COALESCE("0", '\\N'::text)), (COALESCE("1", '\\N'::text)), (COALESCE("2", '\\N'::text)), (COALESCE("3", '\\N'::text)), (COALESCE("4", '\\N'::text)), (COALESCE("5", '\\N'::text)), (COALESCE("6", '\\N'::text)), (COALESCE("7", '\\N'::text)), (COALESCE("8", '\\N'::text)), (COALESCE("9", '\\N'::text)));


--
-- Name: _nullIf_null; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX "_nullIf_null" ON "_nullIf" USING btree ("null");


--
-- Name: _nullIf_type; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX "_nullIf_type" ON "_nullIf" USING btree (type);


--
-- Name: _nullIf_unique; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX "_nullIf_unique" ON "_nullIf" USING btree ("null", type, (COALESCE(value, '\\N'::text)));


--
-- Name: _nullIf_value; Type: INDEX; Schema: functions; Owner: -; Tablespace: 
--

CREATE INDEX "_nullIf_value" ON "_nullIf" USING btree (value);


--
-- Name: _alt; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER _alt BEFORE INSERT OR UPDATE ON _alt FOR EACH ROW EXECUTE PROCEDURE _alt();


--
-- Name: _label; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER _label BEFORE INSERT OR UPDATE ON _label FOR EACH ROW EXECUTE PROCEDURE _label();


--
-- Name: _merge; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER _merge BEFORE INSERT OR UPDATE ON _merge FOR EACH ROW EXECUTE PROCEDURE _merge();


--
-- Name: _nullIf; Type: TRIGGER; Schema: functions; Owner: -
--

CREATE TRIGGER "_nullIf" BEFORE INSERT OR UPDATE ON "_nullIf" FOR EACH ROW EXECUTE PROCEDURE "_nullIf"();


--
-- PostgreSQL database dump complete
--

