Skip to content

Commit

Permalink
revert before concatenating new entries to preserve the order of orig…
Browse files Browse the repository at this point in the history
…inal target entries
  • Loading branch information
onurctirtir committed Feb 8, 2022
1 parent 12aa54d commit 9914df2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/backend/distributed/deparser/citus_ruleutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,12 @@ ExpandMergedSubscriptingRefEntries(List *targetEntryList)
}
else
{
newTargetEntryList = list_concat(newTargetEntryList, expandedTargetEntries);
/*
* Need to concat expanded target list entries in reverse order
* to preserve ordering of the original target entry list.
*/
newTargetEntryList = list_concat(newTargetEntryList,
list_reverse(expandedTargetEntries));
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/backend/distributed/utils/listutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,20 @@ GenerateListFromElement(void *listElement, int listLength)

return list;
}


/*
* list_reverse returns a new list by reverting order of the elements within
* given list.
*/
List *
list_reverse(const List *list)
{
List *newList = NIL;
for (int i = list_length(list) - 1; i >= 0; i--)
{
newList = lappend(newList, list_nth(list, i));
}

return newList;
}
1 change: 1 addition & 0 deletions src/include/distributed/listutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ extern List * ListTake(List *pointerList, int size);
extern void * safe_list_nth(const List *list, int index);
extern List * GeneratePositiveIntSequenceList(int upTo);
extern List * GenerateListFromElement(void *listElement, int listLength);
extern List * list_reverse(const List *list);

#endif /* CITUS_LISTUTILS_H */
34 changes: 34 additions & 0 deletions src/test/regress/expected/pg14.out
Original file line number Diff line number Diff line change
Expand Up @@ -1313,5 +1313,39 @@ SELECT * FROM jsonb_subscript_update ORDER BY 1,2;
4 | {"a": 4, "b": 10}
(3 rows)

CREATE TABLE nested_obj_update(id INT, data JSONB, text_col TEXT);
SELECT create_distributed_table('nested_obj_update', 'id');
create_distributed_table
---------------------------------------------------------------------

(1 row)

INSERT INTO nested_obj_update VALUES
(1, '{"a": [1,2,3], "b": [4,5,6], "c": [7,8,9], "d": [1,2,1,2]}', '4'),
(2, '{"a": [10,20,30], "b": [41,51,61], "c": [72,82,92], "d": [11,21,11,21]}', '6');
UPDATE nested_obj_update
SET data['a'][0] = (updated_vals.b * 1)::TEXT::JSONB,
data['b'][2] = (updated_vals.c * 2)::TEXT::JSONB,
data['c'][0] = (updated_vals.d * 3)::TEXT::JSONB,
text_col = (nested_obj_update.id*1000)::TEXT,
data['a'][0] = (text_col::INT * data['a'][0]::INT)::TEXT::JSONB,
data['d'][6] = (nested_obj_update.id*1)::TEXT::JSONB,
data['d'][4] = (nested_obj_update.id*2)::TEXT::JSONB
FROM (
SELECT id,
data['a'][0] AS a,
data['b'][0]::NUMERIC + 1 AS b,
data['c'][0]::NUMERIC + 2 AS c,
data['c'][1]::NUMERIC + 3 AS d
FROM nested_obj_update
) updated_vals
WHERE nested_obj_update.id = updated_vals.id;
SELECT * FROM nested_obj_update ORDER BY 1,2,3;
id | data | text_col
---------------------------------------------------------------------
1 | {"a": [4, 2, 3], "b": [4, 5, 18], "c": [33, 8, 9], "d": [1, 2, 1, 2, 2, null, 1]} | 1000
2 | {"a": [60, 20, 30], "b": [41, 51, 148], "c": [255, 82, 92], "d": [11, 21, 11, 21, 4, null, 2]} | 2000
(2 rows)

set client_min_messages to error;
drop schema pg14 cascade;
26 changes: 26 additions & 0 deletions src/test/regress/sql/pg14.sql
Original file line number Diff line number Diff line change
Expand Up @@ -673,5 +673,31 @@ DO UPDATE SET data['d']=(jsonb_subscript_update.data['a']::INT*100)::TEXT::JSONB

SELECT * FROM jsonb_subscript_update ORDER BY 1,2;

CREATE TABLE nested_obj_update(id INT, data JSONB, text_col TEXT);
SELECT create_distributed_table('nested_obj_update', 'id');
INSERT INTO nested_obj_update VALUES
(1, '{"a": [1,2,3], "b": [4,5,6], "c": [7,8,9], "d": [1,2,1,2]}', '4'),
(2, '{"a": [10,20,30], "b": [41,51,61], "c": [72,82,92], "d": [11,21,11,21]}', '6');

UPDATE nested_obj_update
SET data['a'][0] = (updated_vals.b * 1)::TEXT::JSONB,
data['b'][2] = (updated_vals.c * 2)::TEXT::JSONB,
data['c'][0] = (updated_vals.d * 3)::TEXT::JSONB,
text_col = (nested_obj_update.id*1000)::TEXT,
data['a'][0] = (text_col::INT * data['a'][0]::INT)::TEXT::JSONB,
data['d'][6] = (nested_obj_update.id*1)::TEXT::JSONB,
data['d'][4] = (nested_obj_update.id*2)::TEXT::JSONB
FROM (
SELECT id,
data['a'][0] AS a,
data['b'][0]::NUMERIC + 1 AS b,
data['c'][0]::NUMERIC + 2 AS c,
data['c'][1]::NUMERIC + 3 AS d
FROM nested_obj_update
) updated_vals
WHERE nested_obj_update.id = updated_vals.id;

SELECT * FROM nested_obj_update ORDER BY 1,2,3;

set client_min_messages to error;
drop schema pg14 cascade;

0 comments on commit 9914df2

Please sign in to comment.