Skip to content

feat: Add Rand and Trunc expressions#16037

Draft
Linchin wants to merge 3 commits intogoogleapis:mainfrom
Linchin:fs-rand-trunc
Draft

feat: Add Rand and Trunc expressions#16037
Linchin wants to merge 3 commits intogoogleapis:mainfrom
Linchin:fs-rand-trunc

Conversation

@Linchin
Copy link
Contributor

@Linchin Linchin commented Mar 5, 2026

No description provided.

@Linchin Linchin changed the title feat: Add Rand and Trunc expression feat: Add Rand and Trunc expressions Mar 5, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Firestore client library by introducing a new Rand expression. This addition provides users with the capability to incorporate pseudorandom number generation directly into their pipeline expressions, which can be useful for various data manipulation and testing scenarios. The change is accompanied by both unit and system tests to ensure its correctness and expected behavior.

Highlights

  • New Expression Added: Introduced the Rand expression to the Firestore client library, enabling the generation of pseudorandom floating-point numbers within pipeline expressions.
  • System Test Coverage: Added a new system test case in math.yaml to verify that the Rand expression produces values between 0.0 (inclusive) and 1.0 (exclusive).
  • Unit Test Coverage: Included a unit test for the Rand expression in test_pipeline_expressions.py to ensure its proper instantiation and attribute values.
Changelog
  • packages/google-cloud-firestore/google/cloud/firestore_v1/pipeline_expressions.py
    • Added Rand class, a new FunctionExpression for generating pseudorandom floats.
  • packages/google-cloud-firestore/tests/system/pipeline_e2e/math.yaml
    • Appended a system test case to validate the Rand expression's output range.
  • packages/google-cloud-firestore/tests/unit/v1/test_pipeline_expressions.py
    • Included a unit test for the Rand expression to confirm its basic functionality.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

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

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 adds a Rand expression for Firestore pipelines, along with corresponding unit and system tests. The implementation of the Rand expression itself looks good.

However, I found a logical flaw in the system test for Rand, where it uses two separate calls to Rand() to check if the value is within a range, which doesn't guarantee that a single random number is within that range. I've left a suggestion to improve the test.

Also, the pull request title mentions adding a Trunc expression, but it doesn't seem to be included in the changes. This might be intentional, but it's worth clarifying.

Note: Security Review did not run due to the size of the PR.

I am having trouble creating individual review comments. Click here to see my feedback.

packages/google-cloud-firestore/tests/system/pipeline_e2e/math.yaml (316-322)

high

This test is logically flawed because it calls Rand() twice within the And expression. This means two different random numbers will be generated, and the test is checking if rand1 >= 0.0 and rand2 < 1.0. This doesn't correctly verify that a single random number is within the [0.0, 1.0) range.

For example, if the first Rand() returned 2.0 and the second returned 0.5, the expression (2.0 >= 0.0) AND (0.5 < 1.0) would evaluate to true, even though 2.0 is outside the expected range.

A more robust test would use the result of a single Rand() call. Since the PR title mentions Trunc, I assume a Trunc expression is available or will be soon. You could use it to verify the range with a single Rand() call, which would be a much stronger guarantee of correctness.

            - FunctionExpression.equal:
                - FunctionExpression.trunc:
                    - Rand: []
                - Constant: 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant