+ date
Tue Apr  2 15:19:52 PDT 2013
+ time env commit=1 env in_database=vegbien in_schema=ACAD in_table=Specimen out_database=vegbien ../../map Specimen/VegBIEN.csv
Processing input rows 0-99
Using 1 parallel CPUs
Connecting to PostgreSQL database vegbien
<pre><code class="SQL">
SELECT * FROM "ACAD"."Specimen"
LIMIT 0
</code></pre>Put template:
<VegBIEN>
    <_setDefault id="-1">
        <source_id><source><shortname>ACAD</shortname></source></source_id>
        <path>
            <_simplifyPath>
                <next>parent_id</next>
                <path>
                    <location>
                        <authorlocationcode>
                            <_alt>
                                <1>$id</1>
                                <2>
                                    <_join>
                                        <1>
                                            <_map>
                                                <Acadia University>ACAD</Acadia University>
                                                <*>*</*>
                                                <value>$institutionCode</value>
                                            </_map>
                                        </1>
                                        <2>
                                            <_join>
                                                <1>$collectionCode</1>
                                                <2>$collectionID</2>
                                            </_join>
                                        </2>
                                        <3>$catalogNumber</3>
                                    </_join>
                                </2>
                            </_alt>
                        </authorlocationcode>
                        <accessconditions>$rights</accessconditions>
                        <elevation_m>
                            <_units>
                                <to>m</to>
                                <to></to>
                                <value>$verbatimElevation</value>
                            </_units>
                        </elevation_m>
                        <iscultivated>
                            <_locationnarrative_is_cultivated>
                                <locationnarrative>
                                    <_join>
                                        <1>$locality</1>
                                        <3>$habitat</3>
                                    </_join>
                                </locationnarrative>
                            </_locationnarrative_is_cultivated>
                        </iscultivated>
                        <locationevent>
                            <obsenddate><_dateRangeEnd><value>$eventDate</value></_dateRangeEnd></obsenddate>
                            <obsstartdate><_dateRangeStart><value>$eventDate</value></_dateRangeStart></obsstartdate>
                            <taxonoccurrence>
                                <aggregateoccurrence>
                                    <notes>$occurrenceRemarks</notes>
                                    <plantobservation>
                                        <authorplantcode>$recordNumber</authorplantcode>
                                        <reproductivecondition>$reproductiveCondition</reproductivecondition>
                                        <specimenreplicate>
                                            <catalognumber_dwc>$catalogNumber</catalognumber_dwc>
                                            <collectioncode_dwc>
                                                <_alt>
                                                    <1>$collectionCode</1>
                                                    <2>$collectionID</2>
                                                </_alt>
                                            </collectioncode_dwc>
                                            <collectionnumber>$recordNumber</collectionnumber>
                                            <institution_id>
                                                <sourcelist>
                                                    <name>
                                                        <_map>
                                                            <Acadia University>ACAD</Acadia University>
                                                            <*>*</*>
                                                            <value>$institutionCode</value>
                                                        </_map>
                                                    </name>
                                                    <sourcename>
                                                        <name>
                                                            <_split>
                                                                <separator>[,;] *</separator>
                                                                <value>
                                                                    <_map>
                                                                        <Acadia University>ACAD</Acadia University>
                                                                        <*>*</*>
                                                                        <value>$institutionCode</value>
                                                                    </_map>
                                                                </value>
                                                            </_split>
                                                        </name>
                                                    </sourcename>
                                                </sourcelist>
                                            </institution_id>
                                            <sourceaccessioncode>$id</sourceaccessioncode>
                                        </specimenreplicate>
                                    </plantobservation>
                                </aggregateoccurrence>
                                <collector_id><party><fullname>$recordedBy</fullname></party></collector_id>
                                <sourceaccessioncode>$id</sourceaccessioncode>
                                <taxondetermination>
                                    <party_id><party><fullname>$identifiedBy</fullname></party></party_id>
                                    <taxonverbatim_id>
                                        <taxonverbatim>
                                            <taxonlabel_id>
                                                <taxonlabel>
                                                    <parent_id>
                                                        <taxonlabel>
                                                            <rank>$taxonRank</rank>
                                                            <parent_id>
                                                                <taxonlabel>
                                                                    <rank>cultivar</rank>
                                                                    <parent_id>
                                                                        <taxonlabel>
                                                                            <rank>forma</rank>
                                                                            <parent_id>
                                                                                <taxonlabel>
                                                                                    <rank>variety</rank>
                                                                                    <parent_id>
                                                                                        <taxonlabel>
                                                                                            <rank>subspecies</rank>
                                                                                            <parent_id>
                                                                                                <taxonlabel>
                                                                                                    <rank>species</rank>
                                                                                                    <taxonepithet>$specificEpithet</taxonepithet>
                                                                                                    <parent_id>
                                                                                                        <taxonlabel>
                                                                                                            <rank>genus</rank>
                                                                                                            <taxonepithet>$genus</taxonepithet>
                                                                                                            <parent_id>
                                                                                                                <taxonlabel>
                                                                                                                    <rank>family</rank>
                                                                                                                    <taxonepithet>$family</taxonepithet>
                                                                                                                    <parent_id>
                                                                                                                        <taxonlabel>
                                                                                                                            <rank>order</rank>
                                                                                                                            <taxonepithet>$order</taxonepithet>
                                                                                                                            <parent_id>
                                                                                                                                <taxonlabel>
                                                                                                                                    <rank>class</rank>
                                                                                                                                    <taxonepithet>$class</taxonepithet>
                                                                                                                                    <parent_id>
                                                                                                                                        <taxonlabel>
                                                                                                                                            <rank>phylum</rank>
                                                                                                                                            <taxonepithet>$phylum</taxonepithet>
                                                                                                                                            <parent_id>
                                                                                                                                                <taxonlabel>
                                                                                                                                                    <rank>kingdom</rank>
                                                                                                                                                    <taxonepithet>$kingdom</taxonepithet>
                                                                                                                                                </taxonlabel>
                                                                                                                                            </parent_id>
                                                                                                                                        </taxonlabel>
                                                                                                                                    </parent_id>
                                                                                                                                </taxonlabel>
                                                                                                                            </parent_id>
                                                                                                                        </taxonlabel>
                                                                                                                    </parent_id>
                                                                                                                </taxonlabel>
                                                                                                            </parent_id>
                                                                                                        </taxonlabel>
                                                                                                    </parent_id>
                                                                                                </taxonlabel>
                                                                                            </parent_id>
                                                                                        </taxonlabel>
                                                                                    </parent_id>
                                                                                </taxonlabel>
                                                                            </parent_id>
                                                                        </taxonlabel>
                                                                    </parent_id>
                                                                </taxonlabel>
                                                            </parent_id>
                                                            <taxonepithet>$infraspecificEpithet</taxonepithet>
                                                        </taxonlabel>
                                                    </parent_id>
                                                    <rank>$taxonRank</rank>
                                                    <taxonomicname>
                                                        <_merge_prefix>
                                                            <prefix><_taxon_family_require_std><family>$family</family></_taxon_family_require_std></prefix>
                                                            <value>
                                                                <_alt>
                                                                    <1>$scientificName</1>
                                                                    <2>
                                                                        <_join_words>
                                                                            <1>
                                                                                <_join_words>
                                                                                    <2>$genus</2>
                                                                                    <3>$specificEpithet</3>
                                                                                    <5>
                                                                                        <_join_words>
                                                                                            <1>
                                                                                                <_if name="if has infraspecificEpithet">
                                                                                                    <cond>$infraspecificEpithet</cond>
                                                                                                    <then>$taxonRank</then>
                                                                                                </_if>
                                                                                            </1>
                                                                                            <2>$infraspecificEpithet</2>
                                                                                        </_join_words>
                                                                                    </5>
                                                                                </_join_words>
                                                                            </1>
                                                                            <2>$scientificNameAuthorship</2>
                                                                        </_join_words>
                                                                    </2>
                                                                </_alt>
                                                            </value>
                                                        </_merge_prefix>
                                                    </taxonomicname>
                                                </taxonlabel>
                                            </taxonlabel_id>
                                            <author>$scientificNameAuthorship</author>
                                            <family>$family</family>
                                            <genus>$genus</genus>
                                            <specific_epithet>$specificEpithet</specific_epithet>
                                            <taxonomicname>$scientificName</taxonomicname>
                                            <verbatimrank>$taxonRank</verbatimrank>
                                        </taxonverbatim>
                                    </taxonverbatim_id>
                                    <determinationdate><_dateRangeStart><value>$dateIdentified</value></_dateRangeStart></determinationdate>
                                </taxondetermination>
                            </taxonoccurrence>
                        </locationevent>
                        <locationnarrative>
                            <_merge>
                                <1>$locality</1>
                                <3>
                                    <_label>
                                        <label>habitat</label>
                                        <value>$habitat</value>
                                    </_label>
                                </3>
                                <4>$locationRemarks</4>
                            </_merge>
                        </locationnarrative>
                        <locationplace>
                            <place_id>
                                <place>
                                    <coordinates_id>
                                        <coordinates>
                                            <latitude_deg>
                                                <_nullIf>
                                                    <null>0</null>
                                                    <type>float</type>
                                                    <value>$decimalLatitude</value>
                                                </_nullIf>
                                            </latitude_deg>
                                            <longitude_deg>
                                                <_nullIf>
                                                    <null>0</null>
                                                    <type>float</type>
                                                    <value>$decimalLongitude</value>
                                                </_nullIf>
                                            </longitude_deg>
                                        </coordinates>
                                    </coordinates_id>
                                    <continent>$continent</continent>
                                    <country>$country</country>
                                    <county>$county</county>
                                    <matched_place_id>
                                        <place>
                                            <source_id><source><shortname>geoscrub</shortname></source></source_id>
                                            <coordinates_id>
                                                <coordinates>
                                                    <source_id><source><shortname>geoscrub</shortname></source></source_id>
                                                    <latitude_deg>
                                                        <_nullIf>
                                                            <null>0</null>
                                                            <type>float</type>
                                                            <value>$decimalLatitude</value>
                                                        </_nullIf>
                                                    </latitude_deg>
                                                    <longitude_deg>
                                                        <_nullIf>
                                                            <null>0</null>
                                                            <type>float</type>
                                                            <value>$decimalLongitude</value>
                                                        </_nullIf>
                                                    </longitude_deg>
                                                </coordinates>
                                            </coordinates_id>
                                            <country>$country</country>
                                            <county>$county</county>
                                            <stateprovince>$stateProvince</stateprovince>
                                        </place>
                                    </matched_place_id>
                                    <stateprovince>$stateProvince</stateprovince>
                                </place>
                            </place_id>
                        </locationplace>
                        <verbatimelevation>$verbatimElevation</verbatimelevation>
                    </location>
                </path>
            </_simplifyPath>
        </path>
    </_setDefault>
</VegBIEN>
Putting stripped:
<VegBIEN>
    <_setDefault id="-1">
        <source_id><source><shortname>ACAD</shortname></source></source_id>
        <path>
            <_simplifyPath>
                <next>parent_id</next>
                <path>
                    <location>
                        <authorlocationcode>
                            <_alt>
                                <1>$id</1>
                                <2>
                                    <_join>
                                        <1>
                                            <_map>
                                                <Acadia University>ACAD</Acadia University>
                                                <*>*</*>
                                                <value>$institutionCode</value>
                                            </_map>
                                        </1>
                                        <2>
                                            <_join>
                                                <1>$collectionCode</1>
                                                <2>$collectionID</2>
                                            </_join>
                                        </2>
                                        <3>$catalogNumber</3>
                                    </_join>
                                </2>
                            </_alt>
                        </authorlocationcode>
                        <accessconditions>$rights</accessconditions>
                        <elevation_m>$verbatimElevation</elevation_m>
                        <iscultivated>
                            <_locationnarrative_is_cultivated>
                                <locationnarrative>
                                    <_join>
                                        <1>$locality</1>
                                        <3>$habitat</3>
                                    </_join>
                                </locationnarrative>
                            </_locationnarrative_is_cultivated>
                        </iscultivated>
                        <locationevent>
                            <obsenddate><_dateRangeEnd><value>$eventDate</value></_dateRangeEnd></obsenddate>
                            <obsstartdate><_dateRangeStart><value>$eventDate</value></_dateRangeStart></obsstartdate>
                            <taxonoccurrence>
                                <aggregateoccurrence>
                                    <notes>$occurrenceRemarks</notes>
                                    <plantobservation>
                                        <authorplantcode>$recordNumber</authorplantcode>
                                        <reproductivecondition>$reproductiveCondition</reproductivecondition>
                                        <specimenreplicate>
                                            <catalognumber_dwc>$catalogNumber</catalognumber_dwc>
                                            <collectioncode_dwc>
                                                <_alt>
                                                    <1>$collectionCode</1>
                                                    <2>$collectionID</2>
                                                </_alt>
                                            </collectioncode_dwc>
                                            <collectionnumber>$recordNumber</collectionnumber>
                                            <institution_id>
                                                <sourcelist>
                                                    <name>
                                                        <_map>
                                                            <Acadia University>ACAD</Acadia University>
                                                            <*>*</*>
                                                            <value>$institutionCode</value>
                                                        </_map>
                                                    </name>
                                                    <sourcename>
                                                        <name>
                                                            <_split>
                                                                <separator>[,;] *</separator>
                                                                <value>
                                                                    <_map>
                                                                        <Acadia University>ACAD</Acadia University>
                                                                        <*>*</*>
                                                                        <value>$institutionCode</value>
                                                                    </_map>
                                                                </value>
                                                            </_split>
                                                        </name>
                                                    </sourcename>
                                                </sourcelist>
                                            </institution_id>
                                            <sourceaccessioncode>$id</sourceaccessioncode>
                                        </specimenreplicate>
                                    </plantobservation>
                                </aggregateoccurrence>
                                <collector_id><party><fullname>$recordedBy</fullname></party></collector_id>
                                <sourceaccessioncode>$id</sourceaccessioncode>
                                <taxondetermination>
                                    <party_id><party><fullname>$identifiedBy</fullname></party></party_id>
                                    <taxonverbatim_id>
                                        <taxonverbatim>
                                            <taxonlabel_id>
                                                <taxonlabel>
                                                    <parent_id>
                                                        <taxonlabel>
                                                            <rank>$taxonRank</rank>
                                                            <parent_id>
                                                                <taxonlabel>
                                                                    <rank>cultivar</rank>
                                                                    <parent_id>
                                                                        <taxonlabel>
                                                                            <rank>forma</rank>
                                                                            <parent_id>
                                                                                <taxonlabel>
                                                                                    <rank>variety</rank>
                                                                                    <parent_id>
                                                                                        <taxonlabel>
                                                                                            <rank>subspecies</rank>
                                                                                            <parent_id>
                                                                                                <taxonlabel>
                                                                                                    <rank>species</rank>
                                                                                                    <taxonepithet>$specificEpithet</taxonepithet>
                                                                                                    <parent_id>
                                                                                                        <taxonlabel>
                                                                                                            <rank>genus</rank>
                                                                                                            <taxonepithet>$genus</taxonepithet>
                                                                                                            <parent_id>
                                                                                                                <taxonlabel>
                                                                                                                    <rank>family</rank>
                                                                                                                    <taxonepithet>$family</taxonepithet>
                                                                                                                    <parent_id>
                                                                                                                        <taxonlabel>
                                                                                                                            <rank>order</rank>
                                                                                                                            <taxonepithet>$order</taxonepithet>
                                                                                                                            <parent_id>
                                                                                                                                <taxonlabel>
                                                                                                                                    <rank>class</rank>
                                                                                                                                    <taxonepithet>$class</taxonepithet>
                                                                                                                                    <parent_id>
                                                                                                                                        <taxonlabel>
                                                                                                                                            <rank>phylum</rank>
                                                                                                                                            <taxonepithet>$phylum</taxonepithet>
                                                                                                                                            <parent_id>
                                                                                                                                                <taxonlabel>
                                                                                                                                                    <rank>kingdom</rank>
                                                                                                                                                    <taxonepithet>$kingdom</taxonepithet>
                                                                                                                                                </taxonlabel>
                                                                                                                                            </parent_id>
                                                                                                                                        </taxonlabel>
                                                                                                                                    </parent_id>
                                                                                                                                </taxonlabel>
                                                                                                                            </parent_id>
                                                                                                                        </taxonlabel>
                                                                                                                    </parent_id>
                                                                                                                </taxonlabel>
                                                                                                            </parent_id>
                                                                                                        </taxonlabel>
                                                                                                    </parent_id>
                                                                                                </taxonlabel>
                                                                                            </parent_id>
                                                                                        </taxonlabel>
                                                                                    </parent_id>
                                                                                </taxonlabel>
                                                                            </parent_id>
                                                                        </taxonlabel>
                                                                    </parent_id>
                                                                </taxonlabel>
                                                            </parent_id>
                                                            <taxonepithet>$infraspecificEpithet</taxonepithet>
                                                        </taxonlabel>
                                                    </parent_id>
                                                    <rank>$taxonRank</rank>
                                                    <taxonomicname>
                                                        <_merge_prefix>
                                                            <prefix><_taxon_family_require_std><family>$family</family></_taxon_family_require_std></prefix>
                                                            <value>
                                                                <_alt>
                                                                    <1>$scientificName</1>
                                                                    <2>
                                                                        <_join_words>
                                                                            <1>
                                                                                <_join_words>
                                                                                    <2>$genus</2>
                                                                                    <3>$specificEpithet</3>
                                                                                    <5>
                                                                                        <_join_words>
                                                                                            <1>
                                                                                                <_if name="if has infraspecificEpithet">
                                                                                                    <cond>$infraspecificEpithet</cond>
                                                                                                    <then>$taxonRank</then>
                                                                                                </_if>
                                                                                            </1>
                                                                                            <2>$infraspecificEpithet</2>
                                                                                        </_join_words>
                                                                                    </5>
                                                                                </_join_words>
                                                                            </1>
                                                                            <2>$scientificNameAuthorship</2>
                                                                        </_join_words>
                                                                    </2>
                                                                </_alt>
                                                            </value>
                                                        </_merge_prefix>
                                                    </taxonomicname>
                                                </taxonlabel>
                                            </taxonlabel_id>
                                            <author>$scientificNameAuthorship</author>
                                            <family>$family</family>
                                            <genus>$genus</genus>
                                            <specific_epithet>$specificEpithet</specific_epithet>
                                            <taxonomicname>$scientificName</taxonomicname>
                                            <verbatimrank>$taxonRank</verbatimrank>
                                        </taxonverbatim>
                                    </taxonverbatim_id>
                                    <determinationdate><_dateRangeStart><value>$dateIdentified</value></_dateRangeStart></determinationdate>
                                </taxondetermination>
                            </taxonoccurrence>
                        </locationevent>
                        <locationnarrative>
                            <_merge>
                                <1>$locality</1>
                                <3>
                                    <_label>
                                        <label>habitat</label>
                                        <value>$habitat</value>
                                    </_label>
                                </3>
                                <4>$locationRemarks</4>
                            </_merge>
                        </locationnarrative>
                        <locationplace>
                            <place_id>
                                <place>
                                    <coordinates_id>
                                        <coordinates>
                                            <latitude_deg>
                                                <_nullIf>
                                                    <null>0</null>
                                                    <type>float</type>
                                                    <value>$decimalLatitude</value>
                                                </_nullIf>
                                            </latitude_deg>
                                            <longitude_deg>
                                                <_nullIf>
                                                    <null>0</null>
                                                    <type>float</type>
                                                    <value>$decimalLongitude</value>
                                                </_nullIf>
                                            </longitude_deg>
                                        </coordinates>
                                    </coordinates_id>
                                    <continent>$continent</continent>
                                    <country>$country</country>
                                    <county>$county</county>
                                    <matched_place_id>
                                        <place>
                                            <source_id><source><shortname>geoscrub</shortname></source></source_id>
                                            <coordinates_id>
                                                <coordinates>
                                                    <source_id><source><shortname>geoscrub</shortname></source></source_id>
                                                    <latitude_deg>
                                                        <_nullIf>
                                                            <null>0</null>
                                                            <type>float</type>
                                                            <value>$decimalLatitude</value>
                                                        </_nullIf>
                                                    </latitude_deg>
                                                    <longitude_deg>
                                                        <_nullIf>
                                                            <null>0</null>
                                                            <type>float</type>
                                                            <value>$decimalLongitude</value>
                                                        </_nullIf>
                                                    </longitude_deg>
                                                </coordinates>
                                            </coordinates_id>
                                            <country>$country</country>
                                            <county>$county</county>
                                            <stateprovince>$stateProvince</stateprovince>
                                        </place>
                                    </matched_place_id>
                                    <stateprovince>$stateProvince</stateprovince>
                                </place>
                            </place_id>
                        </locationplace>
                        <verbatimelevation>$verbatimElevation</verbatimelevation>
                    </location>
                </path>
            </_simplifyPath>
        </path>
    </_setDefault>
</VegBIEN>
********** Partition: rows 1-100 **********
<pre><code class="SQL">
CREATE TEMP TABLE "ACAD.Specimen" (
LIKE "ACAD"."Specimen" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "ACAD.Specimen"
SELECT * FROM "ACAD"."Specimen"
ORDER BY row_num
LIMIT 100
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_map"@:

|Output|Input|
|@'map'@|@{'Acadia University': u'ACAD', '*': u'*'}@|
|@'value'@|@"ACAD.Specimen"."institutionCode"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."institutionCode" AS "ACAD.Specimen.institutionCode"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_map(institutionCode)" AS
SELECT
"in"."row_num"
, "_map"("map" := hstore(ARRAY['Acadia University', '*'], ARRAY['ACAD', '*']), "value" := "in"."ACAD.Specimen.institutionCode") AS "result"
FROM "in"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_map(institutionCode)_wrap"()
RETURNS SETOF "_map(institutionCode)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_map"("map" := hstore(ARRAY['Acadia University', '*'], ARRAY['ACAD', '*']), "value" := "row"."ACAD.Specimen.institutionCode")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.institutionCode" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('institutionCode')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_map(institutionCode)"
("row_num", "result")
SELECT * FROM "pg_temp"."_map(institutionCode)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_join"@:

|Output|Input|
|@'1'@|@"ACAD.Specimen"."collectionCode"@|
|@'2'@|@"ACAD.Specimen"."collectionID"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#1" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."collectionCode" AS "ACAD.Specimen.collectionCode"
, "ACAD.Specimen"."collectionID" AS "ACAD.Specimen.collectionID"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#1"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_join(1=collectionCode, 2=collectionID)" AS
SELECT
"in#1"."row_num"
, "_join"("1" := "in#1"."ACAD.Specimen.collectionCode", "2" := "in#1"."ACAD.Specimen.collectionID") AS "result"
FROM "in#1"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_join(1=collectionCode, 2=collectionID)_wrap"()
RETURNS SETOF "_join(1=collectionCode, 2=collectionID)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#1"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#1"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_join"("1" := "row"."ACAD.Specimen.collectionCode", "2" := "row"."ACAD.Specimen.collectionID")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.collectionCode" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.collectionID" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('collectionCode,collectionID')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_join(1=collectionCode, 2=collectionID)"
("row_num", "result")
SELECT * FROM "pg_temp"."_join(1=collectionCode, 2=collectionID)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_join"@:

|Output|Input|
|@'1'@|@"_map(institutionCode)"."result"@|
|@'3'@|@"ACAD.Specimen"."catalogNumber"@|
|@'2'@|@"_join(1=collectionCode, 2=collectionID)"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#2" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_map(institutionCode)"."result" AS "_map(institutionCode).result"
, "ACAD.Specimen"."catalogNumber" AS "ACAD.Specimen.catalogNumber"
, "_join(1=collectionCode, 2=collectionID)"."result" AS "_join(1=collectionCode, 2=collectionID).result"
FROM "ACAD.Specimen"
JOIN "_map(institutionCode)" USING ("row_num")
JOIN "_join(1=collectionCode, 2=collectionID)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#2"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=colle" AS
SELECT
"in#2"."row_num"
, "_join"("1" := "in#2"."_map(institutionCode).result", "3" := "in#2"."ACAD.Specimen.catalogNumber", "2" := "in#2"."_join(1=collectionCode, 2=collectionID).result") AS "result"
FROM "in#2"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=_wrap"()
RETURNS SETOF "_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=colle"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#2"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#2"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_join"("1" := "row"."_map(institutionCode).result", "3" := "row"."ACAD.Specimen.catalogNumber", "2" := "row"."_join(1=collectionCode, 2=collectionID).result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."_map(institutionCode).result" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.catalogNumber" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."_join(1=collectionCode, 2=collectionID).result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('institutionCode,catalogNumber,collectionCode'), ('institutionCode,catalogNumber,collectionID')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=colle"
("row_num", "result")
SELECT * FROM "pg_temp"."_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_alt"@:

|Output|Input|
|@'1'@|@"ACAD.Specimen"."id"@|
|@'2'@|@"_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=colle"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#3" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."id" AS "ACAD.Specimen.id"
, "_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=colle"."result" AS "_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(.result"
FROM "ACAD.Specimen"
JOIN "_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(1=colle" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#3"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumber, 2=" AS
SELECT
"in#3"."row_num"
, "_alt"("1" := "in#3"."ACAD.Specimen.id", "2" := "in#3"."_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(.result") AS "result"
FROM "in#3"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumbe_wrap"()
RETURNS SETOF "_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumber, 2="
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#3"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#3"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_alt"("1" := "row"."ACAD.Specimen.id", "2" := "row"."_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(.result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.id" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."_join(1=_map(institutionCode), 3=catalogNumber, 2=_join(.result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('id,institutionCode'), ('id,catalogNumber'), ('id,collectionCode'), ('id,collectionID')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumber, 2="
("row_num", "result")
SELECT * FROM "pg_temp"."_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumbe_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_label"@:

|Output|Input|
|@'value'@|@"ACAD.Specimen"."habitat"@|
|@'label'@|@'habitat'@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#4" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."habitat" AS "ACAD.Specimen.habitat"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#4"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_label(habitat)" AS
SELECT
"in#4"."row_num"
, "_label"("value" := "in#4"."ACAD.Specimen.habitat", "label" := 'habitat') AS "result"
FROM "in#4"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_label(habitat)_wrap"()
RETURNS SETOF "_label(habitat)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#4"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#4"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_label"("value" := "row"."ACAD.Specimen.habitat", "label" := 'habitat')
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.habitat" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('habitat')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_label(habitat)"
("row_num", "result")
SELECT * FROM "pg_temp"."_label(habitat)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_merge"@:

|Output|Input|
|@'1'@|@"ACAD.Specimen"."locality"@|
|@'3'@|@"_label(habitat)"."result"@|
|@'4'@|@"ACAD.Specimen"."locationRemarks"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#5" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."locality" AS "ACAD.Specimen.locality"
, "_label(habitat)"."result" AS "_label(habitat).result"
, "ACAD.Specimen"."locationRemarks" AS "ACAD.Specimen.locationRemarks"
FROM "ACAD.Specimen"
JOIN "_label(habitat)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#5"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_merge(1=locality, 3=_label(habitat), 4=locationRemarks)" AS
SELECT
"in#5"."row_num"
, "_merge"("1" := "in#5"."ACAD.Specimen.locality", "3" := "in#5"."_label(habitat).result", "4" := "in#5"."ACAD.Specimen.locationRemarks") AS "result"
FROM "in#5"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_merge(1=locality, 3=_label(habitat), 4=locationRemarks)_wrap"()
RETURNS SETOF "_merge(1=locality, 3=_label(habitat), 4=locationRemarks)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#5"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#5"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_merge"("1" := "row"."ACAD.Specimen.locality", "3" := "row"."_label(habitat).result", "4" := "row"."ACAD.Specimen.locationRemarks")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.locality" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."_label(habitat).result" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.locationRemarks" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('locality,habitat,locationRemarks')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_merge(1=locality, 3=_label(habitat), 4=locationRemarks)"
("row_num", "result")
SELECT * FROM "pg_temp"."_merge(1=locality, 3=_label(habitat), 4=locationRemarks)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_join"@:

|Output|Input|
|@'1'@|@"ACAD.Specimen"."locality"@|
|@'3'@|@"ACAD.Specimen"."habitat"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#6" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."locality" AS "ACAD.Specimen.locality"
, "ACAD.Specimen"."habitat" AS "ACAD.Specimen.habitat"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#6"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_join(1=locality, 3=habitat)" AS
SELECT
"in#6"."row_num"
, "_join"("1" := "in#6"."ACAD.Specimen.locality", "3" := "in#6"."ACAD.Specimen.habitat") AS "result"
FROM "in#6"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_join(1=locality, 3=habitat)_wrap"()
RETURNS SETOF "_join(1=locality, 3=habitat)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#6"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#6"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_join"("1" := "row"."ACAD.Specimen.locality", "3" := "row"."ACAD.Specimen.habitat")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.locality" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.habitat" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('locality,habitat')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_join(1=locality, 3=habitat)"
("row_num", "result")
SELECT * FROM "pg_temp"."_join(1=locality, 3=habitat)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_locationnarrative_is_cultivated"@:

|Output|Input|
|@'locationnarrative'@|@"_join(1=locality, 3=habitat)"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#7" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_join(1=locality, 3=habitat)"."result" AS "_join(1=locality, 3=habitat).result"
FROM "ACAD.Specimen"
JOIN "_join(1=locality, 3=habitat)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#7"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_locationnarrative_is_cultivated(locationnarrative=_join(1=loca" AS
SELECT
"in#7"."row_num"
, "_locationnarrative_is_cultivated"("locationnarrative" := "in#7"."_join(1=locality, 3=habitat).result") AS "result"
FROM "in#7"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_locationnarrative_is_cultivated(locationnarrative=_join(1_wrap"()
RETURNS SETOF "_locationnarrative_is_cultivated(locationnarrative=_join(1=loca"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#7"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#7"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_locationnarrative_is_cultivated"("locationnarrative" := "row"."_join(1=locality, 3=habitat).result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."_join(1=locality, 3=habitat).result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('locality'), ('habitat')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS boolean)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_locationnarrative_is_cultivated(locationnarrative=_join(1=loca"
("row_num", "result")
SELECT * FROM "pg_temp"."_locationnarrative_is_cultivated(locationnarrative=_join(1_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"location"@:

|Output|Input|
|@'verbatimelevation'@|@"ACAD.Specimen"."verbatimElevation"@|
|@'authorlocationcode'@|@"_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumber, 2="."result"@|
|@'elevation_m'@|@"ACAD.Specimen"."verbatimElevation"@|
|@'locationnarrative'@|@"_merge(1=locality, 3=_label(habitat), 4=locationRemarks)"."result"@|
|@'accessconditions'@|@"ACAD.Specimen"."rights"@|
|@'iscultivated'@|@"_locationnarrative_is_cultivated(locationnarrative=_join(1=loca"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#8" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."verbatimElevation" AS "ACAD.Specimen.verbatimElevation"
, "_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumber, 2="."result" AS "_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNum.result"
, "_merge(1=locality, 3=_label(habitat), 4=locationRemarks)"."result" AS "_merge(1=locality, 3=_label(habitat), 4=locationRemarks).result"
, "ACAD.Specimen"."rights" AS "ACAD.Specimen.rights"
, "_locationnarrative_is_cultivated(locationnarrative=_join(1=loca"."result" AS "_locationnarrative_is_cultivated(locationnarrative=_join.result"
FROM "ACAD.Specimen"
JOIN "_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNumber, 2=" USING ("row_num")
JOIN "_merge(1=locality, 3=_label(habitat), 4=locationRemarks)" USING ("row_num")
JOIN "_locationnarrative_is_cultivated(locationnarrative=_join(1=loca" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#8"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#8_full" (
LIKE "in#8" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#8_full"
SELECT * FROM "in#8"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO location"()
RETURNS SETOF "location"."location_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "location"
("verbatimelevation", "authorlocationcode", "elevation_m", "locationnarrative", "accessconditions", "iscultivated")
SELECT
"in#8"."ACAD.Specimen.verbatimElevation"
, "in#8"."_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNum.result"
, "in#8"."ACAD.Specimen.verbatimElevation"
, "in#8"."_merge(1=locality, 3=_label(habitat), 4=locationRemarks).result"
, "in#8"."ACAD.Specimen.rights"
, "in#8"."_locationnarrative_is_cultivated(locationnarrative=_join.result"
FROM "in#8"
RETURNING "location_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @double precision@ on column: @elevation_m@
# Casting @'elevation_m'@ input to @double precision@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."double(verbatimElevation)"("value" anyelement)
RETURNS double precision
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS double precision);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('verbatimElevation')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#8" ADD COLUMN "ACAD.Specimen.verbatimElevation::double" double precision /*"in#8"."ACAD.Specimen.verbatimElevation"::double precision*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#8"
ALTER COLUMN "ACAD.Specimen.verbatimElevation::double" TYPE double precision
USING "pg_temp"."double(verbatimElevation)"("in#8"."ACAD.Specimen.verbatimElevation")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO location"()
RETURNS SETOF "location"."location_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "location"
("verbatimelevation", "authorlocationcode", "elevation_m", "locationnarrative", "accessconditions", "iscultivated")
SELECT
"in#8"."ACAD.Specimen.verbatimElevation"
, "in#8"."_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNum.result"
, "in#8"."ACAD.Specimen.verbatimElevation::double"
, "in#8"."_merge(1=locality, 3=_label(habitat), 4=locationRemarks).result"
, "in#8"."ACAD.Specimen.rights"
, "in#8"."_locationnarrative_is_cultivated(locationnarrative=_join.result"
FROM "in#8"
RETURNING "location_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#8_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO location"() AS "f" ("location_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO location#1"()
RETURNS SETOF "location"."location_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "location"
("verbatimelevation", "authorlocationcode", "elevation_m", "locationnarrative", "accessconditions", "iscultivated", "source_id")
SELECT
"in#8"."ACAD.Specimen.verbatimElevation"
, "in#8"."_alt(1=id, 2=_join(1=_map(institutionCode), 3=catalogNum.result"
, "in#8"."ACAD.Specimen.verbatimElevation::double"
, "in#8"."_merge(1=locality, 3=_label(habitat), 4=locationRemarks).result"
, "in#8"."ACAD.Specimen.rights"
, "in#8"."_locationnarrative_is_cultivated(locationnarrative=_join.result"
, 1 AS "source_id"
FROM "in#8"
RETURNING "location_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#8_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO location#1"() AS "f" ("location_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#8_insert_in_pkeys" AS
SELECT "row_num" FROM "in#8"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "location_pkeys" AS
SELECT
"in#8_insert_in_pkeys"."row_num"
, "in#8_insert_out_pkeys"."location_id" AS "out.location_id"
FROM "in#8_insert_in_pkeys"
JOIN "in#8_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "location_pkeys"
("row_num", "out.location_id")
SELECT
"in#8_full"."row_num"
, NULL AS "out.location_id"
FROM "in#8_full"
LEFT JOIN "location_pkeys" ON "location_pkeys"."row_num" = "in#8_full"."row_num"
WHERE "location_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_dateRangeEnd"@:

|Output|Input|
|@'value'@|@"ACAD.Specimen"."eventDate"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#9" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."eventDate" AS "ACAD.Specimen.eventDate"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#9"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_dateRangeEnd(eventDate)" AS
SELECT
"in#9"."row_num"
, "_dateRangeEnd"("value" := "in#9"."ACAD.Specimen.eventDate") AS "result"
FROM "in#9"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_dateRangeEnd(eventDate)_wrap"()
RETURNS SETOF "_dateRangeEnd(eventDate)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#9"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#9"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_dateRangeEnd"("value" := "row"."ACAD.Specimen.eventDate")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.eventDate" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('eventDate')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_dateRangeEnd(eventDate)"
("row_num", "result")
SELECT * FROM "pg_temp"."_dateRangeEnd(eventDate)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_dateRangeStart"@:

|Output|Input|
|@'value'@|@"ACAD.Specimen"."eventDate"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#10" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."eventDate" AS "ACAD.Specimen.eventDate"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#10"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_dateRangeStart(eventDate)" AS
SELECT
"in#10"."row_num"
, "_dateRangeStart"("value" := "in#10"."ACAD.Specimen.eventDate") AS "result"
FROM "in#10"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_dateRangeStart(eventDate)_wrap"()
RETURNS SETOF "_dateRangeStart(eventDate)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#10"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#10"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_dateRangeStart"("value" := "row"."ACAD.Specimen.eventDate")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.eventDate" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('eventDate')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_dateRangeStart(eventDate)"
("row_num", "result")
SELECT * FROM "pg_temp"."_dateRangeStart(eventDate)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"locationevent"@:

|Output|Input|
|@'obsenddate'@|@"_dateRangeEnd(eventDate)"."result"@|
|@'location_id'@|@"location_pkeys"."out.location_id"@|
|@'obsstartdate'@|@"_dateRangeStart(eventDate)"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#11" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_dateRangeEnd(eventDate)"."result" AS "_dateRangeEnd(eventDate).result"
, "location_pkeys"."out.location_id" AS "location_pkeys.out.location_id"
, "_dateRangeStart(eventDate)"."result" AS "_dateRangeStart(eventDate).result"
FROM "ACAD.Specimen"
JOIN "_dateRangeEnd(eventDate)" USING ("row_num")
JOIN "location_pkeys" USING ("row_num")
JOIN "_dateRangeStart(eventDate)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#11"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#11_full" (
LIKE "in#11" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#11_full"
SELECT * FROM "in#11"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO locationevent"()
RETURNS SETOF "locationevent"."locationevent_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "locationevent"
("obsenddate", "location_id", "obsstartdate")
SELECT
"in#11"."_dateRangeEnd(eventDate).result"
, "in#11"."location_pkeys.out.location_id"
, "in#11"."_dateRangeStart(eventDate).result"
FROM "in#11"
RETURNING "locationevent_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @date@ on column: @obsenddate@
# Casting @'obsenddate'@ input to @date@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."date(eventDate)"("value" anyelement)
RETURNS date
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS date);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('eventDate')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#11" ADD COLUMN "_dateRangeEnd(eventDate).result::date" date /*"in#11"."_dateRangeEnd(eventDate).result"::date*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#11"
ALTER COLUMN "_dateRangeEnd(eventDate).result::date" TYPE date
USING "pg_temp"."date(eventDate)"("in#11"."_dateRangeEnd(eventDate).result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO locationevent"()
RETURNS SETOF "locationevent"."locationevent_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "locationevent"
("obsenddate", "location_id", "obsstartdate")
SELECT
"in#11"."_dateRangeEnd(eventDate).result::date"
, "in#11"."location_pkeys.out.location_id"
, "in#11"."_dateRangeStart(eventDate).result"
FROM "in#11"
RETURNING "locationevent_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @date@ on column: @obsstartdate@
# Casting @'obsstartdate'@ input to @date@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."date(eventDate)"("value" anyelement)
RETURNS date
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS date);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('eventDate')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#11" ADD COLUMN "_dateRangeStart(eventDate).result::date" date /*"in#11"."_dateRangeStart(eventDate).result"::date*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#11"
ALTER COLUMN "_dateRangeStart(eventDate).result::date" TYPE date
USING "pg_temp"."date(eventDate)"("in#11"."_dateRangeStart(eventDate).result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO locationevent"()
RETURNS SETOF "locationevent"."locationevent_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "locationevent"
("obsenddate", "location_id", "obsstartdate")
SELECT
"in#11"."_dateRangeEnd(eventDate).result::date"
, "in#11"."location_pkeys.out.location_id"
, "in#11"."_dateRangeStart(eventDate).result::date"
FROM "in#11"
RETURNING "locationevent_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#11_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO locationevent"() AS "f" ("locationevent_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO locationevent#1"()
RETURNS SETOF "locationevent"."locationevent_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "locationevent"
("obsenddate", "location_id", "obsstartdate", "source_id")
SELECT
"in#11"."_dateRangeEnd(eventDate).result::date"
, "in#11"."location_pkeys.out.location_id"
, "in#11"."_dateRangeStart(eventDate).result::date"
, 1 AS "source_id"
FROM "in#11"
RETURNING "locationevent_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#11_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO locationevent#1"() AS "f" ("locationevent_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#11_insert_in_pkeys" AS
SELECT "row_num" FROM "in#11"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "locationevent_pkeys" AS
SELECT
"in#11_insert_in_pkeys"."row_num"
, "in#11_insert_out_pkeys"."locationevent_id" AS "out.locationevent_id"
FROM "in#11_insert_in_pkeys"
JOIN "in#11_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "locationevent_pkeys"
("row_num", "out.locationevent_id")
SELECT
"in#11_full"."row_num"
, NULL AS "out.locationevent_id"
FROM "in#11_full"
LEFT JOIN "locationevent_pkeys" ON "locationevent_pkeys"."row_num" = "in#11_full"."row_num"
WHERE "locationevent_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"party"@:

|Output|Input|
|@'fullname'@|@"ACAD.Specimen"."recordedBy"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#12" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."recordedBy" AS "ACAD.Specimen.recordedBy"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#12"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#12_full" (
LIKE "in#12" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#12_full"
SELECT * FROM "in#12"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party"()
RETURNS SETOF "party"."party_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "party"
("fullname")
SELECT "in#12"."ACAD.Specimen.recordedBy" FROM "in#12"
RETURNING "party_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#12_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO party"() AS "f" ("party_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party#1"()
RETURNS SETOF "party"."party_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "party"
("fullname", "source_id")
SELECT
"in#12"."ACAD.Specimen.recordedBy"
, 1 AS "source_id"
FROM "in#12"
RETURNING "party_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#12_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO party#1"() AS "f" ("party_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @party_unique_name@ constraint with condition @sourceaccessioncode IS NULL@ on columns: @source_id, organizationname, fullname, surname, givenname, middlename, suffix@
# Ignoring rows that do not satisfy @true@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"party"."source_id": 1
"party"."organizationname": None
"party"."fullname": "in#12"."ACAD.Specimen.recordedBy"
"party"."surname": None
"party"."givenname": None
"party"."middlename": None
"party"."suffix": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#12_distinct" (
LIKE "in#12" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#12_distinct"
SELECT
DISTINCT ON ("in#12"."ACAD.Specimen.recordedBy")
"in#12".*
FROM "in#12"
LEFT JOIN "party" ON
"party"."source_id" = 1
AND COALESCE("party"."organizationname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."fullname", CAST('\N' AS text)) = COALESCE("in#12"."ACAD.Specimen.recordedBy", CAST('\N' AS text))
AND COALESCE("party"."surname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."givenname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."middlename", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."suffix", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "party"."party_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party#2"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "party"%ROWTYPE;
BEGIN
    FOR "row"."fullname", "row"."source_id" IN
        SELECT
        "in#12_distinct"."ACAD.Specimen.recordedBy"
        , 1 AS "source_id"
        FROM "in#12_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "party"
                ("fullname", "source_id")
                VALUES ("row"."fullname", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre>WARNING:  column "NULL" has type "unknown"
DETAIL:  Proceeding with relation creation anyway.
<pre><code class="SQL">
CREATE TEMP TABLE "rowcount" AS
SELECT * FROM "pg_temp"."INSERT INTO party#2"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "party_pkeys" AS
SELECT
"in#12"."row_num"
, "party"."party_id" AS "out.party_id"
FROM "in#12"
JOIN "party" ON
"party"."source_id" = 1
AND COALESCE("party"."organizationname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."fullname", CAST('\N' AS text)) = COALESCE("in#12"."ACAD.Specimen.recordedBy", CAST('\N' AS text))
AND COALESCE("party"."surname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."givenname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."middlename", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."suffix", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "party_pkeys"
("row_num", "out.party_id")
SELECT
"in#12_full"."row_num"
, NULL AS "out.party_id"
FROM "in#12_full"
LEFT JOIN "party_pkeys" ON "party_pkeys"."row_num" = "in#12_full"."row_num"
WHERE "party_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonoccurrence"@:

|Output|Input|
|@'sourceaccessioncode'@|@"ACAD.Specimen"."id"@|
|@'collector_id'@|@"party_pkeys"."out.party_id"@|
|@'locationevent_id'@|@"locationevent_pkeys"."out.locationevent_id"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#13" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."id" AS "ACAD.Specimen.id"
, "party_pkeys"."out.party_id" AS "party_pkeys.out.party_id"
, "locationevent_pkeys"."out.locationevent_id" AS "locationevent_pkeys.out.locationevent_id"
FROM "ACAD.Specimen"
JOIN "party_pkeys" USING ("row_num")
JOIN "locationevent_pkeys" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#13"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#13_full" (
LIKE "in#13" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#13_full"
SELECT * FROM "in#13"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonoccurrence"()
RETURNS SETOF "taxonoccurrence"."taxonoccurrence_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonoccurrence"
("sourceaccessioncode", "collector_id", "locationevent_id")
SELECT
"in#13"."ACAD.Specimen.id"
, "in#13"."party_pkeys.out.party_id"
, "in#13"."locationevent_pkeys.out.locationevent_id"
FROM "in#13"
RETURNING "taxonoccurrence_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#13_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonoccurrence"() AS "f" ("taxonoccurrence_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonoccurrence#1"()
RETURNS SETOF "taxonoccurrence"."taxonoccurrence_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonoccurrence"
("sourceaccessioncode", "collector_id", "locationevent_id", "source_id")
SELECT
"in#13"."ACAD.Specimen.id"
, "in#13"."party_pkeys.out.party_id"
, "in#13"."locationevent_pkeys.out.locationevent_id"
, 1 AS "source_id"
FROM "in#13"
RETURNING "taxonoccurrence_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#13_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonoccurrence#1"() AS "f" ("taxonoccurrence_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#13_insert_in_pkeys" AS
SELECT "row_num" FROM "in#13"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "taxonoccurrence_pkeys" AS
SELECT
"in#13_insert_in_pkeys"."row_num"
, "in#13_insert_out_pkeys"."taxonoccurrence_id" AS "out.taxonoccurrence_id"
FROM "in#13_insert_in_pkeys"
JOIN "in#13_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "taxonoccurrence_pkeys"
("row_num", "out.taxonoccurrence_id")
SELECT
"in#13_full"."row_num"
, NULL AS "out.taxonoccurrence_id"
FROM "in#13_full"
LEFT JOIN "taxonoccurrence_pkeys" ON "taxonoccurrence_pkeys"."row_num" = "in#13_full"."row_num"
WHERE "taxonoccurrence_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"aggregateoccurrence"@:

|Output|Input|
|@'notes'@|@"ACAD.Specimen"."occurrenceRemarks"@|
|@'taxonoccurrence_id'@|@"taxonoccurrence_pkeys"."out.taxonoccurrence_id"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#14" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."occurrenceRemarks" AS "ACAD.Specimen.occurrenceRemarks"
, "taxonoccurrence_pkeys"."out.taxonoccurrence_id" AS "taxonoccurrence_pkeys.out.taxonoccurrence_id"
FROM "ACAD.Specimen"
JOIN "taxonoccurrence_pkeys" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#14"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#14_full" (
LIKE "in#14" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#14_full"
SELECT * FROM "in#14"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO aggregateoccurrence"()
RETURNS SETOF "aggregateoccurrence"."aggregateoccurrence_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "aggregateoccurrence"
("notes", "taxonoccurrence_id")
SELECT
"in#14"."ACAD.Specimen.occurrenceRemarks"
, "in#14"."taxonoccurrence_pkeys.out.taxonoccurrence_id"
FROM "in#14"
RETURNING "aggregateoccurrence_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#14_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO aggregateoccurrence"() AS "f" ("aggregateoccurrence_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO aggregateoccurrence#1"()
RETURNS SETOF "aggregateoccurrence"."aggregateoccurrence_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "aggregateoccurrence"
("notes", "taxonoccurrence_id", "source_id")
SELECT
"in#14"."ACAD.Specimen.occurrenceRemarks"
, "in#14"."taxonoccurrence_pkeys.out.taxonoccurrence_id"
, 1 AS "source_id"
FROM "in#14"
RETURNING "aggregateoccurrence_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#14_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO aggregateoccurrence#1"() AS "f" ("aggregateoccurrence_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#14_insert_in_pkeys" AS
SELECT "row_num" FROM "in#14"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "aggregateoccurrence_pkeys" AS
SELECT
"in#14_insert_in_pkeys"."row_num"
, "in#14_insert_out_pkeys"."aggregateoccurrence_id" AS "out.aggregateoccurrence_id"
FROM "in#14_insert_in_pkeys"
JOIN "in#14_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "aggregateoccurrence_pkeys"
("row_num", "out.aggregateoccurrence_id")
SELECT
"in#14_full"."row_num"
, NULL AS "out.aggregateoccurrence_id"
FROM "in#14_full"
LEFT JOIN "aggregateoccurrence_pkeys" ON "aggregateoccurrence_pkeys"."row_num" = "in#14_full"."row_num"
WHERE "aggregateoccurrence_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"plantobservation"@:

|Output|Input|
|@'authorplantcode'@|@"ACAD.Specimen"."recordNumber"@|
|@'aggregateoccurrence_id'@|@"aggregateoccurrence_pkeys"."out.aggregateoccurrence_id"@|
|@'reproductivecondition'@|@"ACAD.Specimen"."reproductiveCondition"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#15" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."recordNumber" AS "ACAD.Specimen.recordNumber"
, "aggregateoccurrence_pkeys"."out.aggregateoccurrence_id" AS "aggregateoccurrence_pkeys.out.aggregateoccurrence_id"
, "ACAD.Specimen"."reproductiveCondition" AS "ACAD.Specimen.reproductiveCondition"
FROM "ACAD.Specimen"
JOIN "aggregateoccurrence_pkeys" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#15"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#15_full" (
LIKE "in#15" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#15_full"
SELECT * FROM "in#15"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO plantobservation"()
RETURNS SETOF "plantobservation"."plantobservation_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "plantobservation"
("authorplantcode", "aggregateoccurrence_id", "reproductivecondition")
SELECT
"in#15"."ACAD.Specimen.recordNumber"
, "in#15"."aggregateoccurrence_pkeys.out.aggregateoccurrence_id"
, "in#15"."ACAD.Specimen.reproductiveCondition"
FROM "in#15"
RETURNING "plantobservation_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#15_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO plantobservation"() AS "f" ("plantobservation_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO plantobservation#1"()
RETURNS SETOF "plantobservation"."plantobservation_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "plantobservation"
("authorplantcode", "aggregateoccurrence_id", "reproductivecondition", "source_id")
SELECT
"in#15"."ACAD.Specimen.recordNumber"
, "in#15"."aggregateoccurrence_pkeys.out.aggregateoccurrence_id"
, "in#15"."ACAD.Specimen.reproductiveCondition"
, 1 AS "source_id"
FROM "in#15"
RETURNING "plantobservation_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#15_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO plantobservation#1"() AS "f" ("plantobservation_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#15_insert_in_pkeys" AS
SELECT "row_num" FROM "in#15"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "plantobservation_pkeys" AS
SELECT
"in#15_insert_in_pkeys"."row_num"
, "in#15_insert_out_pkeys"."plantobservation_id" AS "out.plantobservation_id"
FROM "in#15_insert_in_pkeys"
JOIN "in#15_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "plantobservation_pkeys"
("row_num", "out.plantobservation_id")
SELECT
"in#15_full"."row_num"
, NULL AS "out.plantobservation_id"
FROM "in#15_full"
LEFT JOIN "plantobservation_pkeys" ON "plantobservation_pkeys"."row_num" = "in#15_full"."row_num"
WHERE "plantobservation_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_map"@:

|Output|Input|
|@'map'@|@{'Acadia University': u'ACAD', '*': u'*'}@|
|@'value'@|@"ACAD.Specimen"."institutionCode"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#16" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."institutionCode" AS "ACAD.Specimen.institutionCode"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#16"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_map(institutionCode)#1" AS
SELECT
"in#16"."row_num"
, "_map"("map" := hstore(ARRAY['Acadia University', '*'], ARRAY['ACAD', '*']), "value" := "in#16"."ACAD.Specimen.institutionCode") AS "result"
FROM "in#16"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_map(institutionCode)#1_wrap"()
RETURNS SETOF "_map(institutionCode)#1"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#16"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#16"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_map"("map" := hstore(ARRAY['Acadia University', '*'], ARRAY['ACAD', '*']), "value" := "row"."ACAD.Specimen.institutionCode")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.institutionCode" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('institutionCode')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_map(institutionCode)#1"
("row_num", "result")
SELECT * FROM "pg_temp"."_map(institutionCode)#1_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"sourcelist"@:

|Output|Input|
|@'name'@|@"_map(institutionCode)#1"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#17" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_map(institutionCode)#1"."result" AS "_map(institutionCode)#1.result"
FROM "ACAD.Specimen"
JOIN "_map(institutionCode)#1" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#17"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#17_full" (
LIKE "in#17" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#17_full"
SELECT * FROM "in#17"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO sourcelist"()
RETURNS SETOF "sourcelist"."sourcelist_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "sourcelist"
("name")
SELECT "in#17"."_map(institutionCode)#1.result" FROM "in#17"
RETURNING "sourcelist_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#17_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO sourcelist"() AS "f" ("sourcelist_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO sourcelist#1"()
RETURNS SETOF "sourcelist"."sourcelist_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "sourcelist"
("name", "source_id")
SELECT
"in#17"."_map(institutionCode)#1.result"
, 1 AS "source_id"
FROM "in#17"
RETURNING "sourcelist_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#17_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO sourcelist#1"() AS "f" ("sourcelist_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @sourcelist_unique@ constraint on columns: @source_id, name@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"sourcelist"."source_id": 1
"sourcelist"."name": "in#17"."_map(institutionCode)#1.result"
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#17_distinct" (
LIKE "in#17" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#17_distinct"
SELECT
DISTINCT ON ("in#17"."_map(institutionCode)#1.result")
"in#17".*
FROM "in#17"
LEFT JOIN "sourcelist" ON
"sourcelist"."source_id" = 1
AND "sourcelist"."name" = "in#17"."_map(institutionCode)#1.result"
WHERE "sourcelist"."sourcelist_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO sourcelist#2"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "sourcelist"%ROWTYPE;
BEGIN
    FOR "row"."name", "row"."source_id" IN
        SELECT
        "in#17_distinct"."_map(institutionCode)#1.result"
        , 1 AS "source_id"
        FROM "in#17_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "sourcelist"
                ("name", "source_id")
                VALUES ("row"."name", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#1" AS
SELECT * FROM "pg_temp"."INSERT INTO sourcelist#2"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "sourcelist_pkeys" AS
SELECT
"in#17"."row_num"
, "sourcelist"."sourcelist_id" AS "out.sourcelist_id"
FROM "in#17"
JOIN "sourcelist" ON
"sourcelist"."source_id" = 1
AND "sourcelist"."name" = "in#17"."_map(institutionCode)#1.result"
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "sourcelist_pkeys"
("row_num", "out.sourcelist_id")
SELECT
"in#17_full"."row_num"
, NULL AS "out.sourcelist_id"
FROM "in#17_full"
LEFT JOIN "sourcelist_pkeys" ON "sourcelist_pkeys"."row_num" = "in#17_full"."row_num"
WHERE "sourcelist_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_map"@:

|Output|Input|
|@'map'@|@{'Acadia University': u'ACAD', '*': u'*'}@|
|@'value'@|@"ACAD.Specimen"."institutionCode"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#18" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."institutionCode" AS "ACAD.Specimen.institutionCode"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#18"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_map(institutionCode)#2" AS
SELECT
"in#18"."row_num"
, "_map"("map" := hstore(ARRAY['Acadia University', '*'], ARRAY['ACAD', '*']), "value" := "in#18"."ACAD.Specimen.institutionCode") AS "result"
FROM "in#18"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_map(institutionCode)#2_wrap"()
RETURNS SETOF "_map(institutionCode)#2"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#18"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#18"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_map"("map" := hstore(ARRAY['Acadia University', '*'], ARRAY['ACAD', '*']), "value" := "row"."ACAD.Specimen.institutionCode")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.institutionCode" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('institutionCode')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_map(institutionCode)#2"
("row_num", "result")
SELECT * FROM "pg_temp"."_map(institutionCode)#2_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_split"@:

|Output|Input|
|@'separator'@|@'[,;] *'@|
|@'value'@|@"_map(institutionCode)#2"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#19" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_map(institutionCode)#2"."result" AS "_map(institutionCode)#2.result"
FROM "ACAD.Specimen"
JOIN "_map(institutionCode)#2" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#19"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_split(_map(institutionCode)#2)" AS
SELECT
"in#19"."row_num"
, "_split"("separator" := '[,;] *', "value" := "in#19"."_map(institutionCode)#2.result") AS "result"
FROM "in#19"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_split(_map(institutionCode)#2)_wrap"()
RETURNS SETOF "_split(_map(institutionCode)#2)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#19"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#19"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_split"("separator" := '[,;] *', "value" := "row"."_map(institutionCode)#2.result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."_map(institutionCode)#2.result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('institutionCode')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_split(_map(institutionCode)#2)"
("row_num", "result")
SELECT * FROM "pg_temp"."_split(_map(institutionCode)#2)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"sourcename"@:

|Output|Input|
|@'sourcelist_id'@|@"sourcelist_pkeys"."out.sourcelist_id"@|
|@'name'@|@"_split(_map(institutionCode)#2)"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#20" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "sourcelist_pkeys"."out.sourcelist_id" AS "sourcelist_pkeys.out.sourcelist_id"
, "_split(_map(institutionCode)#2)"."result" AS "_split(_map(institutionCode)#2).result"
FROM "ACAD.Specimen"
JOIN "sourcelist_pkeys" USING ("row_num")
JOIN "_split(_map(institutionCode)#2)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#20"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#20_full" (
LIKE "in#20" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#20_full"
SELECT * FROM "in#20"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO sourcename"()
RETURNS SETOF "sourcename"."sourcelist_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "sourcename"
("sourcelist_id", "name")
SELECT
"in#20"."sourcelist_pkeys.out.sourcelist_id"
, "in#20"."_split(_map(institutionCode)#2).result"
FROM "in#20"
RETURNING "sourcelist_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#20_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO sourcename"() AS "f" ("sourcelist_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @sourcename_pkey@ constraint on columns: @sourcelist_id, name@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"sourcename"."sourcelist_id": "in#20"."sourcelist_pkeys.out.sourcelist_id"
"sourcename"."name": "in#20"."_split(_map(institutionCode)#2).result"
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#20_distinct" (
LIKE "in#20" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#20_distinct"
SELECT
DISTINCT ON ("in#20"."sourcelist_pkeys.out.sourcelist_id", "in#20"."_split(_map(institutionCode)#2).result")
"in#20".*
FROM "in#20"
LEFT JOIN "sourcename" ON
"sourcename"."sourcelist_id" = "in#20"."sourcelist_pkeys.out.sourcelist_id"
AND "sourcename"."name" = "in#20"."_split(_map(institutionCode)#2).result"
WHERE "sourcename"."sourcelist_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO sourcename#1"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "sourcename"%ROWTYPE;
BEGIN
    FOR "row"."sourcelist_id", "row"."name" IN
        SELECT
        "in#20_distinct"."sourcelist_pkeys.out.sourcelist_id"
        , "in#20_distinct"."_split(_map(institutionCode)#2).result"
        FROM "in#20_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "sourcename"
                ("sourcelist_id", "name")
                VALUES ("row"."sourcelist_id", "row"."name")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#2" AS
SELECT * FROM "pg_temp"."INSERT INTO sourcename#1"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "sourcename_pkeys" AS
SELECT
"in#20"."row_num"
, "sourcename"."sourcelist_id" AS "out.sourcelist_id"
FROM "in#20"
JOIN "sourcename" ON
"sourcename"."sourcelist_id" = "in#20"."sourcelist_pkeys.out.sourcelist_id"
AND "sourcename"."name" = "in#20"."_split(_map(institutionCode)#2).result"
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "sourcename_pkeys"
("row_num", "out.sourcelist_id")
SELECT
"in#20_full"."row_num"
, NULL AS "out.sourcelist_id"
FROM "in#20_full"
LEFT JOIN "sourcename_pkeys" ON "sourcename_pkeys"."row_num" = "in#20_full"."row_num"
WHERE "sourcename_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_alt"@:

|Output|Input|
|@'1'@|@"ACAD.Specimen"."collectionCode"@|
|@'2'@|@"ACAD.Specimen"."collectionID"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#21" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."collectionCode" AS "ACAD.Specimen.collectionCode"
, "ACAD.Specimen"."collectionID" AS "ACAD.Specimen.collectionID"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#21"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_alt(1=collectionCode, 2=collectionID)" AS
SELECT
"in#21"."row_num"
, "_alt"("1" := "in#21"."ACAD.Specimen.collectionCode", "2" := "in#21"."ACAD.Specimen.collectionID") AS "result"
FROM "in#21"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_alt(1=collectionCode, 2=collectionID)_wrap"()
RETURNS SETOF "_alt(1=collectionCode, 2=collectionID)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#21"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#21"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_alt"("1" := "row"."ACAD.Specimen.collectionCode", "2" := "row"."ACAD.Specimen.collectionID")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.collectionCode" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.collectionID" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('collectionCode,collectionID')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_alt(1=collectionCode, 2=collectionID)"
("row_num", "result")
SELECT * FROM "pg_temp"."_alt(1=collectionCode, 2=collectionID)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"specimenreplicate"@:

|Output|Input|
|@'catalognumber_dwc'@|@"ACAD.Specimen"."catalogNumber"@|
|@'plantobservation_id'@|@"plantobservation_pkeys"."out.plantobservation_id"@|
|@'institution_id'@|@"sourcelist_pkeys"."out.sourcelist_id"@|
|@'sourceaccessioncode'@|@"ACAD.Specimen"."id"@|
|@'collectioncode_dwc'@|@"_alt(1=collectionCode, 2=collectionID)"."result"@|
|@'collectionnumber'@|@"ACAD.Specimen"."recordNumber"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#22" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."catalogNumber" AS "ACAD.Specimen.catalogNumber"
, "plantobservation_pkeys"."out.plantobservation_id" AS "plantobservation_pkeys.out.plantobservation_id"
, "sourcelist_pkeys"."out.sourcelist_id" AS "sourcelist_pkeys.out.sourcelist_id"
, "ACAD.Specimen"."id" AS "ACAD.Specimen.id"
, "_alt(1=collectionCode, 2=collectionID)"."result" AS "_alt(1=collectionCode, 2=collectionID).result"
, "ACAD.Specimen"."recordNumber" AS "ACAD.Specimen.recordNumber"
FROM "ACAD.Specimen"
JOIN "plantobservation_pkeys" USING ("row_num")
JOIN "sourcelist_pkeys" USING ("row_num")
JOIN "_alt(1=collectionCode, 2=collectionID)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#22"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#22_full" (
LIKE "in#22" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#22_full"
SELECT * FROM "in#22"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO specimenreplicate"()
RETURNS SETOF "specimenreplicate"."specimenreplicate_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "specimenreplicate"
("catalognumber_dwc", "plantobservation_id", "institution_id", "sourceaccessioncode", "collectioncode_dwc", "collectionnumber")
SELECT
"in#22"."ACAD.Specimen.catalogNumber"
, "in#22"."plantobservation_pkeys.out.plantobservation_id"
, "in#22"."sourcelist_pkeys.out.sourcelist_id"
, "in#22"."ACAD.Specimen.id"
, "in#22"."_alt(1=collectionCode, 2=collectionID).result"
, "in#22"."ACAD.Specimen.recordNumber"
FROM "in#22"
RETURNING "specimenreplicate_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#22_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO specimenreplicate"() AS "f" ("specimenreplicate_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO specimenreplicate#1"()
RETURNS SETOF "specimenreplicate"."specimenreplicate_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "specimenreplicate"
("catalognumber_dwc", "plantobservation_id", "institution_id", "sourceaccessioncode", "collectioncode_dwc", "collectionnumber", "source_id")
SELECT
"in#22"."ACAD.Specimen.catalogNumber"
, "in#22"."plantobservation_pkeys.out.plantobservation_id"
, "in#22"."sourcelist_pkeys.out.sourcelist_id"
, "in#22"."ACAD.Specimen.id"
, "in#22"."_alt(1=collectionCode, 2=collectionID).result"
, "in#22"."ACAD.Specimen.recordNumber"
, 1 AS "source_id"
FROM "in#22"
RETURNING "specimenreplicate_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#22_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO specimenreplicate#1"() AS "f" ("specimenreplicate_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#22_insert_in_pkeys" AS
SELECT "row_num" FROM "in#22"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "specimenreplicate_pkeys" AS
SELECT
"in#22_insert_in_pkeys"."row_num"
, "in#22_insert_out_pkeys"."specimenreplicate_id" AS "out.specimenreplicate_id"
FROM "in#22_insert_in_pkeys"
JOIN "in#22_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "specimenreplicate_pkeys"
("row_num", "out.specimenreplicate_id")
SELECT
"in#22_full"."row_num"
, NULL AS "out.specimenreplicate_id"
FROM "in#22_full"
LEFT JOIN "specimenreplicate_pkeys" ON "specimenreplicate_pkeys"."row_num" = "in#22_full"."row_num"
WHERE "specimenreplicate_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_dateRangeStart"@:

|Output|Input|
|@'value'@|@"ACAD.Specimen"."dateIdentified"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#23" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."dateIdentified" AS "ACAD.Specimen.dateIdentified"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#23"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_dateRangeStart(dateIdentified)" AS
SELECT
"in#23"."row_num"
, "_dateRangeStart"("value" := "in#23"."ACAD.Specimen.dateIdentified") AS "result"
FROM "in#23"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_dateRangeStart(dateIdentified)_wrap"()
RETURNS SETOF "_dateRangeStart(dateIdentified)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#23"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#23"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_dateRangeStart"("value" := "row"."ACAD.Specimen.dateIdentified")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.dateIdentified" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('dateIdentified')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_dateRangeStart(dateIdentified)"
("row_num", "result")
SELECT * FROM "pg_temp"."_dateRangeStart(dateIdentified)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"party"@:

|Output|Input|
|@'fullname'@|@"ACAD.Specimen"."identifiedBy"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#24" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."identifiedBy" AS "ACAD.Specimen.identifiedBy"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#24"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#24_full" (
LIKE "in#24" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#24_full"
SELECT * FROM "in#24"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party#3"()
RETURNS SETOF "party"."party_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "party"
("fullname")
SELECT "in#24"."ACAD.Specimen.identifiedBy" FROM "in#24"
RETURNING "party_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#24_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO party#3"() AS "f" ("party_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party#4"()
RETURNS SETOF "party"."party_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "party"
("fullname", "source_id")
SELECT
"in#24"."ACAD.Specimen.identifiedBy"
, 1 AS "source_id"
FROM "in#24"
RETURNING "party_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#24_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO party#4"() AS "f" ("party_id")
</code></pre>
# Caught exception: CheckException: Violated @"party"."party_required_key"@ constraint with condition @((organizationname IS NOT NULL) OR ((source_id <> party_id) AND ((fullname IS NOT NULL) OR (surname IS NOT NULL))))@
# Ignoring rows that do not satisfy @(((1 <> nextval('party_party_id_seq'::regclass)) AND ("ACAD.Specimen.identifiedBy" IS NOT NULL)))@
<pre><code class="SQL">
DELETE FROM "in#24"
WHERE NOT COALESCE((((1 <> nextval('party_party_id_seq'::regclass)) AND ("ACAD.Specimen.identifiedBy" IS NOT NULL))), false)
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party#4"()
RETURNS SETOF "party"."party_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "party"
("fullname", "source_id")
SELECT
"in#24"."ACAD.Specimen.identifiedBy"
, 1 AS "source_id"
FROM "in#24"
RETURNING "party_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#24_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO party#4"() AS "f" ("party_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @party_unique_name@ constraint with condition @sourceaccessioncode IS NULL@ on columns: @source_id, organizationname, fullname, surname, givenname, middlename, suffix@
# Ignoring rows that do not satisfy @true@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"party"."source_id": 1
"party"."organizationname": None
"party"."fullname": "in#24"."ACAD.Specimen.identifiedBy"
"party"."surname": None
"party"."givenname": None
"party"."middlename": None
"party"."suffix": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#24_distinct" (
LIKE "in#24" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#24_distinct"
SELECT
DISTINCT ON ("in#24"."ACAD.Specimen.identifiedBy")
"in#24".*
FROM "in#24"
LEFT JOIN "party" ON
"party"."source_id" = 1
AND COALESCE("party"."organizationname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."fullname", CAST('\N' AS text)) = COALESCE("in#24"."ACAD.Specimen.identifiedBy", CAST('\N' AS text))
AND COALESCE("party"."surname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."givenname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."middlename", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."suffix", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "party"."party_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO party#5"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "party"%ROWTYPE;
BEGIN
    FOR "row"."fullname", "row"."source_id" IN
        SELECT
        "in#24_distinct"."ACAD.Specimen.identifiedBy"
        , 1 AS "source_id"
        FROM "in#24_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "party"
                ("fullname", "source_id")
                VALUES ("row"."fullname", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#3" AS
SELECT * FROM "pg_temp"."INSERT INTO party#5"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "party_pkeys#1" AS
SELECT
"in#24"."row_num"
, "party"."party_id" AS "out.party_id"
FROM "in#24"
JOIN "party" ON
"party"."source_id" = 1
AND COALESCE("party"."organizationname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."fullname", CAST('\N' AS text)) = COALESCE("in#24"."ACAD.Specimen.identifiedBy", CAST('\N' AS text))
AND COALESCE("party"."surname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."givenname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."middlename", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("party"."suffix", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "party_pkeys#1"
("row_num", "out.party_id")
SELECT
"in#24_full"."row_num"
, NULL AS "out.party_id"
FROM "in#24_full"
LEFT JOIN "party_pkeys#1" ON "party_pkeys#1"."row_num" = "in#24_full"."row_num"
WHERE "party_pkeys#1"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'rank'@|@'kingdom'@|
|@'taxonepithet'@|@"ACAD.Specimen"."kingdom"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#25" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."kingdom" AS "ACAD.Specimen.kingdom"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#25"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#25_full" (
LIKE "in#25" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#25_full"
SELECT * FROM "in#25"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("rank", "taxonepithet")
SELECT
'kingdom' AS "rank"
, "in#25"."ACAD.Specimen.kingdom"
FROM "in#25"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#25_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#1"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("rank", "taxonepithet", "source_id")
SELECT
'kingdom' AS "rank"
, "in#25"."ACAD.Specimen.kingdom"
, 1 AS "source_id"
FROM "in#25"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#25_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#1"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": None
"taxonlabel"."taxonepithet": "in#25"."ACAD.Specimen.kingdom"
"taxonlabel"."rank": 'kingdom'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#25_distinct" (
LIKE "in#25" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#25_distinct"
SELECT
DISTINCT ON ("in#25"."ACAD.Specimen.kingdom")
"in#25".*
FROM "in#25"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE(NULL, CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#25"."ACAD.Specimen.kingdom", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'kingdom'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#2"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        'kingdom' AS "rank"
        , "in#25_distinct"."ACAD.Specimen.kingdom"
        , 1 AS "source_id"
        FROM "in#25_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("rank", "taxonepithet", "source_id")
                VALUES ("row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#4" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#2"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='kingdom']" AS
SELECT
"in#25"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#25"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE(NULL, CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#25"."ACAD.Specimen.kingdom", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'kingdom'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='kingdom']"
("row_num", "out.taxonlabel_id")
SELECT
"in#25_full"."row_num"
, NULL AS "out.taxonlabel_id"
FROM "in#25_full"
LEFT JOIN "taxonlabel[rank='kingdom']" ON "taxonlabel[rank='kingdom']"."row_num" = "in#25_full"."row_num"
WHERE "taxonlabel[rank='kingdom']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='kingdom']"."out.taxonlabel_id"@|
|@'rank'@|@'phylum'@|
|@'taxonepithet'@|@"ACAD.Specimen"."phylum"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#26" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='kingdom']"."out.taxonlabel_id" AS "taxonlabel[rank='kingdom'].out.taxonlabel_id"
, "ACAD.Specimen"."phylum" AS "ACAD.Specimen.phylum"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='kingdom']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#26"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#26_full" (
LIKE "in#26" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#26_full"
SELECT * FROM "in#26"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#3"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id"
, 'phylum' AS "rank"
, "in#26"."ACAD.Specimen.phylum"
FROM "in#26"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#26_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#3"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#4"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id"
, 'phylum' AS "rank"
, "in#26"."ACAD.Specimen.phylum"
, 1 AS "source_id"
FROM "in#26"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#26_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#4"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#26"."ACAD.Specimen.phylum"
"taxonlabel"."rank": 'phylum'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#26_distinct" (
LIKE "in#26" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#26_distinct"
SELECT
DISTINCT ON ("in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id", "in#26"."ACAD.Specimen.phylum")
"in#26".*
FROM "in#26"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#26"."ACAD.Specimen.phylum", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'phylum'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#5"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#26_distinct"."taxonlabel[rank='kingdom'].out.taxonlabel_id"
        , 'phylum' AS "rank"
        , "in#26_distinct"."ACAD.Specimen.phylum"
        , 1 AS "source_id"
        FROM "in#26_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#5" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#5"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='phylum']" AS
SELECT
"in#26"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#26"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#26"."ACAD.Specimen.phylum", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'phylum'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#26"."taxonlabel[rank='kingdom'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='phylum']"
("row_num", "out.taxonlabel_id")
SELECT
"in#26_full"."row_num"
, "in#26_full"."taxonlabel[rank='kingdom'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#26_full"
LEFT JOIN "taxonlabel[rank='phylum']" ON "taxonlabel[rank='phylum']"."row_num" = "in#26_full"."row_num"
WHERE "taxonlabel[rank='phylum']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='phylum']"."out.taxonlabel_id"@|
|@'rank'@|@'class'@|
|@'taxonepithet'@|@"ACAD.Specimen"."class"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#27" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='phylum']"."out.taxonlabel_id" AS "taxonlabel[rank='phylum'].out.taxonlabel_id"
, "ACAD.Specimen"."class" AS "ACAD.Specimen.class"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='phylum']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#27"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#27_full" (
LIKE "in#27" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#27_full"
SELECT * FROM "in#27"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#6"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id"
, 'class' AS "rank"
, "in#27"."ACAD.Specimen.class"
FROM "in#27"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#27_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#6"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#7"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id"
, 'class' AS "rank"
, "in#27"."ACAD.Specimen.class"
, 1 AS "source_id"
FROM "in#27"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#27_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#7"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#27"."ACAD.Specimen.class"
"taxonlabel"."rank": 'class'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#27_distinct" (
LIKE "in#27" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#27_distinct"
SELECT
DISTINCT ON ("in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id", "in#27"."ACAD.Specimen.class")
"in#27".*
FROM "in#27"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#27"."ACAD.Specimen.class", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'class'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#8"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#27_distinct"."taxonlabel[rank='phylum'].out.taxonlabel_id"
        , 'class' AS "rank"
        , "in#27_distinct"."ACAD.Specimen.class"
        , 1 AS "source_id"
        FROM "in#27_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#6" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#8"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='class']" AS
SELECT
"in#27"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#27"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#27"."ACAD.Specimen.class", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'class'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#27"."taxonlabel[rank='phylum'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='class']"
("row_num", "out.taxonlabel_id")
SELECT
"in#27_full"."row_num"
, "in#27_full"."taxonlabel[rank='phylum'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#27_full"
LEFT JOIN "taxonlabel[rank='class']" ON "taxonlabel[rank='class']"."row_num" = "in#27_full"."row_num"
WHERE "taxonlabel[rank='class']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='class']"."out.taxonlabel_id"@|
|@'rank'@|@'order'@|
|@'taxonepithet'@|@"ACAD.Specimen"."order"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#28" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='class']"."out.taxonlabel_id" AS "taxonlabel[rank='class'].out.taxonlabel_id"
, "ACAD.Specimen"."order" AS "ACAD.Specimen.order"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='class']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#28"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#28_full" (
LIKE "in#28" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#28_full"
SELECT * FROM "in#28"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#9"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#28"."taxonlabel[rank='class'].out.taxonlabel_id"
, 'order' AS "rank"
, "in#28"."ACAD.Specimen.order"
FROM "in#28"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#28_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#9"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#10"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#28"."taxonlabel[rank='class'].out.taxonlabel_id"
, 'order' AS "rank"
, "in#28"."ACAD.Specimen.order"
, 1 AS "source_id"
FROM "in#28"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#28_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#10"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#28"."taxonlabel[rank='class'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#28"."ACAD.Specimen.order"
"taxonlabel"."rank": 'order'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#28_distinct" (
LIKE "in#28" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#28_distinct"
SELECT
DISTINCT ON ("in#28"."taxonlabel[rank='class'].out.taxonlabel_id", "in#28"."ACAD.Specimen.order")
"in#28".*
FROM "in#28"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#28"."taxonlabel[rank='class'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#28"."ACAD.Specimen.order", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'order'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#11"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#28_distinct"."taxonlabel[rank='class'].out.taxonlabel_id"
        , 'order' AS "rank"
        , "in#28_distinct"."ACAD.Specimen.order"
        , 1 AS "source_id"
        FROM "in#28_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#7" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#11"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='order']" AS
SELECT
"in#28"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#28"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#28"."taxonlabel[rank='class'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#28"."ACAD.Specimen.order", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'order'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#28"."taxonlabel[rank='class'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='order']"
("row_num", "out.taxonlabel_id")
SELECT
"in#28_full"."row_num"
, "in#28_full"."taxonlabel[rank='class'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#28_full"
LEFT JOIN "taxonlabel[rank='order']" ON "taxonlabel[rank='order']"."row_num" = "in#28_full"."row_num"
WHERE "taxonlabel[rank='order']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='order']"."out.taxonlabel_id"@|
|@'rank'@|@'family'@|
|@'taxonepithet'@|@"ACAD.Specimen"."family"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#29" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='order']"."out.taxonlabel_id" AS "taxonlabel[rank='order'].out.taxonlabel_id"
, "ACAD.Specimen"."family" AS "ACAD.Specimen.family"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='order']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#29"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#29_full" (
LIKE "in#29" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#29_full"
SELECT * FROM "in#29"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#12"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#29"."taxonlabel[rank='order'].out.taxonlabel_id"
, 'family' AS "rank"
, "in#29"."ACAD.Specimen.family"
FROM "in#29"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#29_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#12"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#13"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#29"."taxonlabel[rank='order'].out.taxonlabel_id"
, 'family' AS "rank"
, "in#29"."ACAD.Specimen.family"
, 1 AS "source_id"
FROM "in#29"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#29_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#13"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#29"."taxonlabel[rank='order'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#29"."ACAD.Specimen.family"
"taxonlabel"."rank": 'family'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#29_distinct" (
LIKE "in#29" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#29_distinct"
SELECT
DISTINCT ON ("in#29"."taxonlabel[rank='order'].out.taxonlabel_id", "in#29"."ACAD.Specimen.family")
"in#29".*
FROM "in#29"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#29"."taxonlabel[rank='order'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#29"."ACAD.Specimen.family", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'family'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#14"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#29_distinct"."taxonlabel[rank='order'].out.taxonlabel_id"
        , 'family' AS "rank"
        , "in#29_distinct"."ACAD.Specimen.family"
        , 1 AS "source_id"
        FROM "in#29_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#8" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#14"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='family']" AS
SELECT
"in#29"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#29"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#29"."taxonlabel[rank='order'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#29"."ACAD.Specimen.family", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'family'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#29"."taxonlabel[rank='order'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='family']"
("row_num", "out.taxonlabel_id")
SELECT
"in#29_full"."row_num"
, "in#29_full"."taxonlabel[rank='order'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#29_full"
LEFT JOIN "taxonlabel[rank='family']" ON "taxonlabel[rank='family']"."row_num" = "in#29_full"."row_num"
WHERE "taxonlabel[rank='family']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='family']"."out.taxonlabel_id"@|
|@'rank'@|@'genus'@|
|@'taxonepithet'@|@"ACAD.Specimen"."genus"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#30" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='family']"."out.taxonlabel_id" AS "taxonlabel[rank='family'].out.taxonlabel_id"
, "ACAD.Specimen"."genus" AS "ACAD.Specimen.genus"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='family']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#30"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#30_full" (
LIKE "in#30" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#30_full"
SELECT * FROM "in#30"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#15"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#30"."taxonlabel[rank='family'].out.taxonlabel_id"
, 'genus' AS "rank"
, "in#30"."ACAD.Specimen.genus"
FROM "in#30"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#30_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#15"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#16"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#30"."taxonlabel[rank='family'].out.taxonlabel_id"
, 'genus' AS "rank"
, "in#30"."ACAD.Specimen.genus"
, 1 AS "source_id"
FROM "in#30"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#30_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#16"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#30"."taxonlabel[rank='family'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#30"."ACAD.Specimen.genus"
"taxonlabel"."rank": 'genus'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#30_distinct" (
LIKE "in#30" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#30_distinct"
SELECT
DISTINCT ON ("in#30"."taxonlabel[rank='family'].out.taxonlabel_id", "in#30"."ACAD.Specimen.genus")
"in#30".*
FROM "in#30"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#30"."taxonlabel[rank='family'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#30"."ACAD.Specimen.genus", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'genus'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#17"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#30_distinct"."taxonlabel[rank='family'].out.taxonlabel_id"
        , 'genus' AS "rank"
        , "in#30_distinct"."ACAD.Specimen.genus"
        , 1 AS "source_id"
        FROM "in#30_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#9" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#17"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='genus']" AS
SELECT
"in#30"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#30"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#30"."taxonlabel[rank='family'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#30"."ACAD.Specimen.genus", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'genus'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#30"."taxonlabel[rank='family'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='genus']"
("row_num", "out.taxonlabel_id")
SELECT
"in#30_full"."row_num"
, "in#30_full"."taxonlabel[rank='family'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#30_full"
LEFT JOIN "taxonlabel[rank='genus']" ON "taxonlabel[rank='genus']"."row_num" = "in#30_full"."row_num"
WHERE "taxonlabel[rank='genus']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='genus']"."out.taxonlabel_id"@|
|@'rank'@|@'species'@|
|@'taxonepithet'@|@"ACAD.Specimen"."specificEpithet"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#31" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='genus']"."out.taxonlabel_id" AS "taxonlabel[rank='genus'].out.taxonlabel_id"
, "ACAD.Specimen"."specificEpithet" AS "ACAD.Specimen.specificEpithet"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='genus']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#31"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#31_full" (
LIKE "in#31" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#31_full"
SELECT * FROM "in#31"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#18"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#31"."taxonlabel[rank='genus'].out.taxonlabel_id"
, 'species' AS "rank"
, "in#31"."ACAD.Specimen.specificEpithet"
FROM "in#31"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#31_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#18"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#19"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#31"."taxonlabel[rank='genus'].out.taxonlabel_id"
, 'species' AS "rank"
, "in#31"."ACAD.Specimen.specificEpithet"
, 1 AS "source_id"
FROM "in#31"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#31_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#19"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#31"."taxonlabel[rank='genus'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#31"."ACAD.Specimen.specificEpithet"
"taxonlabel"."rank": 'species'
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#31_distinct" (
LIKE "in#31" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#31_distinct"
SELECT
DISTINCT ON ("in#31"."taxonlabel[rank='genus'].out.taxonlabel_id", "in#31"."ACAD.Specimen.specificEpithet")
"in#31".*
FROM "in#31"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#31"."taxonlabel[rank='genus'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#31"."ACAD.Specimen.specificEpithet", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'species'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#20"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#31_distinct"."taxonlabel[rank='genus'].out.taxonlabel_id"
        , 'species' AS "rank"
        , "in#31_distinct"."ACAD.Specimen.specificEpithet"
        , 1 AS "source_id"
        FROM "in#31_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#10" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#20"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank='species']" AS
SELECT
"in#31"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#31"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#31"."taxonlabel[rank='genus'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#31"."ACAD.Specimen.specificEpithet", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = 'species'
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#31"."taxonlabel[rank='genus'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank='species']"
("row_num", "out.taxonlabel_id")
SELECT
"in#31_full"."row_num"
, "in#31_full"."taxonlabel[rank='genus'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#31_full"
LEFT JOIN "taxonlabel[rank='species']" ON "taxonlabel[rank='species']"."row_num" = "in#31_full"."row_num"
WHERE "taxonlabel[rank='species']"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='species']"."out.taxonlabel_id"@|
|@'rank'@|@'subspecies'@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#32" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='species']"."out.taxonlabel_id" AS "taxonlabel[rank='species'].out.taxonlabel_id"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='species']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#32"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#32_full" (
LIKE "in#32" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#32_full"
SELECT * FROM "in#32"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#21"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank")
SELECT
"in#32"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'subspecies' AS "rank"
FROM "in#32"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#32_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#21"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#22"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "source_id")
SELECT
"in#32"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'subspecies' AS "rank"
, 1 AS "source_id"
FROM "in#32"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#32_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#22"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: CheckException: Violated @"taxonlabel"."taxonlabel_required_key"@ constraint with condition @(((sourceaccessioncode IS NOT NULL) OR (taxonepithet IS NOT NULL)) OR (taxonomicname IS NOT NULL))@
# Ignoring rows that do not satisfy @false@
# Ignoring all rows
# Returning default: @"taxonlabel[rank='species']"."out.taxonlabel_id"@
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='species']"."out.taxonlabel_id"@|
|@'rank'@|@'variety'@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#33" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='species']"."out.taxonlabel_id" AS "taxonlabel[rank='species'].out.taxonlabel_id"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='species']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#33"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#33_full" (
LIKE "in#33" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#33_full"
SELECT * FROM "in#33"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#23"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank")
SELECT
"in#33"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'variety' AS "rank"
FROM "in#33"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#33_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#23"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#24"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "source_id")
SELECT
"in#33"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'variety' AS "rank"
, 1 AS "source_id"
FROM "in#33"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#33_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#24"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: CheckException: Violated @"taxonlabel"."taxonlabel_required_key"@ constraint with condition @(((sourceaccessioncode IS NOT NULL) OR (taxonepithet IS NOT NULL)) OR (taxonomicname IS NOT NULL))@
# Ignoring rows that do not satisfy @false@
# Ignoring all rows
# Returning default: @"taxonlabel[rank='species']"."out.taxonlabel_id"@
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='species']"."out.taxonlabel_id"@|
|@'rank'@|@'forma'@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#34" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='species']"."out.taxonlabel_id" AS "taxonlabel[rank='species'].out.taxonlabel_id"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='species']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#34"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#34_full" (
LIKE "in#34" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#34_full"
SELECT * FROM "in#34"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#25"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank")
SELECT
"in#34"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'forma' AS "rank"
FROM "in#34"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#34_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#25"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#26"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "source_id")
SELECT
"in#34"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'forma' AS "rank"
, 1 AS "source_id"
FROM "in#34"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#34_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#26"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: CheckException: Violated @"taxonlabel"."taxonlabel_required_key"@ constraint with condition @(((sourceaccessioncode IS NOT NULL) OR (taxonepithet IS NOT NULL)) OR (taxonomicname IS NOT NULL))@
# Ignoring rows that do not satisfy @false@
# Ignoring all rows
# Returning default: @"taxonlabel[rank='species']"."out.taxonlabel_id"@
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='species']"."out.taxonlabel_id"@|
|@'rank'@|@'cultivar'@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#35" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='species']"."out.taxonlabel_id" AS "taxonlabel[rank='species'].out.taxonlabel_id"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='species']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#35"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#35_full" (
LIKE "in#35" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#35_full"
SELECT * FROM "in#35"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#27"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank")
SELECT
"in#35"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'cultivar' AS "rank"
FROM "in#35"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#35_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#27"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#28"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "source_id")
SELECT
"in#35"."taxonlabel[rank='species'].out.taxonlabel_id"
, 'cultivar' AS "rank"
, 1 AS "source_id"
FROM "in#35"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#35_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#28"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: CheckException: Violated @"taxonlabel"."taxonlabel_required_key"@ constraint with condition @(((sourceaccessioncode IS NOT NULL) OR (taxonepithet IS NOT NULL)) OR (taxonomicname IS NOT NULL))@
# Ignoring rows that do not satisfy @false@
# Ignoring all rows
# Returning default: @"taxonlabel[rank='species']"."out.taxonlabel_id"@
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank='species']"."out.taxonlabel_id"@|
|@'rank'@|@"ACAD.Specimen"."taxonRank"@|
|@'taxonepithet'@|@"ACAD.Specimen"."infraspecificEpithet"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#36" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank='species']"."out.taxonlabel_id" AS "taxonlabel[rank='species'].out.taxonlabel_id"
, "ACAD.Specimen"."taxonRank" AS "ACAD.Specimen.taxonRank"
, "ACAD.Specimen"."infraspecificEpithet" AS "ACAD.Specimen.infraspecificEpithet"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank='species']" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#36"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#36_full" (
LIKE "in#36" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#36_full"
SELECT * FROM "in#36"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#29"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#36"."taxonlabel[rank='species'].out.taxonlabel_id"
, "in#36"."ACAD.Specimen.taxonRank"
, "in#36"."ACAD.Specimen.infraspecificEpithet"
FROM "in#36"
RETURNING "taxonlabel_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @taxonrank@ on column: @rank@
# Casting @'rank'@ input to @taxonrank@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."taxonrank(taxonRank)"("value" anyelement)
RETURNS taxonrank
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS taxonrank);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('taxonRank')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#36" ADD COLUMN "ACAD.Specimen.taxonRank::taxonrank" taxonrank /*"in#36"."ACAD.Specimen.taxonRank"::taxonrank*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#36"
ALTER COLUMN "ACAD.Specimen.taxonRank::taxonrank" TYPE taxonrank
USING "pg_temp"."taxonrank(taxonRank)"("in#36"."ACAD.Specimen.taxonRank")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#29"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet")
SELECT
"in#36"."taxonlabel[rank='species'].out.taxonlabel_id"
, "in#36"."ACAD.Specimen.taxonRank::taxonrank"
, "in#36"."ACAD.Specimen.infraspecificEpithet"
FROM "in#36"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#36_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#29"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#30"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#36"."taxonlabel[rank='species'].out.taxonlabel_id"
, "in#36"."ACAD.Specimen.taxonRank::taxonrank"
, "in#36"."ACAD.Specimen.infraspecificEpithet"
, 1 AS "source_id"
FROM "in#36"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#36_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#30"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: CheckException: Violated @"taxonlabel"."taxonlabel_required_key"@ constraint with condition @(((sourceaccessioncode IS NOT NULL) OR (taxonepithet IS NOT NULL)) OR (taxonomicname IS NOT NULL))@
# Ignoring rows that do not satisfy @("ACAD.Specimen.infraspecificEpithet" IS NOT NULL)@
<pre><code class="SQL">
DELETE FROM "in#36"
WHERE NOT COALESCE(("ACAD.Specimen.infraspecificEpithet" IS NOT NULL), false)
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#30"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "rank", "taxonepithet", "source_id")
SELECT
"in#36"."taxonlabel[rank='species'].out.taxonlabel_id"
, "in#36"."ACAD.Specimen.taxonRank::taxonrank"
, "in#36"."ACAD.Specimen.infraspecificEpithet"
, 1 AS "source_id"
FROM "in#36"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#36_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#30"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_unique@ constraint on columns: @parent_id, taxonepithet, rank, source_id, sourceaccessioncode, taxonomicname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."parent_id": "in#36"."taxonlabel[rank='species'].out.taxonlabel_id"
"taxonlabel"."taxonepithet": "in#36"."ACAD.Specimen.infraspecificEpithet"
"taxonlabel"."rank": "in#36"."ACAD.Specimen.taxonRank::taxonrank"
"taxonlabel"."source_id": 1
"taxonlabel"."sourceaccessioncode": None
"taxonlabel"."taxonomicname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#36_distinct" (
LIKE "in#36" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#36_distinct"
SELECT
DISTINCT ON ("in#36"."taxonlabel[rank='species'].out.taxonlabel_id", "in#36"."ACAD.Specimen.infraspecificEpithet", "in#36"."ACAD.Specimen.taxonRank::taxonrank")
"in#36".*
FROM "in#36"
LEFT JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#36"."taxonlabel[rank='species'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#36"."ACAD.Specimen.infraspecificEpithet", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = COALESCE("in#36"."ACAD.Specimen.taxonRank::taxonrank", CAST('unknown' AS taxonrank))
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#31"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id" IN
        SELECT
        "in#36_distinct"."taxonlabel[rank='species'].out.taxonlabel_id"
        , "in#36_distinct"."ACAD.Specimen.taxonRank::taxonrank"
        , "in#36_distinct"."ACAD.Specimen.infraspecificEpithet"
        , 1 AS "source_id"
        FROM "in#36_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "rank", "taxonepithet", "source_id")
                VALUES ("row"."parent_id", "row"."rank", "row"."taxonepithet", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#11" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#31"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank=taxonRank]" AS
SELECT
"in#36"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#36"
JOIN "taxonlabel" ON
COALESCE("taxonlabel"."parent_id", CAST(2147483647 AS integer)) = COALESCE("in#36"."taxonlabel[rank='species'].out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonlabel"."taxonepithet", CAST('\N' AS text)) = COALESCE("in#36"."ACAD.Specimen.infraspecificEpithet", CAST('\N' AS text))
AND COALESCE("taxonlabel"."rank", CAST('unknown' AS taxonrank)) = COALESCE("in#36"."ACAD.Specimen.taxonRank::taxonrank", CAST('unknown' AS taxonrank))
AND "taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."sourceaccessioncode", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#36"."taxonlabel[rank='species'].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank=taxonRank]"
("row_num", "out.taxonlabel_id")
SELECT
"in#36_full"."row_num"
, "in#36_full"."taxonlabel[rank='species'].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#36_full"
LEFT JOIN "taxonlabel[rank=taxonRank]" ON "taxonlabel[rank=taxonRank]"."row_num" = "in#36_full"."row_num"
WHERE "taxonlabel[rank=taxonRank]"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_taxon_family_require_std"@:

|Output|Input|
|@'family'@|@"ACAD.Specimen"."family"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#37" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."family" AS "ACAD.Specimen.family"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#37"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_taxon_family_require_std(family=family)" AS
SELECT
"in#37"."row_num"
, "_taxon_family_require_std"("family" := "in#37"."ACAD.Specimen.family") AS "result"
FROM "in#37"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_taxon_family_require_std(family=family)_wrap"()
RETURNS SETOF "_taxon_family_require_std(family=family)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#37"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#37"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_taxon_family_require_std"("family" := "row"."ACAD.Specimen.family")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.family" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('family')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_taxon_family_require_std(family=family)"
("row_num", "result")
SELECT * FROM "pg_temp"."_taxon_family_require_std(family=family)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_if"@:

|Output|Input|
|@'then'@|@"ACAD.Specimen"."taxonRank"@|
|@'cond'@|@"ACAD.Specimen"."infraspecificEpithet"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#38" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."taxonRank" AS "ACAD.Specimen.taxonRank"
, "ACAD.Specimen"."infraspecificEpithet" AS "ACAD.Specimen.infraspecificEpithet"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#38"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_if(then=taxonRank, cond=infraspecificEpithet)" AS
SELECT
"in#38"."row_num"
, "_if"("then" := "in#38"."ACAD.Specimen.taxonRank", "cond" := "in#38"."ACAD.Specimen.infraspecificEpithet") AS "result"
FROM "in#38"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_if(then=taxonRank, cond=infraspecificEpithet)_wrap"()
RETURNS SETOF "_if(then=taxonRank, cond=infraspecificEpithet)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#38"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#38"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_if"("then" := "row"."ACAD.Specimen.taxonRank", "cond" := "row"."ACAD.Specimen.infraspecificEpithet")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.taxonRank" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.infraspecificEpithet" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('taxonRank,infraspecificEpithet')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_if(then=taxonRank, cond=infraspecificEpithet)"
("row_num", "result")
SELECT * FROM "pg_temp"."_if(then=taxonRank, cond=infraspecificEpithet)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_join_words"@:

|Output|Input|
|@'1'@|@"_if(then=taxonRank, cond=infraspecificEpithet)"."result"@|
|@'2'@|@"ACAD.Specimen"."infraspecificEpithet"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#39" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_if(then=taxonRank, cond=infraspecificEpithet)"."result" AS "_if(then=taxonRank, cond=infraspecificEpithet).result"
, "ACAD.Specimen"."infraspecificEpithet" AS "ACAD.Specimen.infraspecificEpithet"
FROM "ACAD.Specimen"
JOIN "_if(then=taxonRank, cond=infraspecificEpithet)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#39"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_join_words(1=_if(then=taxonRank, cond=infraspecificEpithet), 2" AS
SELECT
"in#39"."row_num"
, "_join_words"("1" := "in#39"."_if(then=taxonRank, cond=infraspecificEpithet).result", "2" := "in#39"."ACAD.Specimen.infraspecificEpithet") AS "result"
FROM "in#39"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_join_words(1=_if(then=taxonRank, cond=infraspecificEpithe_wrap"()
RETURNS SETOF "_join_words(1=_if(then=taxonRank, cond=infraspecificEpithet), 2"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#39"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#39"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_join_words"("1" := "row"."_if(then=taxonRank, cond=infraspecificEpithet).result", "2" := "row"."ACAD.Specimen.infraspecificEpithet")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."_if(then=taxonRank, cond=infraspecificEpithet).result" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.infraspecificEpithet" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('taxonRank,infraspecificEpithet'), ('infraspecificEpithet,infraspecificEpithet')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_join_words(1=_if(then=taxonRank, cond=infraspecificEpithet), 2"
("row_num", "result")
SELECT * FROM "pg_temp"."_join_words(1=_if(then=taxonRank, cond=infraspecificEpithe_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_join_words"@:

|Output|Input|
|@'3'@|@"ACAD.Specimen"."specificEpithet"@|
|@'2'@|@"ACAD.Specimen"."genus"@|
|@'5'@|@"_join_words(1=_if(then=taxonRank, cond=infraspecificEpithet), 2"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#40" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."specificEpithet" AS "ACAD.Specimen.specificEpithet"
, "ACAD.Specimen"."genus" AS "ACAD.Specimen.genus"
, "_join_words(1=_if(then=taxonRank, cond=infraspecificEpithet), 2"."result" AS "_join_words(1=_if(then=taxonRank, cond=infraspecificEpit.result"
FROM "ACAD.Specimen"
JOIN "_join_words(1=_if(then=taxonRank, cond=infraspecificEpithet), 2" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#40"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_if(the" AS
SELECT
"in#40"."row_num"
, "_join_words"("3" := "in#40"."ACAD.Specimen.specificEpithet", "2" := "in#40"."ACAD.Specimen.genus", "5" := "in#40"."_join_words(1=_if(then=taxonRank, cond=infraspecificEpit.result") AS "result"
FROM "in#40"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_i_wrap"()
RETURNS SETOF "_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_if(the"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#40"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#40"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_join_words"("3" := "row"."ACAD.Specimen.specificEpithet", "2" := "row"."ACAD.Specimen.genus", "5" := "row"."_join_words(1=_if(then=taxonRank, cond=infraspecificEpit.result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.specificEpithet" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.genus" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."_join_words(1=_if(then=taxonRank, cond=infraspecificEpit.result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('specificEpithet,genus,taxonRank'), ('specificEpithet,genus,infraspecificEpithet')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_if(the"
("row_num", "result")
SELECT * FROM "pg_temp"."_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_i_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_join_words"@:

|Output|Input|
|@'1'@|@"_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_if(the"."result"@|
|@'2'@|@"ACAD.Specimen"."scientificNameAuthorship"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#41" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_if(the"."result" AS "_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=.result"
, "ACAD.Specimen"."scientificNameAuthorship" AS "ACAD.Specimen.scientificNameAuthorship"
FROM "ACAD.Specimen"
JOIN "_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=_if(the" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#41"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_join_w" AS
SELECT
"in#41"."row_num"
, "_join_words"("1" := "in#41"."_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=.result", "2" := "in#41"."ACAD.Specimen.scientificNameAuthorship") AS "result"
FROM "in#41"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_j_wrap"()
RETURNS SETOF "_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_join_w"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#41"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#41"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_join_words"("1" := "row"."_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=.result", "2" := "row"."ACAD.Specimen.scientificNameAuthorship")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."_join_words(3=specificEpithet, 2=genus, 5=_join_words(1=.result" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."ACAD.Specimen.scientificNameAuthorship" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('specificEpithet,scientificNameAuthorship'), ('genus,scientificNameAuthorship'), ('taxonRank,scientificNameAuthorship'), ('infraspecificEpithet,scientificNameAuthorship')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_join_w"
("row_num", "result")
SELECT * FROM "pg_temp"."_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_j_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_alt"@:

|Output|Input|
|@'1'@|@"ACAD.Specimen"."scientificName"@|
|@'2'@|@"_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_join_w"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#42" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."scientificName" AS "ACAD.Specimen.scientificName"
, "_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_join_w"."result" AS "_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=.result"
FROM "ACAD.Specimen"
JOIN "_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=_join_w" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#42"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_alt(1=scientificName, 2=_join_words(1=_join_words(3=specificEp" AS
SELECT
"in#42"."row_num"
, "_alt"("1" := "in#42"."ACAD.Specimen.scientificName", "2" := "in#42"."_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=.result") AS "result"
FROM "in#42"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_alt(1=scientificName, 2=_join_words(1=_join_words(3=speci_wrap"()
RETURNS SETOF "_alt(1=scientificName, 2=_join_words(1=_join_words(3=specificEp"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#42"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#42"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_alt"("1" := "row"."ACAD.Specimen.scientificName", "2" := "row"."_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=.result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.scientificName" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."_join_words(1=_join_words(3=specificEpithet, 2=genus, 5=.result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('scientificName,specificEpithet'), ('scientificName,genus'), ('scientificName,taxonRank'), ('scientificName,infraspecificEpithet'), ('scientificName,scientificNameAuthorship')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_alt(1=scientificName, 2=_join_words(1=_join_words(3=specificEp"
("row_num", "result")
SELECT * FROM "pg_temp"."_alt(1=scientificName, 2=_join_words(1=_join_words(3=speci_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_merge_prefix"@:

|Output|Input|
|@'prefix'@|@"_taxon_family_require_std(family=family)"."result"@|
|@'value'@|@"_alt(1=scientificName, 2=_join_words(1=_join_words(3=specificEp"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#43" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_taxon_family_require_std(family=family)"."result" AS "_taxon_family_require_std(family=family).result"
, "_alt(1=scientificName, 2=_join_words(1=_join_words(3=specificEp"."result" AS "_alt(1=scientificName, 2=_join_words(1=_join_words(3=spe.result"
FROM "ACAD.Specimen"
JOIN "_taxon_family_require_std(family=family)" USING ("row_num")
JOIN "_alt(1=scientificName, 2=_join_words(1=_join_words(3=specificEp" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#43"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_word" AS
SELECT
"in#43"."row_num"
, "_merge_prefix"("prefix" := "in#43"."_taxon_family_require_std(family=family).result", "value" := "in#43"."_alt(1=scientificName, 2=_join_words(1=_join_words(3=spe.result") AS "result"
FROM "in#43"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_wrap"()
RETURNS SETOF "_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_word"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#43"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#43"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_merge_prefix"("prefix" := "row"."_taxon_family_require_std(family=family).result", "value" := "row"."_alt(1=scientificName, 2=_join_words(1=_join_words(3=spe.result")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."_taxon_family_require_std(family=family).result" AS text), CAST(NULL AS text)), COALESCE(CAST("row"."_alt(1=scientificName, 2=_join_words(1=_join_words(3=spe.result" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('family,scientificName'), ('family,specificEpithet'), ('family,genus'), ('family,taxonRank'), ('family,infraspecificEpithet'), ('family,scientificNameAuthorship')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_word"
("row_num", "result")
SELECT * FROM "pg_temp"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonlabel"@:

|Output|Input|
|@'parent_id'@|@"taxonlabel[rank=taxonRank]"."out.taxonlabel_id"@|
|@'taxonomicname'@|@"_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_word"."result"@|
|@'rank'@|@"ACAD.Specimen"."taxonRank"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#44" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "taxonlabel[rank=taxonRank]"."out.taxonlabel_id" AS "taxonlabel[rank=taxonRank].out.taxonlabel_id"
, "_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_word"."result" AS "_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result"
, "ACAD.Specimen"."taxonRank" AS "ACAD.Specimen.taxonRank"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank=taxonRank]" USING ("row_num")
JOIN "_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_join_word" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#44"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#44_full" (
LIKE "in#44" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#44_full"
SELECT * FROM "in#44"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#32"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "taxonomicname", "rank")
SELECT
"in#44"."taxonlabel[rank=taxonRank].out.taxonlabel_id"
, "in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result"
, "in#44"."ACAD.Specimen.taxonRank"
FROM "in#44"
RETURNING "taxonlabel_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @taxonrank@ on column: @rank@
# Casting @'rank'@ input to @taxonrank@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."taxonrank(taxonRank)"("value" anyelement)
RETURNS taxonrank
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS taxonrank);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('taxonRank')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#44" ADD COLUMN "ACAD.Specimen.taxonRank::taxonrank" taxonrank /*"in#44"."ACAD.Specimen.taxonRank"::taxonrank*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#44"
ALTER COLUMN "ACAD.Specimen.taxonRank::taxonrank" TYPE taxonrank
USING "pg_temp"."taxonrank(taxonRank)"("in#44"."ACAD.Specimen.taxonRank")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#32"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "taxonomicname", "rank")
SELECT
"in#44"."taxonlabel[rank=taxonRank].out.taxonlabel_id"
, "in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result"
, "in#44"."ACAD.Specimen.taxonRank::taxonrank"
FROM "in#44"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#44_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#32"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#33"()
RETURNS SETOF "taxonlabel"."taxonlabel_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonlabel"
("parent_id", "taxonomicname", "rank", "source_id")
SELECT
"in#44"."taxonlabel[rank=taxonRank].out.taxonlabel_id"
, "in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result"
, "in#44"."ACAD.Specimen.taxonRank::taxonrank"
, 1 AS "source_id"
FROM "in#44"
RETURNING "taxonlabel_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#44_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#33"() AS "f" ("taxonlabel_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonlabel_0_unique_identifying_name@ constraint with condition @taxonomicname IS NOT NULL@ on columns: @source_id, taxonomicname@
# Ignoring rows that do not satisfy @"_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result" IS NOT NULL@
<pre><code class="SQL">
DELETE FROM "in#44"
WHERE NOT COALESCE("_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result" IS NOT NULL, false)
</code></pre>
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonlabel"."source_id": 1
"taxonlabel"."taxonomicname": "in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result"
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#44_distinct" (
LIKE "in#44" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#44_distinct"
SELECT
DISTINCT ON ("in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result")
"in#44".*
FROM "in#44"
LEFT JOIN "taxonlabel" ON
"taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE("in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result", CAST('\N' AS text))
WHERE "taxonlabel"."taxonlabel_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonlabel#34"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonlabel"%ROWTYPE;
BEGIN
    FOR "row"."parent_id", "row"."taxonomicname", "row"."rank", "row"."source_id" IN
        SELECT
        "in#44_distinct"."taxonlabel[rank=taxonRank].out.taxonlabel_id"
        , "in#44_distinct"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result"
        , "in#44_distinct"."ACAD.Specimen.taxonRank::taxonrank"
        , 1 AS "source_id"
        FROM "in#44_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonlabel"
                ("parent_id", "taxonomicname", "rank", "source_id")
                VALUES ("row"."parent_id", "row"."taxonomicname", "row"."rank", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#12" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonlabel#34"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonlabel[rank=taxonRank]#1" AS
SELECT
"in#44"."row_num"
, "taxonlabel"."taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#44"
JOIN "taxonlabel" ON
"taxonlabel"."source_id" = 1
AND COALESCE("taxonlabel"."taxonomicname", CAST('\N' AS text)) = COALESCE("in#44"."_merge_prefix(_alt(1=scientificName, 2=_join_words(1=_jo.result", CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @"in#44"."taxonlabel[rank=taxonRank].out.taxonlabel_id"@
<pre><code class="SQL">
INSERT INTO "taxonlabel[rank=taxonRank]#1"
("row_num", "out.taxonlabel_id")
SELECT
"in#44_full"."row_num"
, "in#44_full"."taxonlabel[rank=taxonRank].out.taxonlabel_id" AS "out.taxonlabel_id"
FROM "in#44_full"
LEFT JOIN "taxonlabel[rank=taxonRank]#1" ON "taxonlabel[rank=taxonRank]#1"."row_num" = "in#44_full"."row_num"
WHERE "taxonlabel[rank=taxonRank]#1"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxonverbatim"@:

|Output|Input|
|@'family'@|@"ACAD.Specimen"."family"@|
|@'author'@|@"ACAD.Specimen"."scientificNameAuthorship"@|
|@'specific_epithet'@|@"ACAD.Specimen"."specificEpithet"@|
|@'taxonlabel_id'@|@"taxonlabel[rank=taxonRank]#1"."out.taxonlabel_id"@|
|@'verbatimrank'@|@"ACAD.Specimen"."taxonRank"@|
|@'genus'@|@"ACAD.Specimen"."genus"@|
|@'taxonomicname'@|@"ACAD.Specimen"."scientificName"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#45" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."family" AS "ACAD.Specimen.family"
, "ACAD.Specimen"."scientificNameAuthorship" AS "ACAD.Specimen.scientificNameAuthorship"
, "ACAD.Specimen"."specificEpithet" AS "ACAD.Specimen.specificEpithet"
, "taxonlabel[rank=taxonRank]#1"."out.taxonlabel_id" AS "taxonlabel[rank=taxonRank]#1.out.taxonlabel_id"
, "ACAD.Specimen"."taxonRank" AS "ACAD.Specimen.taxonRank"
, "ACAD.Specimen"."genus" AS "ACAD.Specimen.genus"
, "ACAD.Specimen"."scientificName" AS "ACAD.Specimen.scientificName"
FROM "ACAD.Specimen"
JOIN "taxonlabel[rank=taxonRank]#1" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#45"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#45_full" (
LIKE "in#45" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#45_full"
SELECT * FROM "in#45"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonverbatim"()
RETURNS SETOF "taxonverbatim"."taxonverbatim_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonverbatim"
("family", "author", "specific_epithet", "taxonlabel_id", "verbatimrank", "genus", "taxonomicname")
SELECT
"in#45"."ACAD.Specimen.family"
, "in#45"."ACAD.Specimen.scientificNameAuthorship"
, "in#45"."ACAD.Specimen.specificEpithet"
, "in#45"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id"
, "in#45"."ACAD.Specimen.taxonRank"
, "in#45"."ACAD.Specimen.genus"
, "in#45"."ACAD.Specimen.scientificName"
FROM "in#45"
RETURNING "taxonverbatim_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#45_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonverbatim"() AS "f" ("taxonverbatim_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonverbatim#1"()
RETURNS SETOF "taxonverbatim"."taxonverbatim_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxonverbatim"
("family", "author", "specific_epithet", "taxonlabel_id", "verbatimrank", "genus", "taxonomicname", "source_id")
SELECT
"in#45"."ACAD.Specimen.family"
, "in#45"."ACAD.Specimen.scientificNameAuthorship"
, "in#45"."ACAD.Specimen.specificEpithet"
, "in#45"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id"
, "in#45"."ACAD.Specimen.taxonRank"
, "in#45"."ACAD.Specimen.genus"
, "in#45"."ACAD.Specimen.scientificName"
, 1 AS "source_id"
FROM "in#45"
RETURNING "taxonverbatim_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#45_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonverbatim#1"() AS "f" ("taxonverbatim_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @taxonverbatim_unique@ constraint on columns: @taxonlabel_id, morphospecies, morphoname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"taxonverbatim"."taxonlabel_id": "in#45"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id"
"taxonverbatim"."morphospecies": None
"taxonverbatim"."morphoname": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#45_distinct" (
LIKE "in#45" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#45_distinct"
SELECT
DISTINCT ON ("in#45"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id")
"in#45".*
FROM "in#45"
LEFT JOIN "taxonverbatim" ON
COALESCE("taxonverbatim"."taxonlabel_id", CAST(2147483647 AS integer)) = COALESCE("in#45"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonverbatim"."morphospecies", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonverbatim"."morphoname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "taxonverbatim"."taxonverbatim_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxonverbatim#2"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "taxonverbatim"%ROWTYPE;
BEGIN
    FOR "row"."family", "row"."author", "row"."specific_epithet", "row"."taxonlabel_id", "row"."verbatimrank", "row"."genus", "row"."taxonomicname", "row"."source_id" IN
        SELECT
        "in#45_distinct"."ACAD.Specimen.family"
        , "in#45_distinct"."ACAD.Specimen.scientificNameAuthorship"
        , "in#45_distinct"."ACAD.Specimen.specificEpithet"
        , "in#45_distinct"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id"
        , "in#45_distinct"."ACAD.Specimen.taxonRank"
        , "in#45_distinct"."ACAD.Specimen.genus"
        , "in#45_distinct"."ACAD.Specimen.scientificName"
        , 1 AS "source_id"
        FROM "in#45_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "taxonverbatim"
                ("family", "author", "specific_epithet", "taxonlabel_id", "verbatimrank", "genus", "taxonomicname", "source_id")
                VALUES ("row"."family", "row"."author", "row"."specific_epithet", "row"."taxonlabel_id", "row"."verbatimrank", "row"."genus", "row"."taxonomicname", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#13" AS
SELECT * FROM "pg_temp"."INSERT INTO taxonverbatim#2"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "taxonverbatim_pkeys" AS
SELECT
"in#45"."row_num"
, "taxonverbatim"."taxonverbatim_id" AS "out.taxonverbatim_id"
FROM "in#45"
JOIN "taxonverbatim" ON
COALESCE("taxonverbatim"."taxonlabel_id", CAST(2147483647 AS integer)) = COALESCE("in#45"."taxonlabel[rank=taxonRank]#1.out.taxonlabel_id", CAST(2147483647 AS integer))
AND COALESCE("taxonverbatim"."morphospecies", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("taxonverbatim"."morphoname", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "taxonverbatim_pkeys"
("row_num", "out.taxonverbatim_id")
SELECT
"in#45_full"."row_num"
, NULL AS "out.taxonverbatim_id"
FROM "in#45_full"
LEFT JOIN "taxonverbatim_pkeys" ON "taxonverbatim_pkeys"."row_num" = "in#45_full"."row_num"
WHERE "taxonverbatim_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"taxondetermination"@:

|Output|Input|
|@'determinationdate'@|@"_dateRangeStart(dateIdentified)"."result"@|
|@'taxonoccurrence_id'@|@"taxonoccurrence_pkeys"."out.taxonoccurrence_id"@|
|@'party_id'@|@"party_pkeys#1"."out.party_id"@|
|@'taxonverbatim_id'@|@"taxonverbatim_pkeys"."out.taxonverbatim_id"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#46" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_dateRangeStart(dateIdentified)"."result" AS "_dateRangeStart(dateIdentified).result"
, "taxonoccurrence_pkeys"."out.taxonoccurrence_id" AS "taxonoccurrence_pkeys.out.taxonoccurrence_id"
, "party_pkeys#1"."out.party_id" AS "party_pkeys#1.out.party_id"
, "taxonverbatim_pkeys"."out.taxonverbatim_id" AS "taxonverbatim_pkeys.out.taxonverbatim_id"
FROM "ACAD.Specimen"
JOIN "_dateRangeStart(dateIdentified)" USING ("row_num")
JOIN "taxonoccurrence_pkeys" USING ("row_num")
JOIN "party_pkeys#1" USING ("row_num")
JOIN "taxonverbatim_pkeys" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#46"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#46_full" (
LIKE "in#46" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#46_full"
SELECT * FROM "in#46"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxondetermination"()
RETURNS SETOF "taxondetermination"."taxondetermination_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxondetermination"
("party_id", "taxonoccurrence_id", "determinationdate", "taxonverbatim_id")
SELECT
"in#46"."party_pkeys#1.out.party_id"
, "in#46"."taxonoccurrence_pkeys.out.taxonoccurrence_id"
, "in#46"."_dateRangeStart(dateIdentified).result"
, "in#46"."taxonverbatim_pkeys.out.taxonverbatim_id"
FROM "in#46"
RETURNING "taxondetermination_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @date@ on column: @determinationdate@
# Casting @'determinationdate'@ input to @date@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."date(dateIdentified)"("value" anyelement)
RETURNS date
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS date);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('dateIdentified')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#46" ADD COLUMN "_dateRangeStart(dateIdentified).result::date" date /*"in#46"."_dateRangeStart(dateIdentified).result"::date*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#46"
ALTER COLUMN "_dateRangeStart(dateIdentified).result::date" TYPE date
USING "pg_temp"."date(dateIdentified)"("in#46"."_dateRangeStart(dateIdentified).result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxondetermination"()
RETURNS SETOF "taxondetermination"."taxondetermination_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxondetermination"
("party_id", "taxonoccurrence_id", "determinationdate", "taxonverbatim_id")
SELECT
"in#46"."party_pkeys#1.out.party_id"
, "in#46"."taxonoccurrence_pkeys.out.taxonoccurrence_id"
, "in#46"."_dateRangeStart(dateIdentified).result::date"
, "in#46"."taxonverbatim_pkeys.out.taxonverbatim_id"
FROM "in#46"
RETURNING "taxondetermination_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#46_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxondetermination"() AS "f" ("taxondetermination_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO taxondetermination#1"()
RETURNS SETOF "taxondetermination"."taxondetermination_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "taxondetermination"
("party_id", "taxonoccurrence_id", "determinationdate", "taxonverbatim_id", "source_id")
SELECT
"in#46"."party_pkeys#1.out.party_id"
, "in#46"."taxonoccurrence_pkeys.out.taxonoccurrence_id"
, "in#46"."_dateRangeStart(dateIdentified).result::date"
, "in#46"."taxonverbatim_pkeys.out.taxonverbatim_id"
, 1 AS "source_id"
FROM "in#46"
RETURNING "taxondetermination_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#46_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO taxondetermination#1"() AS "f" ("taxondetermination_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#46_insert_in_pkeys" AS
SELECT "row_num" FROM "in#46"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "taxondetermination_pkeys" AS
SELECT
"in#46_insert_in_pkeys"."row_num"
, "in#46_insert_out_pkeys"."taxondetermination_id" AS "out.taxondetermination_id"
FROM "in#46_insert_in_pkeys"
JOIN "in#46_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "taxondetermination_pkeys"
("row_num", "out.taxondetermination_id")
SELECT
"in#46_full"."row_num"
, NULL AS "out.taxondetermination_id"
FROM "in#46_full"
LEFT JOIN "taxondetermination_pkeys" ON "taxondetermination_pkeys"."row_num" = "in#46_full"."row_num"
WHERE "taxondetermination_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_nullIf"@:

|Output|Input|
|@'null'@|@'0'@|
|@'type'@|@'float'@|
|@'value'@|@"ACAD.Specimen"."decimalLatitude"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#47" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."decimalLatitude" AS "ACAD.Specimen.decimalLatitude"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#47"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_nullIf(decimalLatitude)" AS
SELECT
"in#47"."row_num"
, "_nullIf"("null" := '0', "type" := 'float', "value" := "in#47"."ACAD.Specimen.decimalLatitude") AS "result"
FROM "in#47"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_nullIf(decimalLatitude)_wrap"()
RETURNS SETOF "_nullIf(decimalLatitude)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#47"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#47"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_nullIf"("null" := '0', "type" := 'float', "value" := "row"."ACAD.Specimen.decimalLatitude")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.decimalLatitude" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('decimalLatitude')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_nullIf(decimalLatitude)"
("row_num", "result")
SELECT * FROM "pg_temp"."_nullIf(decimalLatitude)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_nullIf"@:

|Output|Input|
|@'null'@|@'0'@|
|@'type'@|@'float'@|
|@'value'@|@"ACAD.Specimen"."decimalLongitude"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#48" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."decimalLongitude" AS "ACAD.Specimen.decimalLongitude"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#48"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_nullIf(decimalLongitude)" AS
SELECT
"in#48"."row_num"
, "_nullIf"("null" := '0', "type" := 'float', "value" := "in#48"."ACAD.Specimen.decimalLongitude") AS "result"
FROM "in#48"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_nullIf(decimalLongitude)_wrap"()
RETURNS SETOF "_nullIf(decimalLongitude)"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#48"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#48"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_nullIf"("null" := '0', "type" := 'float', "value" := "row"."ACAD.Specimen.decimalLongitude")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.decimalLongitude" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('decimalLongitude')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_nullIf(decimalLongitude)"
("row_num", "result")
SELECT * FROM "pg_temp"."_nullIf(decimalLongitude)_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"coordinates"@:

|Output|Input|
|@'latitude_deg'@|@"_nullIf(decimalLatitude)"."result"@|
|@'longitude_deg'@|@"_nullIf(decimalLongitude)"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#49" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_nullIf(decimalLatitude)"."result" AS "_nullIf(decimalLatitude).result"
, "_nullIf(decimalLongitude)"."result" AS "_nullIf(decimalLongitude).result"
FROM "ACAD.Specimen"
JOIN "_nullIf(decimalLatitude)" USING ("row_num")
JOIN "_nullIf(decimalLongitude)" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#49"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#49_full" (
LIKE "in#49" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#49_full"
SELECT * FROM "in#49"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("latitude_deg", "longitude_deg")
SELECT
"in#49"."_nullIf(decimalLatitude).result"
, "in#49"."_nullIf(decimalLongitude).result"
FROM "in#49"
RETURNING "coordinates_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @double precision@ on column: @latitude_deg@
# Casting @'latitude_deg'@ input to @double precision@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."double(decimalLatitude)"("value" anyelement)
RETURNS double precision
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS double precision);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('decimalLatitude')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#49" ADD COLUMN "_nullIf(decimalLatitude).result::double" double precision /*"in#49"."_nullIf(decimalLatitude).result"::double precision*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#49"
ALTER COLUMN "_nullIf(decimalLatitude).result::double" TYPE double precision
USING "pg_temp"."double(decimalLatitude)"("in#49"."_nullIf(decimalLatitude).result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("latitude_deg", "longitude_deg")
SELECT
"in#49"."_nullIf(decimalLatitude).result::double"
, "in#49"."_nullIf(decimalLongitude).result"
FROM "in#49"
RETURNING "coordinates_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @double precision@ on column: @longitude_deg@
# Casting @'longitude_deg'@ input to @double precision@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."double(decimalLongitude)"("value" anyelement)
RETURNS double precision
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS double precision);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('decimalLongitude')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#49" ADD COLUMN "_nullIf(decimalLongitude).result::double" double precision /*"in#49"."_nullIf(decimalLongitude).result"::double precision*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#49"
ALTER COLUMN "_nullIf(decimalLongitude).result::double" TYPE double precision
USING "pg_temp"."double(decimalLongitude)"("in#49"."_nullIf(decimalLongitude).result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("latitude_deg", "longitude_deg")
SELECT
"in#49"."_nullIf(decimalLatitude).result::double"
, "in#49"."_nullIf(decimalLongitude).result::double"
FROM "in#49"
RETURNING "coordinates_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#49_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO coordinates"() AS "f" ("coordinates_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates#1"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("latitude_deg", "longitude_deg", "source_id")
SELECT
"in#49"."_nullIf(decimalLatitude).result::double"
, "in#49"."_nullIf(decimalLongitude).result::double"
, 1 AS "source_id"
FROM "in#49"
RETURNING "coordinates_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#49_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO coordinates#1"() AS "f" ("coordinates_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @coordinates_unique@ constraint on columns: @source_id, latitude_deg, longitude_deg, verbatimlatitude, verbatimlongitude, verbatimcoordinates, footprintgeometry_dwc@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"coordinates"."source_id": 1
"coordinates"."latitude_deg": "in#49"."_nullIf(decimalLatitude).result::double"
"coordinates"."longitude_deg": "in#49"."_nullIf(decimalLongitude).result::double"
"coordinates"."verbatimlatitude": None
"coordinates"."verbatimlongitude": None
"coordinates"."verbatimcoordinates": None
"coordinates"."footprintgeometry_dwc": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#49_distinct" (
LIKE "in#49" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#49_distinct"
SELECT
DISTINCT ON ("in#49"."_nullIf(decimalLatitude).result::double", "in#49"."_nullIf(decimalLongitude).result::double")
"in#49".*
FROM "in#49"
LEFT JOIN "coordinates" ON
"coordinates"."source_id" = 1
AND "coordinates"."latitude_deg" = "in#49"."_nullIf(decimalLatitude).result::double"
AND "coordinates"."longitude_deg" = "in#49"."_nullIf(decimalLongitude).result::double"
AND COALESCE("coordinates"."verbatimlatitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimlongitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimcoordinates", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."footprintgeometry_dwc", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "coordinates"."coordinates_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates#2"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "coordinates"%ROWTYPE;
BEGIN
    FOR "row"."latitude_deg", "row"."longitude_deg", "row"."source_id" IN
        SELECT
        "in#49_distinct"."_nullIf(decimalLatitude).result::double"
        , "in#49_distinct"."_nullIf(decimalLongitude).result::double"
        , 1 AS "source_id"
        FROM "in#49_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "coordinates"
                ("latitude_deg", "longitude_deg", "source_id")
                VALUES ("row"."latitude_deg", "row"."longitude_deg", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#14" AS
SELECT * FROM "pg_temp"."INSERT INTO coordinates#2"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "coordinates_pkeys" AS
SELECT
"in#49"."row_num"
, "coordinates"."coordinates_id" AS "out.coordinates_id"
FROM "in#49"
JOIN "coordinates" ON
"coordinates"."source_id" = 1
AND "coordinates"."latitude_deg" = "in#49"."_nullIf(decimalLatitude).result::double"
AND "coordinates"."longitude_deg" = "in#49"."_nullIf(decimalLongitude).result::double"
AND COALESCE("coordinates"."verbatimlatitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimlongitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimcoordinates", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."footprintgeometry_dwc", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "coordinates_pkeys"
("row_num", "out.coordinates_id")
SELECT
"in#49_full"."row_num"
, NULL AS "out.coordinates_id"
FROM "in#49_full"
LEFT JOIN "coordinates_pkeys" ON "coordinates_pkeys"."row_num" = "in#49_full"."row_num"
WHERE "coordinates_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'geoscrub'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'geoscrub' AS "shortname"
RETURNING "source_id"
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'geoscrub'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'geoscrub' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'geoscrub'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'geoscrub'
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_nullIf"@:

|Output|Input|
|@'null'@|@'0'@|
|@'type'@|@'float'@|
|@'value'@|@"ACAD.Specimen"."decimalLatitude"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#50" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."decimalLatitude" AS "ACAD.Specimen.decimalLatitude"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#50"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_nullIf(decimalLatitude)#1" AS
SELECT
"in#50"."row_num"
, "_nullIf"("null" := '0', "type" := 'float', "value" := "in#50"."ACAD.Specimen.decimalLatitude") AS "result"
FROM "in#50"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_nullIf(decimalLatitude)#1_wrap"()
RETURNS SETOF "_nullIf(decimalLatitude)#1"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#50"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#50"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_nullIf"("null" := '0', "type" := 'float', "value" := "row"."ACAD.Specimen.decimalLatitude")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.decimalLatitude" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('decimalLatitude')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_nullIf(decimalLatitude)#1"
("row_num", "result")
SELECT * FROM "pg_temp"."_nullIf(decimalLatitude)#1_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"_nullIf"@:

|Output|Input|
|@'null'@|@'0'@|
|@'type'@|@'float'@|
|@'value'@|@"ACAD.Specimen"."decimalLongitude"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#51" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "ACAD.Specimen"."decimalLongitude" AS "ACAD.Specimen.decimalLongitude"
FROM "ACAD.Specimen"
ORDER BY row_num
</code></pre>Temp table: @"in#51"@
# Defining wrapper function
<pre><code class="SQL">
CREATE TEMP TABLE "_nullIf(decimalLongitude)#1" AS
SELECT
"in#51"."row_num"
, "_nullIf"("null" := '0', "type" := 'float', "value" := "in#51"."ACAD.Specimen.decimalLongitude") AS "result"
FROM "in#51"
LIMIT 0
</code></pre><pre><code class="SQL">
CREATE FUNCTION "pg_temp"."_nullIf(decimalLongitude)#1_wrap"()
RETURNS SETOF "_nullIf(decimalLongitude)#1"
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "in#51"%ROWTYPE;
BEGIN
    FOR "row" IN
        SELECT * FROM "in#51"
    LOOP
        BEGIN
            BEGIN
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , "_nullIf"("null" := '0', "type" := 'float', "value" := "row"."ACAD.Specimen.decimalLongitude")
                ;
            EXCEPTION
                WHEN internal_error THEN
                    -- Handle PL/Python exceptions
                    DECLARE
                        matches text[] := regexp_matches(SQLERRM,
                            E'^(?:PL/Python: )?(\\w+): (.*)$'); -- .* also matches \n
                        exc_name text := matches[1];
                        msg text := matches[2];
                    BEGIN
                        /* Re-raise PL/Python exceptions with the PL/Python prefix removed.
                        This allows the exception to be parsed like a native exception.
                        Always raise as data_exception so it goes in the errors table. */
                        IF exc_name IS NOT NULL THEN
                            RAISE data_exception USING MESSAGE = msg;
                        -- Re-raise non-PL/Python exceptions
                        ELSE
                            RAISE USING ERRCODE = SQLSTATE, MESSAGE = SQLERRM;
                        END IF;
                    END;
            END;
        EXCEPTION
            WHEN data_exception THEN
                -- Save error in errors table.
                DECLARE
                    error_code text := SQLSTATE;
                    error text := SQLERRM;
                    value text := array_to_string(ARRAY[COALESCE(CAST("row"."ACAD.Specimen.decimalLongitude" AS text), CAST(NULL AS text))], ',');
                    "column" text;
                BEGIN
                    -- Insert the value and error for *each* source column.
                    FOR "column" IN
                        SELECT * FROM (VALUES ('decimalLongitude')) AS "c"
                    LOOP
                        BEGIN
                            INSERT INTO "ACAD"."Specimen.errors"
                            ("column", "value", "error_code", "error")
                            VALUES ("column", "value", "error_code", "error");
                        EXCEPTION
                            WHEN unique_violation THEN NULL;
                        END;
                    END LOOP;
                
                END;
                
                RETURN QUERY
                    SELECT
                    "row"."row_num"
                    , CAST(NULL AS text)
                ;
        END;
    END LOOP;
