Skip to content

random() cached across rows when inside list comprehension #8108

Closed
@paulrosenzweig

Description

What happens?

The value for random() repeats across rows when used inside a list comprehension.

To Reproduce

D select [random() for _ in range(1)]  from range(2);
┌──────────────────────────────────────────┐
│ main.list_apply(range(1), _ -> random()) │
│                 double[]                 │
├──────────────────────────────────────────┤
│ [0.765232979785651]                      │
│ [0.765232979785651]                      │
└──────────────────────────────────────────┘

The two rows are always the same. Contrast that with:

D select random() from range(2);
┌────────────────────┐
│      random()      │
│       double       │
├────────────────────┤
│ 0.5257333947811276 │
│ 0.5609628073871136 │
└────────────────────┘
D select [random()]  from range(2);
┌───────────────────────────┐
│ main.list_value(random()) │
│         double[]          │
├───────────────────────────┤
│ [0.3676034810487181]      │
│ [0.6335314936004579]      │
└───────────────────────────┘

Including a value that changes in the list comprehension fixes the issue:

select [random() + range * 0 for _ in range(1)]  from range(2);
┌────────────────────────────────────────────────────────────┐
│ main.list_apply(range(1), _ -> (random() + ("range" * 0))) │
│                          double[]                          │
├────────────────────────────────────────────────────────────┤
│ [0.25919290888123214]                                      │
│ [0.4220627495087683]                                       │
└────────────────────────────────────────────────────────────┘

OS:

MacOS

DuckDB Version:

v0.8.2-dev161 563662b

DuckDB Client:

CLI

Full Name:

Paul Rosenzweig

Affiliation:

None

Have you tried this on the latest master branch?

  • I agree

Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?

  • I agree

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions