@dbsp/adapter-pgsql 7.1.1 → 7.1.3

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
@@ -14142,7 +14156,11 @@ function columnType(operand, ctx, state) {
14142
14156
  }
14143
14157
  function parameter(p, ctx, state, left, force = false) {
14144
14158
  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);
14159
+ if (!p.bound && !force && (p.value === null || p.value === void 0))
14160
+ return nullConstNode();
14161
+ const index = ++state.paramIndex;
14162
+ state.parameters.push(p.value);
14163
+ return type ? createTypeCastParamRef(index, type) : createParamRef(index);
14146
14164
  }
14147
14165
  function rhs(value, ctx, state, left) {
14148
14166
  if ("range" in value && "column" in value) return column(value, ctx);
@@ -15245,6 +15263,7 @@ function buildModelColumnProjections(tableName, columns, model, declaredNames) {
15245
15263
  }
15246
15264
 
15247
15265
  // src/condition-compiler-factory.ts
15266
+ import { refuseTableRootedQuantifier } from "@dbsp/core/internal";
15248
15267
  import { EXPRESSION_BRAND as EXPRESSION_BRAND2, REF_BRAND } from "@dbsp/types";
15249
15268
  import { toColumnList as toColumnList2 } from "@dbsp/types";
15250
15269
  import {
@@ -15516,7 +15535,12 @@ function intentToDecisions(intent, rootTable, options) {
15516
15535
  const decisions = [];
15517
15536
  if (intent.select) {
15518
15537
  decisions.push(
15519
- ...convertSelect(intent.select, rootTable, options?.directConditions)
15538
+ ...convertSelect(
15539
+ intent.select,
15540
+ rootTable,
15541
+ options?.directConditions,
15542
+ options?.resolvedFilters
15543
+ )
15520
15544
  );
15521
15545
  } else {
15522
15546
  decisions.push({ type: "select", column: "*", table: rootTable });
@@ -15553,13 +15577,15 @@ function intentToDecisions(intent, rootTable, options) {
15553
15577
  }
15554
15578
  return decisions;
15555
15579
  }
15556
- function applyFilterCondition(decision, filter, rootTable) {
15580
+ function applyFilterCondition(decision, filter, rootTable, resolvedFilters = false) {
15557
15581
  if (filter) {
15582
+ decision.filterIntent = filter;
15583
+ if (resolvedFilters) return;
15558
15584
  const filterDecision = convertWhereCondition(filter, rootTable);
15559
15585
  if (filterDecision) decision.filterCondition = filterDecision;
15560
15586
  }
15561
15587
  }
15562
- function convertSelect(select, rootTable, directConditions = false) {
15588
+ function convertSelect(select, rootTable, directConditions = false, resolvedFilters = false) {
15563
15589
  const selectType = "type" in select ? select.type : void 0;
15564
15590
  if ("all" in select && select.all === true) {
15565
15591
  return [{ type: "select", column: "*", table: rootTable }];
@@ -15594,7 +15620,7 @@ function convertSelect(select, rootTable, directConditions = false) {
15594
15620
  expr,
15595
15621
  rootTable,
15596
15622
  decisions,
15597
- (decision, filter, table2) => applyFilterCondition(decision, filter, table2),
15623
+ (decision, filter, table2) => applyFilterCondition(decision, filter, table2, resolvedFilters),
15598
15624
  (condition, table2) => convertWhereCondition(condition, table2)
15599
15625
  );
15600
15626
  }
@@ -15620,7 +15646,7 @@ function convertSelect(select, rootTable, directConditions = false) {
15620
15646
  if (agg.distinct === true) {
15621
15647
  aggDecision.distinct = true;
15622
15648
  }
15623
- applyFilterCondition(aggDecision, agg.filter, rootTable);
15649
+ applyFilterCondition(aggDecision, agg.filter, rootTable, resolvedFilters);
15624
15650
  decisions.push(aggDecision);
15625
15651
  }
15626
15652
  return decisions;
@@ -17464,15 +17490,31 @@ function createConditionCompiler(createWhereDispatcher2, buildCustomFnFilter3) {
17464
17490
  };
17465
17491
  return compileComparison2(leftOperand, { SubLink: subLink });
17466
17492
  }
17493
+ function assertLegacyTableRootedQuantifier(rf, ctx) {
17494
+ const table2 = rf.pathTable;
17495
+ if (table2 === void 0) return;
17496
+ const enclosing = ctx.enclosingRanges ?? [];
17497
+ if (enclosing.length === 0) {
17498
+ if (ctx.position === "subquery") refuseTableRootedQuantifier(rf.relation);
17499
+ return;
17500
+ }
17501
+ const qualifiers = new Set(
17502
+ [...ctx.queryRanges ?? [], ...enclosing.flat()].filter((binding) => binding.logicalTable === table2).map((binding) => identifierText(binding.qualifier))
17503
+ );
17504
+ const current = ctx.logicalSourceTable ?? ctx.rootTable;
17505
+ if (current !== table2 || qualifiers.size !== 1)
17506
+ refuseTableRootedQuantifier(rf.relation);
17507
+ }
17467
17508
  function handleRelationFilterIntent(intent, ctx) {
17468
17509
  const rf = intent;
17510
+ assertLegacyTableRootedQuantifier(rf, ctx);
17469
17511
  const preResolved = getTrustedNqlRelationFilterFields3(rf);
17470
17512
  if (ctx.position === "where" && ctx.directRootWhere && ("recursive" in rf && rf.recursive !== void 0 || preResolved?.recursive !== void 0))
17471
17513
  throw new Error(
17472
17514
  `WHERE relationFilter('${rf.relation}'): recursive relation predicates are not supported inside WHERE.`
17473
17515
  );
17474
17516
  const relationPath = preResolved?.relation ?? rf.relation;
17475
- const hops = Array.isArray(relationPath) ? [...relationPath] : [relationPath];
17517
+ const hops = typeof relationPath === "string" ? relationPath.split(".") : [...relationPath];
17476
17518
  const existsKind = rf.mode === "none" || rf.mode === "every" ? "notExists" : "exists";
17477
17519
  const rfWhere = rf.where;
17478
17520
  const isVacuousEvery = rf.mode === "every" && (!rfWhere || typeof rfWhere === "object" && rfWhere.kind === "and" && Array.isArray(rfWhere.conditions) && rfWhere.conditions.length === 0);
@@ -19277,7 +19319,7 @@ var WINDOW_FRAME_DEFAULT = 1034;
19277
19319
  function windowColumnRef(name, ctx) {
19278
19320
  const dotIdx = name.indexOf(".");
19279
19321
  if (dotIdx === -1) return expressionColumnRef(name, ctx);
19280
- return expressionQualifiedColumnRef(
19322
+ return expressionUserQualifiedColumnRef(
19281
19323
  name.slice(dotIdx + 1),
19282
19324
  name.slice(0, dotIdx),
19283
19325
  ctx
@@ -24181,6 +24223,7 @@ var PlanCompiler = class _PlanCompiler {
24181
24223
  outerAlias: identifierText(plan.enclosingRanges[0][0].qualifier)
24182
24224
  }
24183
24225
  },
24226
+ ...plan.resolvedExecution && { resolvedExecution: true },
24184
24227
  ...plan.directConditions && {
24185
24228
  compileCaseCondition: (intent, ctx, state) => this.compilePositionCondition(intent, plan, ctx, state, "case-when")
24186
24229
  },
@@ -24874,32 +24917,31 @@ var PlanCompiler = class _PlanCompiler {
24874
24917
  }
24875
24918
  return compileValue(arg, state);
24876
24919
  }
24920
+ compileLegacyNqlCaseCondition(condition, ctx, state) {
24921
+ const conditionDecision = convertWhereCondition(condition, ctx.rootTable);
24922
+ if (!conditionDecision) {
24923
+ throw new Error("NQL CASE WHEN condition could not be compiled");
24924
+ }
24925
+ return createWhereDispatcher(compileWhereIntent)(
24926
+ mapToHandlerDecision(
24927
+ conditionDecision,
24928
+ ctx.rootTable,
24929
+ this.defaultPk,
24930
+ this.deriveFk
24931
+ ),
24932
+ ctx,
24933
+ state
24934
+ );
24935
+ }
24877
24936
  compileNqlCaseExpressionArg(expr, ctx, state) {
24878
24937
  const whenClauses = expr.when;
24879
24938
  if (!Array.isArray(whenClauses) || whenClauses.length === 0) {
24880
24939
  throw new Error("NQL CASE expression requires at least one WHEN clause");
24881
24940
  }
24882
- const dispatcher = createWhereDispatcher(compileWhereIntent);
24883
24941
  const args = whenClauses.map((branch) => {
24884
24942
  const condition = branch.condition;
24885
24943
  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
- );
24944
+ const whenNode = ctx.resolvedExecution && ctx.compileCaseCondition ? ctx.compileCaseCondition(condition, ctx, state) : this.compileLegacyNqlCaseCondition(condition, ctx, state);
24903
24945
  const thenNode = this.compileNqlFunctionArg(result, ctx, state);
24904
24946
  return {
24905
24947
  CaseWhen: {
@@ -24962,7 +25004,13 @@ var PlanCompiler = class _PlanCompiler {
24962
25004
  this.defaultPk,
24963
25005
  this.deriveFk
24964
25006
  );
24965
- const filterNode = compileFilterCondition(
25007
+ const filterNode = plan.resolvedExecution && decision.filterIntent ? this.compilePositionCondition(
25008
+ decision.filterIntent,
25009
+ plan,
25010
+ ctx,
25011
+ state,
25012
+ "filter"
25013
+ ) : compileFilterCondition(
24966
25014
  decision.filterCondition,
24967
25015
  createWhereDispatcher(compileWhereIntent),
24968
25016
  ctx,
@@ -26292,7 +26340,12 @@ function compileUpdate(config, ctx, state) {
26292
26340
  )
26293
26341
  }));
26294
26342
  let whereClause;
26295
- if (config.where && config.where.length > 0) {
26343
+ if (config.whereCompiler) {
26344
+ whereClause = config.whereCompiler(
26345
+ { ...ctx, currentAlias: tableAlias },
26346
+ state
26347
+ );
26348
+ } else if (config.where && config.where.length > 0) {
26296
26349
  const dispatch = createWhereDispatcher(compileWhereIntent);
26297
26350
  const subCtx = { ...ctx, currentAlias: tableAlias };
26298
26351
  if (config.where.length === 1) {
@@ -26410,7 +26463,12 @@ function compileUnnestUpdate(config, ctx, state) {
26410
26463
  function compileDelete(config, ctx, state) {
26411
26464
  const tableAlias = config.tableMetadata?.physicalName ?? config.table;
26412
26465
  let whereClause;
26413
- if (config.where && config.where.length > 0) {
26466
+ if (config.whereCompiler) {
26467
+ whereClause = config.whereCompiler(
26468
+ { ...ctx, currentAlias: tableAlias },
26469
+ state
26470
+ );
26471
+ } else if (config.where && config.where.length > 0) {
26414
26472
  const dispatch = createWhereDispatcher(compileWhereIntent);
26415
26473
  const subCtx = { ...ctx, currentAlias: tableAlias };
26416
26474
  if (config.where.length === 1) {
@@ -27064,8 +27122,12 @@ import {
27064
27122
  POSTGRESQL_CAPABILITIES as POSTGRESQL_CAPABILITIES3,
27065
27123
  plan as planFn
27066
27124
  } from "@dbsp/core";
27067
- import { inspectMutationRows } from "@dbsp/core/internal";
27068
- import { isParamIntent as isParamIntent8 } from "@dbsp/types";
27125
+ import {
27126
+ assertNoPathRefInConditionBodies,
27127
+ inspectMutationRows,
27128
+ resolveSelectWhere
27129
+ } from "@dbsp/core/internal";
27130
+ import { isParamIntent as isParamIntent8, RangeAllocator } from "@dbsp/types";
27069
27131
 
27070
27132
  // src/resolved-include-decisions.ts
27071
27133
  function lowerResolvedIncludes(execution, defaultPk) {
@@ -28629,7 +28691,8 @@ function compileSelectEnvelope(plan, options, deps) {
28629
28691
  const resolvedWhere = plan.execution?.where;
28630
28692
  const decisions = intentToDecisions(execIntent, plan.rootTable, {
28631
28693
  omitRootWhere: true,
28632
- directConditions: true
28694
+ directConditions: true,
28695
+ resolvedFilters: plan.execution !== void 0
28633
28696
  });
28634
28697
  const enrichedUnifiedDecisions = planForCompilation.execution ? lowerResolvedIncludes(planForCompilation.execution, deps.defaultPk) : [];
28635
28698
  applyJoinHydrationPrefixes(enrichedUnifiedDecisions);
@@ -28675,6 +28738,8 @@ function compileSelectEnvelope(plan, options, deps) {
28675
28738
  const projection2 = compilePlan(
28676
28739
  {
28677
28740
  rootTable: plan.rootTable,
28741
+ directConditions: true,
28742
+ ...plan.execution !== void 0 && { resolvedExecution: true },
28678
28743
  ...expressionConditions && { expressionConditions },
28679
28744
  ...plan.execution?.expressionSubqueries && {
28680
28745
  expressionSubqueries: bindExpressionSubqueries(
@@ -28689,7 +28754,11 @@ function compileSelectEnvelope(plan, options, deps) {
28689
28754
  from: plan.rootTable,
28690
28755
  ...execIntent.select && { select: execIntent.select }
28691
28756
  },
28692
- plan.rootTable
28757
+ plan.rootTable,
28758
+ {
28759
+ directConditions: true,
28760
+ resolvedFilters: plan.execution !== void 0
28761
+ }
28693
28762
  ).filter(
28694
28763
  (d) => d.type !== "selectRelationColumn" || !includedRelations.has(d.relation ?? "")
28695
28764
  ),
@@ -28736,6 +28805,7 @@ function compileSelectEnvelope(plan, options, deps) {
28736
28805
  simplifiedPlan = {
28737
28806
  ...simplifiedPlan,
28738
28807
  directConditions: true,
28808
+ ...plan.execution !== void 0 && { resolvedExecution: true },
28739
28809
  ...expressionConditions && { expressionConditions },
28740
28810
  ...plan.execution?.expressionSubqueries && {
28741
28811
  expressionSubqueries: bindExpressionSubqueries(
@@ -29049,6 +29119,18 @@ function getColumnTypes(tableName, columns, deps, unnestedColumns = /* @__PURE__
29049
29119
  }
29050
29120
  return result;
29051
29121
  }
29122
+ function guardSubqueryParent(parent) {
29123
+ return parent && {
29124
+ ...parent,
29125
+ // A guard context is a where context of the mutation's table; the
29126
+ // binding helper reads only its current alias, root table and scope.
29127
+ queryRanges: parent.queryRanges ?? [
29128
+ currentExpressionBinding(
29129
+ parent
29130
+ )
29131
+ ]
29132
+ };
29133
+ }
29052
29134
  function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29053
29135
  const whereCtx = {
29054
29136
  rootTable: table2,
@@ -29068,7 +29150,7 @@ function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29068
29150
  ...deps.dialectCapabilities !== void 0 && {
29069
29151
  dialectCapabilities: deps.dialectCapabilities
29070
29152
  },
29071
- compileSubquery: (sqIntent, paramOffset) => buildSubqueryFromIntent(
29153
+ compileSubquery: (sqIntent, paramOffset, parent) => buildSubqueryFromIntent(
29072
29154
  sqIntent,
29073
29155
  paramOffset,
29074
29156
  deps.declaredNames,
@@ -29076,7 +29158,8 @@ function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29076
29158
  "rawExists",
29077
29159
  deps.scope,
29078
29160
  deps.dialectCapabilities,
29079
- deps.dbCasing
29161
+ deps.dbCasing,
29162
+ guardSubqueryParent(parent)
29080
29163
  )
29081
29164
  };
29082
29165
  return compileWhereIntent(where, whereCtx);
@@ -29145,6 +29228,7 @@ function compileInsertFrom2(intent, options, deps) {
29145
29228
  MAX_DEPTH_LIMIT
29146
29229
  );
29147
29230
  const state = createCompilerState();
29231
+ assertNoPathRefInConditionBodies(intent.where);
29148
29232
  const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29149
29233
  const sourceBinding2 = mutationBinding(
29150
29234
  { ...deps, ...bindingNames !== void 0 && { bindingNames } },
@@ -29197,12 +29281,41 @@ function compileInsertFrom2(intent, options, deps) {
29197
29281
  sourceCte
29198
29282
  );
29199
29283
  }
29284
+ function resolveModelLessWhere(where, table2, deps) {
29285
+ assertNoPathRefInConditionBodies(where);
29286
+ return resolveExistsIntent(where, table2, deps);
29287
+ }
29288
+ function resolvedMutationWhere(where, table2, deps) {
29289
+ if (!where || deps.model === void 0) return void 0;
29290
+ const allocator = new RangeAllocator();
29291
+ const root = allocator.bind(table2, table2);
29292
+ for (const name of [
29293
+ ...deps.bindingNames ?? [],
29294
+ ...deps.scope?.bindings.keys() ?? []
29295
+ ]) {
29296
+ allocator.reserve(name);
29297
+ }
29298
+ const resolved = resolveSelectWhere(
29299
+ where,
29300
+ root,
29301
+ [root],
29302
+ allocator,
29303
+ deps.model
29304
+ );
29305
+ if (!resolved) {
29306
+ throw new Error(
29307
+ `Internal error: the mutation WHERE on '${table2}' resolved to nothing.`
29308
+ );
29309
+ }
29310
+ return (ctx, state) => compileResolvedCondition(resolved, ctx, state);
29311
+ }
29200
29312
  function compileUpdate2(intent, options, deps) {
29201
29313
  const ctx = mutationContext(deps, intent.table, MAX_DEPTH_LIMIT);
29202
29314
  const state = createCompilerState();
29203
29315
  const setColumns = Object.keys(intent.set ?? {});
29204
29316
  const columnTypes = getColumnTypes(intent.table, setColumns, deps);
29205
- const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29317
+ const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29318
+ const resolvedWhere = intent.where && !whereCompiler ? resolveModelLessWhere(intent.where, intent.table, deps) : void 0;
29206
29319
  const config = {
29207
29320
  table: declaredMutationTable(deps, intent.table),
29208
29321
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29211,6 +29324,7 @@ function compileUpdate2(intent, options, deps) {
29211
29324
  value
29212
29325
  })),
29213
29326
  ...resolvedWhere && { where: [whereIntentAsDecision(resolvedWhere)] },
29327
+ ...whereCompiler && { whereCompiler },
29214
29328
  ...intent.returning && { returning: [...intent.returning] },
29215
29329
  ...intent.returning && {
29216
29330
  returningSources: mutationReturningSources(
@@ -29278,6 +29392,7 @@ function compileBatchUpdate(intent, options, deps) {
29278
29392
  })) : void 0;
29279
29393
  let whereGuard;
29280
29394
  if (intent.where) {
29395
+ assertNoPathRefInConditionBodies(intent.where);
29281
29396
  const resolvedWhere = resolveExistsIntent(intent.where, intent.table, deps);
29282
29397
  const whereCtx = {
29283
29398
  rootTable: intent.table,
@@ -29296,7 +29411,7 @@ function compileBatchUpdate(intent, options, deps) {
29296
29411
  ...deps.dialectCapabilities !== void 0 && {
29297
29412
  dialectCapabilities: deps.dialectCapabilities
29298
29413
  },
29299
- compileSubquery: (sqIntent, paramOffset) => buildSubqueryFromIntent(
29414
+ compileSubquery: (sqIntent, paramOffset, parent) => buildSubqueryFromIntent(
29300
29415
  sqIntent,
29301
29416
  paramOffset,
29302
29417
  deps.declaredNames,
@@ -29304,7 +29419,8 @@ function compileBatchUpdate(intent, options, deps) {
29304
29419
  "rawExists",
29305
29420
  deps.scope,
29306
29421
  deps.dialectCapabilities,
29307
- deps.dbCasing
29422
+ deps.dbCasing,
29423
+ guardSubqueryParent(parent)
29308
29424
  )
29309
29425
  };
29310
29426
  whereGuard = compileWhereIntent(resolvedWhere, whereCtx);
@@ -29344,11 +29460,13 @@ function compileBatchUpdate(intent, options, deps) {
29344
29460
  function compileDelete2(intent, options, deps) {
29345
29461
  const ctx = mutationContext(deps, intent.table, MAX_DEPTH_LIMIT);
29346
29462
  const state = createCompilerState();
29347
- const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29463
+ const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29464
+ const resolvedWhere = intent.where && !whereCompiler ? resolveModelLessWhere(intent.where, intent.table, deps) : void 0;
29348
29465
  const config = {
29349
29466
  table: declaredMutationTable(deps, intent.table),
29350
29467
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
29351
29468
  ...resolvedWhere && { where: [whereIntentAsDecision(resolvedWhere)] },
29469
+ ...whereCompiler && { whereCompiler },
29352
29470
  ...intent.returning && { returning: [...intent.returning] },
29353
29471
  ...intent.returning && {
29354
29472
  returningSources: mutationReturningSources(
@@ -29444,7 +29562,12 @@ function compileUpsert2(intent, options, deps, rowShape) {
29444
29562
  );
29445
29563
  const hasRawExprs = Object.keys(rawExprs).length > 0;
29446
29564
  const actionWhere = intent.action.type === "doUpdate" && intent.action.where ? intent.action.where : void 0;
29447
- const resolvedActionWhere = actionWhere ? resolveExistsIntent(actionWhere, intent.table, deps) : void 0;
29565
+ const actionWhereCompiler = resolvedMutationWhere(
29566
+ actionWhere,
29567
+ intent.table,
29568
+ deps
29569
+ );
29570
+ const resolvedActionWhere = actionWhere ? actionWhereCompiler ? actionWhere : resolveModelLessWhere(actionWhere, intent.table, deps) : void 0;
29448
29571
  const config = {
29449
29572
  table: declaredMutationTable(deps, intent.table),
29450
29573
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29491,7 +29614,13 @@ function compileUpsert2(intent, options, deps, rowShape) {
29491
29614
  actionWhereIntent: resolvedActionWhere,
29492
29615
  compileActionWhere: (where, paramState) => {
29493
29616
  const actionState = createCompilerState();
29494
- const actionWhere2 = compileUpsertActionWhere(
29617
+ const actionWhere2 = actionWhereCompiler ? actionWhereCompiler(
29618
+ {
29619
+ ...mutationContext(deps, intent.table, MAX_DEPTH_LIMIT),
29620
+ currentAlias: declaredMutationTable(deps, intent.table)
29621
+ },
29622
+ actionState
29623
+ ) : compileUpsertActionWhere(
29495
29624
  where,
29496
29625
  intent.table,
29497
29626
  actionState,
@@ -29527,6 +29656,7 @@ function compileUpsertFrom2(intent, options, deps) {
29527
29656
  MAX_DEPTH_LIMIT
29528
29657
  );
29529
29658
  const state = createCompilerState();
29659
+ assertNoPathRefInConditionBodies(intent.where);
29530
29660
  const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29531
29661
  let columns;
29532
29662
  if (intent.columns) {
@@ -29823,7 +29953,7 @@ function compileRecursive(report, model, _options, deps) {
29823
29953
  qualifier: queryLocal("__n")
29824
29954
  });
29825
29955
  const anchorScope = withRelationBinding(deps.scope, anchorBinding);
29826
- const anchorWhere = intent.start.where ? compileCondition(intent.start.where, {
29956
+ const anchorWhere = intent.start.where && !report.recursiveFilters?.start ? compileCondition(intent.start.where, {
29827
29957
  position: "recursive-anchor",
29828
29958
  logicalSourceTable: startTable,
29829
29959
  emittedAlias: "__n",
@@ -30030,6 +30160,12 @@ function compileRecursive(report, model, _options, deps) {
30030
30160
  `PgAdapter.compileRecursive: Unsupported traversal kind '${_exhaustive.kind}'`
30031
30161
  );
30032
30162
  }
30163
+ if (report.recursiveFilters?.start)
30164
+ config.anchorWhere = compileResolvedCondition(
30165
+ report.recursiveFilters.start,
30166
+ config.ctx,
30167
+ state
30168
+ );
30033
30169
  if (report.recursiveFilters?.anchor) {
30034
30170
  const predicate = compileResolvedCondition(
30035
30171
  report.recursiveFilters.anchor,
@@ -33943,6 +34079,21 @@ var PgAdapter = class {
33943
34079
  // correctly via a fresh inner PlanCompiler (same pattern as PlanCompiler
33944
34080
  // case 'selectCustomExpression').
33945
34081
  compileSubquery(query, paramOffset) {
34082
+ if (deps.model !== void 0) {
34083
+ const issued = planFn3(query, deps.model, {
34084
+ ...options?.defaultFilters && {
34085
+ defaultFilters: options.defaultFilters
34086
+ },
34087
+ ...dialectCapabilities !== void 0 && { dialectCapabilities }
34088
+ });
34089
+ const envelope = compileSelectEnvelope(issued, options, deps);
34090
+ if (envelope.ast === void 0)
34091
+ throw new Error("A planned selectExpression body produced no AST.");
34092
+ return {
34093
+ ast: renumberParamRefsInAst(envelope.ast, paramOffset),
34094
+ parameters: envelope.parameters
34095
+ };
34096
+ }
33946
34097
  const innerCompiler = new PlanCompiler({
33947
34098
  dbCasing: deps.dbCasing ?? "preserve",
33948
34099
  defaultPkColumnName: deps.defaultPk,
@@ -59280,4 +59431,4 @@ export {
59280
59431
  planPgTransitionRun,
59281
59432
  applyPgTransitionRun
59282
59433
  };
59283
- //# sourceMappingURL=chunk-XFAIB2BL.js.map
59434
+ //# sourceMappingURL=chunk-LXEAEGJK.js.map