END;

$$;
</code></pre>
# Calling function
<pre><code class="SQL">
INSERT INTO "_nullIf(decimalLongitude)#1"
("row_num", "result")
SELECT * FROM "pg_temp"."_nullIf(decimalLongitude)#1_wrap"()
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"coordinates"@:

|Output|Input|
|@'source_id'@|@26@|
|@'latitude_deg'@|@"_nullIf(decimalLatitude)#1"."result"@|
|@'longitude_deg'@|@"_nullIf(decimalLongitude)#1"."result"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#52" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "_nullIf(decimalLatitude)#1"."result" AS "_nullIf(decimalLatitude)#1.result"
, "_nullIf(decimalLongitude)#1"."result" AS "_nullIf(decimalLongitude)#1.result"
FROM "ACAD.Specimen"
JOIN "_nullIf(decimalLatitude)#1" USING ("row_num")
JOIN "_nullIf(decimalLongitude)#1" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#52"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#52_full" (
LIKE "in#52" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#52_full"
SELECT * FROM "in#52"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates#3"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("source_id", "latitude_deg", "longitude_deg")
SELECT
26 AS "source_id"
, "in#52"."_nullIf(decimalLatitude)#1.result"
, "in#52"."_nullIf(decimalLongitude)#1.result"
FROM "in#52"
RETURNING "coordinates_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @double precision@ on column: @latitude_deg@
# Casting @'latitude_deg'@ input to @double precision@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."double(decimalLatitude)"("value" anyelement)
RETURNS double precision
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS double precision);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('decimalLatitude')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#52" ADD COLUMN "_nullIf(decimalLatitude)#1.result::double" double precision /*"in#52"."_nullIf(decimalLatitude)#1.result"::double precision*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#52"
ALTER COLUMN "_nullIf(decimalLatitude)#1.result::double" TYPE double precision
USING "pg_temp"."double(decimalLatitude)"("in#52"."_nullIf(decimalLatitude)#1.result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates#3"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("source_id", "latitude_deg", "longitude_deg")
SELECT
26 AS "source_id"
, "in#52"."_nullIf(decimalLatitude)#1.result::double"
, "in#52"."_nullIf(decimalLongitude)#1.result"
FROM "in#52"
RETURNING "coordinates_id"
$$;
</code></pre>
# Caught exception: MissingCastException: Missing cast to type @double precision@ on column: @longitude_deg@
# Casting @'longitude_deg'@ input to @double precision@
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."double(decimalLongitude)"("value" anyelement)
RETURNS double precision
LANGUAGE plpgsql
STRICT
AS $$
BEGIN
    /* The explicit cast to the return type is needed to make the cast happen
    inside the try block. (Implicit casts to the return type happen at the end
    of the function, outside any block.) */
    RETURN CAST(value AS double precision);
EXCEPTION
    WHEN data_exception THEN
        -- Save error in errors table.
        DECLARE
            error_code text := SQLSTATE;
            error text := SQLERRM;
            value text := value;
            "column" text;
        BEGIN
            -- Insert the value and error for *each* source column.
            FOR "column" IN
                SELECT * FROM (VALUES ('decimalLongitude')) AS "c"
            LOOP
                BEGIN
                    INSERT INTO "ACAD"."Specimen.errors"
                    ("column", "value", "error_code", "error")
                    VALUES ("column", "value", "error_code", "error");
                EXCEPTION
                    WHEN unique_violation THEN NULL;
                END;
            END LOOP;
        
        END;
        
        RETURN NULL;
END;
$$;
</code></pre><pre><code class="SQL">
ALTER TABLE "in#52" ADD COLUMN "_nullIf(decimalLongitude)#1.result::double" double precision /*"in#52"."_nullIf(decimalLongitude)#1.result"::double precision*/
</code></pre><pre><code class="SQL">
ALTER TABLE "in#52"
ALTER COLUMN "_nullIf(decimalLongitude)#1.result::double" TYPE double precision
USING "pg_temp"."double(decimalLongitude)"("in#52"."_nullIf(decimalLongitude)#1.result")
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates#3"()
RETURNS SETOF "coordinates"."coordinates_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "coordinates"
("source_id", "latitude_deg", "longitude_deg")
SELECT
26 AS "source_id"
, "in#52"."_nullIf(decimalLatitude)#1.result::double"
, "in#52"."_nullIf(decimalLongitude)#1.result::double"
FROM "in#52"
RETURNING "coordinates_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#52_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO coordinates#3"() AS "f" ("coordinates_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @coordinates_unique@ constraint on columns: @source_id, latitude_deg, longitude_deg, verbatimlatitude, verbatimlongitude, verbatimcoordinates, footprintgeometry_dwc@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"coordinates"."source_id": 26
"coordinates"."latitude_deg": "in#52"."_nullIf(decimalLatitude)#1.result::double"
"coordinates"."longitude_deg": "in#52"."_nullIf(decimalLongitude)#1.result::double"
"coordinates"."verbatimlatitude": None
"coordinates"."verbatimlongitude": None
"coordinates"."verbatimcoordinates": None
"coordinates"."footprintgeometry_dwc": None
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#52_distinct" (
LIKE "in#52" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#52_distinct"
SELECT
DISTINCT ON ("in#52"."_nullIf(decimalLatitude)#1.result::double", "in#52"."_nullIf(decimalLongitude)#1.result::double")
"in#52".*
FROM "in#52"
LEFT JOIN "coordinates" ON
"coordinates"."source_id" = 26
AND "coordinates"."latitude_deg" = "in#52"."_nullIf(decimalLatitude)#1.result::double"
AND "coordinates"."longitude_deg" = "in#52"."_nullIf(decimalLongitude)#1.result::double"
AND COALESCE("coordinates"."verbatimlatitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimlongitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimcoordinates", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."footprintgeometry_dwc", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
WHERE "coordinates"."coordinates_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO coordinates#4"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "coordinates"%ROWTYPE;
BEGIN
    FOR "row"."source_id", "row"."latitude_deg", "row"."longitude_deg" IN
        SELECT
        26 AS "source_id"
        , "in#52_distinct"."_nullIf(decimalLatitude)#1.result::double"
        , "in#52_distinct"."_nullIf(decimalLongitude)#1.result::double"
        FROM "in#52_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "coordinates"
                ("source_id", "latitude_deg", "longitude_deg")
                VALUES ("row"."source_id", "row"."latitude_deg", "row"."longitude_deg")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#15" AS
SELECT * FROM "pg_temp"."INSERT INTO coordinates#4"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "coordinates_pkeys#1" AS
SELECT
"in#52"."row_num"
, "coordinates"."coordinates_id" AS "out.coordinates_id"
FROM "in#52"
JOIN "coordinates" ON
"coordinates"."source_id" = 26
AND "coordinates"."latitude_deg" = "in#52"."_nullIf(decimalLatitude)#1.result::double"
AND "coordinates"."longitude_deg" = "in#52"."_nullIf(decimalLongitude)#1.result::double"
AND COALESCE("coordinates"."verbatimlatitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimlongitude", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."verbatimcoordinates", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("coordinates"."footprintgeometry_dwc", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "coordinates_pkeys#1"
("row_num", "out.coordinates_id")
SELECT
"in#52_full"."row_num"
, NULL AS "out.coordinates_id"
FROM "in#52_full"
LEFT JOIN "coordinates_pkeys#1" ON "coordinates_pkeys#1"."row_num" = "in#52_full"."row_num"
WHERE "coordinates_pkeys#1"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"place"@:

|Output|Input|
|@'source_id'@|@26@|
|@'coordinates_id'@|@"coordinates_pkeys#1"."out.coordinates_id"@|
|@'stateprovince'@|@"ACAD.Specimen"."stateProvince"@|
|@'county'@|@"ACAD.Specimen"."county"@|
|@'country'@|@"ACAD.Specimen"."country"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#53" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "coordinates_pkeys#1"."out.coordinates_id" AS "coordinates_pkeys#1.out.coordinates_id"
, "ACAD.Specimen"."stateProvince" AS "ACAD.Specimen.stateProvince"
, "ACAD.Specimen"."county" AS "ACAD.Specimen.county"
, "ACAD.Specimen"."country" AS "ACAD.Specimen.country"
FROM "ACAD.Specimen"
JOIN "coordinates_pkeys#1" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#53"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#53_full" (
LIKE "in#53" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#53_full"
SELECT * FROM "in#53"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO place"()
RETURNS SETOF "place"."place_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "place"
("source_id", "coordinates_id", "stateprovince", "county", "country")
SELECT
26 AS "source_id"
, "in#53"."coordinates_pkeys#1.out.coordinates_id"
, "in#53"."ACAD.Specimen.stateProvince"
, "in#53"."ACAD.Specimen.county"
, "in#53"."ACAD.Specimen.country"
FROM "in#53"
RETURNING "place_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#53_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO place"() AS "f" ("place_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @place_unique_within_creator_by_name@ constraint on columns: @source_id, continent, country, stateprovince, county, coordinates_id@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"place"."source_id": 26
"place"."continent": None
"place"."country": "in#53"."ACAD.Specimen.country"
"place"."stateprovince": "in#53"."ACAD.Specimen.stateProvince"
"place"."county": "in#53"."ACAD.Specimen.county"
"place"."coordinates_id": "in#53"."coordinates_pkeys#1.out.coordinates_id"
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#53_distinct" (
LIKE "in#53" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#53_distinct"
SELECT
DISTINCT ON ("in#53"."ACAD.Specimen.country", "in#53"."ACAD.Specimen.stateProvince", "in#53"."ACAD.Specimen.county", "in#53"."coordinates_pkeys#1.out.coordinates_id")
"in#53".*
FROM "in#53"
LEFT JOIN "place" ON
"place"."source_id" = 26
AND COALESCE("place"."continent", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("place"."country", CAST('\N' AS text)) = COALESCE("in#53"."ACAD.Specimen.country", CAST('\N' AS text))
AND COALESCE("place"."stateprovince", CAST('\N' AS text)) = COALESCE("in#53"."ACAD.Specimen.stateProvince", CAST('\N' AS text))
AND COALESCE("place"."county", CAST('\N' AS text)) = COALESCE("in#53"."ACAD.Specimen.county", CAST('\N' AS text))
AND COALESCE("place"."coordinates_id", CAST(2147483647 AS integer)) = COALESCE("in#53"."coordinates_pkeys#1.out.coordinates_id", CAST(2147483647 AS integer))
WHERE "place"."place_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO place#1"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "place"%ROWTYPE;
BEGIN
    FOR "row"."source_id", "row"."coordinates_id", "row"."stateprovince", "row"."county", "row"."country" IN
        SELECT
        26 AS "source_id"
        , "in#53_distinct"."coordinates_pkeys#1.out.coordinates_id"
        , "in#53_distinct"."ACAD.Specimen.stateProvince"
        , "in#53_distinct"."ACAD.Specimen.county"
        , "in#53_distinct"."ACAD.Specimen.country"
        FROM "in#53_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "place"
                ("source_id", "coordinates_id", "stateprovince", "county", "country")
                VALUES ("row"."source_id", "row"."coordinates_id", "row"."stateprovince", "row"."county", "row"."country")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#16" AS
SELECT * FROM "pg_temp"."INSERT INTO place#1"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "place_pkeys" AS
SELECT
"in#53"."row_num"
, "place"."place_id" AS "out.place_id"
FROM "in#53"
JOIN "place" ON
"place"."source_id" = 26
AND COALESCE("place"."continent", CAST('\N' AS text)) = COALESCE(NULL, CAST('\N' AS text))
AND COALESCE("place"."country", CAST('\N' AS text)) = COALESCE("in#53"."ACAD.Specimen.country", CAST('\N' AS text))
AND COALESCE("place"."stateprovince", CAST('\N' AS text)) = COALESCE("in#53"."ACAD.Specimen.stateProvince", CAST('\N' AS text))
AND COALESCE("place"."county", CAST('\N' AS text)) = COALESCE("in#53"."ACAD.Specimen.county", CAST('\N' AS text))
AND COALESCE("place"."coordinates_id", CAST(2147483647 AS integer)) = COALESCE("in#53"."coordinates_pkeys#1.out.coordinates_id", CAST(2147483647 AS integer))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "place_pkeys"
("row_num", "out.place_id")
SELECT
"in#53_full"."row_num"
, NULL AS "out.place_id"
FROM "in#53_full"
LEFT JOIN "place_pkeys" ON "place_pkeys"."row_num" = "in#53_full"."row_num"
WHERE "place_pkeys"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"place"@:

|Output|Input|
|@'coordinates_id'@|@"coordinates_pkeys"."out.coordinates_id"@|
|@'matched_place_id'@|@"place_pkeys"."out.place_id"@|
|@'country'@|@"ACAD.Specimen"."country"@|
|@'stateprovince'@|@"ACAD.Specimen"."stateProvince"@|
|@'county'@|@"ACAD.Specimen"."county"@|
|@'continent'@|@"ACAD.Specimen"."continent"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#54" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "coordinates_pkeys"."out.coordinates_id" AS "coordinates_pkeys.out.coordinates_id"
, "place_pkeys"."out.place_id" AS "place_pkeys.out.place_id"
, "ACAD.Specimen"."country" AS "ACAD.Specimen.country"
, "ACAD.Specimen"."stateProvince" AS "ACAD.Specimen.stateProvince"
, "ACAD.Specimen"."county" AS "ACAD.Specimen.county"
, "ACAD.Specimen"."continent" AS "ACAD.Specimen.continent"
FROM "ACAD.Specimen"
JOIN "coordinates_pkeys" USING ("row_num")
JOIN "place_pkeys" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#54"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#54_full" (
LIKE "in#54" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#54_full"
SELECT * FROM "in#54"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO place#2"()
RETURNS SETOF "place"."place_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "place"
("coordinates_id", "matched_place_id", "country", "stateprovince", "county", "continent")
SELECT
"in#54"."coordinates_pkeys.out.coordinates_id"
, "in#54"."place_pkeys.out.place_id"
, "in#54"."ACAD.Specimen.country"
, "in#54"."ACAD.Specimen.stateProvince"
, "in#54"."ACAD.Specimen.county"
, "in#54"."ACAD.Specimen.continent"
FROM "in#54"
RETURNING "place_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#54_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO place#2"() AS "f" ("place_id")
</code></pre>
# Caught exception: NullValueException: Violated @NOT NULL@ constraint on columns: @source_id@
# ********** New iteration **********
# Inserting these input columns into @"source"@:

|Output|Input|
|@'shortname'@|@'ACAD'@|
 
# Trying to insert new rows
<pre><code class="SQL">
INSERT INTO "source"
("shortname")
SELECT 'ACAD' AS "shortname"
RETURNING "source_id"
</code></pre>
# Caught exception: DuplicateKeyException: Violated @source_unique@ constraint on columns: @shortname@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"source"."shortname": 'ACAD'
</pre><pre><code class="SQL">
SELECT "source"."source_id" FROM "source"
WHERE "source"."shortname" = 'ACAD'
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO place#3"()
RETURNS SETOF "place"."place_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "place"
("coordinates_id", "matched_place_id", "country", "stateprovince", "county", "continent", "source_id")
SELECT
"in#54"."coordinates_pkeys.out.coordinates_id"
, "in#54"."place_pkeys.out.place_id"
, "in#54"."ACAD.Specimen.country"
, "in#54"."ACAD.Specimen.stateProvince"
, "in#54"."ACAD.Specimen.county"
, "in#54"."ACAD.Specimen.continent"
, 1 AS "source_id"
FROM "in#54"
RETURNING "place_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#54_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO place#3"() AS "f" ("place_id")
</code></pre>
# Caught exception: DuplicateKeyException: Violated @place_unique_within_creator_by_name@ constraint on columns: @source_id, continent, country, stateprovince, county, coordinates_id@
# Ignoring existing rows, comparing on these columns:
<pre>
Output: Input
"place"."source_id": 1
"place"."continent": "in#54"."ACAD.Specimen.continent"
"place"."country": "in#54"."ACAD.Specimen.country"
"place"."stateprovince": "in#54"."ACAD.Specimen.stateProvince"
"place"."county": "in#54"."ACAD.Specimen.county"
"place"."coordinates_id": "in#54"."coordinates_pkeys.out.coordinates_id"
</pre><pre><code class="SQL">
CREATE TEMP TABLE "in#54_distinct" (
LIKE "in#54" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#54_distinct"
SELECT
DISTINCT ON ("in#54"."ACAD.Specimen.continent", "in#54"."ACAD.Specimen.country", "in#54"."ACAD.Specimen.stateProvince", "in#54"."ACAD.Specimen.county", "in#54"."coordinates_pkeys.out.coordinates_id")
"in#54".*
FROM "in#54"
LEFT JOIN "place" ON
"place"."source_id" = 1
AND COALESCE("place"."continent", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.continent", CAST('\N' AS text))
AND COALESCE("place"."country", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.country", CAST('\N' AS text))
AND COALESCE("place"."stateprovince", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.stateProvince", CAST('\N' AS text))
AND COALESCE("place"."county", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.county", CAST('\N' AS text))
AND COALESCE("place"."coordinates_id", CAST(2147483647 AS integer)) = COALESCE("in#54"."coordinates_pkeys.out.coordinates_id", CAST(2147483647 AS integer))
WHERE "place"."place_id" IS NULL
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO place#4"()
RETURNS SETOF unknown
LANGUAGE plpgsql
AS $$
DECLARE
    "row" "place"%ROWTYPE;
BEGIN
    FOR "row"."coordinates_id", "row"."matched_place_id", "row"."country", "row"."stateprovince", "row"."county", "row"."continent", "row"."source_id" IN
        SELECT
        "in#54_distinct"."coordinates_pkeys.out.coordinates_id"
        , "in#54_distinct"."place_pkeys.out.place_id"
        , "in#54_distinct"."ACAD.Specimen.country"
        , "in#54_distinct"."ACAD.Specimen.stateProvince"
        , "in#54_distinct"."ACAD.Specimen.county"
        , "in#54_distinct"."ACAD.Specimen.continent"
        , 1 AS "source_id"
        FROM "in#54_distinct"
        ORDER BY row_num
    LOOP
        BEGIN
            RETURN QUERY
                INSERT INTO "place"
                ("coordinates_id", "matched_place_id", "country", "stateprovince", "county", "continent", "source_id")
                VALUES ("row"."coordinates_id", "row"."matched_place_id", "row"."country", "row"."stateprovince", "row"."county", "row"."continent", "row"."source_id")
                RETURNING NULL AS "NULL"
            ;
        EXCEPTION
            WHEN unique_violation THEN NULL;
        END;
    END LOOP;
END;

$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "rowcount#17" AS
SELECT * FROM "pg_temp"."INSERT INTO place#4"() AS "f" ("NULL")
</code></pre>
# Getting output table pkeys of existing/inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "place_pkeys#1" AS
SELECT
"in#54"."row_num"
, "place"."place_id" AS "out.place_id"
FROM "in#54"
JOIN "place" ON
"place"."source_id" = 1
AND COALESCE("place"."continent", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.continent", CAST('\N' AS text))
AND COALESCE("place"."country", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.country", CAST('\N' AS text))
AND COALESCE("place"."stateprovince", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.stateProvince", CAST('\N' AS text))
AND COALESCE("place"."county", CAST('\N' AS text)) = COALESCE("in#54"."ACAD.Specimen.county", CAST('\N' AS text))
AND COALESCE("place"."coordinates_id", CAST(2147483647 AS integer)) = COALESCE("in#54"."coordinates_pkeys.out.coordinates_id", CAST(2147483647 AS integer))
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "place_pkeys#1"
("row_num", "out.place_id")
SELECT
"in#54_full"."row_num"
, NULL AS "out.place_id"
FROM "in#54_full"
LEFT JOIN "place_pkeys#1" ON "place_pkeys#1"."row_num" = "in#54_full"."row_num"
WHERE "place_pkeys#1"."row_num" IS NULL
</code></pre>
# ********** New iteration **********
# Inserting these input columns into @"locationplace"@:

|Output|Input|
|@'place_id'@|@"place_pkeys#1"."out.place_id"@|
|@'location_id'@|@"location_pkeys"."out.location_id"@|
 
# Joining together input tables into temp table
<pre><code class="SQL">
CREATE TEMP TABLE "in#55" AS
SELECT
"ACAD.Specimen"."row_num" AS "row_num"
, "place_pkeys#1"."out.place_id" AS "place_pkeys#1.out.place_id"
, "location_pkeys"."out.location_id" AS "location_pkeys.out.location_id"
FROM "ACAD.Specimen"
JOIN "place_pkeys#1" USING ("row_num")
JOIN "location_pkeys" USING ("row_num")
ORDER BY row_num
</code></pre>Temp table: @"in#55"@
<pre><code class="SQL">
CREATE TEMP TABLE "in#55_full" (
LIKE "in#55" INCLUDING ALL
);
</code></pre><pre><code class="SQL">
INSERT INTO "in#55_full"
SELECT * FROM "in#55"
</code></pre>
# Trying to insert new rows
<pre><code class="SQL">
CREATE FUNCTION "pg_temp"."INSERT INTO locationplace"()
RETURNS SETOF "locationplace"."locationplace_id"%TYPE
LANGUAGE sql
AS $$
INSERT INTO "locationplace"
("place_id", "location_id")
SELECT
"in#55"."place_pkeys#1.out.place_id"
, "in#55"."location_pkeys.out.location_id"
FROM "in#55"
RETURNING "locationplace_id"
$$;
</code></pre><pre><code class="SQL">
CREATE TEMP TABLE "in#55_insert_out_pkeys" AS
SELECT * FROM "pg_temp"."INSERT INTO locationplace"() AS "f" ("locationplace_id")
</code></pre>
# Getting input table pkeys of inserted rows
<pre><code class="SQL">
CREATE TEMP TABLE "in#55_insert_in_pkeys" AS
SELECT "row_num" FROM "in#55"
</code></pre>
# Combining output and input pkeys in inserted order
<pre><code class="SQL">
CREATE TEMP TABLE "locationplace_pkeys" AS
SELECT
"in#55_insert_in_pkeys"."row_num"
, "in#55_insert_out_pkeys"."locationplace_id" AS "out.locationplace_id"
FROM "in#55_insert_in_pkeys"
JOIN "in#55_insert_out_pkeys" USING ("_row_num")
</code></pre>
# Setting pkeys of missing rows to @None@
<pre><code class="SQL">
INSERT INTO "locationplace_pkeys"
("row_num", "out.locationplace_id")
SELECT
"in#55_full"."row_num"
, NULL AS "out.locationplace_id"
FROM "in#55_full"
LEFT JOIN "locationplace_pkeys" ON "locationplace_pkeys"."row_num" = "in#55_full"."row_num"
WHERE "locationplace_pkeys"."row_num" IS NULL
</code></pre>Inserted 1325 new rows into database
Processed 100 input rows
Encountered 0 error(s)
       15.65 real         4.66 user         0.49 sys
