@query-doctor/core 0.2.0 → 0.2.2

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.js CHANGED
@@ -879,7 +879,13 @@ var _PgIdentifier = class _PgIdentifier {
879
879
  */
880
880
  static fromParts(...identifiers) {
881
881
  return new _PgIdentifier(
882
- identifiers.map((identifier) => _PgIdentifier.fromString(identifier)).join("."),
882
+ identifiers.map((identifier) => {
883
+ if (typeof identifier === "string") {
884
+ return _PgIdentifier.fromString(identifier);
885
+ } else {
886
+ return identifier;
887
+ }
888
+ }).join("."),
883
889
  false
884
890
  );
885
891
  }
@@ -1092,7 +1098,7 @@ var _IndexOptimizer = class _IndexOptimizer {
1092
1098
  }
1093
1099
  for (const permutation of toCreate) {
1094
1100
  const createIndex = PostgresQueryBuilder.createIndex(
1095
- this.toDefinition(permutation).raw,
1101
+ permutation.definition,
1096
1102
  permutation.name
1097
1103
  ).introspect().build();
1098
1104
  await tx.exec(createIndex);
@@ -1116,13 +1122,17 @@ var _IndexOptimizer = class _IndexOptimizer {
1116
1122
  }
1117
1123
  const baseIndexes = this.findUsedIndexes(baseExplain.Plan);
1118
1124
  const finalIndexes = this.findUsedIndexes(finalExplain.Plan);
1125
+ const triedIndexes = new Map(
1126
+ toCreate.map((index) => [index.name.toString(), index])
1127
+ );
1128
+ this.replaceUsedIndexesWithDefinition(finalExplain.Plan, triedIndexes);
1119
1129
  return {
1120
1130
  kind: "ok",
1121
1131
  baseCost,
1122
1132
  finalCost,
1123
1133
  newIndexes: finalIndexes.newIndexes,
1124
1134
  existingIndexes: baseIndexes.existingIndexes,
1125
- triedIndexes: new Map(toCreate.map((index) => [index.name, index])),
1135
+ triedIndexes,
1126
1136
  baseExplainPlan: baseExplain.Plan,
1127
1137
  explainPlan: finalExplain.Plan
1128
1138
  };
@@ -1156,7 +1166,8 @@ var _IndexOptimizer = class _IndexOptimizer {
1156
1166
  * overflow that limit.
1157
1167
  */
1158
1168
  indexName() {
1159
- return _IndexOptimizer.prefix + Math.random().toString(36).substring(2, 16);
1169
+ const indexName = _IndexOptimizer.prefix + Math.random().toString(36).substring(2, 16);
1170
+ return PgIdentifier.fromString(indexName);
1160
1171
  }
1161
1172
  // TODO: this doesn't belong in the optimizer
1162
1173
  indexAlreadyExists(table, columns) {
@@ -1187,7 +1198,7 @@ var _IndexOptimizer = class _IndexOptimizer {
1187
1198
  continue;
1188
1199
  }
1189
1200
  const indexName = this.indexName();
1190
- const definition = this.toDefinition(permutation).raw;
1201
+ const definition = this.toDefinition({ table, schema, columns: columns2 }).raw;
1191
1202
  iter = permutations.next(PROCEED);
1192
1203
  nextStage.push({
1193
1204
  name: indexName,
@@ -1200,18 +1211,19 @@ var _IndexOptimizer = class _IndexOptimizer {
1200
1211
  }
1201
1212
  return nextStage;
1202
1213
  }
1203
- toDefinition(permuted) {
1214
+ toDefinition({
1215
+ schema,
1216
+ table,
1217
+ columns
1218
+ }) {
1204
1219
  const make = (col, order, where, keyword) => {
1205
1220
  let fullyQualifiedTable;
1206
- if (permuted.schema.toString() === "public") {
1207
- fullyQualifiedTable = PgIdentifier.fromString(permuted.table);
1221
+ if (schema.toString() === "public") {
1222
+ fullyQualifiedTable = table;
1208
1223
  } else {
1209
- fullyQualifiedTable = PgIdentifier.fromParts(
1210
- permuted.schema,
1211
- permuted.table
1212
- );
1224
+ fullyQualifiedTable = PgIdentifier.fromParts(schema, table);
1213
1225
  }
1214
- const baseColumn = `${fullyQualifiedTable}(${permuted.columns.map((c) => {
1226
+ const baseColumn = `${fullyQualifiedTable}(${columns.map((c) => {
1215
1227
  const column = PgIdentifier.fromString(c.column);
1216
1228
  const direction = c.sort && this.sortDirection(c.sort);
1217
1229
  const nulls = c.sort && this.nullsOrder(c.sort);
@@ -1339,8 +1351,8 @@ var _IndexOptimizer = class _IndexOptimizer {
1339
1351
  const newIndexes = /* @__PURE__ */ new Set();
1340
1352
  const existingIndexes = /* @__PURE__ */ new Set();
1341
1353
  const prefix = _IndexOptimizer.prefix;
1342
- function go(plan) {
1343
- const indexName = plan["Index Name"];
1354
+ walkExplain(explain, (stage) => {
1355
+ const indexName = stage["Index Name"];
1344
1356
  if (indexName) {
1345
1357
  if (indexName.startsWith(prefix)) {
1346
1358
  newIndexes.add(indexName);
@@ -1351,21 +1363,36 @@ var _IndexOptimizer = class _IndexOptimizer {
1351
1363
  existingIndexes.add(indexName);
1352
1364
  }
1353
1365
  }
1354
- if (plan.Plans) {
1355
- for (const p of plan.Plans) {
1356
- go(p);
1357
- }
1358
- }
1359
- }
1360
- go(explain);
1366
+ });
1361
1367
  return {
1362
1368
  newIndexes,
1363
1369
  existingIndexes
1364
1370
  };
1365
1371
  }
1372
+ replaceUsedIndexesWithDefinition(explain, triedIndexes) {
1373
+ walkExplain(explain, (stage) => {
1374
+ const indexName = stage["Index Name"];
1375
+ if (indexName) {
1376
+ const recommendation = triedIndexes.get(indexName);
1377
+ if (recommendation) {
1378
+ stage["Index Name"] = recommendation.definition;
1379
+ }
1380
+ }
1381
+ });
1382
+ }
1366
1383
  };
1367
1384
  __publicField(_IndexOptimizer, "prefix", "__qd_");
1368
1385
  var IndexOptimizer = _IndexOptimizer;
1386
+ function walkExplain(explain, f) {
1387
+ function go(plan) {
1388
+ f(plan);
1389
+ if (plan.Plans) {
1390
+ for (const p of plan.Plans) {
1391
+ go(p);
1392
+ }
1393
+ }
1394
+ }
1395
+ }
1369
1396
  var RollbackError = class {
1370
1397
  constructor(value) {
1371
1398
  this.value = value;