Skip to content

Deduplicate SQL query parameters across expression processors#490

Merged
snaumenko-st merged 3 commits into
master-servicetitanfrom
feature/query-parameter-deduplication
Jun 19, 2026
Merged

Deduplicate SQL query parameters across expression processors#490
snaumenko-st merged 3 commits into
master-servicetitanfrom
feature/query-parameter-deduplication

Conversation

@snaumenko-st

@snaumenko-st snaumenko-st commented Jun 18, 2026

Copy link
Copy Markdown

When the same closure variable or row-filter collection is referenced multiple times in a single query (e.g. two .Where() clauses, both sides of a Union, or a Where + Select), the ORM previously emitted one DB parameter per reference. After this change each unique value is bound exactly once per compiled query.

Summary

  • Scalar/TypeId bindings: widens the bindingsWithIdentity dedup table from per-ExpressionProcessor to per-SqlCompiler, so all processors in one compilation share it. TypeId bindings are cached by TypeId in a new typeIdBindings dictionary on SqlCompiler.
  • Row-filter scalar bindings (ComplexCondition / Auto+TempTable path): adds a ScalarFilterKey(TypeMapping[], List<Tuple>) dictionary in CommandFactory.CreateQueryPart that caches the full filterValues[][] result per unique filter list, mirroring the existing TVP dedup pattern.
  • TVP bindings: adds a TvpParameterKey(TypeMapping, List<Tuple>) dictionary in CommandFactory.CreateQueryPart so two row filters referencing the same collection share a single TVP parameter.

Test plan

  • ClosureParameterReferencedMultipleTimesInQueryIsBoundOnce — same int closure in two Where clauses shares one parameter
  • TypeIdReferencedInBothUnionBranchesIsBoundOnce — TypeId parameter shared across Union branches
  • SmallRowFilterArrayReferencedInFilterAndProjectionIsBoundOncePerValue — small Contains list deduped across Where + Select
  • SmallRowFilterArrayReferencedInBothUnionBranchesIsBoundOncePerValue — same array in both Union branches
  • LargeRowFilterCollectionReferencedMultipleTimesUsesDistinctParameterValues — large list (TVP) referenced twice shares one TVP parameter
  • ExplicitTvpAlgorithmReferencedMultipleTimesUsesDistinctParameterValues — explicit TVP algorithm shares one parameter
  • BooleanClosureReferencedMultipleTimesIsNotBoundAsDbParameter — boolean closures expand to SQL constants, not DB params
  • DecimalClosureReferencedInFilterAndProjectionIsBoundOnce — decimal closure in two Where clauses shares one parameter
  • TakeClosureReferencedInFilterAndPagingUsesDistinctParameterValues — same int used in Where (Regular) and Take (LimitOffset) stays distinct
  • DistinctClosureValuesRemainSeparateParameters — guard: different values produce separate parameters

🤖 Generated with Claude Code

When the same closure variable or row-filter collection is referenced
multiple times in a single query (e.g. two .Where() clauses, both sides
of a Union, or a Where + Select), the ORM previously emitted one DB
parameter per reference. This change ensures each unique value is bound
exactly once per compiled query.

Scalar/TypeId bindings: widen the `bindingsWithIdentity` dedup table
from per-ExpressionProcessor to per-SqlCompiler so all processors in
one compilation share it. TypeId bindings are cached by TypeId in a new
`typeIdBindings` dictionary on SqlCompiler.

Row-filter scalar bindings (ComplexCondition / Auto+TempTable path):
add a `ScalarFilterKey(TypeMapping[], List<Tuple>)` dict in
CommandFactory.CreateQueryPart that caches the full `filterValues[][]`
result per unique filter list, mirroring the existing TVP dedup.

TVP bindings: add a `TvpParameterKey(TypeMapping, List<Tuple>)` dict
in CommandFactory.CreateQueryPart so two row filters referencing the
same collection share a single TVP parameter.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
snaumenko-st and others added 2 commits June 19, 2026 03:45
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@snaumenko-st snaumenko-st merged commit 29bc52a into master-servicetitan Jun 19, 2026
34 of 37 checks passed
@snaumenko-st snaumenko-st deleted the feature/query-parameter-deduplication branch June 19, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants