duerwuyi opened a new issue, #37852:
URL: https://github.com/apache/shardingsphere/issues/37852

   ## Bug Report
   
   ### Which version of ShardingSphere did you use?
   
   shardingsphere-proxy 5.5.2
   postgres 17.6 as backend
   
   ### Expected behavior
   
   This query should return distinct result:
   ```sql
   select distinct 
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
         ) as c_0
   from 
     t24 as ref_0
   where true::bool;
   ```
   
   ### Actual behavior
   
   SELECT DISTINCT on a sharded table returns duplicate rows when executed 
through ShardingSphere-Proxy. The same logical query executed on a single 
PostgreSQL instance returns correct globally de-duplicated results.
   
   From proxy logs, ShardingSphere rewrites the query into multiple per-shard 
SELECT DISTINCT ... FROM t24_x combined by UNION ALL. This does not perform 
global deduplication across shards, so duplicate values (including duplicate 
NULLs) are returned.
   
   This is a correctness bug: DISTINCT is a global set operation and cannot be 
implemented as per-shard DISTINCT + UNION ALL without an additional global 
distinct/aggregation step.
   
   ```sql
   testdb=> select distinct 
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
         ) as c_0
   from 
     t24 as ref_0
   where true::bool;
    c_0 
   -----
      0
       
    -95
    -94
       
   (5 rows)
   ```
   
   ### Reason analyze (If you can)
   
   log:
   ```log
   [INFO ] 2026-01-26 11:25:33.802 [ShardingSphere-Command-456] 
ShardingSphere-SQL - Actual SQL: ds_1 ::: select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_1 as ref_0
   
   where true::bool UNION ALL select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_6 as ref_0
   
   where true::bool UNION ALL select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_11 as ref_0
   
   where true::bool UNION ALL select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_16 as ref_0
   
   where true::bool
   
   [INFO ] 2026-01-26 11:25:33.802 [ShardingSphere-Command-456] 
ShardingSphere-SQL - Actual SQL: ds_2 ::: select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_2 as ref_0
   
   where true::bool UNION ALL select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_7 as ref_0
   
   where true::bool UNION ALL select distinct 
   
     pg_catalog.round(case when (null::bool) then (ref_0.c38) else (ref_0.c37) 
end
   
         ) as c_0
   
   from 
   
     t24_12 as ref_0
   
   where true::bool UNION ALL select distinct 
   
   
   ```
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule 
configuration, when exception occur etc.
   
[ss_postgres.zip](https://github.com/user-attachments/files/24228864/ss_postgres.zip)
   
   run `docker compose up -d` in this file.
   
   SQL to execute first:
   ```sql
   CREATE SHARDING TABLE RULE t24 (
   STORAGE_UNITS(ds_0,ds_1,ds_2,ds_3,ds_4),
   SHARDING_COLUMN=colocated_key,TYPE(NAME="MOD",PROPERTIES(
   "sharding-count"="21"))
   );
   
   DROP TABLE IF EXISTS t24;
   
   create table t24 ( 
   vkey int4 ,
   pkey int4 ,
   c36 numeric ,
   c37 numeric ,
   c38 numeric ,
   c39 text ,
   c40 text ,
   c41 text ,
   colocated_key int4 
   
   );
   
   
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3735, 3745000, -53.90, null::numeric, -56.84, null::text, 
'RrHYP~C1vC^)PNa(DjV', null::text, 1826460456);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3742, 3752000, -1456924258.14691, null::numeric, -29.97, 
'*E]74/q<cIcNL7h#-l.eXx4YB', 'bEg^y;640^<', '4nT);kbc.Dz1<QvyN4mXWU', 
451348812);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3745, 3755000, null::numeric, -93.93, 58.15, null::text, 
',AZS=$?X@mcMVWFu5~I`', '--M.OGWU:M7U;n:', -1539890837);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3749, 3759000, -424786407.14788, -94.75, null::numeric, '*9E8bE', 
't`{6}@WtG&A{bOFOc(5<;B', '+JARJ!#xMi8>#`2J', -65);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3750, 3760000, -2114913156.2077, null::numeric, 1968229656.11327, 
'Fq&QteE,#9GEj', 'H#Cngf', '=3VB,Q]qM00,rX=9;xZUOj ;H4oZU', 1652888866);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3755, 3765000, null::numeric, 0.0, 58.74, 'GA4Ojj$EhN26', 
'?Whaf@~(w}Y]Wld<MNm,8', '9:JRiOK&*|#:H!f-=RfY9C4wB`', 1144145842);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3758, 3768000, 1136604594.30098, 0.0, null::numeric, '^:=akh+I', 
null::text, '', 93677795);
   insert into t24 (vkey, pkey, c36, c37, c38, c39, c40, c41, colocated_key) 
values 
   (3765, 3775000, -705783199.6173, null::numeric, -1679695831.13212, 
'&M-]P1r$#', 'LSC :Fi4W-jbu`', '*T ;5&qFEF5^;>>:/?qJW', 0);
   ```
   


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

Reply via email to