Skip to content

feat(firestore): add rand and trunc expressions#9498

Merged
yvonnep165 merged 4 commits intomainfrom
shiqingp/arithmetic-expressions
Feb 23, 2026
Merged

feat(firestore): add rand and trunc expressions#9498
yvonnep165 merged 4 commits intomainfrom
shiqingp/arithmetic-expressions

Conversation

@yvonnep165
Copy link
Contributor

Add new arithmetic expressions rand and trunc and integration tests for the two expressions

@yvonnep165 yvonnep165 requested review from a team as code owners February 17, 2026 17:13
@changeset-bot
Copy link

changeset-bot bot commented Feb 17, 2026

🦋 Changeset detected

Latest commit: 767925b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
firebase Minor
@firebase/firestore Minor
@firebase/firestore-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yvonnep165, 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 significantly enhances Firestore's pipeline capabilities by introducing two new arithmetic expressions: rand and trunc. The rand expression provides a way to generate random numbers, while the trunc expression offers precise control over numeric truncation. These additions expand the functional programming toolkit available to developers, enabling more complex data manipulation directly within Firestore queries.

Highlights

  • New rand Expression: Introduced a new rand() expression that generates a random number between 0.0 (inclusive) and 1.0 (exclusive) within Firestore pipelines.
  • New trunc Expression: Added a new trunc() expression that allows truncating numeric values to an integer or to a specified number of decimal places. This expression supports various overloads for flexibility.
  • API and Documentation Updates: Updated the API review files and developer documentation to reflect the new rand and trunc expressions, including their signatures and usage examples.
  • Integration and Unit Tests: Included comprehensive integration and unit tests to ensure the correct functionality and behavior of both the rand and trunc expressions across different scenarios.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • .changeset/orange-bags-jog.md
    • Added a new changeset entry for the rand and trunc pipeline expressions.
  • common/api-review/firestore-lite-pipelines.api.md
    • Updated the API review file to include new trunc method overloads for Expression.
    • Added rand and trunc function exports to the API review.
  • common/api-review/firestore-pipelines.api.md
    • Updated the API review file to include new trunc method overloads for Expression.
    • Added rand and trunc function exports to the API review.
  • docs-devsite/firestore_lite_pipelines.expression.md
    • Documented the new trunc expression with its various signatures and examples.
  • docs-devsite/firestore_lite_pipelines.md
    • Documented the new rand and trunc functions with their descriptions and examples.
  • docs-devsite/firestore_pipelines.expression.md
    • Documented the new trunc expression with its various signatures and examples.
  • docs-devsite/firestore_pipelines.md
    • Documented the new rand and trunc functions with their descriptions and examples.
  • packages/firestore/lite/pipelines/pipelines.ts
    • Exported the newly added rand and trunc functions.
  • packages/firestore/src/api_pipelines.ts
    • Exported the newly added rand and trunc functions.
  • packages/firestore/src/lite-api/expressions.ts
    • Implemented the trunc method with multiple overloads for the Expression class.
    • Added the rand and trunc top-level functions.
  • packages/firestore/test/integration/api/pipeline.test.ts
    • Added integration tests for the rand expression.
    • Added integration tests for the trunc expression, covering various precision scenarios.
  • packages/firestore/test/lite/pipeline.test.ts
    • Added unit tests for the rand expression.
    • Added unit tests for the trunc expression, covering various precision scenarios.
Activity
  • The pull request introduces new rand and trunc arithmetic expressions.
  • API definitions for both firestore-lite-pipelines and firestore-pipelines were updated to include the new expressions.
  • Developer documentation on docs-devsite was expanded to detail the usage and examples of rand and trunc.
  • Core pipeline implementation files in packages/firestore were modified to export and define the new functions.
  • Comprehensive integration and unit tests were added to validate the functionality of rand and trunc.
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 introduces rand and trunc arithmetic expressions to Firestore pipelines, complete with implementations, comprehensive tests, and documentation updates. The code is well-structured and follows existing patterns. My main feedback relates to improving the clarity of the generated documentation by consolidating JSDoc comments for overloaded functions, for which I've provided specific suggestions.

Comment on lines +2194 to +2226
trunc(): FunctionExpression;

/**
* @beta
* Creates an expression that truncates a numeric value to the specified number of decimal places.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field to two decimal places.
* field("rating").trunc(2);
* ```
*
* @param decimalPlaces - A constant specifying the truncation precision in decimal places.
*
* @returns A new `Expression` representing the truncated value.
*/
trunc(decimalPlaces: number): FunctionExpression;

/**
* @beta
* Creates an expression that truncates a numeric value to the specified number of decimal places.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field to two decimal places.
* field("rating").trunc(constant(2));
* ```
*
* @param decimalPlaces - An expression specifying the truncation precision in decimal places.
*
* @returns A new `Expression` representing the truncated value.
*/
trunc(decimalPlaces: Expression): FunctionExpression;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The documentation for the trunc method overloads is split across multiple JSDoc blocks, leading to verbose and somewhat redundant generated documentation. To improve clarity for developers using the API, I recommend consolidating the documentation for the overloads that accept decimalPlaces into a single JSDoc block.

For example:

  /**
   * @beta
   * Creates an expression that truncates a numeric value to the specified number of decimal places.
   *
   * @example
   * ```typescript
   * // Truncate the value of the 'rating' field to two decimal places.
   * field("rating").trunc(2);
   *
   * // Truncate the value of the 'rating' field to the number of decimal places
   * // specified in the 'precision' field.
   * field("rating").trunc(field("precision"));
   * ```
   *
   * @param decimalPlaces - A constant or expression specifying the truncation precision in decimal places.
   *
   * @returns A new `Expression` representing the truncated value.
   */
  trunc(decimalPlaces: number | Expression): FunctionExpression;

