Is the hstore(text[], text[]) function deterministic?

49 Views Asked by At

Let's say I have two arrays that include the elements [1,1,2] and [3,2,4]. If I perform the following query:

select hstore(array[1,1,2]::text[], array[3,2,4]::text[]);

I get the result:

1 => 3, 2 => 4

This seems to indicate that hstore(text[],text[]) takes the first value it finds for a particular key in the order they appear in the array, and then ignores subsequent elements matching that key. However, I can't find a description of this behaviour anywhere in the documentation, so I don't know whether this is true, and the behaviour is determinate. It could be that a value is assigned to that key semi-arbitrarily, and the ordering of the arrays doesn't necessarily determine which value is selected, which is what happens if you declare an hstore with two similar keys like SELECT 'a=>1,a=>2'::hstore;. The documentation warns that there is no guarantee what the value of a would be in that case. But it's not clear whether this works the same way for hstore(text[],text[]);

Is there some documentation that establishes whether this works the same way every time? Is there some test I can do that will demonstrate that it does or not?

1

There are 1 best solutions below

0
Adrian Klaver On
select hstore(array[1,1,2]::text[], array[3,2,4]::text[]);
       hstore       
--------------------
 "1"=>"3", "2"=>"4"

select hstore(array[1,1,2]::text[], array[3,4,2]::text[]);
       hstore       
--------------------
 "1"=>"3", "2"=>"2"

select hstore(array[1,1,2,3]::text[], array[3,4,2,5]::text[]);
            hstore            
------------------------------
 "1"=>"3", "2"=>"2", "3"=>"5"

Looks deterministic to me. hstore can't have duplicate keys so it iterates over the left array and drops any key/value pairs that duplicate the key as it matches the right array left to right.

You can look at the source hstore_from_arrays. I am not a C programmer but it looks to me like the code maintains the pair matching as I described.