@dbsp/adapter-pgsql 7.1.2 → 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,
@@ -27074,7 +27122,11 @@ import {
27074
27122
  POSTGRESQL_CAPABILITIES as POSTGRESQL_CAPABILITIES3,
27075
27123
  plan as planFn
27076
27124
  } from "@dbsp/core";
27077
- import { inspectMutationRows, resolveSelectWhere } from "@dbsp/core/internal";
27125
+ import {
27126
+ assertNoPathRefInConditionBodies,
27127
+ inspectMutationRows,
27128
+ resolveSelectWhere
27129
+ } from "@dbsp/core/internal";
27078
27130
  import { isParamIntent as isParamIntent8, RangeAllocator } from "@dbsp/types";
27079
27131
 
27080
27132
  // src/resolved-include-decisions.ts
@@ -28639,7 +28691,8 @@ function compileSelectEnvelope(plan, options, deps) {
28639
28691
  const resolvedWhere = plan.execution?.where;
28640
28692
  const decisions = intentToDecisions(execIntent, plan.rootTable, {
28641
28693
  omitRootWhere: true,
28642
- directConditions: true
28694
+ directConditions: true,
28695
+ resolvedFilters: plan.execution !== void 0
28643
28696
  });
28644
28697
  const enrichedUnifiedDecisions = planForCompilation.execution ? lowerResolvedIncludes(planForCompilation.execution, deps.defaultPk) : [];
28645
28698
  applyJoinHydrationPrefixes(enrichedUnifiedDecisions);
@@ -28685,6 +28738,8 @@ function compileSelectEnvelope(plan, options, deps) {
28685
28738
  const projection2 = compilePlan(
28686
28739
  {
28687
28740
  rootTable: plan.rootTable,
28741
+ directConditions: true,
28742
+ ...plan.execution !== void 0 && { resolvedExecution: true },
28688
28743
  ...expressionConditions && { expressionConditions },
28689
28744
  ...plan.execution?.expressionSubqueries && {
28690
28745
  expressionSubqueries: bindExpressionSubqueries(
@@ -28699,7 +28754,11 @@ function compileSelectEnvelope(plan, options, deps) {
28699
28754
  from: plan.rootTable,
28700
28755
  ...execIntent.select && { select: execIntent.select }
28701
28756
  },
28702
- plan.rootTable
28757
+ plan.rootTable,
28758
+ {
28759
+ directConditions: true,
28760
+ resolvedFilters: plan.execution !== void 0
28761
+ }
28703
28762
  ).filter(
28704
28763
  (d) => d.type !== "selectRelationColumn" || !includedRelations.has(d.relation ?? "")
28705
28764
  ),
@@ -28746,6 +28805,7 @@ function compileSelectEnvelope(plan, options, deps) {
28746
28805
  simplifiedPlan = {
28747
28806
  ...simplifiedPlan,
28748
28807
  directConditions: true,
28808
+ ...plan.execution !== void 0 && { resolvedExecution: true },
28749
28809
  ...expressionConditions && { expressionConditions },
28750
28810
  ...plan.execution?.expressionSubqueries && {
28751
28811
  expressionSubqueries: bindExpressionSubqueries(
@@ -29059,6 +29119,18 @@ function getColumnTypes(tableName, columns, deps, unnestedColumns = /* @__PURE__
29059
29119
  }
29060
29120
  return result;
29061
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
+ }
29062
29134
  function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29063
29135
  const whereCtx = {
29064
29136
  rootTable: table2,
@@ -29078,7 +29150,7 @@ function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29078
29150
  ...deps.dialectCapabilities !== void 0 && {
29079
29151
  dialectCapabilities: deps.dialectCapabilities
29080
29152
  },
29081
- compileSubquery: (sqIntent, paramOffset) => buildSubqueryFromIntent(
29153
+ compileSubquery: (sqIntent, paramOffset, parent) => buildSubqueryFromIntent(
29082
29154
  sqIntent,
29083
29155
  paramOffset,
29084
29156
  deps.declaredNames,
@@ -29086,7 +29158,8 @@ function compileUpsertActionWhere(where, table2, state, deps, schemaName) {
29086
29158
  "rawExists",
29087
29159
  deps.scope,
29088
29160
  deps.dialectCapabilities,
29089
- deps.dbCasing
29161
+ deps.dbCasing,
29162
+ guardSubqueryParent(parent)
29090
29163
  )
29091
29164
  };
29092
29165
  return compileWhereIntent(where, whereCtx);
@@ -29155,6 +29228,7 @@ function compileInsertFrom2(intent, options, deps) {
29155
29228
  MAX_DEPTH_LIMIT
29156
29229
  );
29157
29230
  const state = createCompilerState();
29231
+ assertNoPathRefInConditionBodies(intent.where);
29158
29232
  const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29159
29233
  const sourceBinding2 = mutationBinding(
29160
29234
  { ...deps, ...bindingNames !== void 0 && { bindingNames } },
@@ -29207,6 +29281,10 @@ function compileInsertFrom2(intent, options, deps) {
29207
29281
  sourceCte
29208
29282
  );
29209
29283
  }
29284
+ function resolveModelLessWhere(where, table2, deps) {
29285
+ assertNoPathRefInConditionBodies(where);
29286
+ return resolveExistsIntent(where, table2, deps);
29287
+ }
29210
29288
  function resolvedMutationWhere(where, table2, deps) {
29211
29289
  if (!where || deps.model === void 0) return void 0;
29212
29290
  const allocator = new RangeAllocator();
@@ -29237,7 +29315,7 @@ function compileUpdate2(intent, options, deps) {
29237
29315
  const setColumns = Object.keys(intent.set ?? {});
29238
29316
  const columnTypes = getColumnTypes(intent.table, setColumns, deps);
29239
29317
  const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29240
- const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29318
+ const resolvedWhere = intent.where && !whereCompiler ? resolveModelLessWhere(intent.where, intent.table, deps) : void 0;
29241
29319
  const config = {
29242
29320
  table: declaredMutationTable(deps, intent.table),
29243
29321
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29314,6 +29392,7 @@ function compileBatchUpdate(intent, options, deps) {
29314
29392
  })) : void 0;
29315
29393
  let whereGuard;
29316
29394
  if (intent.where) {
29395
+ assertNoPathRefInConditionBodies(intent.where);
29317
29396
  const resolvedWhere = resolveExistsIntent(intent.where, intent.table, deps);
29318
29397
  const whereCtx = {
29319
29398
  rootTable: intent.table,
@@ -29332,7 +29411,7 @@ function compileBatchUpdate(intent, options, deps) {
29332
29411
  ...deps.dialectCapabilities !== void 0 && {
29333
29412
  dialectCapabilities: deps.dialectCapabilities
29334
29413
  },
29335
- compileSubquery: (sqIntent, paramOffset) => buildSubqueryFromIntent(
29414
+ compileSubquery: (sqIntent, paramOffset, parent) => buildSubqueryFromIntent(
29336
29415
  sqIntent,
29337
29416
  paramOffset,
29338
29417
  deps.declaredNames,
@@ -29340,7 +29419,8 @@ function compileBatchUpdate(intent, options, deps) {
29340
29419
  "rawExists",
29341
29420
  deps.scope,
29342
29421
  deps.dialectCapabilities,
29343
- deps.dbCasing
29422
+ deps.dbCasing,
29423
+ guardSubqueryParent(parent)
29344
29424
  )
29345
29425
  };
29346
29426
  whereGuard = compileWhereIntent(resolvedWhere, whereCtx);
@@ -29381,7 +29461,7 @@ function compileDelete2(intent, options, deps) {
29381
29461
  const ctx = mutationContext(deps, intent.table, MAX_DEPTH_LIMIT);
29382
29462
  const state = createCompilerState();
29383
29463
  const whereCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29384
- const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.table, deps) : void 0;
29464
+ const resolvedWhere = intent.where && !whereCompiler ? resolveModelLessWhere(intent.where, intent.table, deps) : void 0;
29385
29465
  const config = {
29386
29466
  table: declaredMutationTable(deps, intent.table),
29387
29467
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29487,7 +29567,7 @@ function compileUpsert2(intent, options, deps, rowShape) {
29487
29567
  intent.table,
29488
29568
  deps
29489
29569
  );
29490
- const resolvedActionWhere = actionWhere ? actionWhereCompiler ? actionWhere : resolveExistsIntent(actionWhere, intent.table, deps) : void 0;
29570
+ const resolvedActionWhere = actionWhere ? actionWhereCompiler ? actionWhere : resolveModelLessWhere(actionWhere, intent.table, deps) : void 0;
29491
29571
  const config = {
29492
29572
  table: declaredMutationTable(deps, intent.table),
29493
29573
  tableMetadata: declaredMutationTableMetadata(deps, intent.table),
@@ -29576,6 +29656,7 @@ function compileUpsertFrom2(intent, options, deps) {
29576
29656
  MAX_DEPTH_LIMIT
29577
29657
  );
29578
29658
  const state = createCompilerState();
29659
+ assertNoPathRefInConditionBodies(intent.where);
29579
29660
  const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29580
29661
  let columns;
29581
29662
  if (intent.columns) {
@@ -29872,7 +29953,7 @@ function compileRecursive(report, model, _options, deps) {
29872
29953
  qualifier: queryLocal("__n")
29873
29954
  });
29874
29955
  const anchorScope = withRelationBinding(deps.scope, anchorBinding);
29875
- const anchorWhere = intent.start.where ? compileCondition(intent.start.where, {
29956
+ const anchorWhere = intent.start.where && !report.recursiveFilters?.start ? compileCondition(intent.start.where, {
29876
29957
  position: "recursive-anchor",
29877
29958
  logicalSourceTable: startTable,
29878
29959
  emittedAlias: "__n",
@@ -30079,6 +30160,12 @@ function compileRecursive(report, model, _options, deps) {
30079
30160
  `PgAdapter.compileRecursive: Unsupported traversal kind '${_exhaustive.kind}'`
30080
30161
  );
30081
30162
  }
30163
+ if (report.recursiveFilters?.start)
30164
+ config.anchorWhere = compileResolvedCondition(
30165
+ report.recursiveFilters.start,
30166
+ config.ctx,
30167
+ state
30168
+ );
30082
30169
  if (report.recursiveFilters?.anchor) {
30083
30170
  const predicate = compileResolvedCondition(
30084
30171
  report.recursiveFilters.anchor,
@@ -59344,4 +59431,4 @@ export {
59344
59431
  planPgTransitionRun,
59345
59432
  applyPgTransitionRun
59346
59433
  };
59347
- //# sourceMappingURL=chunk-ZFU56LKS.js.map
59434
+ //# sourceMappingURL=chunk-LXEAEGJK.js.map