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

Update possiblePlans() to use BigInt #3128

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/smart-needles-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/query-planner": patch
---

Switched plan count from `Number` to a `BigInt`.
14 changes: 7 additions & 7 deletions query-planner-js/src/buildPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ class QueryPlanningTraversal<RV extends Vertex> {
const maxPlansToCompute = this.parameters.config.debug.maxEvaluatedPlans;
while (planCount > maxPlansToCompute && firstBranch.length > 1) {
// we remove the right-most option of the first branch, and them move that branch to it's new place.
const prevSize = firstBranch.length;
const prevSize = BigInt(firstBranch.length);
firstBranch.pop();
planCount -= planCount / prevSize;
this.reorderFirstBranch();
Expand All @@ -678,7 +678,7 @@ class QueryPlanningTraversal<RV extends Vertex> {
// Note that if `!this.isTopLevel`, then this means we're resolving a sub-plan for an edge condition, and we
// don't want to count those as "evaluated plans".
if (this.parameters.statistics && this.isTopLevel) {
this.parameters.statistics.evaluatedPlanCount += planCount;
this.parameters.statistics.evaluatedPlanCount += Number(planCount);
}

debug.log(() => `All branches:${this.closedBranches.map((opts, i) => `\n${i}:${opts.map((opt => `\n - ${closedPathToString(opt)}`))}`)}`);
Expand Down Expand Up @@ -3596,16 +3596,16 @@ function mapOptionsToSelections<RV extends Vertex>(
return selectionSet.selectionsInReverseOrder().map(node => [node, options]);
}

function possiblePlans(closedBranches: ClosedBranch<any>[]): number {
let totalCombinations = 1;
function possiblePlans(closedBranches: ClosedBranch<any>[]): bigint {
let totalCombinations = BigInt(1);
for (let i = 0; i < closedBranches.length; ++i){
const eltSize = closedBranches[i].length;
if (eltSize === 0) {
const eltSize = BigInt(closedBranches[i].length);
if (eltSize === BigInt(0)) {
// This would correspond to not being to find *any* path for a particular queried field, which means we have no plan
// for the overall query. Now, this shouldn't happen in practice if composition validation has been run successfully
// (and is not buggy), since the goal of composition validation is exactly to ensure we can never run into this path.
// In any case, we will throw later if that happens, but let's just return the proper result here, which is no plan at all.
return 0;
return BigInt(0);
}
totalCombinations *= eltSize;
}
Expand Down