pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: james.inform@pharmapp.de
Subject: BUG #18060: Left joining rows using random() function in join condition doesn't work as expected.
Date: Fri, 18 Aug 2023 09:30:51 +0000
Message-ID: <18060-e59408b5655979ed@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      18060
Logged by:          James Inform
Email address:      james.inform@pharmapp.de
PostgreSQL version: 15.4
Operating system:   Linux and Mac
Description:        

/*
    PostgreSQL 14.9 / 15.4 on Linux and Mac
    
    Left joining rows using random() function in join condition
    doesn't work as expected.
   
   
    I have encountered this while I was trying randomly left join a record
of a source table
    with exactly with one record of a link table.
    
    Just execute the create statements under 1.
    
    Then execute the select under 2. multiple times and watch thee
results.
 */

-- 1. Generate two tables (source and link) with 1000 rows having gapless
ids
create temp table source as
select source_id, 'source' as source_name from generate_series(1,1000) as
source_id
;

create temp table link as
select link_id, 'link' as link_name from generate_series(1,1000) as
link_id
;

-- 2. Execute this query multiple time and you will see results where:
-- * no rows from link are joined
-- * extactly one row is joined
-- * multiple rows are joined

select * from source left join link on link_id = 1 + (random()*500)::int8
order by 1

/*
I would expect always exactly one row to be joined. 
Instead I get 1, none or multiple.

Is this an error or am I doing something wrong?
*/



view thread (6+ messages)  latest in thread

Message-ID: <18060-e59408b5655979ed@postgresql.org>
Permalink:  ../18060-e59408b5655979ed@postgresql.org/
Also on:    postgresql.org/message-id/18060-e59408b5655979ed@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, james.inform@pharmapp.de
  Subject: Re: BUG #18060: Left joining rows using random() function in join condition doesn't work as expected.
  In-Reply-To: <18060-e59408b5655979ed@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox