@query-doctor/core 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1172,17 +1172,17 @@ var _IndexOptimizer = class _IndexOptimizer {
1172
1172
  }
1173
1173
  const baseIndexes = this.findUsedIndexes(baseExplain.Plan);
1174
1174
  const finalIndexes = this.findUsedIndexes(finalExplain.Plan);
1175
+ const triedIndexes = new Map(
1176
+ toCreate.map((index) => [index.name.toString(), index])
1177
+ );
1178
+ this.replaceUsedIndexesWithDefinition(finalExplain.Plan, triedIndexes);
1175
1179
  return {
1176
1180
  kind: "ok",
1177
1181
  baseCost,
1178
1182
  finalCost,
1179
1183
  newIndexes: finalIndexes.newIndexes,
1180
1184
  existingIndexes: baseIndexes.existingIndexes,
1181
- triedIndexes: new Map(
1182
- // TODO: don't prematurely stringify PgIdentifier here
1183
- // (requires more work)
1184
- toCreate.map((index) => [index.name.toString(), index])
1185
- ),
1185
+ triedIndexes,
1186
1186
  baseExplainPlan: baseExplain.Plan,
1187
1187
  explainPlan: finalExplain.Plan
1188
1188
  };
@@ -1401,8 +1401,8 @@ var _IndexOptimizer = class _IndexOptimizer {
1401
1401
  const newIndexes = /* @__PURE__ */ new Set();
1402
1402
  const existingIndexes = /* @__PURE__ */ new Set();
1403
1403
  const prefix = _IndexOptimizer.prefix;
1404
- function go(plan) {
1405
- const indexName = plan["Index Name"];
1404
+ walkExplain(explain, (stage) => {
1405
+ const indexName = stage["Index Name"];
1406
1406
  if (indexName) {
1407
1407
  if (indexName.startsWith(prefix)) {
1408
1408
  newIndexes.add(indexName);
@@ -1413,21 +1413,37 @@ var _IndexOptimizer = class _IndexOptimizer {
1413
1413
  existingIndexes.add(indexName);
1414
1414
  }
1415
1415
  }
1416
- if (plan.Plans) {
1417
- for (const p of plan.Plans) {
1418
- go(p);
1419
- }
1420
- }
1421
- }
1422
- go(explain);
1416
+ });
1423
1417
  return {
1424
1418
  newIndexes,
1425
1419
  existingIndexes
1426
1420
  };
1427
1421
  }
1422
+ replaceUsedIndexesWithDefinition(explain, triedIndexes) {
1423
+ walkExplain(explain, (stage) => {
1424
+ const indexName = stage["Index Name"];
1425
+ if (typeof indexName === "string") {
1426
+ const recommendation = triedIndexes.get(indexName);
1427
+ if (recommendation) {
1428
+ stage["Index Name"] = recommendation.definition;
1429
+ }
1430
+ }
1431
+ });
1432
+ }
1428
1433
  };
1429
1434
  __publicField(_IndexOptimizer, "prefix", "__qd_");
1430
1435
  var IndexOptimizer = _IndexOptimizer;
1436
+ function walkExplain(explain, f) {
1437
+ function go(plan) {
1438
+ f(plan);
1439
+ if (plan.Plans) {
1440
+ for (const p of plan.Plans) {
1441
+ go(p);
1442
+ }
1443
+ }
1444
+ }
1445
+ go(explain);
1446
+ }
1431
1447
  var RollbackError = class {
1432
1448
  constructor(value) {
1433
1449
  this.value = value;