[PATCH v1] Fix parsing of a complex type that has an array of complex types

From: Arjan Marku <arjanmarku02(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH v1] Fix parsing of a complex type that has an array of complex types
Date: 2024-07-13 17:03:37
Message-ID: cdbd818f-328c-4c1a-8196-d0e96a8ebb81@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I ran into an issue today when I was trying to insert a complex types
where one of its attributes is also an array of complex types,

As an example:

CREATE TYPE inventory_item AS
(
    name        text,
    supplier_id integer,
    price       numeric
);
CREATE TYPE item_2d AS (id int, items inventory_item[][]);

CREATE TABLE item_2d_table (id int, item item_2d);

INSERT INTO item_2d_table VALUES(1, '(1,{{("inv a",42,1.99),("inv
b",42,1.99)},{("inv c",42,1.99),("inv d",42,2)}})');

The INSERT statement will fail due to how complex types are parsed, I
have included a patch in this email to support this scenario.

The reason why this fails is because record_in lacks support of
detecting an array string when one of the attributes is of type array.
Due to this, it will stop processing the column value prematurely, which
results in a corrupted value for that particular column.
As a result array_in will receive a malformed string which is bound to
error.

To fix this, record_in can detect columns that are of type array and in
such cases leave the input intact.
array_in will attempt to extract the elements one by one. In case it is
dealing with unquoted elements, the logic needs to slightly change,
since if the element is a record, quotes can be allowed, ex: {{("test
field")}

There are some adjustments that can be made to the patch, for example:
We can detect the number of the dimensions of the array in record_in, do
we want to error out in case the string has more dimensions than MAXDIM
in array.h, (to prevent number over/underflow-ing) or whether we want to
error out if number of dimensions is not the same with the number of
dimensions that the attribute is supposed to have, or both?

Regards,
Arjan Marku

Attachment Content-Type Size
v1-0001-PATCH-v1-Fix-parsing-of-a-complex-type-that-has-an-array-of-complex-types.patch text/x-patch 5.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2024-07-13 17:16:14 Re: Converting tab-complete.c's else-if chain to a switch
Previous Message Nathan Bossart 2024-07-13 15:40:43 Re: Remove dependence on integer wrapping