Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,116 @@ describe('ExplainPlanAnalyzer.addIndexStrategyAdvisories', () => {
});
});

it('detects isBitmap under a shard branch in the winning plan', () => {
// Simulates a sharded cluster where the IXSCAN with isBitmap lives
// under a SHARD_MERGE > shards > FETCH > IXSCAN path.
// Before the fix, findStageInPlan() did not recurse into shards.
const analysis = makeAnalysis();
const explainResult: Document = {
queryPlanner: {
winningPlan: {
stage: 'SHARD_MERGE',
shards: [
{
stage: 'SINGLE_SHARD',
inputStage: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
indexName: 'bitmapIndex_1',
isBitmap: true,
},
},
},
],
},
},
executionStats: {
nReturned: 100,
executionTimeMillis: 13,
totalDocsExamined: 100,
totalKeysExamined: 100,
executionStages: {
stage: 'SHARD_MERGE',
shards: [
{
stage: 'SINGLE_SHARD',
inputStage: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
indexName: 'bitmapIndex_1',
indexUsage: [
{
scanKeys: [
'key 1: [(isInequality: false, estimatedEntryCount: 100)]',
],
},
],
},
},
},
],
},
},
};

ExplainPlanAnalyzer.addIndexStrategyAdvisories(analysis, 1000, explainResult);

const ids = getDiagnosticIds(analysis.performanceRating.diagnostics);
expect(ids).toContain('bitmap_index');
});

it('detects IXSCAN in planner shards even when exec stats use flat structure', () => {
// Edge case: planner is sharded, execution stats may not be
const analysis = makeAnalysis();
const explainResult: Document = {
queryPlanner: {
winningPlan: {
stage: 'SHARD_MERGE',
shards: [
{
stage: 'SINGLE_SHARD',
inputStage: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
indexName: 'bitmapIndex_1',
isBitmap: true,
},
},
},
],
},
},
executionStats: {
nReturned: 100,
executionTimeMillis: 13,
totalDocsExamined: 100,
totalKeysExamined: 100,
executionStages: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
indexName: 'bitmapIndex_1',
indexUsage: [
{
scanKeys: [
'key 1: [(isInequality: false, estimatedEntryCount: 100)]',
],
},
],
},
},
},
};

ExplainPlanAnalyzer.addIndexStrategyAdvisories(analysis, 1000, explainResult);

const ids = getDiagnosticIds(analysis.performanceRating.diagnostics);
expect(ids).toContain('bitmap_index');
});

describe('cumulative advisory demotions', () => {
it('demotes score twice when both bitmap and severe multikey thresholds are met', () => {
// Single-field bitmap with 50% coverage + 25× multikey expansion
Expand Down
10 changes: 10 additions & 0 deletions src/documentdb/queryInsights/ExplainPlanAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,16 @@ export class ExplainPlanAnalyzer {
}
}

// Traverse shard branches (sharded clusters)
if (plan.shards && Array.isArray(plan.shards)) {
for (const shard of plan.shards) {
const found = this.findStageInPlan(shard as Document, stageName);
if (found) {
return found;
}
}
}

return undefined;
}

Expand Down
6 changes: 6 additions & 0 deletions src/documentdb/queryInsights/StagePropertyExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class StagePropertyExtractor {

/**
* Collects all stages from the queryPlanner winning plan tree into a flat array.
* Traverses single inputStage, multiple inputStages, and shard branches (sharded clusters).
*/
private static collectPlannerStages(stage: Document, accumulator: Document[]): void {
if (!stage || !stage.stage) {
Expand All @@ -74,6 +75,11 @@ export class StagePropertyExtractor {
this.collectPlannerStages(child, accumulator);
});
}
if (stage.shards && Array.isArray(stage.shards)) {
for (const shard of stage.shards) {
this.collectPlannerStages(shard as Document, accumulator);
}
}
}

/**
Expand Down