Skip to content

feat(firestore): array constants and AggregateFunction serialization#14321

Open
bhshkh wants to merge 2 commits intogoogleapis:mainfrom
bhshkh:fix/fspq-minor-fixes
Open

feat(firestore): array constants and AggregateFunction serialization#14321
bhshkh wants to merge 2 commits intogoogleapis:mainfrom
bhshkh:fix/fspq-minor-fixes

Conversation

@bhshkh
Copy link
Copy Markdown
Contributor

@bhshkh bhshkh commented Apr 1, 2026

  1. Java and Node allow creating Constants from Slices. We need to support the same in Go. E.g. expr := firestore.ConstantOf([]string{"category1", "category2"})
  2. Properly delegate serialization to .toProto() when an AggregateFunction is evaluated within toProtoValue, solving issues when building larger pipeline components containing aggregators. E.g. : below code would fail without the serialization
		res, err = client.Pipeline().Collection(coll.ID).
			Select(Fields("title", "author", "rating")).
			RawStage("aggregate", 
				[]any{
					map[string]any{
						"averageRating": Average("rating"),
					},
					map[string]any{},
				}).
			Execute(ctx).Results().Next()
		if err != nil {
			t.Fatal(err)
		}

The above query currently, gets translated to below rpc request

        {
          "name": "aggregate",
          "args": [
            {
              "mapValue": {
                "fields": {
                  "averageRating": {
                    "mapValue": {}
                  }
                }
              }
            },
            {
              "mapValue": {}
            }
          ]
        }

and receives error "rpc error: code = InvalidArgument desc = Literals are only allowed in a scalar expression."

After fix:

        {
          "name":  "aggregate",
          "args":  [
            {
              "mapValue":  {
                "fields":  {
                  "averageRating":  {
                    "functionValue":  {
                      "name":  "average",
                      "args":  [
                        {
                          "fieldReferenceValue":  "rating"
                        }
                      ]
                    }
                  }
                }
              }
            },
            {
              "mapValue":  {}
            }
          ]
        }

@bhshkh bhshkh requested review from a team as code owners April 1, 2026 14:31
@product-auto-label product-auto-label bot added the api: firestore Issues related to the Firestore API. label Apr 1, 2026
@bhshkh bhshkh enabled auto-merge (squash) April 1, 2026 14:31
Copy link
Copy Markdown
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 support for slices and arrays in the ConstantOf function and implements handling for AggregateFunction types within toProtoValue. The review feedback identifies a potential panic in ConstantOf when processing nil values due to unsafe use of reflect.TypeOf and suggests optimizing redundant reflection calls in toProtoValue for better performance and readability.

Copy link
Copy Markdown
Contributor

@daniel-sanche daniel-sanche left a comment

Choose a reason for hiding this comment

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

LGTM

@bhshkh bhshkh disabled auto-merge April 1, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: firestore Issues related to the Firestore API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants