Support Nested Collections in `add` and `find` Functions

未关闭
#32 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
38/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
c, postgresql, sql
领域
database

调研方向

从现有的 PostgreSQL add 和 find 入口点开始,然后将它们当前的签名与本 issue 中隐藏的 VARIADIC 提案进行比较。确定首选 API 或 add2/find2 替代方案是否符合兼容性需求;当嵌套读写示例能够正常工作且不会造成意外破坏时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

enhancement

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 add and find may 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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aws/pgcollection 的其他 Issue

查看 aws/pgcollection 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。