@dbsp/adapter-pgsql 7.1.3 → 7.3.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.
@@ -12019,6 +12019,29 @@ function collectAndValidateParamRefs(node) {
12019
12019
  return { paramRefs, validationResults, allValid };
12020
12020
  }
12021
12021
 
12022
+ // src/param-renumber.ts
12023
+ function renumberParamRefsInAst(node, offset) {
12024
+ if (offset === 0) return node;
12025
+ return renumberNode(node, offset);
12026
+ }
12027
+ function renumberNode(value, offset) {
12028
+ if (value === null || value === void 0) return value;
12029
+ if (Array.isArray(value)) {
12030
+ return value.map((item) => renumberNode(item, offset));
12031
+ }
12032
+ if (typeof value !== "object") return value;
12033
+ const obj = value;
12034
+ if ("ParamRef" in obj && obj.ParamRef !== null && typeof obj.ParamRef === "object") {
12035
+ const pr = obj.ParamRef;
12036
+ return { ParamRef: { ...pr, number: pr.number + offset } };
12037
+ }
12038
+ const result = {};
12039
+ for (const key of Object.keys(obj)) {
12040
+ result[key] = renumberNode(obj[key], offset);
12041
+ }
12042
+ return result;
12043
+ }
12044
+
12022
12045
  // src/resolved-condition-compiler.ts
12023
12046
  import { escapeDiagnosticText as escapeDiagnosticText2, validateTypeName as validateTypeName2 } from "@dbsp/core/internal";
12024
12047
 
@@ -12087,6 +12110,13 @@ function hasBindingName(scope, name) {
12087
12110
  const binding = scope?.bindings.get(name);
12088
12111
  return binding?.kind === "cte-bind";
12089
12112
  }
12113
+ function localSourcePredicate(bindingNames, scope) {
12114
+ return (name) => {
12115
+ const local = queryLocal(name);
12116
+ const binding = relationBindingFor(scope, local);
12117
+ return hasBindingName(bindingNames, local) || binding?.kind === "cte-bind" || binding?.kind === "batch-values";
12118
+ };
12119
+ }
12090
12120
  function schemaForFromName(schemaName, fromName, scope) {
12091
12121
  return hasBindingName(scope, fromName) ? void 0 : schemaName;
12092
12122
  }
@@ -13643,9 +13673,14 @@ var comparisonHandler = {
13643
13673
  }
13644
13674
  };
13645
13675
  function resolveHavingAggregatePgType(functionName, column2, ctx) {
13676
+ return aggregateResultPgType(
13677
+ functionName,
13678
+ functionName.toLowerCase() === "count" ? void 0 : resolveColumnPgType(column2, ctx)
13679
+ );
13680
+ }
13681
+ function aggregateResultPgType(functionName, argumentType) {
13646
13682
  const aggregate = functionName.toLowerCase();
13647
13683
  if (aggregate === "count") return "bigint";
13648
- const argumentType = resolveColumnPgType(column2, ctx);
13649
13684
  if (aggregate === "min" || aggregate === "max") return argumentType;
13650
13685
  if (argumentType === void 0) return void 0;
13651
13686
  const normalized = argumentType.toLowerCase().replace(/\s+/g, " ").trim();
@@ -14128,7 +14163,7 @@ function validateOperators(input) {
14128
14163
  }
14129
14164
  }
