Skip to content

Condition pushdown for rand() > value multiply the probabilities #17244

@UnamedRus

Description

@UnamedRus

Describe the bug
If we pushdown сonditions with rand() function in subquery, we actually multiply the probabilities.

How to reproduce
Clickhouse version 20.12

SELECT count(*)
FROM
(
    SELECT number
    FROM
    (
        SELECT number
        FROM numbers(1000000)
    )
    WHERE rand64() < (0.01 * 18446744073709552000.)
)

Query id: 13c8ae5e-ed90-4610-b7c3-8e8d3bf27f12

┌─count()─┐
│     114 │
└─────────┘

1 rows in set. Elapsed: 0.008 sec. Processed 1.05 million rows, 8.38 MB (136.06 million rows/s., 1.09 GB/s.)


EXPLAIN SYNTAX
SELECT count(*)
FROM
(
    SELECT number
    FROM
    (
        SELECT number
        FROM numbers(1000000)
    )
    WHERE rand64() < (0.01 * 18446744073709552000.)
)
Query id: 6036d15a-32ba-4161-a6fe-41eff40af246
┌─explain─────────────────────────────────────────────────┐
│ SELECT count()                                          │
│ FROM                                                    │
│ (                                                       │
│     SELECT number                                       │
│     FROM                                                │
│     (                                                   │
│         SELECT number                                   │
│         FROM numbers(1000000)                           │
│         WHERE rand64() < (0.01 * 18446744073709552000.) │
│     )                                                   │
│     WHERE rand64() < (0.01 * 18446744073709552000.)     │
│ )                                                       │
└─────────────────────────────────────────────────────────┘

Metadata

Metadata

Assignees

Labels

bugConfirmed user-visible misbehaviour in official releasest-acceptedThe issue is in our backlog, ready to take

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions