Skip to content

[Data] Add random() and uuid() expression - #59656

Merged
richardliaw merged 1 commit into
ray-project:masterfrom
wingkitlee0:kit/random-expr
Mar 24, 2026
Merged

richardliaw merged 1 commit into
ray-project:masterfrom
wingkitlee0:kit/random-expr

Conversation

@wingkitlee0

@wingkitlee0 wingkitlee0 commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

Description

Adding two new expressions:

  • random() for generating a column of uniform random number.
  • uuid() for generating UUID (a column of unique strings)

random() accepts two arguments, related to reproducibility:

  • seed: a "base" random seed. Default is None.
  • reseed_after_execution: This is a flag that affects the random number generation across different iteration of iter_batches (i.e., across different epochs in the context of epochs). See also, [Train] [Data] Add inter-execution file shuffling to enable deterministic multi-epoch training #59528. It only matters when the seed is set. If False, each dataset execution will generate the same set of random number. If True, each dataset execution (i.e., training epoch) will give a new but reproducible set of random number.

uuid() takes no argument.

Related issues

Related to #55275

Related to #59662 as well. I did this PR first...so probably I could use the same RandomSeedConfig in that PR.

Additional information

[Random seed business] The actual seed sequence used to initialize the random number generator is [block_idx, task_idx, execution_idx, base_seed]. The block idx is tracked by a counter (like in monotonically_increasing_id()). The task and execution indices are coming from TaskContext and DataContext, respectively. The way I used execution_idx is slightly different to what FileShuffleConfig is doing, but I think all these random seed business could be consolidated in the future (e.g., random_sample)

[Independent visit functions] Following the discussion in #59290, there are separate visit_uuid() and visit_random for these expressions.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a random() expression, which is a valuable addition. The core implementation for generating random numbers is solid, especially the seeding strategy for distributed execution. I've identified a few areas for improvement, primarily in the parsing of string expressions for random() and its tree representation. My review includes suggestions to fix a bug in argument handling, improve code consistency, and correct a formatting issue. The new RandomExpr class and the public random() factory function are well-designed and documented.

@wingkitlee0
wingkitlee0 force-pushed the kit/random-expr branch 4 times, most recently from 6c3ed95 to e4f93d4 Compare December 25, 2025 01:28
@wingkitlee0

Copy link
Copy Markdown
Contributor Author

bugbot run

@wingkitlee0 wingkitlee0 added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Dec 26, 2025
@wingkitlee0
wingkitlee0 force-pushed the kit/random-expr branch 4 times, most recently from f857644 to 2789a75 Compare December 26, 2025 22:10
@wingkitlee0

Copy link
Copy Markdown
Contributor Author

bugbot run

1 similar comment
@wingkitlee0

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no bugs!

@wingkitlee0 wingkitlee0 changed the title [Data] Add random() expression [Data] Add random() and uuid() expression Dec 26, 2025
@wingkitlee0

wingkitlee0 commented Dec 27, 2025 •

Copy link
Copy Markdown
Contributor Author

It seems like I have the same TaskContext issue as another PR on monotoncially_increasing_id() .. (Resolved)

@wingkitlee0 wingkitlee0 added the community-contribution Contributed by the community label Dec 27, 2025
@wingkitlee0
wingkitlee0 marked this pull request as ready for review December 27, 2025 21:12
@wingkitlee0
wingkitlee0 requested a review from a team as a code owner December 27, 2025 21:12
Comment thread python/ray/data/expressions.py Resolved
Comment thread python/ray/data/expressions.py Resolved
@wingkitlee0
wingkitlee0 force-pushed the kit/random-expr branch 2 times, most recently from 94773c4 to 62736da Compare December 27, 2025 23:06
@wingkitlee0

Copy link
Copy Markdown
Contributor Author

@goutamvenkat-anyscale How do you think about this PR? Do you the framework here (NullaryExpr) can be used for the monotonically_increasing_id()? #59290
I am kind of interested to use all these...

Thanks!

Comment thread python/ray/data/expressions.py Resolved
Comment thread python/ray/data/expressions.py Resolved
Comment thread python/ray/data/expressions.py Resolved

@aslonnie aslonnie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approval for doc change

@wingkitlee0
wingkitlee0 force-pushed the kit/random-expr branch 2 times, most recently from dc3338a to f750938 Compare March 7, 2026 14:53
Comment thread python/ray/data/expressions.py Resolved
@wingkitlee0

Copy link
Copy Markdown
Contributor Author

@goutamvenkat-anyscale

Re

arr = pa.repeat(pa.scalar(uuid.UUID(int=0).bytes, type=pa.uuid()), num_rows) is an arrow native way of doing this

I think we want different UUID for each row (hence the original list-comp); so pa.repeat is not enough. Anyway, I have implemented a vectorized UUID generation using numpy (added test to confirm to the compatibility). I was thinking about pa.uuid() as well, but it's not easy to convert back to string and is not available in older pyarrow (v9, which Ray data still supports).

Otherwise I think I have addressed all the comments. Let me know if there are more or if this can be approved+merged. Thanks for the review.

@goutamvenkat-anyscale

Copy link
Copy Markdown
Contributor

@iamjustinhsu Need your approval for this PR

@wingkitlee0

Copy link
Copy Markdown
Contributor Author

@iamjustinhsu Can I get your approval? 🙏

@wingkitlee0
wingkitlee0 force-pushed the kit/random-expr branch 2 times, most recently from 978efd0 to bd20df3 Compare March 19, 2026 00:06

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Comment thread python/ray/data/expressions.py Resolved
@wingkitlee0
wingkitlee0 force-pushed the kit/random-expr branch 2 times, most recently from d0c568d to f6aa185 Compare March 19, 2026 12:41
- break down SyntheticExpr into RandomExpr and UUIDExpr

Signed-off-by: Kit Lee <[email protected]>
@richardliaw
richardliaw merged commit b47c0e7 into ray-project:master Mar 24, 2026
3 checks passed
ryanaoleary pushed a commit to ryanaoleary/ray that referenced this pull request Mar 25, 2026
## Description

Adding two new expressions:
* `random()` for generating a column of uniform random number.
* `uuid()` for generating UUID (a column of unique strings)

`random()` accepts two arguments, related to reproducibility:
* `seed`: a "base" random seed. Default is `None`.
* `reseed_after_execution`: This is a flag that affects the random
number generation across different iteration of `iter_batches` (i.e.,
across different epochs in the context of epochs). See also,
ray-project#59528. It only matters when the
`seed` is set. If False, each dataset execution will generate the same
set of random number. If True, each dataset execution (i.e., training
epoch) will give a new but reproducible set of random number.

`uuid()` takes no argument.

## Related issues
Related to ray-project#55275

Related to ray-project#59662 as well. I did this PR first...so probably I could use
the same `RandomSeedConfig` in that PR.

## Additional information

[Random seed business] The actual seed sequence used to initialize the
random number generator is `[block_idx, task_idx, execution_idx,
base_seed]`. The block idx is tracked by a counter (like in
`monotonically_increasing_id()`). The task and execution indices are
coming from `TaskContext` and `DataContext`, respectively. The way I
used `execution_idx` is slightly different to what `FileShuffleConfig`
is doing, but I think all these random seed business could be
consolidated in the future (e.g., random_sample)

[Independent visit functions] Following the discussion in
ray-project#59290, there are separate
`visit_uuid()` and `visit_random` for these expressions.

Signed-off-by: Kit Lee <[email protected]>
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
## Description

Adding two new expressions:
* `random()` for generating a column of uniform random number.
* `uuid()` for generating UUID (a column of unique strings)

`random()` accepts two arguments, related to reproducibility:
* `seed`: a "base" random seed. Default is `None`.
* `reseed_after_execution`: This is a flag that affects the random
number generation across different iteration of `iter_batches` (i.e.,
across different epochs in the context of epochs). See also,
ray-project#59528. It only matters when the
`seed` is set. If False, each dataset execution will generate the same
set of random number. If True, each dataset execution (i.e., training
epoch) will give a new but reproducible set of random number.

`uuid()` takes no argument.

## Related issues
Related to ray-project#55275

Related to ray-project#59662 as well. I did this PR first...so probably I could use
the same `RandomSeedConfig` in that PR.

## Additional information

[Random seed business] The actual seed sequence used to initialize the
random number generator is `[block_idx, task_idx, execution_idx,
base_seed]`. The block idx is tracked by a counter (like in
`monotonically_increasing_id()`). The task and execution indices are
coming from `TaskContext` and `DataContext`, respectively. The way I
used `execution_idx` is slightly different to what `FileShuffleConfig`
is doing, but I think all these random seed business could be
consolidated in the future (e.g., random_sample)

[Independent visit functions] Following the discussion in
ray-project#59290, there are separate
`visit_uuid()` and `visit_random` for these expressions.

Signed-off-by: Kit Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants