jiaqizho commented on PR #1187:
URL: https://github.com/apache/cloudberry/pull/1187#issuecomment-3004213098

   > Just tried:
   > 
   > ```
   > -- 状态转换函数:处理单个输入值并更新状态
   > CREATE OR REPLACE FUNCTION avg_trans_fn(
   >     state numeric[],  -- 当前状态:[sum, count]
   >     next_val numeric  -- 新输入值
   > ) RETURNS numeric[] AS $$
   > BEGIN
   >     IF next_val IS NOT NULL THEN
   >         state[1] := COALESCE(state[1], 0) + next_val;
   >         state[2] := COALESCE(state[2], 0) + 1;
   >     END IF;
   >     RETURN state;
   > END;
   > $$ LANGUAGE plpgsql;
   > 
   > -- 合并函数:合并两个部分聚合状态
   > CREATE OR REPLACE FUNCTION avg_combine_fn(
   >     state1 numeric[],
   >     state2 numeric[]
   > ) RETURNS numeric[] AS $$
   > BEGIN
   >     IF state1 IS NULL THEN
   >         RETURN state2;
   >     ELSIF state2 IS NULL THEN
   >         RETURN state1;
   >     ELSE
   >         RETURN ARRAY[state1[1] + state2[1], state1[2] + state2[2]];
   >     END IF;
   > END;
   > $$ LANGUAGE plpgsql;
   > 
   > -- 最终函数:从状态计算最终结果
   > CREATE OR REPLACE FUNCTION avg_final_fn(
   >     state numeric[]
   > ) RETURNS numeric AS $$
   > BEGIN
   >     IF state[2] = 0 THEN
   >         RETURN NULL;
   >     ELSE
   >         RETURN state[1] / state[2];
   >     END IF;
   > END;
   > $$ LANGUAGE plpgsql;
   > 
   > CREATE AGGREGATE my_avg(numeric) (
   >     sfunc = avg_trans_fn,       -- 状态转换函数
   >     stype = numeric[],          -- 状态类型
   >     combinefunc = avg_combine_fn, -- 合并函数
   >     finalfunc = avg_final_fn,   -- 最终计算函数
   >     initcond = '{0,0}'          -- 初始状态
   > );
   > 
   > CREATE TABLE tbl1(v1 int, v2 numeric);
   > CREATE TABLE tbl2(v1 int, v2 numeric);
   > ```
   > 
   > The agg pushdown doesn't work in ORCA but works in postgres Postgres query 
optimizer. 
![image](https://private-user-images.githubusercontent.com/79948451/458764680-15550048-6f8b-459d-9e15-758c6f16d94a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTA4NDY0MTIsIm5iZiI6MTc1MDg0NjExMiwicGF0aCI6Ii83OTk0ODQ1MS80NTg3NjQ2ODAtMTU1NTAwNDgtNmY4Yi00NTlkLTllMTUtNzU4YzZmMTZkOTRhLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTA2MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwNjI1VDEwMDgzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI4NWZhY2JjZjI4ZTNmNDJiZjcwNjY4YjVkMjZkNDhlYzIzYWU5OWU5NjBjY2QxMGQyNjA2MjBiN2FlNDM0M2ImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.T7L-WU9Ji5YioqzZRxZpIeQPwFdom55KPVeZ1RMzBQU)
   
   Without any data in the table `tbl1` and `tbl2`. ORCA choose the one-stage 
agg, but we do have the pushdown path. 
   
   we can use the `optimizer_force_multistage_agg` to force ORCA choose the 
multi-stage path(But won't create the no-pushdown path, because the enforce of 
distribution).
   
   
![image](https://github.com/user-attachments/assets/91a7e883-5ba8-4b14-9f1a-d304d7e3f78b)
   
   also use the `my_avg`
   
   
![image](https://github.com/user-attachments/assets/511d5472-856d-480a-b4bc-9d383a450b1a)
   
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to