Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g33gw-0001E5-4P for pgsql-general@arkaria.postgresql.org; Thu, 20 Sep 2018 18:28:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g33gs-0007rH-80 for pgsql-general@arkaria.postgresql.org; Thu, 20 Sep 2018 18:28:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g33gs-0007r8-0J for pgsql-general@lists.postgresql.org; Thu, 20 Sep 2018 18:28:38 +0000 Received: from 24-182-127-154.static.stls.mo.charter.com ([24.182.127.154] helo=mail.brookhurstdata.net) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g33gj-0002pM-EH for pgsql-general@lists.postgresql.org; Thu, 20 Sep 2018 18:28:36 +0000 Received: from mail.brookhurstdata.net (mail.brookhurstdata.net [10.1.5.12]) by mail.brookhurstdata.net (Postfix) with ESMTP id 73D2F88901D for ; Thu, 20 Sep 2018 13:28:26 -0500 (CDT) MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_3fce400ba8ae44227821ae3cc31a6cc2" Date: Thu, 20 Sep 2018 13:28:26 -0500 From: Susan Hurst To: pgsql-general@lists.postgresql.org Subject: Re: array must have even number of elements Organization: Brookhurst Data LLC Reply-To: susan.hurst@brookhurstdata.com Mail-Reply-To: susan.hurst@brookhurstdata.com In-Reply-To: References: Message-ID: <2e971d70926958427b495fca817cc30f@mail.brookhurstdata.net> X-Sender: susan.hurst@brookhurstdata.com User-Agent: Roundcube Webmail/RCMAIL_VERSION List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_3fce400ba8ae44227821ae3cc31a6cc2 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Thanks, everyone! I get it now. It's not just an array but an hstore array. I changed my code to include the original values so now it works: -- new will be substituted for $1 during execution with using clause l_query_string := 'select ($1 #= hstore(array[' || l_orig_list || '],' || 'array[' || l_clean_list || '])).*'; Pavel...I am creating a trigger function to look for columns with char, varchar or text data types to purge any incoming or updated data of extraneous spaces and tabs both within the string and on either end. We can use the same function from any table that calls it from a trigger. Now that it works, we can refactor it to make it better. I would welcome your suggestions for alternatives to hstore. Thanks for your help! Sue --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Susan E Hurst Principal Consultant Brookhurst Data LLC Email: susan.hurst@brookhurstdata.com Mobile: 314-486-3261 On 2018-09-20 13:04, Pavel Stehule wrote: > Hi > > čt 20. 9. 2018 v 19:55 odesílatel Susan Hurst napsal: > >> Why must an array have an even number of elements? I need to use a >> trigger function on any table, some of which may have an odd number of >> columns that I want to cleanse before inserting/updating. > > The hstore function get parameters as sequence of pairs (key, value) - so the number should be even. Odd parameter signalize broken format. > > Your example is pretty crazy - I cannot to decode it. Maybe you should to use different function, I don't see a sense for using hstore type there. But I cannot to decode it. > > Regards > > Pavel > >> Is there a workaround for this? >> >> ERROR: array must have even number of elements >> >> SQL state: 2202E >> >> Context: SQL statement "SELECT ($1 #= >> hstore(array[trim(replace(regexp_replace($1.c,'( ){2,}',' ','g'),' ',' >> ')),trim(replace(regexp_replace($1.vc [1],'( ){2,}',' ','g'),' ',' >> ')),trim(replace(regexp_replace($1.t,'( ){2,}',' ','g'),' ',' '))])).*" >> PL/pgSQL function store.trim_string_before_dml() line 44 at EXECUTE >> >> -- my test table >> create table dm.trg_test (c character(8), vc varchar(16), t text); >> insert into dm.trg_test (c,vc,t) values ('ctest',' vctest ',' >> ttest '); >> >> -- code snippet that produced the error. >> -- new will be substituted for $1 during execution with using clause >> l_query_string := 'select ($1 #= hstore(array[' || l_column_list || >> '])).*'; >> execute format(l_query_string) using new into new; >> return new; >> >> Thanks for your help! >> >> -- >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> Susan E Hurst >> Principal Consultant >> Brookhurst Data LLC >> Email: susan.hurst@brookhurstdata.com >> Mobile: 314-486-3261 Links: ------ [1] http://1.vc --=_3fce400ba8ae44227821ae3cc31a6cc2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8

Thanks, everyone!

I get it now.  It's not just an array but an hstore array.  I = changed my code to include the original values so now it works:

-- new will be substituted for $1 during execution with using clause

l_query_string :=3D 'select ($1 #=3D hstore(array[' || l_orig_list || ']= ,'

            =             &nb= sp;          || 'array[' || l_= clean_list || '])).*';

 

Pavel...I am creating a trigger function to look for columns with char, = varchar or text data types to purge any incoming or updated data of extrane= ous spaces and tabs both within the string and on either end.  We can = use the same function from any table that calls it from a trigger.  No= w that it works, we can refactor it to make it better.  I would welcom= e your suggestions for alternatives to hstore.

Thanks for your help!

Sue

 

---
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Susan E Hurst Principal Consultant Brookhurst Data LLC Email: susan.hurst@brookhurstdata.com Mobile: 314-486-3261

On 2018-09-20 13:04, Pavel Stehule wrote:

Hi

=C4=8Dt 20. 9. 2018 v 19:55 odesílatel Susan H= urst <susan.hurst@broo= khurstdata.com> napsal:

Why must an array have an = even number of elements?  I need to use a
trigger function on a= ny table, some of which may have an odd number of
columns that I wan= t to cleanse before inserting/updating.
 
The hstore function get parameters as sequence of pairs (key, value) -= so the number should be even. Odd parameter signalize broken format.
 
Your example is pretty crazy - I cannot to decod= e it. Maybe you should to use different function, I don't see a sense for u= sing hstore type there. But I cannot to decode it.
 
Regards
 
Pavel
 
 
 


Is there a workaroun= d for this?


ERROR: array must have even number of element= s

SQL state: 2202E

Context: SQL statement "SELECT (= $1 #=3D
hstore(array[trim(replace(regexp_replace($1.c,'( ){2,}',' ',= 'g'),' ','
')),trim(replace(regexp_replace($= 1.vc,'( ){2,}',' ','g'),' ','
')),trim(replace(regexp_replace($1= =2Et,'( ){2,}',' ','g'),' ',' '))])).*"
PL/pgSQL function store.trim= _string_before_dml() line 44 at EXECUTE



--  my= test table
create table dm.trg_test (c character(8), vc varchar(16),= t text);
insert into dm.trg_test (c,vc,t) values ('ctest','  &n= bsp;vctest  ','       
ttest    &n= bsp;  ');


-- code snippet that produced the error. -- new will be substituted for $1 during execution with using clause l_query_string :=3D 'select ($1 #=3D hstore(array[' || l_column_list |= |
'])).*';
execute format(l_query_string) using  new into=    new;
return new;


Thanks for your help!=

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
S= usan E Hurst
Principal Consultant
Brookhurst Data LLC
Ema= il: susan.hurst@brookhurs= tdata.com
Mobile: 314-486-3261

--=_3fce400ba8ae44227821ae3cc31a6cc2--