@dbsp/adapter-pgsql 7.1.2 → 7.2.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.
@@ -12737,6 +12737,20 @@ function expressionQualifiedColumnRef(column2, qualifier, ctx) {
12737
12737
  const binding = expressionRelationBinding(qualifier, ctx);
12738
12738
  return expressionColumnRef(column2, ctx, binding);
12739
12739
  }
12740
+ function expressionUserQualifiedColumnRef(column2, qualifier, ctx) {
12741
+ if (relationBindingFor(ctx.scope, queryLocal(qualifier)) === void 0 && qualifier !== ctx.rootTable) {
12742
+ const sameTable = [...ctx.scope?.bindings.values() ?? []].filter(
12743
+ (binding) => binding.kind === "declared-table" && binding.logicalTable === qualifier
12744
+ );
12745
+ if (sameTable.length > 1) {
12746
+ const aliases = sameTable.map((binding) => `'${identifierText(binding.qualifier)}'`).sort().join(", ");
12747
+ throw new Error(
12748
+ `Qualifier '${qualifier}' is ambiguous between ${aliases}.`
12749
+ );
12750
+ }
12751
+ }
12752
+ return expressionQualifiedColumnRef(column2, qualifier, ctx);
12753
+ }
12740
12754
  function expressionWholeRowRef(qualifier, ctx) {
12741
12755
  return sqlColumnRef(expressionRelationBinding(qualifier, ctx).qualifier);
12742
12756
  }
