pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
random generated string matching index in inexplicable ways
5+ messages / 3 participants
[nested] [flat]

* random generated string matching index in inexplicable ways
@ 2019-05-07 11:53  Myles Miller <pg@q7r7.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Myles Miller @ 2019-05-07 11:53 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

PROBLEM:
Strings or characters generated by any random function (including pg_crypto
gen_random_bytes) are matching a string/char index in surprising ways.

Reduced down to its simplest example:

-- use random 0 or 1, plus 65, to get 'A' or 'B'

# SELECT chr(round(random())::int + 65);
chr 
-----
B
(1 row)

# SELECT chr(round(random())::int + 65);
chr 
-----
A
(1 row)


-- simple table for matching:
CREATE TABLE x( y char(1) primary key );
INSERT INTO x(y) VALUES ('A');
INSERT INTO x(y) VALUES ('B');


-- if I query 'A' or 'B' it works as expected

# SELECT y FROM x WHERE y = 'A';
y
---
A
(1 row)

# SELECT y FROM x WHERE y = 'B';                                                                                                                                                    
y
---
B
(1 row)



-- if we use random-generated 'A' or 'B', things get inexplicable

# SELECT y FROM x WHERE y = chr(round(random())::int + 65);
y 
---
A
B
(2 rows)

# SELECT y FROM x WHERE y = chr(round(random())::int + 65);
y 
---
(0 rows)

# SELECT y FROM x WHERE y = chr(round(random())::int + 65);
y 
---
B
(1 row)



I've been wrestling with this for hours. Any suggestions? Thank you.






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: random generated string matching index in inexplicable ways
@ 2019-05-07 12:50  Alban Hertroys <haramrae@gmail.com>
  parent: Myles Miller <pg@q7r7.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Alban Hertroys @ 2019-05-07 12:50 UTC (permalink / raw)
  To: Myles Miller <pg@q7r7.com>; +Cc: pgsql-general@lists.postgresql.org


> On 7 May 2019, at 13:53, Myles Miller <pg@q7r7.com> wrote:
> 
> PROBLEM:
> Strings or characters generated by any random function (including pg_crypto
> gen_random_bytes) are matching a string/char index in surprising ways.

I fail to see anything surprising in your examples.

> Reduced down to its simplest example:

(…)

> -- if we use random-generated 'A' or 'B', things get inexplicable
> 
> # SELECT y FROM x WHERE y = chr(round(random())::int + 65);
> y 
> ---
> A
> B
> (2 rows)

Here you got a random value in the lower range of 0..1 for the record with value ‘A’, so that’s a match, and one in the higher range for value ‘B’, a match again, so you get 2 rows.

> 
> # SELECT y FROM x WHERE y = chr(round(random())::int + 65);
> y 
> ---
> (0 rows)

Here you got a random value in the higher range for the record with value ‘A’, so no match, and one in the lower range for value ‘B’, no match again, so you get 0 rows.


> # SELECT y FROM x WHERE y = chr(round(random())::int + 65);
> y 
> ---
> B
> (1 row)

Here you got two random values in the higher range, so only the row with ‘B’ matches.

You could also get two random values in the lower range and only get a match against ‘A’.

Regards,
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: random generated string matching index in inexplicable ways
@ 2019-05-07 13:12  Myles Miller <pg@q7r7.com>
  parent: Alban Hertroys <haramrae@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Myles Miller @ 2019-05-07 13:12 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

> > # SELECT y FROM x WHERE y = chr(round(random())::int + 65);
> > y 
> > ---
> > A
> > B
> > (2 rows)
> Here you got a random value in the lower range of 0..1 for the record with value ‘A’, so that’s a match, and one in the higher range for value ‘B’, a match again, so you get 2 rows.

No, the function is returning just one letter, either 'A' or 'B', not multiple values.

# SELECT * FROM chr(round(random())::int + 65);
 chr
-----
 B
(1 row)

# SELECT * FROM chr(round(random())::int + 65);
 chr
-----
 A
(1 row)







^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: random generated string matching index in inexplicable ways [EXT]
@ 2019-05-07 13:15  Myles Miller <pg@q7r7.com>
  parent: Myles Miller <pg@q7r7.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Myles Miller @ 2019-05-07 13:15 UTC (permalink / raw)
  To: Daniel Perrett <dp13@sanger.ac.uk>; pgsql-general@lists.postgresql.org

On Tue, May 07, 2019 at 12:17:12PM +0000, Daniel Perrett wrote:
> The WHERE expression (and therefore the random function) is being evaluated once per row, not once per query.
> If you run this query a few times, you will see the problem:
> SELECT y, chr(round(random())::int + 65) FROM (SELECT 'A' UNION SELECT 'B') x(y);

Ah! Thank you! I understand now.






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: random generated string matching index in inexplicable ways
@ 2019-05-07 13:52  Francisco Olarte <folarte@peoplecall.com>
  parent: Myles Miller <pg@q7r7.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Francisco Olarte @ 2019-05-07 13:52 UTC (permalink / raw)
  To: Myles Miller <pg@q7r7.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Tue, May 7, 2019 at 3:12 PM Myles Miller <pg@q7r7.com> wrote:
> No, the function is returning just one letter, either 'A' or 'B', not multiple values.

Your random function is being evaluated ONCE FOR EACH ROW.

i.e, it's doing something like:
select y from
   ( SELECT y, chr(round(random())::int + 65) as z FROM x ) aux
WHERE y = z;
 o
with aux as ( SELECT y, chr(round(random())::int + 65) as z FROM x )
select y from aux WHERE y = z;

You may want to try something like this:

with aux as ( SELECT chr(round(random())::int + 65) as z )
select y from x,aux WHERE y = z;

( Untested, but that's the idea )

Francisco Olarte





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2019-05-07 13:52 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-05-07 11:53 random generated string matching index in inexplicable ways Myles Miller <pg@q7r7.com>
2019-05-07 12:50 ` Alban Hertroys <haramrae@gmail.com>
2019-05-07 13:12   ` Myles Miller <pg@q7r7.com>
2019-05-07 13:52     ` Francisco Olarte <folarte@peoplecall.com>
2019-05-07 13:15 ` Re: random generated string matching index in inexplicable ways [EXT] Myles Miller <pg@q7r7.com>

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