Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mos65o2
Copy link

@mos65o2 mos65o2 commented Jul 18, 2025

Fix incorrect behavior of gp_percentile_* functions (#1928)

Commit b90f952 added five GPDB-specific functions
gp_percentile_cont_float8_transition, gp_percentile_cont_interval_transition,
gp_percentile_cont_timestamp_transition,
gp_percentile_cont_timestamptz_transition and gp_percentile_disc_transition.

  1. All these five functions use five arguments internally in the C
    implementation, but they were added to the catalog in the file
    src/include/catalog/pg_proc.dat with four arguments, which could lead to
    incorrect results, assertions when freeing a null pointer and even segfaults
    when dereferencing a null pointer. This patch adds the missing arguments.

  2. All these five functions under certain conditions can return their first
    argument, but if the first argument was NULL (and the NULL argument is passed
    with the additional isnull flag), then when it was returned, all these five
    functions lost the additional isnull flag, which could also lead to incorrect
    results (for the non-varlena types of the first two arguments), and to
    assertions when freeing a null pointer and even segfaults when dereferencing a
    null pointer (for the varlena types). This patch adds saving of the additional
    isnull flag, and now under the same conditions, all these five functions return
    NULL.

The patch adds three tests for each of the five functions.

(cherry picked from commit 477b04a)

Changes in original commit:

  • pg_proc.dat is not changed.
  • added check in gp_percentile_*_transition functions.
  • test is adapted.

Because changes to pg_proc.dat cause difficulties with cluster upgrades, this
file is left as is. However, in this case, the signature of
the gp_percentile_cont_transition and gp_percentile_disc_transition functions
in pg_proc.dat did not match the number of arguments they processed (they are
called from gp_percentile_*_* functions). This led to an incorrect result or
segfault when calling these functions directly, since the number of arguments
passed was less than the number processed. So, we add check for matching the
number of input arguments (there must be 5). Now, direct calls to these
functions return an error.
This change does not affect 'percentile_* WITHIN GROUP' queries with the
ORCA optimizer.

Ticket: ADBDEV-7771


Commits in the PR should be merged with a rebase mode to preserve authorship.

mos65o2 pushed a commit that referenced this pull request Jul 18, 2025
Commit b90f952 added five GPDB-specific functions
gp_percentile_cont_float8_transition, gp_percentile_cont_interval_transition,
gp_percentile_cont_timestamp_transition,
gp_percentile_cont_timestamptz_transition and gp_percentile_disc_transition.

1) All these five functions use five arguments internally in the C
implementation, but they were added to the catalog in the file
src/include/catalog/pg_proc.dat with four arguments, which could lead to
incorrect results, assertions when freeing a null pointer and even segfaults
when dereferencing a null pointer. This patch adds the missing arguments.

2) All these five functions under certain conditions can return their first
argument, but if the first argument was NULL (and the NULL argument is passed
with the additional isnull flag), then when it was returned, all these five
functions lost the additional isnull flag, which could also lead to incorrect
results (for the non-varlena types of the first two arguments), and to
assertions when freeing a null pointer and even segfaults when dereferencing a
null pointer (for the varlena types). This patch adds saving of the additional
isnull flag, and now under the same conditions, all these five functions return
NULL.

The patch adds three tests for each of the five functions.

Ticket: ADBDEV-7771
(cherry picked from commit 477b04a)
mos65o2 added a commit that referenced this pull request Jul 24, 2025
…1928)

The signature of the gp_percentile_cont_transition and
gp_percentile_disc_transition functions in pg_proc.dat did not match the
number of arguments they processed (they are called from gp_percentile_*_*
functions). This led to an incorrect result or segfault when calling these
functions directly, since the number of arguments passed was less than the
number processed. This commit adds a check for matching the number of input
arguments - there must be 5. Since the number of arguments in pg_proc.dat
is 4, direct calls to these functions return an error. This change does not
affect 'percentile_* WITHIN GROUP' queries with the ORCA optimizer.
@RekGRpth

This comment was marked as resolved.

mos65o2 pushed a commit that referenced this pull request Jul 25, 2025
Commit b90f952 added five GPDB-specific functions
gp_percentile_cont_float8_transition, gp_percentile_cont_interval_transition,
gp_percentile_cont_timestamp_transition,
gp_percentile_cont_timestamptz_transition and gp_percentile_disc_transition.

