Skip to content

Fixed an issue with orderless fields in firestore indexes #8913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Fixed ext:export command so that it correctly returns system params in the .env file (#8881)
- Fixed an issue where the MCP server could not successfully use Application Default Credentials. (#8896)
- Fixed an issue where the incorrect API was enabled for `apptesting` commands.
- Fixed an issue where indexes with fields with no order would be handled incorrectly. (#8910)
5 changes: 4 additions & 1 deletion src/firestore/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Process indexes by filtering out implicit __name__ fields with ASCENDING order.
* Keeps explicit __name__ fields with DESCENDING order.
* @param indexes Array of indexes to process
* @returns Processed array of indexes with filtered fields

Check warning on line 25 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid JSDoc tag (preference). Replace "returns" JSDoc tag with "return"
*/
public static processIndexes(indexes: types.Index[]): types.Index[] {
return indexes.map((index: types.Index): types.Index => {
Expand All @@ -31,7 +31,10 @@
let fields = index.fields;
const lastField = index.fields?.[index.fields.length - 1];
if (lastField?.fieldPath === "__name__") {
const defaultDirection = index.fields?.[index.fields.length - 2]?.order;
const lastOrderedField = [...fields]
.reverse()
.find((f) => f.fieldPath !== "__name__" && f.order);
const defaultDirection = lastOrderedField?.order;
Comment on lines +34 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While this implementation is correct, it can be made more efficient by avoiding the creation of a new reversed array for each index. A simple for loop iterating backwards would be more performant and avoid unnecessary memory allocation, especially for indexes with many fields.

        let defaultDirection: types.Order | undefined;
        // Find the last field with an order, iterating backwards from the second to last field.
        for (let i = fields.length - 2; i >= 0; i--) {
          if (fields[i].order) {
            defaultDirection = fields[i].order;
            break;
          }
        }

if (lastField?.order === defaultDirection) {
fields = fields.slice(0, -1);
}
Expand All @@ -45,7 +48,7 @@

/**
* Deploy an index specification to the specified project.
* @param options the CLI options.

Check warning on line 51 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.force"

Check warning on line 51 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.nonInteractive"

Check warning on line 51 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.project"
* @param indexes an array of objects, each will be validated and then converted
* to an {@link Spec.Index}.
* @param fieldOverrides an array of objects, each will be validated and then
Expand All @@ -53,11 +56,11 @@
*/
async deploy(
options: { project: string; nonInteractive: boolean; force: boolean },
indexes: any[],

Check warning on line 59 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
fieldOverrides: any[],

Check warning on line 60 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
databaseId = "(default)",
): Promise<void> {
const spec = this.upgradeOldSpec({

Check warning on line 63 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
indexes,
fieldOverrides,
});
Expand All @@ -65,8 +68,8 @@
this.validateSpec(spec);

// Now that the spec is validated we can safely assert these types.
const indexesToDeploy: Spec.Index[] = spec.indexes;

Check warning on line 71 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .indexes on an `any` value

Check warning on line 71 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const fieldOverridesToDeploy: Spec.FieldOverride[] = spec.fieldOverrides;

Check warning on line 72 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

const existingIndexes: types.Index[] = await this.listIndexes(options.project, databaseId);
const existingFieldOverrides: types.Field[] = await this.listFieldOverrides(
Expand Down
27 changes: 27 additions & 0 deletions src/firestore/indexes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,33 @@ describe("IndexListingWithNameFields", () => {
expect(result[0].fields[0].fieldPath).to.equal("foo");
expect(result[0].fields[1].fieldPath).to.equal("bar");
});

it("should filter out __name__ fields when the previous field is a vector", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.ASCENDING },
{
fieldPath: "bar",
vectorConfig: {
dimension: 100,
flat: {},
},
},
{ fieldPath: "__name__", order: API.Order.ASCENDING },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

expect(result[0].fields).to.have.length(2);
expect(result[0].fields[0].fieldPath).to.equal("foo");
expect(result[0].fields[1].fieldPath).to.equal("bar");
});
});

describe("IndexSorting", () => {
Expand Down
Loading