1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-12 02:37:31 +03:00

Support assignment to subfields of composite columns in UPDATE and INSERT.

As a side effect, cause subscripts in INSERT targetlists to do something
more or less sensible; previously we evaluated such subscripts and then
effectively ignored them.  Another side effect is that UPDATE-ing an
element or slice of an array value that is NULL now produces a non-null
result, namely an array containing just the assigned-to positions.
This commit is contained in:
Tom Lane
2004-06-09 19:08:20 +00:00
parent 3a0df651da
commit 7e64dbc6b5
27 changed files with 1468 additions and 574 deletions

View File

@@ -11,26 +11,25 @@ CREATE TABLE arrtest (
g varchar(5)[]
);
--
-- only this array as a 0-based 'e', the others are 1-based.
-- 'e' is also a large object.
-- only the 'e' array is 0-based, the others are 1-based.
--
INSERT INTO arrtest (a[5], b[2][1][2], c, d, f, g)
INSERT INTO arrtest (a[1:5], b[1:1][1:2][1:2], c, d, f, g)
VALUES ('{1,2,3,4,5}', '{{{0,0},{1,2}}}', '{}', '{}', '{}', '{}');
UPDATE arrtest SET e[0] = '1.1';
UPDATE arrtest SET e[1] = '2.2';
INSERT INTO arrtest (f)
VALUES ('{"too long"}');
ERROR: value too long for type character(5)
INSERT INTO arrtest (a, b[2][2][1], c, d, e, f, g)
INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
VALUES ('{11,12,23}', '{{3,4},{4,5}}', '{"foobar"}',
'{{"elt1", "elt2"}}', '{"3.4", "6.7"}',
'{"abc","abcde"}', '{"abc","abcde"}');
INSERT INTO arrtest (a, b[1][2][2], c, d[2][1])
INSERT INTO arrtest (a, b[1:2], c, d[1:2])
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
SELECT * FROM arrtest;
a | b | c | d | e | f | g
-------------+-----------------+-----------+---------------+-----------+-----------------+-------------
{1,2,3,4,5} | {{{0,0},{1,2}}} | {} | {} | | {} | {}
{1,2,3,4,5} | {{{0,0},{1,2}}} | {} | {} | {1.1,2.2} | {} | {}
{11,12,23} | {{3,4},{4,5}} | {foobar} | {{elt1,elt2}} | {3.4,6.7} | {"abc ",abcde} | {abc,abcde}
{} | {3,4} | {foo,bar} | {bar,foo} | | |
(3 rows)
@@ -41,20 +40,20 @@ SELECT arrtest.a[1],
arrtest.d[1][1],
arrtest.e[0]
FROM arrtest;
a | b | c | d | e
----+---+--------+------+---
1 | 0 | | |
11 | | foobar | elt1 |
| | foo | |
a | b | c | d | e
----+---+--------+------+-----
1 | 0 | | | 1.1
11 | | foobar | elt1 |
| | foo | |
(3 rows)
SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]
FROM arrtest;
a | b | c | d | e
----+---+--------+------+---
1 | 0 | | |
11 | | foobar | elt1 |
| | foo | |
a | b | c | d | e
----+---+--------+------+-----
1 | 0 | | | 1.1
11 | | foobar | elt1 |
| | foo | |
(3 rows)
SELECT a[1:3],

View File

@@ -87,19 +87,23 @@ select * from people;
(Joe,Blow,) | 01-10-1984
(1 row)
-- This fails at the moment, would like it to work though:
-- test insertion/updating of subfields
update people set fn.suffix = 'Jr';
ERROR: syntax error at or near "." at character 21
LINE 1: update people set fn.suffix = 'Jr';
^
-- ugly workaround:
update people set fn = ((fn).first, (fn).last, 'III');
select * from people;
fn | bd
----------------+------------
(Joe,Blow,III) | 01-10-1984
fn | bd
---------------+------------
(Joe,Blow,Jr) | 01-10-1984
(1 row)
insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
select * from quadtable;
f1 | q
----+---------------------------
1 | ("(3.3,4.4)","(5.5,6.6)")
2 | ("(,4.4)","(5.5,6.6)")
44 | ("(55,)","(,66)")
(3 rows)
-- The object here is to ensure that toasted references inside
-- composite values don't cause problems. The large f1 value will
-- be toasted inside pp, it must still work after being copied to people.

View File

@@ -13,11 +13,10 @@ CREATE TABLE arrtest (
);
--
-- only this array as a 0-based 'e', the others are 1-based.
-- 'e' is also a large object.
-- only the 'e' array is 0-based, the others are 1-based.
--
INSERT INTO arrtest (a[5], b[2][1][2], c, d, f, g)
INSERT INTO arrtest (a[1:5], b[1:1][1:2][1:2], c, d, f, g)
VALUES ('{1,2,3,4,5}', '{{{0,0},{1,2}}}', '{}', '{}', '{}', '{}');
UPDATE arrtest SET e[0] = '1.1';
@@ -27,12 +26,12 @@ UPDATE arrtest SET e[1] = '2.2';
INSERT INTO arrtest (f)
VALUES ('{"too long"}');
INSERT INTO arrtest (a, b[2][2][1], c, d, e, f, g)
INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
VALUES ('{11,12,23}', '{{3,4},{4,5}}', '{"foobar"}',
'{{"elt1", "elt2"}}', '{"3.4", "6.7"}',
'{"abc","abcde"}', '{"abc","abcde"}');
INSERT INTO arrtest (a, b[1][2][2], c, d[2][1])
INSERT INTO arrtest (a, b[1:2], c, d[1:2])
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');

View File

@@ -53,14 +53,15 @@ alter table fullname add column suffix text default null;
select * from people;
-- This fails at the moment, would like it to work though:
-- test insertion/updating of subfields
update people set fn.suffix = 'Jr';
-- ugly workaround:
update people set fn = ((fn).first, (fn).last, 'III');
select * from people;
insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
select * from quadtable;
-- The object here is to ensure that toasted references inside
-- composite values don't cause problems. The large f1 value will
-- be toasted inside pp, it must still work after being copied to people.