Trigger/copy issue

Поиск
Список
Период
Сортировка
Искать

Trigger/copy issue

От:
Sean Davis <sdavis2@mail.nih.gov>
Дата:
I have the following setup and data file that I want to load using copy  
(8.0.0, macos).  The trigger function is to "clean" the input data (see  
earlier posts on the subject).  However, it seems as if the trigger  
function is not doing what I would have thought.  I want it to replace  
'-' in the data in certain columns with NULLs.  Here is the SQL and a  
few lines of sample data (sorry, may be broken up due to issues in the  
email line splits).

Thanks for any insights.

Sean


CREATE TABLE g_refseq (
				      g_refseq_id serial primary key,
				      species integer not null,
				      gene_id integer not null,
				      status VARCHAR,
				      nuc_acc varchar,
				      nuc_gi  integer,
				      prot_acc varchar,
				      prot_gi  integer,
				      chrom_acc varchar,
				      chrom_gi  integer,
				      chrom_start integer,
				      chrom_end   integer,
				      strand      char				      );

create or replace function tgf_g_refseq_clean() returns trigger as $$
begin
NEW.prot_acc:=substring(NEW.prot_acc from '[A-Z][A-Z]_[0-9]*');
NEW.nuc_acc:=substring(NEW.nuc_acc from '[A-Z][A-Z]_[0-9]*');
NEW.chrom_acc:=substring(NEW.chrom_acc from '[A-Z][A-Z]_[0-9]*');
if (NEW.prot_gi='-') THEN
   NEW.prot_gi:= 'NULL';
END IF;
if (NEW.nuc_gi='-') THEN
		begin
   raise notice 'we are here';
   NEW.nuc_gi:= 'NULL';
   end;
END IF;
if (NEW.chrom_gi='-') THEN
   NEW.chrom_gi:= 'NULL';
END IF;
if (NEW.chrom_start='-') THEN
   NEW.chrom_start = 'NULL';
END IF;
if (NEW.chrom_end='-') THEN
   NEW.chrom_end = 'NULL';
END IF;
return NEW;
end;
$$ language plpgsql;

create trigger tg_g_refseq_clean before insert or update on g_refseq  
for each row execute procedure tgf_g_refseq_clean();

DATA BELOW

9	1246500	Provisional	-	-	NP_047184.1	10954455	NC_001911.1	10954454	348 
	1190	-
9	1246501	Provisional	-	-	NP_047186.1	10954457	NC_001911.1	10954454	 
2157	2912	+
9	1246502	Provisional	-	-	NP_047187.1	10954458	NC_001911.1	10954454	 
3040	4590	+
9	1246503	Provisional	-	-	NP_047188.1	10954459	NC_001911.1	10954454	 
4623	5714	+
9	1246504	Provisional	-	-	NP_047189.1	10954460	NC_001911.1	10954454	 
5717	7117	+
9	1246505	Provisional	-	-	NP_047190.1	10954461	NC_001911.1	10954454	 
7141	7767	+
9	1246509	Provisional	-	-	NP_858065.1	31982990	NC_004843.1	31982989	8	 
457	+
9	1246510	Provisional	-	-	NP_858066.1	31982991	NC_004843.1	31982989	607 
	1449	+
139	1343044	NA	-	-	-	-	NC_004971.1	32455274	160	417	+
139	1343045	Provisional	-	-	NP_862625.1	32455275	NC_004971.1	32455274	 
414	650	+

Re: Trigger/copy issue

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Sean Davis  writes:
> I have the following setup and data file that I want to load using copy  
> (8.0.0, macos).  The trigger function is to "clean" the input data (see  
> earlier posts on the subject).  However, it seems as if the trigger  
> function is not doing what I would have thought.  I want it to replace  
> '-' in the data in certain columns with NULLs.

> CREATE TABLE g_refseq (
> ...
> 				      chrom_gi  integer,
> ...

> if (NEW.chrom_gi='-') THEN
>    NEW.chrom_gi:= 'NULL';
> END IF;

This isn't gonna work, because '-' is not a legal value of an integer
column and so the data conversion would have failed long before your
trigger gets to execute.

(You are also wrong in using quotes around the keyword NULL, but that's
a secondary problem.)

If you have to import data that's defined like this, I'd suggest loading
into a temporary table that's declared as all unconstrained text
columns, and then converting with something like

	INSERT INTO realtable
	    SELECT
		...
		(case when chrom_gi = '-' then null else chrom_gi::integer),
		...
	    FROM temptable;

			regards, tom lane
FAQ