This would require adjusting the function signatures to something like the following, which would improve the generated documentation for trunc and could be a good pattern to apply to other methods like round in the future.

  trunc(): FunctionExpression;
  trunc(decimalPlaces: number | Expression): FunctionExpression;
  trunc(decimalPlaces?: number | Expression): FunctionExpression { /* ... */ }

Copy link
Contributor

Choose a reason for hiding this comment

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

You can ignore this comment. This is a pattern done in the other expressions.

Comment on lines +7971 to +8037
/**
* @beta
* Creates an expression that truncates the numeric value of a field to an integer.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field
* trunc("rating");
* ```
*
* @param fieldName - The name of the field containing the number to truncate.
* @returns A new `Expression` representing the truncated value.
*/
export function trunc(fieldName: string): FunctionExpression;

/**
* @beta
* Creates an expression that truncates the numeric value of an expression to an integer.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field.
* trunc(field("rating"));
* ```
*
* @param expression - An expression evaluating to a numeric value, which will be truncated.
* @returns A new `Expression` representing the truncated value.
*/
export function trunc(expression: Expression): FunctionExpression;

/**
* @beta
* Creates an expression that truncates a numeric expression to the specified number of decimal places.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field to two decimal places.
* trunc("rating", 2);
* ```
*
* @param fieldName - The name of the field to truncate.
* @param decimalPlaces - A constant or expression specifying the truncation precision in decimal places.
* @returns A new `Expression` representing the truncated value.
*/
export function trunc(
fieldName: string,
decimalPlaces: number | Expression
): FunctionExpression;

/**
* @beta
* Creates an expression that truncates a numeric value to the specified number of decimal places.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field to two decimal places.
* trunc(field("rating"), constant(2));
* ```
*
* @param expression - An expression evaluating to a numeric value, which will be truncated.
* @param decimalPlaces - A constant or expression specifying the truncation precision in decimal places.
* @returns A new `Expression` representing the truncated value.
*/
export function trunc(
expression: Expression,
decimalPlaces: number | Expression
): FunctionExpression;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to my comment on Expression.trunc(), the top-level trunc() function has multiple overloads with duplicated JSDoc comments, which makes the generated documentation verbose. Consolidating these would improve readability.

Here's a suggestion for how you could combine them:

/**
 * @beta
 * Creates an expression that truncates a numeric value to an integer.
 *
 * @example
 * ```typescript
 * // Truncate the value of the 'rating' field.
 * trunc("rating");
 * trunc(field("rating"));
 * ```
 *
 * @param expression - A field name or an expression evaluating to a numeric value.
 * @returns A new `Expression` representing the truncated value.
 */
export function trunc(expression: string | Expression): FunctionExpression;

/**
 * @beta
 * Creates an expression that truncates a numeric value to the specified number of decimal places.
 *
 * @example
 * ```typescript
 * // Truncate the value of the 'rating' field to two decimal places.
 * trunc("rating", 2);
 * trunc(field("rating"), constant(2));
 * ```
 *
 * @param expression - A field name or an expression evaluating to a numeric value.
 * @param decimalPlaces - A constant or expression specifying the truncation precision.
 * @returns A new `Expression` representing the truncated value.
 */
export function trunc(
  expression: string | Expression,
  decimalPlaces: number | Expression
): FunctionExpression;

This would produce a much cleaner and more user-friendly documentation page.

@yvonnep165 yvonnep165 requested a review from dlarocque February 17, 2026 17:19
Comment on lines +2194 to +2226
trunc(): FunctionExpression;

/**
* @beta
* Creates an expression that truncates a numeric value to the specified number of decimal places.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field to two decimal places.
* field("rating").trunc(2);
* ```
*
* @param decimalPlaces - A constant specifying the truncation precision in decimal places.
*
* @returns A new `Expression` representing the truncated value.
*/
trunc(decimalPlaces: number): FunctionExpression;

/**
* @beta
* Creates an expression that truncates a numeric value to the specified number of decimal places.
*
* @example
* ```typescript
* // Truncate the value of the 'rating' field to two decimal places.
* field("rating").trunc(constant(2));
* ```
*
* @param decimalPlaces - An expression specifying the truncation precision in decimal places.
*
* @returns A new `Expression` representing the truncated value.
*/
trunc(decimalPlaces: Expression): FunctionExpression;
Copy link
Contributor

Choose a reason for hiding this comment

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

You can ignore this comment. This is a pattern done in the other expressions.

yvonnep165 and others added 3 commits February 17, 2026 16:03
Co-authored-by: Daniel La Rocque <dlarocque@google.com>
Co-authored-by: Daniel La Rocque <dlarocque@google.com>
Co-authored-by: Daniel La Rocque <dlarocque@google.com>
@yvonnep165 yvonnep165 merged commit 852162b into main Feb 23, 2026
46 of 48 checks passed
@yvonnep165 yvonnep165 deleted the shiqingp/arithmetic-expressions branch February 23, 2026 15:57
@google-oss-bot google-oss-bot mentioned this pull request Feb 24, 2026
@yvonnep165
Copy link
Contributor Author

Discussed offline. Since rand() takes 0 arguments and produces a value from "nothing" it does not transform an existing expression (for example, field("rating").rand() is confusing and meaningless. So it will only exist as a standalone function and not in the Expression class.

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.

4 participants