1) All these five functions use five arguments internally in the C
implementation, but they were added to the catalog in the file
src/include/catalog/pg_proc.dat with four arguments, which could lead to
incorrect results, assertions when freeing a null pointer and even segfaults
when dereferencing a null pointer. This patch adds the missing arguments.

2) All these five functions under certain conditions can return their first
argument, but if the first argument was NULL (and the NULL argument is passed
with the additional isnull flag), then when it was returned, all these five
functions lost the additional isnull flag, which could also lead to incorrect
results (for the non-varlena types of the first two arguments), and to
assertions when freeing a null pointer and even segfaults when dereferencing a
null pointer (for the varlena types). This patch adds saving of the additional
isnull flag, and now under the same conditions, all these five functions return
NULL.

The patch adds three tests for each of the five functions.

(cherry picked from commit 477b04a)

Changes in original commit:
- pg_proc.dat is not changed.
- added check in gp_percentile_*_* functions.
- test is adapted.

Because changes to pg_proc.dat cause difficulties with cluster upgrades, this
file is left as is. However, in this case, the signature of
the gp_percentile_cont_transition and gp_percentile_disc_transition functions
in pg_proc.dat did not match the number of arguments they processed (they are
called from gp_percentile_*_* functions). This led to an incorrect result or
segfault when calling these functions directly, since the number of arguments
passed was less than the number processed. So, we add check for matching the
number of input arguments (there must be 5). Now, direct calls to these
functions return an error.
This change does not affect 'percentile_* WITHIN GROUP' queries with the
ORCA optimizer.

Ticket: ADBDEV-7771
@mos65o2 mos65o2 force-pushed the ADBDEV-7771 branch 2 times, most recently from bf4a570 to cf19209 Compare July 25, 2025 09:35
mos65o2 pushed a commit that referenced this pull request Jul 25, 2025
Commit b90f952 added five GPDB-specific functions
gp_percentile_cont_float8_transition, gp_percentile_cont_interval_transition,
gp_percentile_cont_timestamp_transition,
gp_percentile_cont_timestamptz_transition and gp_percentile_disc_transition.

1) All these five functions use five arguments internally in the C
implementation, but they were added to the catalog in the file
src/include/catalog/pg_proc.dat with four arguments, which could lead to
incorrect results, assertions when freeing a null pointer and even segfaults
when dereferencing a null pointer. This patch adds the missing arguments.

2) All these five functions under certain conditions can return their first
argument, but if the first argument was NULL (and the NULL argument is passed
with the additional isnull flag), then when it was returned, all these five
functions lost the additional isnull flag, which could also lead to incorrect
results (for the non-varlena types of the first two arguments), and to
assertions when freeing a null pointer and even segfaults when dereferencing a
null pointer (for the varlena types). This patch adds saving of the additional
isnull flag, and now under the same conditions, all these five functions return
NULL.

The patch adds three tests for each of the five functions.

(cherry picked from commit 477b04a)

Changes in original commit:
- pg_proc.dat is not changed.
- added check in gp_percentile_*_transition functions.
- test is adapted.

Because changes to pg_proc.dat cause difficulties with cluster upgrades, this
file is left as is. However, in this case, the signature of
the gp_percentile_cont_transition and gp_percentile_disc_transition functions
in pg_proc.dat did not match the number of arguments they processed (they are
called from gp_percentile_*_* functions). This led to an incorrect result or
segfault when calling these functions directly, since the number of arguments
passed was less than the number processed. So, we add check for matching the
number of input arguments (there must be 5). Now, direct calls to these
functions return an error.
This change does not affect 'percentile_* WITHIN GROUP' queries with the
ORCA optimizer.

Ticket: ADBDEV-7771
@mos65o2
Copy link
Author

mos65o2 commented Jul 25, 2025

1. this PR should have only one commit

2. this commit should be cherry-picked with the -x option to preserve authorship

3. this commit description should include changes made since the original commit

4. this PR description should include a warning that it should be merged with a rebase

Is it ok?

@RekGRpth

This comment was marked as resolved.

mos65o2 pushed a commit that referenced this pull request Jul 25, 2025
Commit b90f952 added five GPDB-specific functions
gp_percentile_cont_float8_transition, gp_percentile_cont_interval_transition,
gp_percentile_cont_timestamp_transition,
gp_percentile_cont_timestamptz_transition and gp_percentile_disc_transition.

1) All these five functions use five arguments internally in the C
implementation, but they were added to the catalog in the file
src/include/catalog/pg_proc.dat with four arguments, which could lead to
incorrect results, assertions when freeing a null pointer and even segfaults
when dereferencing a null pointer. This patch adds the missing arguments.

2) All these five functions under certain conditions can return their first
argument, but if the first argument was NULL (and the NULL argument is passed
with the additional isnull flag), then when it was returned, all these five
functions lost the additional isnull flag, which could also lead to incorrect
results (for the non-varlena types of the first two arguments), and to
assertions when freeing a null pointer and even segfaults when dereferencing a
null pointer (for the varlena types). This patch adds saving of the additional
isnull flag, and now under the same conditions, all these five functions return
NULL.

The patch adds three tests for each of the five functions.

(cherry picked from commit 477b04a)

Changes in original commit:
- pg_proc.dat is not changed.
- added check in gp_percentile_*_transition functions.
- test is adapted.

Because changes to pg_proc.dat cause difficulties with cluster upgrades, this
file is left as is. However, in this case, the signature of
the gp_percentile_cont_transition and gp_percentile_disc_transition functions
in pg_proc.dat did not match the number of arguments they processed (they are
called from gp_percentile_*_* functions). This led to an incorrect result or
segfault when calling these functions directly, since the number of arguments
passed was less than the number processed. So, we add check for matching the
number of input arguments (there must be 5). Now, direct calls to these
functions return an error.
This change does not affect 'percentile_* WITHIN GROUP' queries with the
ORCA optimizer.

Ticket: ADBDEV-7771
@mos65o2 mos65o2 marked this pull request as ready for review July 28, 2025 05:50
Commit b90f952 added five GPDB-specific functions
gp_percentile_cont_float8_transition, gp_percentile_cont_interval_transition,
gp_percentile_cont_timestamp_transition,
gp_percentile_cont_timestamptz_transition and gp_percentile_disc_transition.

1) All these five functions use five arguments internally in the C
implementation, but they were added to the catalog in the file
src/include/catalog/pg_proc.dat with four arguments, which could lead to
incorrect results, assertions when freeing a null pointer and even segfaults
when dereferencing a null pointer. This patch adds the missing arguments.

2) All these five functions under certain conditions can return their first
argument, but if the first argument was NULL (and the NULL argument is passed
with the additional isnull flag), then when it was returned, all these five
functions lost the additional isnull flag, which could also lead to incorrect
results (for the non-varlena types of the first two arguments), and to
assertions when freeing a null pointer and even segfaults when dereferencing a
null pointer (for the varlena types). This patch adds saving of the additional
isnull flag, and now under the same conditions, all these five functions return
NULL.

The patch adds three tests for each of the five functions.

(cherry picked from commit 477b04a)

Changes in original commit:
- pg_proc.dat is not changed.
- added check in gp_percentile_*_transition functions.
- test is adapted.

Because changes to pg_proc.dat cause difficulties with cluster upgrades, this
file is left as is. However, in this case, the signature of
the gp_percentile_cont_transition and gp_percentile_disc_transition functions
in pg_proc.dat did not match the number of arguments they processed (they are
called from gp_percentile_*_* functions). This led to an incorrect result or
segfault when calling these functions directly, since the number of arguments
passed was less than the number processed. So, we add check for matching the
number of input arguments (there must be 5). Now, direct calls to these
functions return an error.
This change does not affect 'percentile_* WITHIN GROUP' queries with the
ORCA optimizer.

Ticket: ADBDEV-7771
@hilltracer hilltracer self-requested a review August 5, 2025 08:13
@mos65o2 mos65o2 merged commit 5cdf21a into adb-7.2.0 Aug 6, 2025
5 checks passed
@mos65o2 mos65o2 deleted the ADBDEV-7771 branch August 6, 2025 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants