@query-doctor/core 0.2.4 → 0.2.5

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
@@ -53,8 +53,7 @@ __export(index_exports, {
53
53
  ignoredIdentifier: () => ignoredIdentifier,
54
54
  isIndexProbablyDroppable: () => isIndexProbablyDroppable,
55
55
  isIndexSupported: () => isIndexSupported,
56
- parseNudges: () => parseNudges,
57
- permuteWithFeedback: () => permuteWithFeedback
56
+ parseNudges: () => parseNudges
58
57
  });
59
58
  module.exports = __toCommonJS(index_exports);
60
59
 
@@ -916,7 +915,9 @@ var _PgIdentifier = class _PgIdentifier {
916
915
  const identifierRegex = /^[a-z_][a-zA-Z0-9_]*$/;
917
916
  const match = identifier.match(/^"(.+)"$/);
918
917
  if (match) {
919
- return new _PgIdentifier(match[1], true);
918
+ const value = match[1];
919
+ const quoted2 = !identifierRegex.test(value) || this.reservedKeywords.has(value.toLowerCase());
920
+ return new _PgIdentifier(value, quoted2);
920
921
  }
921
922
  const quoted = !identifierRegex.test(identifier) || this.reservedKeywords.has(identifier.toLowerCase());
922
923
  return new _PgIdentifier(identifier, quoted);
@@ -1121,6 +1122,24 @@ var PgIdentifier = _PgIdentifier;
1121
1122
 
1122
1123
  // src/optimizer/genalgo.ts
1123
1124
  var import_colorette2 = require("colorette");
1125
+
1126
+ // src/sql/permutations.ts
1127
+ function permutationsWithDescendingLength(arr) {
1128
+ const collected = [];
1129
+ function collect(path, rest) {
1130
+ for (let i = 0; i < rest.length; i++) {
1131
+ const nextRest = [...rest.slice(0, i), ...rest.slice(i + 1)];
1132
+ const nextPath = [...path, rest[i]];
1133
+ collected.push(nextPath);
1134
+ collect(nextPath, nextRest);
1135
+ }
1136
+ }
1137
+ collect([], arr);
1138
+ collected.sort((a, b) => b.length - a.length);
1139
+ return collected;
1140
+ }
1141
+
1142
+ // src/optimizer/genalgo.ts
1124
1143
  var _IndexOptimizer = class _IndexOptimizer {
1125
1144
  constructor(db, statistics, existingIndexes, config = {}) {
1126
1145
  this.db = db;
@@ -1253,14 +1272,12 @@ var _IndexOptimizer = class _IndexOptimizer {
1253
1272
  * Derive the list of indexes [tableA(X, Y, Z), tableB(H, I, J)]
1254
1273
  **/
1255
1274
  indexesToCreate(rootCandidates) {
1256
- const permutedIndexes = this.tableColumnIndexCandidates(rootCandidates);
1275
+ const permutedIndexes = this.groupPotentialIndexColumnsByTable(rootCandidates);
1257
1276
  const nextStage = [];
1258
1277
  for (const permutation of permutedIndexes.values()) {
1259
1278
  const { table: rawTable, schema: rawSchema, columns } = permutation;
1260
- const permutations = permuteWithFeedback(columns);
1261
- let iter = permutations.next(PROCEED);
1262
- while (!iter.done) {
1263
- const columns2 = iter.value;
1279
+ const permutations = permutationsWithDescendingLength(columns);
1280
+ for (const columns2 of permutations) {
1264
1281
  const schema = PgIdentifier.fromString(rawSchema);
1265
1282
  const table = PgIdentifier.fromString(rawTable);
1266
1283
  const existingIndex = this.indexAlreadyExists(
@@ -1268,12 +1285,10 @@ var _IndexOptimizer = class _IndexOptimizer {
1268
1285
  columns2
1269
1286
  );
1270
1287
  if (existingIndex) {
1271
- iter = permutations.next(PROCEED);
1272
1288
  continue;
1273
1289
  }
1274
1290
  const indexName = this.indexName();
1275
1291
  const definition = this.toDefinition({ table, schema, columns: columns2 }).raw;
1276
- iter = permutations.next(PROCEED);
1277
1292
  nextStage.push({
1278
1293
  name: indexName,
1279
1294
  schema: schema.toString(),
@@ -1405,7 +1420,7 @@ var _IndexOptimizer = class _IndexOptimizer {
1405
1420
  }
1406
1421
  throw new Error("Unreachable");
1407
1422
  }
1408
- tableColumnIndexCandidates(indexes) {
1423
+ groupPotentialIndexColumnsByTable(indexes) {
1409
1424
  const tableColumns = /* @__PURE__ */ new Map();
1410
1425
  for (const index of indexes) {
1411
1426
  const existing = tableColumns.get(`${index.schema}.${index.table}`);
@@ -1475,21 +1490,6 @@ var RollbackError = class {
1475
1490
  };
1476
1491
  var PROCEED = Symbol("PROCEED");
1477
1492
  var SKIP = Symbol("SKIP");
1478
- function* permuteWithFeedback(arr) {
1479
- function* helper(path, rest) {
1480
- let i = 0;
1481
- while (i < rest.length) {
1482
- const nextPath = [...path, rest[i]];
1483
- const nextRest = [...rest.slice(0, i), ...rest.slice(i + 1)];
1484
- const input = yield nextPath;
1485
- if (input === PROCEED) {
1486
- yield* helper(nextPath, nextRest);
1487
- }
1488
- i++;
1489
- }
1490
- }
1491
- yield* helper([], arr);
1492
- }
1493
1493
 
1494
1494
  // src/optimizer/statistics.ts
1495
1495
  var import_colorette3 = require("colorette");
@@ -2206,7 +2206,6 @@ var PssRewriter = class {
2206
2206
  ignoredIdentifier,
2207
2207
  isIndexProbablyDroppable,
2208
2208
  isIndexSupported,
2209
- parseNudges,
2210
- permuteWithFeedback
2209
+ parseNudges
2211
2210
  });
2212
2211
  //# sourceMappingURL=index.cjs.map