@crvouga/mockingbird-service-postgres 3.9.0 → 3.10.0

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/unstable.js CHANGED
@@ -13180,6 +13180,45 @@ function validateCaseSubExpr(ctx, scope, e) {
13180
13180
  return;
13181
13181
  }
13182
13182
  }
13183
+ function caseBranchType(ctx, scope, e) {
13184
+ switch (e.type) {
13185
+ case "null_lit":
13186
+ case "string_lit":
13187
+ return UNKNOWN;
13188
+ case "number_lit":
13189
+ return numberLitValue(e.raw).t;
13190
+ case "bool_lit":
13191
+ return "bool";
13192
+ case "colref":
13193
+ return scope.lookupColumn(e.parts)?.t ?? null;
13194
+ case "cast":
13195
+ return resolveTypeName(ctx.state, e.target).column.id;
13196
+ case "collate":
13197
+ return caseBranchType(ctx, scope, e.expr);
13198
+ case "binop": {
13199
+ const left = caseBranchType(ctx, scope, e.left);
13200
+ if (left !== "json" && left !== "jsonb") return null;
13201
+ if (e.op === "->" || e.op === "#>") return left;
13202
+ if (e.op === "->>" || e.op === "#>>") return "text";
13203
+ return null;
13204
+ }
13205
+ case "case": {
13206
+ let type = UNKNOWN;
13207
+ const branches = [...e.whens.map((w) => w.then), ...e.elseExpr ? [e.elseExpr] : []];
13208
+ for (const branch of branches) {
13209
+ const next = caseBranchType(ctx, scope, branch);
13210
+ if (next === null) return null;
13211
+ if (next === UNKNOWN) continue;
13212
+ const unified = unifyTypes(type, next);
13213
+ if (unified === null) return null;
13214
+ type = unified;
13215
+ }
13216
+ return type;
13217
+ }
13218
+ default:
13219
+ return null;
13220
+ }
13221
+ }
13183
13222
  function evalCase(ctx, scope, e) {
13184
13223
  validateSearchedCaseTree(ctx, scope, e);
13185
13224
  let chosen = null;
@@ -13203,8 +13242,10 @@ function evalCase(ctx, scope, e) {
13203
13242
  }
13204
13243
  }
13205
13244
  const resultExpr = chosen ?? e.elseExpr;
13206
- if (resultExpr === null) return tv(UNKNOWN, null);
13207
- return evalExpr(ctx, scope, resultExpr);
13245
+ const result = resultExpr === null ? tv(UNKNOWN, null) : evalExpr(ctx, scope, resultExpr);
13246
+ if (result.t !== UNKNOWN) return result;
13247
+ const type = caseBranchType(ctx, scope, e);
13248
+ return type !== null && type !== UNKNOWN ? castTo(ctx, result, type, {}) : result;
13208
13249
  }
13209
13250
  function evalIn(ctx, scope, e) {
13210
13251
  const left = evalExpr(ctx, scope, e.left);