14130
14165
  function address(operand, ctx, correlationRelation) {
14131
- const alias = operand.range.table === ctx.rootTable && operand.range.alias === ctx.rootTable ? ctx.currentAlias ?? operand.range.alias : operand.range.alias;
14166
+ const alias = operand.kind === "outerRef" && ctx.enclosingQualifiers !== void 0 ? ctx.enclosingQualifiers.get(operand.range.alias) ?? operand.range.alias : operand.range.table === ctx.rootTable && operand.range.alias === ctx.rootTable ? ctx.currentAlias ?? operand.range.alias : operand.range.alias;
14132
14167
  return resolveAddressedColumn(
14133
14168
  { ...operand, range: { ...operand.range, alias } },
14134
14169
  ctx,
@@ -14154,8 +14189,15 @@ function columnType(operand, ctx, state) {
14154
14189
  validateDbType(type);
14155
14190
  return dbTypeCastTarget(type);
14156
14191
  }
14192
+ function leftType(operand, ctx, state) {
14193
+ if (operand.kind !== "aggregate") return columnType(operand, ctx, state);
14194
+ return aggregateResultPgType(
14195
+ operand.function,
14196
+ operand.column ? columnType(operand.column, ctx, state) : void 0
14197
+ );
14198
+ }
14157
14199
  function parameter(p, ctx, state, left, force = false) {
14158
- const type = left && (p.cast === "column-db-type" || p.cast === "column-array") ? columnType(left, ctx, state) : void 0;
14200
+ const type = left && (p.cast === "column-db-type" || p.cast === "column-array") ? leftType(left, ctx, state) : void 0;
14159
14201
  if (!p.bound && !force && (p.value === null || p.value === void 0))
14160
14202
  return nullConstNode();
14161
14203
  const index = ++state.paramIndex;
@@ -14250,6 +14292,17 @@ function compileResolvedSubqueryBodyUnchecked(body, ctx, state) {
14250
14292
  }
14251
14293
  return { SelectStmt: stmt };
14252
14294
  }
14295
+ function nestedSelect(select, ctx, state) {
14296
+ if (!ctx.compileNestedSelect)
14297
+ throw new Error(
14298
+ "An expression subquery reached the adapter without its nested select."
14299
+ );
14300
+ const compiled = ctx.compileNestedSelect(select, ctx);
14301
+ const ast = renumberParamRefsInAst(compiled.ast, state.paramIndex);
14302
+ state.parameters.push(...compiled.parameters);
14303
+ state.paramIndex += compiled.parameters.length;
14304
+ return ast;
14305
+ }
14253
14306
  function compileResolvedExpressionUnchecked(expr, ctx, state) {
14254
14307
  const compile = (e) => compileResolvedExpressionUnchecked(e, ctx, state);
14255
14308
  switch (expr.kind) {
@@ -14311,11 +14364,7 @@ function compileResolvedExpressionUnchecked(expr, ctx, state) {
14311
14364
  return {
14312
14365
  SubLink: {
14313
14366
  subLinkType: "EXPR_SUBLINK",
14314
- subselect: compileResolvedSubqueryBodyUnchecked(
14315
- expr.body,
14316
- ctx,
14317
- state
14318
- )
14367
+ subselect: "select" in expr ? nestedSelect(expr.select, ctx, state) : compileResolvedSubqueryBodyUnchecked(expr.body, ctx, state)
14319
14368
  }
14320
14369
  };
14321
14370
  case "operator": {
@@ -14376,6 +14425,11 @@ function compileResolvedExpressionUnchecked(expr, ctx, state) {
14376
14425
  function compileResolvedConditionUnchecked(tree, ctx, state) {
14377
14426
  const visit = (n) => compileResolvedConditionUnchecked(n, ctx, state);
14378
14427
  const col = (c) => column(c, ctx);
14428
+ const left = (c) => c.kind !== "aggregate" ? col(c) : funcCall(c.function.toLowerCase(), c.column ? [col(c.column)] : [], {
14429
+ ...!c.column && { star: true },
14430
+ ...c.distinct && { distinct: true },
14431
+ ...c.filter && { filter: visit(c.filter) }
14432
+ });
14379
14433
  switch (tree.kind) {
14380
14434
  case "and":
14381
14435
  case "or": {
@@ -14385,10 +14439,10 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14385
14439
  case "not":
14386
14440
  return notExpr(visit(tree.condition));
14387
14441
  case "null":
14388
- return compileNull(col(tree.left), tree.operator === "isNull");
14442
+ return compileNull(left(tree.left), tree.operator === "isNull");
14389
14443
  case "comparison": {
14390
14444
  assertComparisonOperator(tree.operator);
14391
- let left = col(tree.left);
14445
+ let leftNode = left(tree.left);
14392
14446
  if (tree.jsonPath?.length) {
14393
14447
  assertDialectCapability(
14394
14448
  ctx.dialectCapabilities,
@@ -14396,8 +14450,8 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14396
14450
  "JSON operators are"
14397
14451
  );
14398
14452
  tree.jsonPath.forEach((p, i) => {
14399
- left = compileJsonOperator(
14400
- left,
14453
+ leftNode = compileJsonOperator(
14454
+ leftNode,
14401
14455
  parameter(p, ctx, state),
14402
14456
  i === tree.jsonPath.length - 1 && tree.jsonMode !== "json" ? "->>" : "->"
14403
14457
  );
@@ -14405,7 +14459,7 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14405
14459
  }
14406
14460
  return comparison(
14407
14461
  tree.operator,
14408
- left,
14462
+ leftNode,
14409
14463
  tree.right,
14410
14464
  ctx,
14411
14465
  state,
@@ -14413,17 +14467,17 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14413
14467
  );
14414
14468
  }
14415
14469
  case "like": {
14416
- const left = col(tree.left);
14470
+ const left2 = col(tree.left);
14417
14471
  if (!tree.pattern.bound) {
14418
14472
  const n = compileLiteralNullComparison(
14419
14473
  tree.caseInsensitive ? "ilike" : "like",
14420
- left,
14474
+ left2,
14421
14475
  tree.pattern.value
14422
14476
  );
14423
14477
  if (n) return n;
14424
14478
  }
14425
14479
  return compileLike(
14426
- left,
14480
+ left2,
14427
14481
  parameter(tree.pattern, ctx, state, void 0, true),
14428
14482
  tree.caseInsensitive,
14429
14483
  tree.escape && parameter(tree.escape, ctx, state, void 0, true)
@@ -14437,13 +14491,18 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14437
14491
  "supportsArrayType",
14438
14492
  "ANY array operator is"
14439
14493
  );
14440
- const type = columnType(tree.left, ctx, state);
14494
+ const type = leftType(tree.left, ctx, state);
14495
+ const modelBase = (operand) => mapModelIRTypeToPgBase(metadata(operand, ctx, state)?.type ?? "");
14496
+ const base = type ? void 0 : tree.left.kind !== "aggregate" ? modelBase(tree.left) : aggregateResultPgType(
14497
+ tree.left.function,
14498
+ tree.left.column ? modelBase(tree.left.column) : void 0
14499
+ );
14441
14500
  return compileAny(
14442
- col(tree.left),
14501
+ left(tree.left),
14443
14502
  Array.isArray(tree.values.value) ? tree.values.value : [],
14444
14503
  state,
14445
14504
  type,
14446
- type ? void 0 : mapModelIRTypeToPgBase(metadata(tree.left, ctx, state)?.type ?? "")
14505
+ base
14447
14506
  );
14448
14507
  }
14449
14508
  case "in": {
@@ -14451,7 +14510,7 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14451
14510
  const node = {
14452
14511
  SubLink: {
14453
14512
  subLinkType: "ANY_SUBLINK",
14454
- testexpr: col(tree.left),
14513
+ testexpr: left(tree.left),
14455
14514
  operName: [{ String: { sval: "=" } }],
14456
14515
  subselect: compileResolvedSubqueryBodyUnchecked(
14457
14516
  tree.operand.body,
@@ -14463,16 +14522,18 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14463
14522
  return tree.negated ? notExpr(node) : node;
14464
14523
  }
14465
14524
  const values = tree.operand.parameter.value;
14525
+ if (Array.isArray(values) && values.length === 0)
14526
+ return booleanConstNode(tree.negated);
14466
14527
  if (!Array.isArray(values))
14467
14528
  throw new Error(
14468
- `[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.`
14529
+ `[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.`
14469
14530
  );
14470
14531
  return compileInValues(
14471
- col(tree.left),
14532
+ left(tree.left),
14472
14533
  state,
14473
14534
  values,
14474
14535
  tree.negated,
14475
- tree.operand.parameter.cast === "column-array" ? columnType(tree.left, ctx, state) : void 0,
14536
+ tree.operand.parameter.cast === "column-array" ? leftType(tree.left, ctx, state) : void 0,
14476
14537
  supportsDialectCapability(ctx.dialectCapabilities, "supportsArrayType")
14477
14538
  );
14478
14539
  }
@@ -14532,18 +14593,18 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14532
14593
  );
14533
14594
  case "expression": {
14534
14595
  if (tree.comparison) assertComparisonOperator(tree.comparison.operator);
14535
- const left = compileResolvedExpressionUnchecked(
14596
+ const left2 = compileResolvedExpressionUnchecked(
14536
14597
  tree.expression,
14537
14598
  ctx,
14538
14599
  state
14539
14600
  );
14540
14601
  return tree.comparison ? comparison(
14541
14602
  tree.comparison.operator,
14542
- left,
14603
+ left2,
14543
14604
  tree.comparison.right,
14544
14605
  ctx,
14545
14606
  state
14546
- ) : left;
14607
+ ) : left2;
14547
14608
  }
14548
14609
  case "subquery": {
14549
14610
  if (tree.use !== "exists") assertComparisonOperator(tree.operator);
@@ -14557,7 +14618,7 @@ function compileResolvedConditionUnchecked(tree, ctx, state) {
14557
14618
  )
14558
14619
  }
14559
14620
  };
14560
- return tree.use === "exists" ? tree.negated ? notExpr(node) : node : compileComparison(op(tree.operator), col(tree.left), node);
14621
+ return tree.use === "exists" ? tree.negated ? notExpr(node) : node : compileComparison(op(tree.operator), left(tree.left), node);
14561
14622
  }
14562
14623
  case "relation": {
14563
14624
  if (tree.vacuous) return booleanConstNode(true);
@@ -14629,10 +14690,6 @@ function compileResolvedCondition(tree, ctx, state) {
14629
14690
  validateOperators(tree);
14630
14691
  return compileResolvedConditionUnchecked(tree, ctx, state);
14631
14692
  }
14632
- function compileResolvedSubqueryBody(body, ctx, state) {
14633
- validateOperators(body);
14634
- return compileResolvedSubqueryBodyUnchecked(body, ctx, state);
14635
- }
14636
14693
 
14637
14694
  // src/compiler.ts
14638
14695
  import { InvalidOperationError as InvalidOperationError2 } from "@dbsp/core";
@@ -16374,6 +16431,15 @@ function buildCustomFnFilter(filterIntent, ctx, state, conditionCompiler) {
16374
16431
  deriveFkColumnName: ctx.deriveFkColumnName
16375
16432
  },
16376
16433
  compileExpressionSubquery: ctx.compileSubquery,
16434
+ ...ctx.compileNestedSelect !== void 0 && {
16435
+ compileNestedSelect: ctx.compileNestedSelect
16436
+ },
16437
+ ...ctx.enclosingQualifiers !== void 0 && {
16438
+ enclosingQualifiers: ctx.enclosingQualifiers
16439
+ },
16440
+ ...ctx.rootRangeAlias !== void 0 && {
16441
+ rootRangeAlias: ctx.rootRangeAlias
16442
+ },
16377
16443
  paramState: state,
16378
16444
  ...ctx.model !== void 0 && { model: ctx.model },
16379
16445
  ...ctx.declaredNames !== void 0 && {
@@ -17361,6 +17427,15 @@ function createConditionCompiler(createWhereDispatcher2, buildCustomFnFilter3) {
17361
17427
  defaultPkColumnName: ctx.defaultPkColumnName,
17362
17428
  deriveFkColumnName: ctx.deriveFkColumnName,
17363
17429
  compileSubquery: ctx.compileExpressionSubquery,
17430
+ ...ctx.compileNestedSelect !== void 0 && {
17431
+ compileNestedSelect: ctx.compileNestedSelect
17432
+ },
17433
+ ...ctx.enclosingQualifiers !== void 0 && {
17434
+ enclosingQualifiers: ctx.enclosingQualifiers
17435
+ },
17436
+ ...ctx.rootRangeAlias !== void 0 && {
17437
+ rootRangeAlias: ctx.rootRangeAlias
17438
+ },
17364
17439
  ...ctx.resolvedConditions && {
17365
17440
  resolvedConditions: ctx.resolvedConditions
17366
17441
  },
@@ -23631,27 +23706,6 @@ function mergeDuplicateJoinIncludeDecisions(decisions, rootTable) {
23631
23706
  }
23632
23707
  return merged;
23633
23708
  }
23634
- function renumberParamRefsInAst(node, offset) {
23635
- if (offset === 0) return node;
23636
- return renumberNode(node, offset);
23637
- }
23638
- function renumberNode(value, offset) {
23639
- if (value === null || value === void 0) return value;
23640
- if (Array.isArray(value)) {
23641
- return value.map((item) => renumberNode(item, offset));
23642
- }
23643
- if (typeof value !== "object") return value;
23644
- const obj = value;
23645
- if ("ParamRef" in obj && obj.ParamRef !== null && typeof obj.ParamRef === "object") {
23646
- const pr = obj.ParamRef;
23647
- return { ParamRef: { ...pr, number: pr.number + offset } };
23648
- }
23649
- const result = {};
23650
- for (const key of Object.keys(obj)) {
23651
- result[key] = renumberNode(obj[key], offset);
23652
- }
23653
- return result;
23654
- }
23655
23709
  function maxParamRefNumber(value) {
23656
23710
  if (value === null || typeof value !== "object") return 0;
23657
23711
  if (Array.isArray(value)) {
@@ -23681,6 +23735,8 @@ var PlanCompiler = class _PlanCompiler {
23681
23735
  bindingNames;
23682
23736
  scope;
23683
23737
  relationTargetProjections;
23738
+ nestedSelectCompiler;
23739
+ localSubtree;
23684
23740
  /** Mutable state shared with extracted condition/value compilation functions */
23685
23741
  state = {
23686
23742
  parameters: [],
@@ -23691,6 +23747,8 @@ var PlanCompiler = class _PlanCompiler {
23691
23747
  };
23692
23748
  /** Track root table for EXISTS FK correlation */
23693
23749
  currentRootTable = "";
23750
+ /** The emitted alias of the root range when it differs from its table (a nested body). */
23751
+ currentRootAlias;
23694
23752
  /** Pending JOINs registered by filter/include strategies (flushed in compileSelect) */
23695
23753
  pendingJoins = [];
23696
23754
  /** Raw JOIN AST nodes from include handlers (e.g., LATERAL) */
@@ -23738,6 +23796,8 @@ var PlanCompiler = class _PlanCompiler {
23738
23796
  this.bindingNames = options.bindingNames;
23739
23797
  this.scope = options.scope;
23740
23798
  this.relationTargetProjections = options.relationTargetProjections;
23799
+ this.nestedSelectCompiler = options.compileNestedSelect;
23800
+ this.localSubtree = options.localSubtree === true;
23741
23801
  }
23742
23802
  childCompilerOptions(overrides = {}) {
23743
23803
  return {
@@ -23761,6 +23821,10 @@ var PlanCompiler = class _PlanCompiler {
23761
23821
  ...this.relationTargetProjections !== void 0 && {
23762
23822
  relationTargetProjections: this.relationTargetProjections
23763
23823
  },
23824
+ ...this.nestedSelectCompiler !== void 0 && {
23825
+ compileNestedSelect: this.nestedSelectCompiler
23826
+ },
23827
+ ...this.localSubtree && { localSubtree: true },
23764
23828
  ...overrides
23765
23829
  };
23766
23830
  }
@@ -23775,7 +23839,7 @@ var PlanCompiler = class _PlanCompiler {
23775
23839
  if (this.currentRootTable.length > 0 && relationBindingFor(this.scope, queryLocal(this.currentRootTable)) === void 0 && declaredRelationBindingFor(this.scope, this.currentRootTable) === void 0) {
23776
23840
  bindings.push(
23777
23841
  relationBinding({
23778
- qualifier: this.tableIdentifier(this.currentRootTable),
23842
+ qualifier: this.currentRootAlias !== void 0 ? queryLocal(this.currentRootAlias) : this.tableIdentifier(this.currentRootTable),
23779
23843
  kind: "declared-table",
23780
23844
  logicalTable: this.currentRootTable
23781
23845
  })
@@ -24117,6 +24181,7 @@ var PlanCompiler = class _PlanCompiler {
24117
24181
  joins: []
24118
24182
  };
24119
24183
  this.currentRootTable = plan.rootTable;
24184
+ this.currentRootAlias = plan.rootAlias;
24120
24185
  this.pendingJoins = [];
24121
24186
  this.rawJoins = [];
24122
24187
  this.keylessJoinProjections.clear();
@@ -24223,6 +24288,10 @@ var PlanCompiler = class _PlanCompiler {
24223
24288
  outerAlias: identifierText(plan.enclosingRanges[0][0].qualifier)
24224
24289
  }
24225
24290
  },
24291
+ ...plan.enclosingQualifiers && {
24292
+ enclosingQualifiers: plan.enclosingQualifiers
24293
+ },
24294
+ ...plan.rootAlias && { rootRangeAlias: plan.rootAlias },
24226
24295
  ...plan.resolvedExecution && { resolvedExecution: true },
24227
24296
  ...plan.directConditions && {
24228
24297
  compileCaseCondition: (intent, ctx, state) => this.compilePositionCondition(intent, plan, ctx, state, "case-when")
@@ -24256,6 +24325,9 @@ var PlanCompiler = class _PlanCompiler {
24256
24325
  ])
24257
24326
  )
24258
24327
  },
24328
+ ...this.nestedSelectCompiler !== void 0 && {
24329
+ compileNestedSelect: this.nestedSelectCompiler
24330
+ },
24259
24331
  compileSubquery: (query, paramOffset, parent, expression) => this.compileExpressionSubquery(
24260
24332
  query,
24261
24333
  paramOffset,
@@ -24281,6 +24353,10 @@ var PlanCompiler = class _PlanCompiler {
24281
24353
  schemaForRangeVar(plan, table2) {
24282
24354
  return hasBindingName(this.scope ?? this.bindingNames, queryLocal(table2)) ? void 0 : plan.schema ?? this.schema;
24283
24355
  }
24356
+ /** A FROM name bound in this query to a local source (an NQL binding, a CTE or a BatchValues range). */
24357
+ isQueryLocalSource(name) {
24358
+ return localSourcePredicate(this.bindingNames, this.scope)(name);
24359
+ }
24284
24360
  isNqlBindingRoot(plan) {
24285
24361
  return hasBindingName(
24286
24362
  this.scope ?? this.bindingNames,
@@ -24671,16 +24747,21 @@ var PlanCompiler = class _PlanCompiler {
24671
24747
  (entry2) => expression !== void 0 && entry2.expression === expression
24672
24748
  );
24673
24749
  if (resolved) {
24674
- const state = this.createHandlerState();
24675
- state.parameters = [];
24676
- state.paramIndex = paramOffset;
24677
- const ast = compileResolvedSubqueryBody(
24678
- resolved.body,
24679
- parentContext ?? this.createHandlerContext(parentPlan),
24680
- state
24681
- );
24682
- return { ast, parameters: state.parameters };
24750
+ const parent2 = parentContext ?? this.createHandlerContext(parentPlan);
24751
+ if (!this.nestedSelectCompiler)
24752
+ throw new Error(
24753
+ "An expression subquery reached the adapter without its nested select."
24754
+ );
24755
+ const compiled = this.nestedSelectCompiler(resolved.select, parent2);
24756
+ return {
24757
+ ast: renumberParamRefsInAst(compiled.ast, paramOffset),
24758
+ parameters: compiled.parameters
24759
+ };
24683
24760
  }
24761
+ if (this.model !== void 0 && parentPlan.directConditions && !this.localSubtree && !this.isQueryLocalSource(query.from))
24762
+ throw new Error(
24763
+ "An expression subquery reached the adapter without its nested select."
24764
+ );
24684
24765
  if (!parentPlan.directConditions) {
24685
24766
  assertNoSelectExpressionCorrelation(query);
24686
24767
  const innerCompiler2 = new _PlanCompiler(this.childCompilerOptions());
@@ -24706,7 +24787,9 @@ var PlanCompiler = class _PlanCompiler {
24706
24787
  const suffix = i === 0 ? "_sq" : `_sq_${i}`;
24707
24788
  rootAlias = `${truncateIdentifier2(query.from, 63 - suffix.length)}${suffix}`;
24708
24789
  }
24709
- const innerCompiler = new _PlanCompiler(this.childCompilerOptions());
24790
+ const innerCompiler = new _PlanCompiler(
24791
+ this.childCompilerOptions({ localSubtree: true })
24792
+ );
24710
24793
  const innerPlan = {
24711
24794
  rootTable: query.from,
24712
24795
  decisions: intentToDecisions(query, query.from, {
@@ -25491,12 +25574,13 @@ var PlanCompiler = class _PlanCompiler {
25491
25574
  */
25492
25575
  compileRelationAwareColumnRef(column2, table2) {
25493
25576
  const dot = column2.lastIndexOf(".");
25577
+ const root = (qualifier) => qualifier === this.currentRootTable && this.currentRootAlias !== void 0 ? this.currentRootAlias : qualifier;
25494
25578
  if (dot !== -1) {
25495
25579
  const relation = column2.slice(0, dot);
25496
25580
  if (relation === this.currentRootTable) {
25497
25581
  return expressionQualifiedColumnRef(
25498
25582
  column2.slice(dot + 1),
25499
- relation,
25583
+ root(relation),
25500
25584
  this.handlerCtx()
25501
25585
  );
25502
25586
  }
@@ -25510,7 +25594,7 @@ var PlanCompiler = class _PlanCompiler {
25510
25594
  this.handlerCtx()
25511
25595
  );
25512
25596
  }
25513
- return table2 === void 0 ? expressionColumnRef(column2, this.handlerCtx()) : expressionQualifiedColumnRef(column2, table2, this.handlerCtx());
25597
+ return table2 === void 0 ? expressionColumnRef(column2, this.handlerCtx()) : expressionQualifiedColumnRef(column2, root(table2), this.handlerCtx());
25514
25598
  }
25515
25599
  /**
25516
25600
  * Compile a single groupBy decision into a ColumnRef AST node.
@@ -26522,7 +26606,12 @@ function compileInsertFrom(config, ctx, state) {
26522
26606
  ];
26523
26607
  }
26524
26608
  let whereClause;
26525
- if (config.where && config.where.length > 0) {
26609
+ if (config.whereCompiler) {
26610
+ whereClause = config.whereCompiler(
26611
+ { ...ctx, currentAlias: sourceAlias },
26612
+ state
26613
+ );
26614
+ } else if (config.where && config.where.length > 0) {
26526
26615
  const dispatch = createWhereDispatcher(compileWhereIntent);
26527
26616
  const subCtx = { ...ctx, currentAlias: sourceAlias };
26528
26617
  if (config.where.length === 1) {
@@ -26599,7 +26688,12 @@ function compileUpsertFrom(config, ctx, state) {
26599
26688
  ];
26600
26689
  }
26601
26690
  let whereClause;
26602
- if (config.where && config.where.length > 0) {
26691
+ if (config.whereCompiler) {
26692
+ whereClause = config.whereCompiler(
26693
+ { ...ctx, currentAlias: sourceAlias },
26694
+ state
26695
+ );
26696
+ } else if (config.where && config.where.length > 0) {
26603
26697
  const dispatch = createWhereDispatcher(compileWhereIntent);
26604
26698
  const subCtx = { ...ctx, currentAlias: sourceAlias };
26605
26699
  if (config.where.length === 1) {
@@ -27084,6 +27178,7 @@ import {
27084
27178
  import {
27085
27179
  copyDefaultFilters,
27086
27180
  getRelationalReadPlan as getRelationalReadPlan3,
27181
+ planInScope as planInScope2,
27087
27182
  resolveReportIncludes as resolveReportIncludes2,
27088
27183
  validateRecursiveSetOperation
27089
27184
  } from "@dbsp/core/internal";
@@ -27100,7 +27195,7 @@ function assertRelationalOutput(plan, context) {
27100
27195
  // src/pgsql-adapter.ts
27101
27196
  import { randomBytes } from "crypto";
27102
27197
  import { isProxy } from "util/types";
27103
- import { POSTGRESQL_CAPABILITIES as POSTGRESQL_CAPABILITIES4, plan as planFn3 } from "@dbsp/core";
27198
+ import { POSTGRESQL_CAPABILITIES as POSTGRESQL_CAPABILITIES4, plan as planFn2 } from "@dbsp/core";
27104
27199
  import { inspectMutationRows as inspectMutationRows2 } from "@dbsp/core/internal";
27105
27200
  import { convertBigintJsReadValue } from "@dbsp/types";
27106
27201
  import {
@@ -28627,7 +28722,7 @@ function assertPlannedReportAuthority(plan) {
28627
28722
  );
28628
28723
  }
28629
28724
  }
28630
- function compileSelectEnvelope(plan, options, deps) {
28725
+ function compileSelectEnvelope(plan, options, deps, nested) {
28631
28726
  const schemaName = deps.schemaName;
28632
28727
  const resolvedModelForCompiler = options?.model ?? deps.model;
28633
28728
  if ([plan.intent, plan.executableIntent].some(
@@ -28670,7 +28765,8 @@ function compileSelectEnvelope(plan, options, deps) {
28670
28765
  },
28671
28766
  ...resolvedModelForCompiler != null && {
28672
28767
  model: resolvedModelForCompiler
28673
- }
28768
+ },
28769
+ compileNestedSelect: nestedSelectCompiler(options, deps)
28674
28770
  };
28675
28771
  const expressionConditions = plan.execution?.expressionConditions?.map(
28676
28772
  (entry2) => ({
@@ -28813,7 +28909,12 @@ function compileSelectEnvelope(plan, options, deps) {
28813
28909
  plan.execution.expressionSubqueries
28814
28910
  )
28815
28911
  },
28816
- ...execIntent.having && { rawHaving: execIntent.having }
28912
+ ...execIntent.having && { rawHaving: execIntent.having },
28913
+ ...nested?.rootAlias !== void 0 && { rootAlias: nested.rootAlias },
28914
+ ...nested && {
28915
+ enclosingRanges: nested.enclosingRanges,
28916
+ enclosingQualifiers: nested.enclosingQualifiers
28917
+ }
28817
28918
  };
28818
28919
  if (resolvedWhere)
28819
28920
  simplifiedPlan = {
@@ -28828,6 +28929,8 @@ function compileSelectEnvelope(plan, options, deps) {
28828
28929
  };
28829
28930
  }
28830
28931
  const result = compilePlan(simplifiedPlan, compilerOptions);
28932
+ if (nested)
28933
+ assertScalarBodyArity(result.ast, plan.rootTable, resolvedModelForCompiler);
28831
28934
  const baseEnv = fromAstProjection({
28832
28935
  sql: result.sql,
28833
28936
  parameters: result.parameters,
@@ -28881,10 +28984,88 @@ function bindExpressionSubqueries(intent, entries) {
28881
28984
  throw new Error(`Missing expression subquery at ${entry2.intentPath}`);
28882
28985
  return {
28883
28986
  expression: node,
28884
- body: entry2.body
28987
+ select: entry2.select
28885
28988
  };
28886
28989
  });
28887
28990
  }
28991
+ function nestedSelectCompiler(options, deps) {
28992
+ return (select, parent) => {
28993
+ const range = select.execution.rootRange;
28994
+ const enclosingQualifiers = new Map(parent.enclosingQualifiers ?? []);
28995
+ const parentRoot = parent.rootRangeAlias ?? parent.rootTable;
28996
+ enclosingQualifiers.set(
28997
+ parentRoot,
28998
+ parent.currentAlias !== void 0 && parent.currentAlias !== parentRoot ? parent.currentAlias : identifierText(
28999
+ declaredRelationBindingFor(parent.scope, parent.rootTable)?.qualifier ?? queryLocal(parentRoot)
29000
+ )
29001
+ );
29002
+ enclosingQualifiers.set(
29003
+ range.alias,
29004
+ range.alias !== range.table ? range.alias : identifierText(
29005
+ resolveDeclaredIdentifier(
29006
+ deps.declaredNames,
29007
+ deps.dbCasing ?? "preserve",
29008
+ { kind: "table", table: range.table }
29009
+ )
29010
+ )
29011
+ );
29012
+ const known = [
29013
+ ...parent.queryRanges ?? [],
29014
+ ...(parent.enclosingRanges ?? []).flat(),
29015
+ ...parent.scope?.bindings.values() ?? []
29016
+ ];
29017
+ const enclosingRanges = select.enclosing.map(
29018
+ (scope) => scope.map((enclosingRange) => {
29019
+ const emitted = enclosingQualifiers.get(enclosingRange.alias) ?? enclosingRange.alias;
29020
+ return known.find(
29021
+ (binding) => identifierText(binding.qualifier) === emitted
29022
+ ) ?? relationBinding({
29023
+ qualifier: queryLocal(emitted),
29024
+ kind: "declared-table",
29025
+ logicalTable: enclosingRange.table
29026
+ });
29027
+ })
29028
+ );
29029
+ const envelope = compileSelectEnvelope(
29030
+ markPlannedReport({
29031
+ rootTable: select.intent.from,
29032
+ decisions: select.decisions,
29033
+ warnings: [],
29034
+ ctes: [],
29035
+ intent: select.intent,
29036
+ execution: select.execution,
29037
+ metadata: {
29038
+ planningTimeMs: 0,
29039
+ relationsAnalyzed: 0,
29040
+ isAmbiguous: false
29041
+ }
29042
+ }),
29043
+ options,
29044
+ deps,
29045
+ {
29046
+ ...range.alias !== range.table && { rootAlias: range.alias },
29047
+ enclosingRanges,
29048
+ enclosingQualifiers
29049
+ }
29050
+ );
29051
+ if (envelope.ast === void 0)
29052
+ throw new Error("A nested select produced no AST.");
29053
+ return { ast: envelope.ast, parameters: envelope.parameters };
29054
+ };
29055
+ }
29056
+ function assertScalarBodyArity(ast, table2, model) {
29057
+ const targets = ast.SelectStmt?.targetList ?? [];
29058
+ const star = model?.getTable(table2)?.columns.length ?? 1;
29059
+ const columns = targets.reduce((n, target) => {
29060
+ const fields = target.ResTarget?.val?.ColumnRef?.fields;
29061
+ const last = Array.isArray(fields) ? fields.at(-1) : void 0;
29062
+ return n + (last !== null && typeof last === "object" && "A_Star" in last ? star : 1);
29063
+ }, 0);
29064
+ if (columns !== 1)
29065
+ throw new Error(
29066
+ `scalar subquery body projects ${columns} columns; a scalar subquery yields one value.`
29067
+ );
29068
+ }
28888
29069
  function planningNodeAtPath(intent, path) {
28889
29070
  let node = intent;
28890
29071
  for (const key of path.replaceAll("[", ".").replaceAll("]", "").split(".")) {
@@ -29035,7 +29216,8 @@ function mutationContext(deps, rootTable, maxRecursiveDepth) {
29035
29216
  ...deps.schemaName !== void 0 && { schema: deps.schemaName },
29036
29217
  maxRecursiveDepth,
29037
29218
  compileCustomFnFilter: buildCustomFnFilter2,
29038
- createWhereDispatcher: () => createWhereDispatcher(compileWhereIntent)
29219
+ createWhereDispatcher: () => createWhereDispatcher(compileWhereIntent),
29220
+ compileNestedSelect: nestedSelectCompiler(void 0, deps)
29039
29221
  };
29040
29222
  }
29041
29223
  function resolveExistsRelation(sourceTable, relation, model) {
@@ -29229,7 +29411,11 @@ function compileInsertFrom2(intent, options, deps) {
29229
29411
  );
29230
29412
  const state = createCompilerState();
29231
29413
  assertNoPathRefInConditionBodies(intent.where);
29232
- const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29414
+ const whereCompiler = resolvedMutationWhere(intent.where, intent.source, {
29415
+ ...deps,
29416
+ ...bindingNames !== void 0 && { bindingNames }
29417
+ });
29418
+ const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29233
29419
  const sourceBinding2 = mutationBinding(
29234
29420
  { ...deps, ...bindingNames !== void 0 && { bindingNames } },
29235
29421
  intent.source
@@ -29262,6 +29448,7 @@ function compileInsertFrom2(intent, options, deps) {
29262
29448
  }))
29263
29449
  },
29264
29450
  ...resolvedWhere && { where: [whereIntentAsDecision(resolvedWhere)] },
29451
+ ...whereCompiler && { whereCompiler },
29265
29452
  ...intent.limit !== void 0 && { limit: intent.limit },
29266
29453
  ...intent.returning && { returning: [...intent.returning] },
29267
29454
  ...intent.returning && {
@@ -29300,7 +29487,10 @@ function resolvedMutationWhere(where, table2, deps) {
29300
29487
  root,
29301
29488
  [root],
29302
29489
  allocator,
29303
- deps.model
29490
+ deps.model,
29491
+ 0,
29492
+ [],
29493
+ localSourcePredicate(deps.bindingNames, deps.scope)
29304
29494
  );
29305
29495
  if (!resolved) {
29306
29496
  throw new Error(
@@ -29391,7 +29581,16 @@ function compileBatchUpdate(intent, options, deps) {
29391
29581
  value
29392
29582
  })) : void 0;
29393
29583
  let whereGuard;
29394
- if (intent.where) {
29584
+ const guardCompiler = resolvedMutationWhere(intent.where, intent.table, deps);
29585
+ if (guardCompiler)
29586
+ whereGuard = guardCompiler(
29587
+ {
29588
+ ...ctx,
29589
+ currentAlias: declaredMutationTableMetadata(deps, intent.table)?.physicalName ?? declaredMutationTable(deps, intent.table)
29590
+ },
29591
+ state
29592
+ );
29593
+ else if (intent.where) {
29395
29594
  assertNoPathRefInConditionBodies(intent.where);
29396
29595
  const resolvedWhere = resolveExistsIntent(intent.where, intent.table, deps);
29397
29596
  const whereCtx = {
@@ -29657,7 +29856,11 @@ function compileUpsertFrom2(intent, options, deps) {
29657
29856
  );
29658
29857
  const state = createCompilerState();
29659
29858
  assertNoPathRefInConditionBodies(intent.where);
29660
- const resolvedWhere = intent.where ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29859
+ const whereCompiler = resolvedMutationWhere(intent.where, intent.source, {
29860
+ ...deps,
29861
+ ...bindingNames !== void 0 && { bindingNames }
29862
+ });
29863
+ const resolvedWhere = intent.where && !whereCompiler ? resolveExistsIntent(intent.where, intent.source, deps) : void 0;
29661
29864
  let columns;
29662
29865
  if (intent.columns) {
29663
29866
  columns = [...intent.columns];
@@ -29705,6 +29908,7 @@ function compileUpsertFrom2(intent, options, deps) {
29705
29908
  }))
29706
29909
  },
29707
29910
  ...resolvedWhere && { where: [whereIntentAsDecision(resolvedWhere)] },
29911
+ ...whereCompiler && { whereCompiler },
29708
29912
  ...intent.limit !== void 0 && { limit: intent.limit },
29709
29913
  ...intent.returning && { returning: [...intent.returning] },
29710
29914
  ...intent.returning && {
@@ -29728,10 +29932,10 @@ function compileUpsertFrom2(intent, options, deps) {
29728
29932
  // src/adapter-compiler-recursive.ts
29729
29933
  import {
29730
29934
  getRelationalReadPlan,
29935
+ planInScope,
29731
29936
  resolveReportIncludes
29732
29937
  } from "@dbsp/core/internal";
29733
29938
  import { markPlannedReport as markPlannedReport2 } from "@dbsp/types/internal";
29734
- import { plan as planFn2 } from "@dbsp/core";
29735
29939
  function withBindingProjectionScope(deps, bindingNames, projections) {
29736
29940
  const scope = queryScopeForBindingProjections(bindingNames, projections);
29737
29941
  return {
@@ -29745,17 +29949,22 @@ function getRegisteredProjection(registry, name) {
29745
29949
  if (registry.size === 0) return void 0;
29746
29950
  return registry.get(name);
29747
29951
  }
29748
- function createPlanReportForQuery(query, model, defaultFilters) {
29952
+ function createPlanReportForQuery(query, model, defaultFilters, isLocalSource) {
29953
+ const resolution = resolveReportIncludes(query, [], model, {
29954
+ ...defaultFilters && { defaultFilters },
29955
+ skipRootDefaultFilter: true,
29956
+ isLocalSource
29957
+ });
29749
29958
  return markPlannedReport2({
29750
29959
  rootTable: query.from,
29751
29960
  decisions: [],
29752
29961
  warnings: [],
29753
29962
  ctes: [],
29754
29963
  intent: query,
29755
- execution: resolveReportIncludes(query, [], model, {
29756
- ...defaultFilters && { defaultFilters },
29757
- skipRootDefaultFilter: true
29758
- }),
29964
+ ...resolution.intent !== query && {
29965
+ executableIntent: resolution.intent
29966
+ },
29967
+ execution: resolution.execution,
29759
29968
  metadata: {
29760
29969
  planningTimeMs: 0,
29761
29970
  relationsAnalyzed: 0,
@@ -29765,20 +29974,36 @@ function createPlanReportForQuery(query, model, defaultFilters) {
29765
29974
  }
29766
29975
  function createPlanReportForCteQuery(query, deps, hasRegisteredSource = false) {
29767
29976
  const queryLocalSource = hasRegisteredSource || hasBindingName(deps.bindingNames, query.from);
29977
+ const isLocalSource = localSourcePredicate(deps.bindingNames, deps.scope);
29768
29978
  const issued = getRelationalReadPlan(query, queryLocalSource);
29769
29979
  if (issued) return issued;
29770
29980
  if (queryLocalSource) {
29771
- return createPlanReportForQuery(query, deps.model, deps.defaultFilters);
29981
+ return createPlanReportForQuery(
29982
+ query,
29983
+ deps.model,
29984
+ deps.defaultFilters,
29985
+ isLocalSource
29986
+ );
29772
29987
  }
29773
29988
  if (deps.model === void 0 || deps.model.getTable(query.from) === void 0) {
29774
- return createPlanReportForQuery(query, deps.model, deps.defaultFilters);
29989
+ return createPlanReportForQuery(
29990
+ query,
29991
+ deps.model,
29992
+ deps.defaultFilters,
29993
+ isLocalSource
29994
+ );
29775
29995
  }
29776
- return planFn2(query, deps.model, {
29777
- ...deps.defaultFilters && { defaultFilters: deps.defaultFilters },
29778
- ...deps.dialectCapabilities !== void 0 && {
29779
- dialectCapabilities: deps.dialectCapabilities
29780
- }
29781
- });
29996
+ return planInScope(
29997
+ query,
29998
+ deps.model,
29999
+ {
30000
+ ...deps.defaultFilters && { defaultFilters: deps.defaultFilters },
30001
+ ...deps.dialectCapabilities !== void 0 && {
30002
+ dialectCapabilities: deps.dialectCapabilities
30003
+ }
30004
+ },
30005
+ { isLocalSource }
30006
+ );
29782
30007
  }
29783
30008
  function dbOutputKey(name) {
29784
30009
  return name;
@@ -29929,7 +30154,7 @@ function rehomeQueryEnvelope(source, query, compiled, sql, parameters) {
29929
30154
  compiled.hydrationPlan
29930
30155
  );
29931
30156
  }
29932
- function compileRecursive(report, model, _options, deps) {
30157
+ function compileRecursive(report, model, options, deps) {
29933
30158
  const schemaName = deps.schemaName;
29934
30159
  const state = createCompilerState();
29935
30160
  const intent = report.intent;
@@ -30002,7 +30227,8 @@ function compileRecursive(report, model, _options, deps) {
30002
30227
  ...schemaName !== void 0 && { schema: schemaName },
30003
30228
  maxRecursiveDepth: intent.maxDepth,
30004
30229
  compileCustomFnFilter: buildCustomFnFilter2,
30005
- createWhereDispatcher: () => createWhereDispatcher(compileWhereIntent)
30230
+ createWhereDispatcher: () => createWhereDispatcher(compileWhereIntent),
30231
+ compileNestedSelect: nestedSelectCompiler(options, deps)
30006
30232
  };
30007
30233
  const startSelect = intent.start.select ?? [];
30008
30234
  const nodeIdColumn = intent.start.nodeIdExpr.kind === "column" ? intent.start.nodeIdExpr.name : pkColumn;
@@ -30095,7 +30321,8 @@ function compileRecursive(report, model, _options, deps) {
30095
30321
  ...schemaName !== void 0 && { schema: schemaName },
30096
30322
  maxRecursiveDepth: intent.maxDepth,
30097
30323
  compileCustomFnFilter: buildCustomFnFilter2,
30098
- createWhereDispatcher: () => createWhereDispatcher(compileWhereIntent)
30324
+ createWhereDispatcher: () => createWhereDispatcher(compileWhereIntent),
30325
+ compileNestedSelect: nestedSelectCompiler(options, deps)
30099
30326
  };
30100
30327
  const startSelect = intent.start.select ?? [];
30101
30328
  const nodeIdColumn = intent.start.nodeIdExpr.kind === "column" ? intent.start.nodeIdExpr.name : pkColumn;
@@ -31938,9 +32165,9 @@ function compileLeafOrBranch(intent, compileFn) {
31938
32165
  }
31939
32166
  return toProjectionEnvelope(compileFn(intent));
31940
32167
  }
31941
- function createLeafCompileFn(adapter, model, planFn4, options) {
32168
+ function createLeafCompileFn(adapter, model, planFn3, options) {
31942
32169
  return (query) => {
31943
- const planReport = getRelationalReadPlan2(query) ?? planFn4(query, model, {
32170
+ const planReport = getRelationalReadPlan2(query) ?? planFn3(query, model, {
31944
32171
  dialectCapabilities: adapter.dialectCapabilities
31945
32172
  });
31946
32173
  return fromCompiledQuery(
@@ -33293,17 +33520,22 @@ function compileNqlRuntimeBindingCte(name, binding, deps, parameterOffset, sourc
33293
33520
  parameters
33294
33521
  };
33295
33522
  }
33296
- function createNqlBindingSelectPlan(query, model, defaultFilters) {
33523
+ function createNqlBindingSelectPlan(query, model, defaultFilters, isLocalSource) {
33524
+ const resolution = resolveReportIncludes2(query, [], model, {
33525
+ ...defaultFilters && { defaultFilters },
33526
+ skipRootDefaultFilter: true,
33527
+ isLocalSource
33528
+ });
33297
33529
  return markPlannedReport3({
33298
33530
  rootTable: query.from,
33299
33531
  decisions: [],
33300
33532
  warnings: [],
33301
33533
  ctes: [],
33302
33534
  intent: query,
33303
- execution: resolveReportIncludes2(query, [], model, {
33304
- ...defaultFilters && { defaultFilters },
33305
- skipRootDefaultFilter: true
33306
- }),
33535
+ ...resolution.intent !== query && {
33536
+ executableIntent: resolution.intent
33537
+ },
33538
+ execution: resolution.execution,
33307
33539
  metadata: {
33308
33540
  planningTimeMs: 0,
33309
33541
  relationsAnalyzed: 0,
@@ -33778,6 +34010,7 @@ var PgAdapter = class {
33778
34010
  bindingProjections
33779
34011
  );
33780
34012
  const queryFromBinding = hasBindingName(bindingNames, bundle.query.from);
34013
+ const isLocalSource = localSourcePredicate(bindingNames, deps.scope);
33781
34014
  const scanFilters = options?.defaultFilters ? copyDefaultFilters(
33782
34015
  options.defaultFilters,
33783
34016
  (table2) => !hasBindingName(bindingNames, table2)
@@ -33785,13 +34018,19 @@ var PgAdapter = class {
33785
34018
  const planReport = bundle.plan ?? (queryFromBinding ? createNqlBindingSelectPlan(
33786
34019
  bundle.query,
33787
34020
  deps.model,
33788
- scanFilters
33789
- ) : planFn3(bundle.query, this.requireNqlCompileModel(options), {
33790
- ...scanFilters && {
33791
- defaultFilters: scanFilters
34021
+ scanFilters,
34022
+ isLocalSource
34023
+ ) : planInScope2(
34024
+ bundle.query,
34025
+ this.requireNqlCompileModel(options),
34026
+ {
34027
+ ...scanFilters && {
34028
+ defaultFilters: scanFilters
34029
+ },
34030
+ dialectCapabilities: options?.dialectCapabilities ?? this.dialectCapabilities
33792
34031
  },
33793
- dialectCapabilities: options?.dialectCapabilities ?? this.dialectCapabilities
33794
- }));
34032
+ { isLocalSource }
34033
+ ));
33795
34034
  const compiled = compileSelectEnvelope(planReport, options, deps);
33796
34035
  const registeredSource = getNqlBindingProjection(
33797
34036
  bindingProjections,
@@ -34080,7 +34319,7 @@ var PgAdapter = class {
34080
34319
  // case 'selectCustomExpression').
34081
34320
  compileSubquery(query, paramOffset) {
34082
34321
  if (deps.model !== void 0) {
34083
- const issued = planFn3(query, deps.model, {
34322
+ const issued = planFn2(query, deps.model, {
34084
34323
  ...options?.defaultFilters && {
34085
34324
  defaultFilters: options.defaultFilters
34086
34325
  },
@@ -34344,13 +34583,24 @@ var PgAdapter = class {
34344
34583
  bindingProjections
34345
34584
  );
34346
34585
  const queryFromBinding = hasBindingName(bindingNames, query.from);
34586
+ const isLocalSource = localSourcePredicate(bindingNames, deps.scope);
34347
34587
  const issuedPlan = getRelationalReadPlan3(query, queryFromBinding);
34348
- const planReport = issuedPlan ?? (queryFromBinding ? createNqlBindingSelectPlan(query, model, options?.defaultFilters) : planFn3(query, model, {
34349
- ...options?.defaultFilters && {
34350
- defaultFilters: options.defaultFilters
34588
+ const planReport = issuedPlan ?? (queryFromBinding ? createNqlBindingSelectPlan(
34589
+ query,
34590
+ model,
34591
+ options?.defaultFilters,
34592
+ isLocalSource
34593
+ ) : planInScope2(
34594
+ query,
34595
+ model,
34596
+ {
34597
+ ...options?.defaultFilters && {
34598
+ defaultFilters: options.defaultFilters
34599
+ },
34600
+ dialectCapabilities: options?.dialectCapabilities ?? this.dialectCapabilities
34351
34601
  },
34352
- dialectCapabilities: options?.dialectCapabilities ?? this.dialectCapabilities
34353
- }));
34602
+ { isLocalSource }
34603
+ ));
34354
34604
  const compiled = compileSelectEnvelope(planReport, leafOptions, deps);
34355
34605
  const registeredSource = getNqlBindingProjection(
34356
34606
  bindingProjections,
@@ -59431,4 +59681,4 @@ export {
59431
59681
  planPgTransitionRun,
59432
59682
  applyPgTransitionRun
59433
59683
  };
59434
- //# sourceMappingURL=chunk-LXEAEGJK.js.map
59684
+ //# sourceMappingURL=chunk-CQSIDXP5.js.map