Skip to content
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

WIP: query profiling #1870

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 59 additions & 0 deletions dev/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,48 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
return this._get();
}

/**
* Performs the planning stage of this query, without actually executing the
* query. Returns a Promise that will be resolved with the result of the
* query planning information.
*
* Note: the information included in the output of this function is subject
* to change.
*
* @return A Promise that will be resolved with the results of the query
* planning information.
*/
plan(): Promise<Record<string, unknown>> {
return Promise.resolve({foo: 'bar'});
}

/**
* Plans and executes this query. Returns a Promise that will be resolved
* with the planning information, statistics from the query execution, and
* the query results.
*
* Note: the information included in the output of this function is subject
* to change.
*
* @return A Promise that will be resolved with the planning information,
* statistics from the query execution, and the query results.
*/
profile(): Promise<firestore.QueryProfileInfo<QuerySnapshot<T>>> {
const mock = {
plan: {foo: 'bar'},
stats: {cpu: '3ms'},
snapshot: new QuerySnapshot(
this,
new Timestamp(0, 0),
0,
() => [],
() => []
),
};

return Promise.resolve(mock);
}

/**
* Internal get() method that accepts an optional transaction id.
*
Expand Down Expand Up @@ -3277,6 +3319,23 @@ export class AggregateQuery<T extends firestore.AggregateSpec>
}
return deepEqual(this._aggregates, other._aggregates);
}

plan(): Promise<Record<string, unknown>> {
return Promise.resolve({foo: 'bar'});
}

profile(): Promise<firestore.QueryProfileInfo<AggregateQuerySnapshot<T>>> {
const mock = {
plan: {foo: 'bar'},
stats: {cpu: '3ms'},
snapshot: new AggregateQuerySnapshot(
this,
new Timestamp(0, 0),
this.decodeResult({})
),
};
return Promise.resolve(mock);
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions dev/system-test/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,40 @@
});
});

it.only('can plan a query', async () => {
const plan = await randomCol.where('a', '>', 10).plan();
console.log(plan);
});

it.only('can profile a query', async () => {
const profile = await randomCol.where('a', '>', 10).profile();
console.log(profile.plan);
console.log(profile.stats);
console.log(profile.snapshot.size);
});

it.only('can plan an aggregate query', async () => {
const plan = await randomCol.where('a', '>', 10).count().plan();
console.log(plan);
});

Check failure on line 146 in dev/system-test/firestore.ts

View workflow job for this annotation

GitHub Actions / lint

'it.only' is restricted from being used
it.only('can profile an aggregate query', async () => {
const profile = await randomCol.where('a', '>', 10).count().profile();
console.log(profile.plan);
console.log(profile.stats);
console.log(profile.snapshot.data());

Check failure on line 151 in dev/system-test/firestore.ts

View workflow job for this annotation

GitHub Actions / lint

'it.only' is restricted from being used
});

it('getAll() supports array destructuring', () => {
const ref1 = randomCol.doc('doc1');
const ref2 = randomCol.doc('doc2');
return Promise.all([ref1.set({foo: 'a'}), ref2.set({foo: 'a'})])
.then(() => {

Check failure on line 158 in dev/system-test/firestore.ts

View workflow job for this annotation

GitHub Actions / lint

'it.only' is restricted from being used
return firestore.getAll(...[ref1, ref2]);
})
.then(docs => {
expect(docs.length).to.equal(2);
});

Check failure on line 163 in dev/system-test/firestore.ts

View workflow job for this annotation

GitHub Actions / lint

'it.only' is restricted from being used
});

it('getAll() supports field mask', () => {
Expand Down
72 changes: 72 additions & 0 deletions types/firestore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,32 @@ declare namespace FirebaseFirestore {
*/
get(): Promise<QuerySnapshot<T>>;

/**
* Performs the planning stage of this query, without actually executing the
* query. Returns a Promise that will be resolved with the result of the
* query planning information.
*
* Note: the information included in the output of this function is subject
* to change.
*
* @return A Promise that will be resolved with the results of the query
* planning information.
*/
plan(): Promise<Record<string, unknown>>;

/**
* Plans and executes this query. Returns a Promise that will be resolved
* with the planning information, statistics from the query execution, and
* the query results.
*
* Note: the information included in the output of this function is subject
* to change.
*
* @return A Promise that will be resolved with the planning information,
* statistics from the query execution, and the query results.
*/
profile(): Promise<QueryProfileInfo<QuerySnapshot<T>>>;

/*
* Executes the query and returns the results as Node Stream.
*
Expand Down Expand Up @@ -2108,6 +2134,32 @@ declare namespace FirebaseFirestore {
*/
get(): Promise<AggregateQuerySnapshot<T>>;

/**
* Performs the planning stage of this query, without actually executing the
* query. Returns a Promise that will be resolved with the result of the
* query planning information.
*
* Note: the information included in the output of this function is subject
* to change.
*
* @return A Promise that will be resolved with the results of the query
* planning information.
*/
plan(): Promise<Record<string, unknown>>;

/**
* Plans and executes this query. Returns a Promise that will be resolved
* with the planning information, statistics from the query execution, and
* the query results.
*
* Note: the information included in the output of this function is subject
* to change.
*
* @return A Promise that will be resolved with the planning information,
* statistics from the query execution, and the query results.
*/
profile(): Promise<QueryProfileInfo<AggregateQuerySnapshot<T>>>;

/**
* Compares this object with the given object for equality.
*
Expand Down Expand Up @@ -2539,6 +2591,26 @@ declare namespace FirebaseFirestore {
*/
static and(...filters: Filter[]): Filter;
}

/** A QueryProfileInfo contains information about planning, execution, and results of a query. */
export interface QueryProfileInfo<T> {
/**
* A Map that contains information about the query plan.
* Contents are subject to change.
*/
readonly plan: Record<string, unknown>;

/**
* A Map that contains statistics about the execution of the query.
* Contents are subject to change.
*/
readonly stats: Record<string, unknown>;

/**
* The snapshot that contains the results of executing the query.
*/
readonly snapshot: T;
}
}

declare module '@google-cloud/firestore' {
Expand Down
Loading