Skip to content

Commit

Permalink
fix the bug which leading to wrong answer of sql string_agg(orderby) …
Browse files Browse the repository at this point in the history
…& agg(distinct)
  • Loading branch information
erchuan.ty committed Jan 25, 2024
1 parent af90388 commit 2f303b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/backend/executor/nodeAgg.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ initialize_aggregates(AggState *aggstate,

/* CDB: Set enhanced sort options. */
{
int unique = peragg->aggref->aggdistinct &&
int unique = peraggstate->aggref->aggdistinct &&
( gp_enable_sort_distinct ? 1 : 0) ;
int sort_flags = gp_sort_flags; /* get the guc */
int maxdistinct = gp_sort_max_distinct; /* get guc */
Expand Down
35 changes: 35 additions & 0 deletions src/test/regress/expected/aggregate_with_sort_distinct.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
drop schema if exists agg_sort_distinct_regress_schema cascade;
NOTICE: schema "agg_sort_distinct_regress_schema" does not exist, skipping
create schema if not exists agg_sort_distinct_regress_schema;
set search_path to agg_sort_distinct_regress_schema;
create table test_multi_order_agg(a int, b int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into test_multi_order_agg values (1, 1), (1, null), (null, 1);
set gp_enable_sort_distinct to on;
select string_agg(a::text, ','), string_agg(distinct b::text, ',') from test_multi_order_agg;
string_agg | string_agg
------------+------------
1,1 | 1
(1 row)

select string_agg(a::text, ',' order by b), string_agg(distinct b::text, ',') from test_multi_order_agg;
string_agg | string_agg
------------+------------
1,1 | 1
(1 row)

set gp_enable_sort_distinct to off;
select string_agg(a::text, ','), string_agg(distinct b::text, ',') from test_multi_order_agg;
string_agg | string_agg
------------+------------
1,1 | 1
(1 row)

select string_agg(a::text, ',' order by b), string_agg(distinct b::text, ',') from test_multi_order_agg;
string_agg | string_agg
------------+------------
1,1 | 1
(1 row)


1 change: 1 addition & 0 deletions src/test/regress/greenplum_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ test: bfv_catalog bfv_index bfv_olap bfv_aggregate bfv_partition bfv_partition_p
# NOTE: gporca_faults uses gp_fault_injector - so do not add to a parallel group
test: gporca_faults

test: aggregate_with_sort_distinct
test: aggregate_with_groupingsets

test: nested_case_null sort bb_mpph
Expand Down
16 changes: 16 additions & 0 deletions src/test/regress/sql/aggregate_with_sort_distinct.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
drop schema if exists agg_sort_distinct_regress_schema cascade;
create schema if not exists agg_sort_distinct_regress_schema;

set search_path to agg_sort_distinct_regress_schema;

create table test_multi_order_agg(a int, b int);

insert into test_multi_order_agg values (1, 1), (1, null), (null, 1);

set gp_enable_sort_distinct to on;
select string_agg(a::text, ','), string_agg(distinct b::text, ',') from test_multi_order_agg;
select string_agg(a::text, ',' order by b), string_agg(distinct b::text, ',') from test_multi_order_agg;

set gp_enable_sort_distinct to off;
select string_agg(a::text, ','), string_agg(distinct b::text, ',') from test_multi_order_agg;
select string_agg(a::text, ',' order by b), string_agg(distinct b::text, ',') from test_multi_order_agg;

0 comments on commit 2f303b5

Please sign in to comment.