Alena0704 opened a new pull request, #1937:
URL: https://github.com/apache/cloudberry/pull/1937

   gp_percentile_cont() / gp_percentile_disc() are the split ordered-set
   aggregates ORCA rewrites percentile_cont(), percentile_disc() and median()
   into. Two bugs in their transition functions. Only the ORCA path reaches
   these aggregates on its own, so percentile_* WITHIN GROUP on the Postgres
   planner is unaffected.
   
   1. The catalog declares the wrong number of arguments
   
   The transition functions read five arguments in C (state + the four
   aggregate arguments), but pg_proc.dat declares four. As transition
   functions they are called correctly; a direct SQL call makes
   PG_GETARG_INT64(4) pick up garbage — wrong results, an assertion on
   pfree(NULL), or a segfault.
   
   2. The isnull flag is lost when the previous state is returned
   
   When the current row isn't one the percentile is computed from, the
   previous state is handed back untouched. On the first call that state is
   NULL, and returning a bare Datum(0) with isnull false drops the flag: 0
   instead of NULL for by-value types, a NULL pointer dereference for
   by-reference ones — an empty input set crashed the backend for interval,
   timestamp and timestamptz.
   
   Both ported from Greengage commit 477b04a (ADBDEV-7770).
   
   The bug reproduction:
   
   ```
   -- Bug 1: pg_proc.dat declares four arguments, the code reads five
   select gp_percentile_disc_transition(NULL::numeric, 1, 1, 1);  -- garbage or 
crash
   -- after the fix the five-argument form resolves and works:
   select gp_percentile_disc_transition(NULL::numeric, 1::numeric, 1, 1, 1);  
-- 1
   
   -- Bug 2, by-value: silently wrong answer
   select gp_percentile_cont(0::float8, 0, 0, 0);             -- 0, expecting 
NULL
   -- Bug 2, by-reference: backend crash
   select gp_percentile_cont('0 hour'::interval, 0, 0, 0);    -- server closed 
the connection
   -- Bug 2, the 6c289ad regression: a selected value whose Datum is 0 turns 
into NULL
   select gp_percentile_disc(0::float8, 0, 1, 1);             -- NULL, 
expecting 0
   select gp_percentile_disc(0::int,    0, 1, 1);             -- NULL, 
expecting 0
   
   -- The same, end to end: ORCA and the Postgres planner disagree
   create table perczero (a int, b float8) distributed by (a);
   insert into perczero select i, (i - 1)::float8 from generate_series(1, 10) i;
   select percentile_disc(0) within group (order by b) from perczero;  -- NULL
   set optimizer = off;
   select percentile_disc(0) within group (order by b) from perczero;  -- 0
   ```
   
   
   ### Type of Change
   - [x] Bug fix (non-breaking change)
   - [ ] New feature (non-breaking change)
   - [ ] Breaking change (fix or feature with breaking changes)
   - [ ] Documentation update
   
   ### Breaking Changes
   <!-- Remove if not applicable. If yes, explain impact and migration path -->
   
   ### Test Plan
   <!-- How did you test these changes? -->
   - [ ] Unit tests added/updated
   - [ ] Integration tests added/updated
   - [ ] Passed `make installcheck`
   - [ ] Passed `make -C src/test installcheck-cbdb-parallel`
   
   ### Impact
   <!-- Remove sections that don't apply -->
   **Performance:**
   <!-- Any performance implications? -->
   
   **User-facing changes:**
   <!-- Any changes visible to users? -->
   
   **Dependencies:**
   <!-- New dependencies or version changes? -->
   
   ### Checklist
   - [x] Followed [contribution 
guide](https://cloudberry.apache.org/contribute/code)
   - [ ] Added/updated documentation
   - [ ] Reviewed code for security implications
   - [ ] This PR contains AI-assisted code generation
   - [x] Requested review from [cloudberry 
committers](https://github.com/orgs/apache/teams/cloudberry-committers)
   
   ### Additional Context
   <!-- Any other information that would help reviewers? Remove if none -->
   
   ### CI Skip Instructions
   <!--
   To skip CI builds, add the appropriate CI skip identifier to your PR title.
   The identifier must:
   - Be in square brackets []
   - Include the word "ci" and either "skip" or "no"
   - Only use for documentation-only changes or when absolutely necessary
   -->
   
   ---
   <!-- Join our community:
   - Mailing list: 
[[email protected]](https://lists.apache.org/[email protected])
 (subscribe: [email protected])
   - Discussions: https://github.com/apache/cloudberry/discussions -->
   


-- 
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