@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/server.js CHANGED
@@ -13452,6 +13452,45 @@ function validateCaseSubExpr(ctx, scope, e) {
13452
13452
  return;
13453
13453
  }
13454
13454
  }
13455
+ function caseBranchType(ctx, scope, e) {
13456
+ switch (e.type) {
13457
+ case "null_lit":
13458
+ case "string_lit":
13459
+ return UNKNOWN;
13460
+ case "number_lit":
13461
+ return numberLitValue(e.raw).t;
13462
+ case "bool_lit":
13463
+ return "bool";
13464
+ case "colref":
13465
+ return scope.lookupColumn(e.parts)?.t ?? null;
13466
+ case "cast":
13467
+ return resolveTypeName(ctx.state, e.target).column.id;
13468
+ case "collate":
13469
+ return caseBranchType(ctx, scope, e.expr);
13470
+ case "binop": {
13471
+ const left = caseBranchType(ctx, scope, e.left);
13472
+ if (left !== "json" && left !== "jsonb") return null;
13473
+ if (e.op === "->" || e.op === "#>") return left;
13474
+ if (e.op === "->>" || e.op === "#>>") return "text";
13475
+ return null;
13476
+ }
13477
+ case "case": {
13478
+ let type = UNKNOWN;
13479
+ const branches = [...e.whens.map((w) => w.then), ...e.elseExpr ? [e.elseExpr] : []];
13480
+ for (const branch of branches) {
13481
+ const next = caseBranchType(ctx, scope, branch);
13482
+ if (next === null) return null;
13483
+ if (next === UNKNOWN) continue;
13484
+ const unified = unifyTypes(type, next);
13485
+ if (unified === null) return null;
13486
+ type = unified;
13487
+ }
13488
+ return type;
13489
+ }
13490
+ default:
13491
+ return null;
13492
+ }
13493
+ }
13455
13494
  function evalCase(ctx, scope, e) {
13456
13495
  validateSearchedCaseTree(ctx, scope, e);
13457
13496
  let chosen = null;
@@ -13475,8 +13514,10 @@ function evalCase(ctx, scope, e) {
13475
13514
  }
13476
13515
  }
13477
13516
  const resultExpr = chosen ?? e.elseExpr;
13478
- if (resultExpr === null) return tv(UNKNOWN, null);
13479
- return evalExpr(ctx, scope, resultExpr);
13517
+ const result = resultExpr === null ? tv(UNKNOWN, null) : evalExpr(ctx, scope, resultExpr);
13518
+ if (result.t !== UNKNOWN) return result;
13519
+ const type = caseBranchType(ctx, scope, e);
13520
+ return type !== null && type !== UNKNOWN ? castTo(ctx, result, type, {}) : result;
13480
13521
  }
13481
13522
  function evalIn(ctx, scope, e) {
13482
13523
  const left = evalExpr(ctx, scope, e.left);