Support Nested Collections in `add` and `find` Functions
还没有人认领这个 Issue。
评估
调研方向
从现有的 PostgreSQL add 和 find 入口点开始,然后将它们当前的签名与本 issue 中隐藏的 VARIADIC 提案进行比较。确定首选 API 或 add2/find2 替代方案是否符合兼容性需求;当嵌套读写示例能够正常工作且不会造成意外破坏时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Description
Currently, pgCollection supports storing collections inside collections. However, reading and writing nested collections is verbose for 2-level, 3-level, or deeper structures:
DO $$
DECLARE
arr_instance collection('collection');
var1 text;
BEGIN
-- Write a 3-level nested array
arr_instance := add(arr_instance, 'A',
add(CASE WHEN exist(arr_instance, 'A') THEN find(arr_instance, 'A', NULL::collection) ELSE NULL::collection END, 'AA',
add(CASE WHEN exist(arr_instance, 'A') AND exist(find(arr_instance, 'A', NULL::collection), 'AA')
THEN find(find(arr_instance, 'A', NULL::collection), 'AA', NULL::collection)
ELSE NULL::collection END, 'AAA', 'some text'::text)));
-- Read a 3-level nested array
var1 := (find(find(arr_instance, 'A', NULL::collection), 'AA', NULL::collection))['AAA']::text;
RAISE NOTICE 'var1: %', var1;
END $$;
This becomes cumbersome for 2-level, 3-level, or deeper nested collections.
Proposal
Main Proposal (preferred)
Extend the existing add and find functions to accept a variadic list of keys, enabling direct read/write of nested collections:
add(collection, value, VARIADIC keys text[])
find(collection, dummy, VARIADIC keys text[])
Alternative
Create new functions (to avoid changing existing APIs):
add2(collection, value, VARIADIC keys text[])
find2(collection, dummy, VARIADIC keys text[])
Examples of Usage
Writing Nested Collections
DO $$
DECLARE
arr collection('collection');
BEGIN
arr := add(arr, 'Hello', 'A','B','C'); -- 3-level nested write
END $$;
Reading Nested Collections
DO $$
DECLARE
arr collection('collection');
val text;
BEGIN
val := find(arr, NULL::text, 'A','B','C'); -- 3-level nested read
END $$;
Implementation (hidden)
Click to expand the proposed `add` and `find` VARIADIC implementations
-- find: traverse delegating to built-in find()
CREATE OR REPLACE FUNCTION find(coll collection, dummy anyelement, VARIADIC keys text[])
RETURNS anyelement
LANGUAGE plpgsql
AS $$
DECLARE
cur_coll collection := coll;
key text;
n int := coalesce(array_length(keys,1), 0);
i int;
BEGIN
IF n = 0 THEN
RETURN NULL;
END IF;
IF n > 1 THEN
FOR i IN 1 .. n-1 LOOP
key := keys[i];
-- delegate to built-in find - preserves its error behavior
cur_coll := find(cur_coll, key, NULL::collection);
END LOOP;
END IF;
key := keys[n];
RETURN find(cur_coll, key, dummy);
END;
$$;
-- add: recursive, builds/merges nested collections correctly
CREATE OR REPLACE FUNCTION add(coll collection, val anyelement, VARIADIC keys text[])
RETURNS collection
LANGUAGE plpgsql
AS $$
DECLARE
n int := coalesce(array_length(keys,1),0);
first_key text;
rest_keys text[];
child collection;
base_coll collection := coalesce(coll, NULL::collection);
BEGIN
IF n = 0 THEN
RAISE EXCEPTION 'add: no keys provided';
END IF;
-- base case: single key -> use built-in add(collection, key, value)
IF n = 1 THEN
RETURN add(base_coll, keys[1], val);
END IF;
-- prepare first key and rest slice
first_key := keys[1];
rest_keys := keys[2:n];
-- get existing child (if present), otherwise start with NULL::collection
IF exist(base_coll, first_key) THEN
child := find(base_coll, first_key, NULL::collection);
ELSE
child := NULL::collection;
END IF;
-- update the child recursively
child := add(child, val, VARIADIC rest_keys);
-- put updated child back into parent collection
RETURN add(base_coll, first_key, child);
END;
$$;
Pros
- Allows direct reading and writing of nested collections.
- Makes code cleaner and more readable for deep nesting.
Cons
- Changing the sequence of input parameters for
addandfindmay break existing code (if modifying the original functions). - Using new functions (
add2/find2) avoids breaking existing code but adds similar APIs.
- 主要语言
- C
- 星标
- 24
- 派生
- 6
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
aws/pgcollection 的其他 Issue
-
The LLVM object can build before collection_config.h is generated可能已有人在做 @Vonng 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 90/100
aws/pgcollection#51 ·
-
难度 4/5 3-5 天 新手友好度 52/100
aws/pgcollection#50 ·
-
Nonempty icollection values cannot be read back from text or restored from a dump可能已有人在做 @Vonng 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
aws/pgcollection#48 ·
-
enhancement
难度 4/5 3-5 天 新手友好度 42/100
aws/pgcollection#6 · 2 条评论 ·
相似的 Issue
-
issue: bug report
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 2 天内回复
-
AI/LLM documentation Professional Contributors Only
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 2 天内回复
-
severity: low
难度 2/5 1-3 小时 新手友好度 82/100
luainkernel/lunatik#2752 ·
维护者通常 1 天内回复
-
feature:config kind:docs os:windows toolchain:cmake triaged
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 1/5 1-3 小时 新手友好度 88/100
BasedHardware/omi#26486 ·
维护者通常 1 天内回复