Deduplicate SQL query parameters across expression processors#490
Merged
snaumenko-st merged 3 commits intoJun 19, 2026
Merged
Conversation
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>
SergeiPavlov
approved these changes
Jun 18, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
botinko
approved these changes
Jun 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aUnion, or aWhere+Select), the ORM previously emitted one DB parameter per reference. After this change each unique value is bound exactly once per compiled query.Summary
bindingsWithIdentitydedup table from per-ExpressionProcessorto per-SqlCompiler, so all processors in one compilation share it. TypeId bindings are cached byTypeIdin a newtypeIdBindingsdictionary onSqlCompiler.ScalarFilterKey(TypeMapping[], List<Tuple>)dictionary inCommandFactory.CreateQueryPartthat caches the fullfilterValues[][]result per unique filter list, mirroring the existing TVP dedup pattern.TvpParameterKey(TypeMapping, List<Tuple>)dictionary inCommandFactory.CreateQueryPartso two row filters referencing the same collection share a single TVP parameter.Test plan
ClosureParameterReferencedMultipleTimesInQueryIsBoundOnce— same int closure in twoWhereclauses shares one parameterTypeIdReferencedInBothUnionBranchesIsBoundOnce— TypeId parameter shared across Union branchesSmallRowFilterArrayReferencedInFilterAndProjectionIsBoundOncePerValue— smallContainslist deduped acrossWhere+SelectSmallRowFilterArrayReferencedInBothUnionBranchesIsBoundOncePerValue— same array in both Union branchesLargeRowFilterCollectionReferencedMultipleTimesUsesDistinctParameterValues— large list (TVP) referenced twice shares one TVP parameterExplicitTvpAlgorithmReferencedMultipleTimesUsesDistinctParameterValues— explicit TVP algorithm shares one parameterBooleanClosureReferencedMultipleTimesIsNotBoundAsDbParameter— boolean closures expand to SQL constants, not DB paramsDecimalClosureReferencedInFilterAndProjectionIsBoundOnce— decimal closure in twoWhereclauses shares one parameterTakeClosureReferencedInFilterAndPagingUsesDistinctParameterValues— same int used inWhere(Regular) andTake(LimitOffset) stays distinctDistinctClosureValuesRemainSeparateParameters— guard: different values produce separate parameters🤖 Generated with Claude Code