@@ -12934,7 +12948,7 @@ function assertSafeOperator(op2, opts) {
12934
12948
  function expressionNamedColumnRef(name, ctx) {
12935
12949
  const dotIdx = name.indexOf(".");
12936
12950
  if (dotIdx === -1) return expressionUnqualifiedColumnRef(name, ctx);
12937
- return expressionQualifiedColumnRef(
12951
+ return expressionUserQualifiedColumnRef(
12938
12952
  name.slice(dotIdx + 1),
12939
12953
  name.slice(0, dotIdx),
12940
12954
  ctx
@@ -13629,9 +13643,14 @@ var comparisonHandler = {
13629
13643
  }
13630
13644
  };
13631
13645
  function resolveHavingAggregatePgType(functionName, column2, ctx) {
13646
+ return aggregateResultPgType(
13647
+ functionName,
13648
+ functionName.toLowerCase() === "count" ? void 0 : resolveColumnPgType(column2, ctx)
13649
+ );
13650
+ }
13651
+ function aggregateResultPgType(functionName, argumentType) {
13632
13652
  const aggregate = functionName.toLowerCase();
13633
13653
  if (aggregate === "count") return "bigint";
13634
- const argumentType = resolveColumnPgType(column2, ctx);
13635
13654
  if (aggregate === "min" || aggregate === "max") return argumentType;
13636
13655
  if (argumentType === void 0) return void 0;
13637
13656
  const normalized = argumentType.toLowerCase().replace(/\s+/g, " ").trim();
@@ -14140,9 +14159,20 @@ function columnType(operand, ctx, state) {
14140
14159
  validateDbType(type);
14141
14160
  return dbTypeCastTarget(type);
14142
14161
  }
14162
+ function leftType(operand, ctx, state) {
14163
+ if (operand.kind !== "aggregate") return columnType(operand, ctx, state);
14164
+ return aggregateResultPgType(
14165
+ operand.function,
14166
+ operand.column ? columnType(operand.column, ctx, state) : void 0
14167
+ );
14168
+ }
14143
14169
  function parameter(p, ctx, state, left, force = false) {
14144
- const type = left && (p.cast === "column-db-type" || p.cast === "column-array") ? columnType(left, ctx, state) : void 0;
14145
- return compileValue(p.value, state, type, p.bound || force);
14170
+ const type = left && (p.cast === "column-db-type" || p.cast === "column-array") ? leftType(left, ctx, state) : void 0;
14171
+ if (!p.bound && !force && (p.value === null || p.value === void 0))
14172
+ return nullConstNode();
14173
+ const index = ++state.paramIndex;
14174
+ state.parameters.push(p.value);
14175
+ return type ? createTypeCastParamRef(index, type) : createParamRef(index);
14146
14176
  }
14147
14177
  function rhs(value, ctx, state, left) {
14148
14178
  if ("range" in value && "column" in value) return column(value, ctx);
@@ -14358,6 +14388,11 @@ function compileResolvedExpressionUnchecked(expr, ctx, state) {
14358
14388
  function compileResolvedConditionUnchecked(tree, ctx, state) {
14359
14389
  const visit = (n) => compileResolvedConditionUnchecked(n, ctx, state);
14360
14390
  const col = (c) => column(c, ctx);
14391
+ const left = (c) => c.kind !== "aggregate" ? col(c) : funcCall(c.function.toLowerCase(), c.column ? [col(c.column)] : [], {
14392
+ ...!c.column && { star: true },
14393
+ ...c.distinct && { distinct: true },
14394
+ ...c.filter && { filter: visit(c.filter) }
14395
+ });
14361
14396
  switch (tree.kind) {
14362
14397
  case "and":
14363
14398
  case "or": {
@@ -14367,10 +14402,10 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14367
14402
  case "not":
14368
14403
  return notExpr(visit(tree.condition));
14369
14404
  case "null":
14370
- return compileNull(col(tree.left), tree.operator === "isNull");
14405
+ return compileNull(left(tree.left), tree.operator === "isNull");
14371
14406
  case "comparison": {
14372
14407
  assertComparisonOperator(tree.operator);
14373
- let left = col(tree.left);
14408
+ let leftNode = left(tree.left);
14374
14409
  if (tree.jsonPath?.length) {
14375
14410
  assertDialectCapability(
14376
14411
  ctx.dialectCapabilities,
@@ -14378,8 +14413,8 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14378
14413
  "JSON operators are"
14379
14414
  );
14380
14415
  tree.jsonPath.forEach((p, i) => {
14381
- left = compileJsonOperator(
14382
- left,
14416
+ leftNode = compileJsonOperator(
14417
+ leftNode,
14383
14418
  parameter(p, ctx, state),
14384
14419
  i === tree.jsonPath.length - 1 && tree.jsonMode !== "json" ? "->>" : "->"
14385
14420
  );
@@ -14387,7 +14422,7 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14387
14422
  }
14388
14423
  return comparison(
14389
14424
  tree.operator,
14390
- left,
14425
+ leftNode,
14391
14426
  tree.right,
14392
14427
  ctx,
14393
14428
  state,
@@ -14395,17 +14430,17 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14395
14430
  );
14396
14431
  }
14397
14432
  case "like": {
14398
- const left = col(tree.left);
14433
+ const left2 = col(tree.left);
14399
14434
  if (!tree.pattern.bound) {
14400
14435
  const n = compileLiteralNullComparison(
14401
14436
  tree.caseInsensitive ? "ilike" : "like",
14402
- left,
14437
+ left2,
14403
14438
  tree.pattern.value
14404
14439
  );
14405
14440
  if (n) return n;
14406
14441
  }
14407
14442
  return compileLike(
14408
- left,
14443
+ left2,
14409
14444
  parameter(tree.pattern, ctx, state, void 0, true),
14410
14445
  tree.caseInsensitive,
14411
14446
  tree.escape && parameter(tree.escape, ctx, state, void 0, true)
@@ -14419,13 +14454,18 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14419
14454
  "supportsArrayType",
14420
14455
  "ANY array operator is"
14421
14456
  );
14422
- const type = columnType(tree.left, ctx, state);
14457
+ const type = leftType(tree.left, ctx, state);
14458
+ const modelBase = (operand) => mapModelIRTypeToPgBase(metadata(operand, ctx, state)?.type ?? "");
14459
+ const base = type ? void 0 : tree.left.kind !== "aggregate" ? modelBase(tree.left) : aggregateResultPgType(
14460
+ tree.left.function,
14461
+ tree.left.column ? modelBase(tree.left.column) : void 0
14462
+ );
14423
14463
  return compileAny(
14424
- col(tree.left),
14464
+ left(tree.left),
14425
14465
  Array.isArray(tree.values.value) ? tree.values.value : [],
14426
14466
  state,
14427
14467
  type,
14428
- type ? void 0 : mapModelIRTypeToPgBase(metadata(tree.left, ctx, state)?.type ?? "")
14468
+ base
14429
14469
  );
14430
14470
  }
14431
14471
  case "in": {
@@ -14433,7 +14473,7 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14433
14473
  const node = {
14434
14474
  SubLink: {
14435
14475
  subLinkType: "ANY_SUBLINK",
14436
- testexpr: col(tree.left),
14476
+ testexpr: left(tree.left),
14437
14477
  operName: [{ String: { sval: "=" } }],
14438
14478
  subselect: compileResolvedSubqueryBodyUnchecked(
14439
14479
  tree.operand.body,
@@ -14445,16 +14485,18 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14445
14485
  return tree.negated ? notExpr(node) : node;
14446
14486
  }
14447
14487
  const values = tree.operand.parameter.value;
14488
+ if (Array.isArray(values) && values.length === 0)
14489
+ return booleanConstNode(tree.negated);
14448
14490
  if (!Array.isArray(values))
14449
14491
  throw new Error(
14450
- `[in handler] Received a non-array value for operator '${tree.negated ? "notIn" : "in"}' on column '${tree.left.column}'. This is a compiler bug: expected a scalar array but received ${values == null ? String(values) : typeof values}. File a bug report.`
14492
+ `[in handler] Received a non-array value for operator '${tree.negated ? "notIn" : "in"}' on column '${tree.left.kind === "aggregate" ? tree.left.function : tree.left.column}'. This is a compiler bug: expected a scalar array but received ${values == null ? String(values) : typeof values}. File a bug report.`
14451
14493
  );
14452
14494
  return compileInValues(
14453
- col(tree.left),
14495
+ left(tree.left),
14454
14496
  state,
14455
14497
  values,
14456
14498
  tree.negated,
14457
- tree.operand.parameter.cast === "column-array" ? columnType(tree.left, ctx, state) : void 0,
14499
+ tree.operand.parameter.cast === "column-array" ? leftType(tree.left, ctx, state) : void 0,
14458
14500
  supportsDialectCapability(ctx.dialectCapabilities, "supportsArrayType")
14459
14501
  );
14460
14502
  }
@@ -14514,18 +14556,18 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14514
14556
  );
14515
14557
  case "expression": {
14516
14558
  if (tree.comparison) assertComparisonOperator(tree.comparison.operator);
14517
- const left = compileResolvedExpressionUnchecked(
14559
+ const left2 = compileResolvedExpressionUnchecked(
14518
14560
  tree.expression,
14519
14561
  ctx,
14520
14562
  state
14521
14563
  );
14522
14564
  return tree.comparison ? comparison(
14523
14565
  tree.comparison.operator,
14524
- left,
14566
+ left2,
14525
14567
  tree.comparison.right,
14526
14568
  ctx,
14527
14569
  state
14528
- ) : left;
14570
+ ) : left2;
14529
14571
  }
14530
14572
  case "subquery": {
14531
14573
  if (tree.use !== "exists") assertComparisonOperator(tree.operator);
@@ -14539,7 +14581,7 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14539
14581
  )
14540
14582
  }
14541
14583
  };
14542
- return tree.use === "exists" ? tree.negated ? notExpr(node) : node : compileComparison(op(tree.operator), col(tree.left), node);
14584
+ return tree.use === "exists" ? tree.negated ? notExpr(node) : node : compileComparison(op(tree.operator), left(tree.left), node);
14543
14585
  }
14544
14586
  case "relation": {
14545
14587
  if (tree.vacuous) return booleanConstNode(true);
@@ -15245,6 +15287,7 @@ function buildModelColumnProjections(tableName, columns, model, declaredNames) {
15245
15287
  }
15246
15288
 
15247
15289
  // src/condition-compiler-factory.ts
15290
+ import { refuseTableRootedQuantifier } from "@dbsp/core/internal";
15248
15291
  import { EXPRESSION_BRAND as EXPRESSION_BRAND2, REF_BRAND } from "@dbsp/types";
15249
15292
  import { toColumnList as toColumnList2 } from "@dbsp/types";
15250
15293
  import {
@@ -15516,7 +15559,12 @@ function intentToDecisions(intent, rootTable, options) {
15516
15559
  const decisions = [];
15517
15560
  if (intent.select) {
15518
15561
  decisions.push(
15519
- ...convertSelect(intent.select, rootTable, options?.directConditions)
15562
+ ...convertSelect(
15563
+ intent.select,
15564
+ rootTable,
15565
+ options?.directConditions,
15566
+ options?.resolvedFilters
15567
+ )
15520
15568
  );
15521
15569
  } else {
15522
15570
  decisions.push({ type: "select", column: "*", table: rootTable });
@@ -15553,13 +15601,15 @@ function intentToDecisions(intent, rootTable, options) {
15553
15601
  }
15554
15602
  return decisions;
15555
15603
  }
15556
- function applyFilterCondition(decision, filter, rootTable) {
15604
+ function applyFilterCondition(decision, filter, rootTable, resolvedFilters = false) {
15557
15605
  if (filter) {
15606
+ decision.filterIntent = filter;
15607
+ if (resolvedFilters) return;
15558
15608
  const filterDecision = convertWhereCondition(filter, rootTable);
15559
15609
  if (filterDecision) decision.filterCondition = filterDecision;
15560
15610
  }
15561
15611
  }
15562
- function convertSelect(select, rootTable, directConditions = false) {
15612
+ function convertSelect(select, rootTable, directConditions = false, resolvedFilters = false) {
15563
15613
  const selectType = "type" in select ? select.type : void 0;
15564
15614
  if ("all" in select && select.all === true) {
15565
15615
  return [{ type: "select", column: "*", table: rootTable }];
@@ -15594,7 +15644,7 @@ function convertSelect(select, rootTable, directConditions = false) {
15594
15644
  expr,
15595
15645
  rootTable,
15596
15646
  decisions,
15597
- (decision, filter, table2) => applyFilterCondition(decision, filter, table2),
15647
+ (decision, filter, table2) => applyFilterCondition(decision, filter, table2, resolvedFilters),
15598
15648
  (condition, table2) => convertWhereCondition(condition, table2)
15599
15649
  );
15600
15650
  }
@@ -15620,7 +15670,7 @@ function convertSelect(select, rootTable, directConditions = false) {
15620
15670
  if (agg.distinct === true) {
15621
15671
  aggDecision.distinct = true;
15622
15672
  }
15623
- applyFilterCondition(aggDecision, agg.filter, rootTable);
15673
+ applyFilterCondition(aggDecision, agg.filter, rootTable, resolvedFilters);
15624
15674
  decisions.push(aggDecision);
15625
15675
  }
15626
15676
  return decisions;
@@ -17464,15 +17514,31 @@ function createConditionCompiler(createWhereDispatcher2, buildCustomFnFilter3) {
17464
17514
  };
17465
17515
  return compileComparison2(leftOperand, { SubLink: subLink });
17466
17516
  }
17517
+ function assertLegacyTableRootedQuantifier(rf, ctx) {
17518
+ const table2 = rf.pathTable;
17519
+ if (table2 === void 0) return;
17520
+ const enclosing = ctx.enclosingRanges ?? [];
17521
+ if (enclosing.length === 0) {
17522
+ if (ctx.position === "subquery") refuseTableRootedQuantifier(rf.relation);
17523
+ return;
17524
+ }
17525
+ const qualifiers = new Set(
17526
+ [...ctx.queryRanges ?? [], ...enclosing.flat()].filter((binding) => binding.logicalTable === table2).map((binding) => identifierText(binding.qualifier))
17527
+ );
17528
+ const current = ctx.logicalSourceTable ?? ctx.rootTable;
17529
+ if (current !== table2 || qualifiers.size !== 1)
17530
+ refuseTableRootedQuantifier(rf.relation);
17531
+ }
17467
17532
  function handleRelationFilterIntent(intent, ctx) {
17468
17533
  const rf = intent;
17534
+ assertLegacyTableRootedQuantifier(rf, ctx);
17469
17535
  const preResolved = getTrustedNqlRelationFilterFields3(rf);
17470
17536
  if (ctx.position === "where" && ctx.directRootWhere && ("recursive" in rf && rf.recursive !== void 0 || preResolved?.recursive !== void 0))
17471
17537
  throw new Error(
17472
17538
  `WHERE relationFilter('${rf.relation}'): recursive relation predicates are not supported inside WHERE.`
17473
17539
  );
17474
17540
  const relationPath = preResolved?.relation ?? rf.relation;
17475
- const hops = Array.isArray(relationPath) ? [...relationPath] : [relationPath];
17541
+ const hops = typeof relationPath === "string" ? relationPath.split(".") : [...relationPath];
17476
17542
  const existsKind = rf.mode === "none" || rf.mode === "every" ? "notExists" : "exists";
17477
17543
  const rfWhere = rf.where;
17478
17544
  const isVacuousEvery = rf.mode === "every" && (!rfWhere || typeof rfWhere === "object" && rfWhere.kind === "and" && Array.isArray(rfWhere.conditions) && rfWhere.conditions.length === 0);
@@ -19277,7 +19343,7 @@ var WINDOW_FRAME_DEFAULT = 1034;
19277
19343
  function windowColumnRef(name, ctx) {
19278
19344
  const dotIdx = name.indexOf(".");
19279
19345
  if (dotIdx === -1) return expressionColumnRef(name, ctx);
19280
- return expressionQualifiedColumnRef(
19346
+ return expressionUserQualifiedColumnRef(
19281
19347
  name.slice(dotIdx + 1),
19282
19348
  name.slice(0, dotIdx),
19283
19349
  ctx
@@ -24181,6 +24247,7 @@ var PlanCompiler = class _PlanCompiler {
24181
24247
  outerAlias: identifierText(plan.enclosingRanges[0][0].qualifier)
24182
24248
  }
24183
24249
  },
24250
+ ...plan.resolvedExecution && { resolvedExecution: true },
24184
24251
  ...plan.directConditions && {
24185
24252
  compileCaseCondition: (intent, ctx, state) => this.compilePositionCondition(intent, plan, ctx, state, "case-when")
24186
24253
  },
@@ -24874,32 +24941,31 @@ var PlanCompiler = class _PlanCompiler {
24874
24941
  }
24875
24942
  return compileValue(arg, state);
24876
24943
  }
24944
+ compileLegacyNqlCaseCondition(condition, ctx, state) {
24945
+ const conditionDecision = convertWhereCondition(condition, ctx.rootTable);
24946
+ if (!conditionDecision) {
24947
+ throw new Error("NQL CASE WHEN condition could not be compiled");
24948
+ }
24949
+ return createWhereDispatcher(compileWhereIntent)(
24950
+ mapToHandlerDecision(
24951
+ conditionDecision,
24952
+ ctx.rootTable,
24953
+ this.defaultPk,
24954
+ this.deriveFk
24955
+ ),
24956
+ ctx,
24957
+ state
24958
+ );
24959
+ }
24877
24960
  compileNqlCaseExpressionArg(expr, ctx, state) {
24878
24961
  const whenClauses = expr.when;
24879
24962
  if (!Array.isArray(whenClauses) || whenClauses.length === 0) {
24880
24963
  throw new Error("NQL CASE expression requires at least one WHEN clause");
24881
24964
  }
24882
- const dispatcher = createWhereDispatcher(compileWhereIntent);
24883
24965
  const args = whenClauses.map((branch) => {
24884
24966
  const condition = branch.condition;
24885
24967
  const result = branch.result;
24886
- const conditionDecision = convertWhereCondition(
24887
- condition,
24888
- ctx.rootTable
24889
- );
24890
- if (!conditionDecision) {
24891
- throw new Error("NQL CASE WHEN condition could not be compiled");
24892
- }
24893
- const whenNode = dispatcher(
24894
- mapToHandlerDecision(
24895
- conditionDecision,
24896
- ctx.rootTable,
24897
- this.defaultPk,
24898
- this.deriveFk
24899
- ),
24900
- ctx,
24901
- state
24902
- );
24968
+ const whenNode = ctx.resolvedExecution && ctx.compileCaseCondition ? ctx.compileCaseCondition(condition, ctx, state) : this.compileLegacyNqlCaseCondition(condition, ctx, state);
24903
24969
  const thenNode = this.compileNqlFunctionArg(result, ctx, state);
24904
24970
  return {
24905
24971
  CaseWhen: {
@@ -24962,7 +25028,13 @@ var PlanCompiler = class _PlanCompiler {
24962
25028
  this.defaultPk,
24963
25029
  this.deriveFk
24964
25030
  );
24965
- const filterNode = compileFilterCondition(
25031
+ const filterNode = plan.resolvedExecution && decision.filterIntent ? this.compilePositionCondition(
25032
+ decision.filterIntent,
25033
+ plan,
25034
+ ctx,
25035
+ state,
25036
+ "filter"
25037
+ ) : compileFilterCondition(
24966
25038
  decision.filterCondition,
24967
25039
  createWhereDispatcher(compileWhereIntent),
24968
25040
  ctx,
@@ -27074,7 +27146,11 @@ import {
27074
27146
  POSTGRESQL_CAPABILITIES as POSTGRESQL_CAPABILITIES3,
27075
27147
  plan as planFn
27076
27148
  } from "@dbsp/core";
27077
- import { inspectMutationRows, resolveSelectWhere } from "@dbsp/core/internal";
27149
+ import {
27150
+ assertNoPathRefInConditionBodies,
27151
+ inspectMutationRows,
27152
+ resolveSelectWhere
27153
+ } from "@dbsp/core/internal";
27078
27154
  import { isParamIntent as isParamIntent8, RangeAllocator } from "@dbsp/types";
27079
27155
 
27080
27156
  // src/resolved-include-decisions.ts
@@ -28639,7 +28715,8 @@ function compileSelectEnvelope(plan, options, deps) {
28639
28715
  const resolvedWhere = plan.execution?.where;
28640
28716
  const decisions = intentToDecisions(execIntent, plan.rootTable, {
28641
28717
  omitRootWhere: true,
28642
- directConditions: true
28718
+ directConditions: true,
28719
+ resolvedFilters: plan.execution !== void 0
28643
28720
  });
28644
28721
  const enrichedUnifiedDecisions = planForCompilation.execution ? lowerResolvedIncludes(planForCompilation.execution, deps.defaultPk) : [];
28645
28722
  applyJoinHydrationPrefixes(enrichedUnifiedDecisions);
@@ -28685,6 +28762,8 @@ function compileSelectEnvelope(plan, options, deps) {
28685
28762
  const projection2 = compilePlan(
28686
28763
  {
28687
28764
  rootTable: plan.rootTable,
28765
+ directConditions: true,
28766
+ ...plan.execution !== void 0 && { resolvedExecution: true },
28688
28767
  ...expressionConditions && { expressionConditions },
28689
28768
  ...plan.execution?.expressionSubqueries && {
28690
28769
  expressionSubqueries: bindExpressionSubqueries(
@@ -28699,7 +28778,11 @@ function compileSelectEnvelope(plan, options, deps) {
28699
28778
  from: plan.rootTable,
28700
28779
  ...execIntent.select && { select: execIntent.select }
28701
28780
  },
28702
- plan.rootTable
28781
+ plan.rootTable,
28782
+ {
28783
+ directConditions: true,
28784
+ resolvedFilters: plan.execution !== void 0
28785
+ }
28703
28786
  ).filter(
28704
28787
  (d) => d.type !== "selectRelationColumn" || !includedRelations.has(d.relation ?? "")
28705
28788
  ),
@@ -28746,6 +28829,7 @@ function compileSelectEnvelope(plan, options, deps) {
28746
28829
  simplifiedPlan = {
28747
28830
  ...simplifiedPlan,
28748
28831
  directConditions: true,
28832
+ ...plan.execution !== void 0 && { resolvedExecution: true },
28749
28833
  ...expressionConditions && { expressionConditions },
28750
28834
  ...plan.execution?.expressionSubqueries && {
28751
28835
  expressionSubqueries: bindExpressionSubqueries(
@@ -29059,6 +29143,18 @@ function getColumnTypes(tableName, columns, deps, unnestedColumns = /* @__PURE__
29059
29143
  }
29060
29144
  return result;
29061
29145
  }
29146
+ function guardSubqueryParent(parent) {
29147
+ return parent && {
29148
+ ...parent,
29149
+ // A guard context is a where context of the mutation's table; the
29150
+ // binding helper reads only its current alias, root table and scope.
29151
+ queryRanges: parent.queryRanges ?? [
29152
+ currentExpressionBinding(
29153
+ parent
29154
+ )
29155
+ ]
29156
+ };
29157
+ }
29062
29158
  function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29063
29159
  const whereCtx = {
29064
29160
  rootTable: table2,
@@ -29078,7 +29174,7 @@ function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29078
29174
  ...deps.dialectCapabilities !== void 0 && {
29079
29175
  dialectCapabilities: deps.dialectCapabilities
29080
29176
  },
29081
- compileSubquery: (sqIntent, paramOffset) => buildSubqueryFromIntent(
29177
+ compileSubquery: (sqIntent, paramOffset, parent) => buildSubqueryFromIntent(
29082
29178
  sqIntent,
29083
29179
  paramOffset,
29084
29180
  deps.declaredNames,
@@ -29086,7 +29182,8 @@ function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29086
29182
  "rawExists",
29087
29183
  deps.scope,
29088
29184
  deps.dialectCapabilities,
29089
- deps.dbCasing
29185
+ deps.dbCasing,
29186
+ guardSubqueryParent(parent)
29090
29187
  )
29091
29188
  };
29092
29189
  return compileWhereIntent(where, whereCtx);
@@ -29155,6 +29252,7 @@ function compileInsertFrom2(intent, options, deps) {
29155
29252
  MAX_DEPTH_LIMIT
29156
29253
  );
29157
29254
  const state = createCompilerState();
29255
+ assertNoPathRefInConditionBodies(intent.where);
29158
29256
  const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29159
29257
  const sourceBinding2 = mutationBinding(
29160
29258
  { ...deps, ...bindingNames !== void 0 && { bindingNames } },
@@ -29207,6 +29305,10 @@ function compileInsertFrom2(intent, options, deps) {
29207
29305
  sourceCte
29208
29306
  );
29209
29307
  }
29308
+ function resolveModelLessWhere(where, table2, deps) {
29309
+ assertNoPathRefInConditionBodies(where);
29310
+ return resolveExistsIntent(where, table2, deps);
29311
+ }
29210
29312
  function resolvedMutationWhere(where, table2, deps) {
29211
29313
  if (!where || deps.model === void 0) return void 0;
29212
29314
  const allocator = new RangeAllocator();
@@ -29237,7 +29339,7 @@ function compileUpdate2(intent, options, deps) {
29237
29339
  const setColumns = Object.keys(intent.set ?? {});
29238
29340
  const columnTypes = getColumnTypes(intent.table, setColumns, deps);
29239
29341
  const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29240
- const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29342
+ const resolvedWhere = intent.where && !whereCompiler ? resolveModelLessWhere(intent.where, intent.table, deps) : void 0;
29241
29343
  const config = {
29242
29344
  table: declaredMutationTable(deps, intent.table),
29243
29345
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29314,6 +29416,7 @@ function compileBatchUpdate(intent, options, deps) {
29314
29416
  })) : void 0;
29315
29417
  let whereGuard;
29316
29418
  if (intent.where) {
29419
+ assertNoPathRefInConditionBodies(intent.where);
29317
29420
  const resolvedWhere = resolveExistsIntent(intent.where, intent.table, deps);
29318
29421
  const whereCtx = {
29319
29422
  rootTable: intent.table,
@@ -29332,7 +29435,7 @@ function compileBatchUpdate(intent, options, deps) {
29332
29435
  ...deps.dialectCapabilities !== void 0 && {
29333
29436
  dialectCapabilities: deps.dialectCapabilities
29334
29437
  },
29335
- compileSubquery: (sqIntent, paramOffset) => buildSubqueryFromIntent(
29438
+ compileSubquery: (sqIntent, paramOffset, parent) => buildSubqueryFromIntent(
29336
29439
  sqIntent,
29337
29440
  paramOffset,
29338
29441
  deps.declaredNames,
@@ -29340,7 +29443,8 @@ function compileBatchUpdate(intent, options, deps) {
29340
29443
  "rawExists",
29341
29444
  deps.scope,
29342
29445
  deps.dialectCapabilities,
29343
- deps.dbCasing
29446
+ deps.dbCasing,
29447
+ guardSubqueryParent(parent)
29344
29448
  )
29345
29449
  };
29346
29450
  whereGuard = compileWhereIntent(resolvedWhere, whereCtx);
@@ -29381,7 +29485,7 @@ function compileDelete2(intent, options, deps) {
29381
29485
  const ctx = mutationContext(deps, intent.table, MAX_DEPTH_LIMIT);
29382
29486
  const state = createCompilerState();
29383
29487
  const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29384
- const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29488
+ const resolvedWhere = intent.where && !whereCompiler ? resolveModelLessWhere(intent.where, intent.table, deps) : void 0;
29385
29489
  const config = {
29386
29490
  table: declaredMutationTable(deps, intent.table),
29387
29491
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29487,7 +29591,7 @@ function compileUpsert2(intent, options, deps, rowShape) {
29487
29591
  intent.table,
29488
29592
  deps
29489
29593
  );
29490
- const resolvedActionWhere = actionWhere ? actionWhereCompiler ? actionWhere : resolveExistsIntent(actionWhere, intent.table, deps) : void 0;
29594
+ const resolvedActionWhere = actionWhere ? actionWhereCompiler ? actionWhere : resolveModelLessWhere(actionWhere, intent.table, deps) : void 0;
29491
29595
  const config = {
29492
29596
  table: declaredMutationTable(deps, intent.table),
29493
29597
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29576,6 +29680,7 @@ function compileUpsertFrom2(intent, options, deps) {
29576
29680
  MAX_DEPTH_LIMIT
29577
29681
  );
29578
29682
  const state = createCompilerState();
29683
+ assertNoPathRefInConditionBodies(intent.where);
29579
29684
  const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29580
29685
  let columns;
29581
29686
  if (intent.columns) {
@@ -29665,16 +29770,20 @@ function getRegisteredProjection(registry, name) {
29665
29770
  return registry.get(name);
29666
29771
  }
29667
29772
  function createPlanReportForQuery(query, model, defaultFilters) {
29773
+ const resolution = resolveReportIncludes(query, [], model, {
29774
+ ...defaultFilters && { defaultFilters },
29775
+ skipRootDefaultFilter: true
29776
+ });
29668
29777
  return markPlannedReport2({
29669
29778
  rootTable: query.from,
29670
29779
  decisions: [],
29671
29780
  warnings: [],
29672
29781
  ctes: [],
29673
29782
  intent: query,
29674
- execution: resolveReportIncludes(query, [], model, {
29675
- ...defaultFilters && { defaultFilters },
29676
- skipRootDefaultFilter: true
29677
- }),
29783
+ ...resolution.intent !== query && {
29784
+ executableIntent: resolution.intent
29785
+ },
29786
+ execution: resolution.execution,
29678
29787
  metadata: {
29679
29788
  planningTimeMs: 0,
29680
29789
  relationsAnalyzed: 0,
@@ -29872,7 +29981,7 @@ function compileRecursive(report, model, _options, deps) {
29872
29981
  qualifier: queryLocal("__n")
29873
29982
  });
29874
29983
  const anchorScope = withRelationBinding(deps.scope, anchorBinding);
29875
- const anchorWhere = intent.start.where ? compileCondition(intent.start.where, {
29984
+ const anchorWhere = intent.start.where && !report.recursiveFilters?.start ? compileCondition(intent.start.where, {
29876
29985
  position: "recursive-anchor",
29877
29986
  logicalSourceTable: startTable,
29878
29987
  emittedAlias: "__n",
@@ -30079,6 +30188,12 @@ function compileRecursive(report, model, _options, deps) {
30079
30188
  `PgAdapter.compileRecursive: Unsupported traversal kind '${_exhaustive.kind}'`
30080
30189
  );
30081
30190
  }
30191
+ if (report.recursiveFilters?.start)
30192
+ config.anchorWhere = compileResolvedCondition(
30193
+ report.recursiveFilters.start,
30194
+ config.ctx,
30195
+ state
30196
+ );
30082
30197
  if (report.recursiveFilters?.anchor) {
30083
30198
  const predicate = compileResolvedCondition(
30084
30199
  report.recursiveFilters.anchor,
@@ -33207,16 +33322,20 @@ function compileNqlRuntimeBindingCte(name, binding, deps, parameterOffset, sourc
33207
33322
  };
33208
33323
  }
33209
33324
  function createNqlBindingSelectPlan(query, model, defaultFilters) {
33325
+ const resolution = resolveReportIncludes2(query, [], model, {
33326
+ ...defaultFilters && { defaultFilters },
33327
+ skipRootDefaultFilter: true
33328
+ });
33210
33329
  return markPlannedReport3({
33211
33330
  rootTable: query.from,
33212
33331
  decisions: [],
33213
33332
  warnings: [],
33214
33333
  ctes: [],
33215
33334
  intent: query,
33216
- execution: resolveReportIncludes2(query, [], model, {
33217
- ...defaultFilters && { defaultFilters },
33218
- skipRootDefaultFilter: true
33219
- }),
33335
+ ...resolution.intent !== query && {
33336
+ executableIntent: resolution.intent
33337
+ },
33338
+ execution: resolution.execution,
33220
33339
  metadata: {
33221
33340
  planningTimeMs: 0,
33222
33341
  relationsAnalyzed: 0,
@@ -59344,4 +59463,4 @@ export {
59344
59463
  planPgTransitionRun,
59345
59464
  applyPgTransitionRun
59346
59465
  };
59347
- //# sourceMappingURL=chunk-ZFU56LKS.js.map
59466
+ //# sourceMappingURL=chunk-WGJ4IBEN.js.map