| | | 1 | | using NGql.Core.Abstractions; |
| | | 2 | | using NGql.Core.Builders; |
| | | 3 | | using NGql.Core.Exceptions; |
| | | 4 | | using NGql.Core.Extensions; |
| | | 5 | | |
| | | 6 | | namespace NGql.Core.Features; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Handles merging of query definitions using different strategies |
| | | 10 | | /// </summary> |
| | | 11 | | internal static class QueryMerger |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Merges an incoming query definition into the target query definition, updating all related state. |
| | | 15 | | /// </summary> |
| | | 16 | | public static void MergeQuery( |
| | | 17 | | QueryDefinition targetDefinition, |
| | | 18 | | QueryMap queryMap, |
| | | 19 | | QueryBuilder? queryBuilder, |
| | | 20 | | in QueryDefinition incomingQuery) |
| | | 21 | | { |
| | | 22 | | // Named fragments are operation-scoped, not field-scoped: merge them even when the incoming |
| | | 23 | | // query declares nothing but fragments (an unusual but valid shape — the renderer emits |
| | | 24 | | // declared-but-unused fragment definitions). |
| | 2184 | 25 | | MergeNamedFragments(targetDefinition, incomingQuery); |
| | 2181 | 26 | | MergeVariables(targetDefinition, incomingQuery); |
| | | 27 | | |
| | 2190 | 28 | | if (incomingQuery._fields == null || incomingQuery._fields.Count == 0) return; |
| | | 29 | | |
| | 2172 | 30 | | var beforeCount = targetDefinition.Fields.Count; |
| | 2172 | 31 | | ApplyFieldMerge(targetDefinition, incomingQuery, targetDefinition.MergingStrategy, queryMap); |
| | | 32 | | |
| | 2157 | 33 | | if (targetDefinition.Fields.Count != beforeCount) |
| | | 34 | | { |
| | 1011 | 35 | | queryMap.UpdateRootMapping(targetDefinition); |
| | | 36 | | } |
| | 2157 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Merges every named fragment the incoming query declares into <paramref name="targetDefinition"/> |
| | | 41 | | /// using the target's <see cref="QueryDefinition.GetOrAddNamedFragment"/> collision semantics: |
| | | 42 | | /// same name + same <c>onType</c> reuses the existing fragment (and merges its body); same name + |
| | | 43 | | /// a DIFFERENT <c>onType</c> throws <see cref="InvalidOperationException"/>. Each fragment's body |
| | | 44 | | /// (fields, nested inline fragments, and spreads) is deep-cloned before entering the target so the |
| | | 45 | | /// merged operation never aliases the source's fragment definitions. |
| | | 46 | | /// </summary> |
| | | 47 | | private static void MergeNamedFragments(QueryDefinition targetDefinition, in QueryDefinition incomingQuery) |
| | | 48 | | { |
| | 2184 | 49 | | var incomingFragments = incomingQuery._namedFragments; |
| | 4329 | 50 | | if (incomingFragments is not { Count: > 0 }) return; |
| | | 51 | | |
| | 159 | 52 | | foreach (var incoming in incomingFragments.Values) |
| | | 53 | | { |
| | | 54 | | // Throws InvalidOperationException on a name collision with a conflicting onType — |
| | | 55 | | // reuses the existing declaration semantics rather than inventing new merge rules. |
| | 42 | 56 | | var target = targetDefinition.GetOrAddNamedFragment(incoming.Name, incoming.OnType); |
| | 39 | 57 | | MergeNamedFragmentBody(target, incoming); |
| | | 58 | | } |
| | 36 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static void MergeNamedFragmentBody(NamedFragmentDefinition target, NamedFragmentDefinition incoming) |
| | | 62 | | { |
| | 39 | 63 | | if (incoming._fields is { Count: > 0 }) |
| | | 64 | | { |
| | 33 | 65 | | var targetFields = target.GetOrCreateFieldsStore(); |
| | 168 | 66 | | foreach (var child in incoming._fields.AsSpan()) |
| | | 67 | | { |
| | 51 | 68 | | FieldBuilder.Include(targetFields, child); |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | |
| | 39 | 72 | | FieldDefinitionExtensions.MergeInlineFragmentsInto(ref target._fragments, incoming._fragments); |
| | 39 | 73 | | FieldDefinitionExtensions.MergeSpreadNamesInto(ref target._spreadFragments, incoming._spreadFragments); |
| | 39 | 74 | | } |
| | | 75 | | |
| | | 76 | | private static void MergeVariables(QueryDefinition targetDefinition, in QueryDefinition incomingQuery) |
| | | 77 | | { |
| | 2181 | 78 | | var incomingVars = incomingQuery._variables; |
| | 4329 | 79 | | if (incomingVars is null || incomingVars.Count == 0) return; |
| | | 80 | | |
| | 33 | 81 | | var targetVars = targetDefinition._variables; |
| | 33 | 82 | | if (targetVars is null) |
| | | 83 | | { |
| | 18 | 84 | | targetDefinition.Variables = new SortedSet<Variable>(incomingVars); |
| | 18 | 85 | | return; |
| | | 86 | | } |
| | | 87 | | |
| | 60 | 88 | | foreach (var v in incomingVars) |
| | 15 | 89 | | targetVars.Add(v); |
| | 15 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Mutates <paramref name="targetDefinition"/>'s root fields in place by applying every field |
| | | 94 | | /// from the incoming query according to the resolved merging strategy. Avoids the O(N) |
| | | 95 | | /// copy-out / copy-back of the existing approach so a chain of <c>Include()</c>s is O(K) per |
| | | 96 | | /// call instead of O(N+K). |
| | | 97 | | /// </summary> |
| | | 98 | | private static void ApplyFieldMerge( |
| | | 99 | | QueryDefinition targetDefinition, |
| | | 100 | | QueryDefinition incomingQuery, |
| | | 101 | | MergingStrategy rootStrategy, |
| | | 102 | | QueryMap queryMap) |
| | | 103 | | { |
| | 2172 | 104 | | var fields = targetDefinition.FieldsInternal; |
| | 2172 | 105 | | var strategy = GetEffectiveMergingStrategy(rootStrategy, incomingQuery.MergingStrategy); |
| | 2172 | 106 | | var queryName = incomingQuery.Name; |
| | | 107 | | |
| | 8679 | 108 | | foreach (var (originalFieldKey, incomingField) in incomingQuery.FieldsInternal) |
| | | 109 | | { |
| | | 110 | | switch (strategy) |
| | | 111 | | { |
| | | 112 | | case MergingStrategy.MergeByDefault: |
| | | 113 | | // FieldBuilder.Include may insert a brand-new root key (when no existing |
| | | 114 | | // field shares its Name) — resync the merge index by count on next use rather |
| | | 115 | | // than tracking this path's insertions individually, since MergeByDefault |
| | | 116 | | // never consults FindMergeTarget/the fingerprint index itself. |
| | 276 | 117 | | FieldBuilder.Include(fields, incomingField); |
| | 276 | 118 | | queryMap.SetMapping(queryName, originalFieldKey); |
| | 276 | 119 | | break; |
| | | 120 | | |
| | | 121 | | case MergingStrategy.NeverMerge: |
| | 132 | 122 | | AddFieldWithUniqueKey(targetDefinition, originalFieldKey, MarkAsNeverMerge(incomingField), queryMap, |
| | 132 | 123 | | break; |
| | | 124 | | |
| | | 125 | | case MergingStrategy.MergeByFieldPath: |
| | 1764 | 126 | | ApplyMergeByFieldPath(targetDefinition, originalFieldKey, incomingField, queryMap, queryName); |
| | 1752 | 127 | | break; |
| | | 128 | | |
| | | 129 | | default: |
| | 3 | 130 | | throw new ArgumentOutOfRangeException(nameof(rootStrategy), $"Merging strategy {strategy} is not imp |
| | | 131 | | } |
| | | 132 | | } |
| | 2157 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static void ApplyMergeByFieldPath( |
| | | 136 | | QueryDefinition targetDefinition, |
| | | 137 | | string originalFieldKey, |
| | | 138 | | FieldDefinition incomingField, |
| | | 139 | | QueryMap queryMap, |
| | | 140 | | string queryName) |
| | | 141 | | { |
| | 1764 | 142 | | var fields = targetDefinition.FieldsInternal; |
| | 1764 | 143 | | var mergeTarget = FindMergeTarget(targetDefinition, fields, incomingField); |
| | | 144 | | |
| | 1764 | 145 | | if (mergeTarget == null) |
| | | 146 | | { |
| | 786 | 147 | | AddFieldWithUniqueKey(targetDefinition, originalFieldKey, incomingField, queryMap, queryName); |
| | 786 | 148 | | return; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | try |
| | | 152 | | { |
| | | 153 | | // Mutate the existing field in place — we own the reference (we're about to overwrite the |
| | | 154 | | // dictionary entry with the same instance). Avoids cloning the entire subtree on every Include. |
| | 978 | 155 | | FieldDefinitionExtensions.MergeFieldsInPlace(mergeTarget.Value.Field, incomingField); |
| | | 156 | | |
| | | 157 | | // The merge can change the target's deep fingerprint (new argument-bearing content |
| | | 158 | | // merged deeper into an already-significant subtree — see MergeFieldsInPlace's memo |
| | | 159 | | // invalidation). The fingerprint-bucketed index must be told explicitly: the field kept |
| | | 160 | | // its identity (same reference, same key) but may now belong under a different |
| | | 161 | | // fingerprint bucket, and nothing else would ever notice the move. |
| | 966 | 162 | | targetDefinition.MergeIndex.ReindexFingerprint(fields, incomingField.Name, mergeTarget.Value.Key); |
| | 966 | 163 | | queryMap.SetMapping(queryName, mergeTarget.Value.Key); |
| | 966 | 164 | | } |
| | 12 | 165 | | catch (QueryMergeException ex) |
| | | 166 | | { |
| | 12 | 167 | | throw new QueryMergeException($"Cannot merge query '{queryName}' due to type conflicts in field '{incomingFi |
| | | 168 | | } |
| | 966 | 169 | | } |
| | | 170 | | |
| | | 171 | | private static MergingStrategy GetEffectiveMergingStrategy(MergingStrategy rootStrategy, MergingStrategy childStrate |
| | | 172 | | { |
| | 2172 | 173 | | if (childStrategy == MergingStrategy.NeverMerge) |
| | | 174 | | { |
| | 48 | 175 | | return MergingStrategy.NeverMerge; |
| | | 176 | | } |
| | | 177 | | |
| | 2124 | 178 | | return rootStrategy switch |
| | 2124 | 179 | | { |
| | 84 | 180 | | MergingStrategy.NeverMerge => MergingStrategy.NeverMerge, |
| | 282 | 181 | | MergingStrategy.MergeByDefault => childStrategy, |
| | 1758 | 182 | | _ => rootStrategy, |
| | 2124 | 183 | | }; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private static void AddFieldWithUniqueKey( |
| | | 187 | | QueryDefinition targetDefinition, |
| | | 188 | | string originalFieldKey, |
| | | 189 | | FieldDefinition incomingField, |
| | | 190 | | QueryMap queryMap, |
| | | 191 | | string queryName) |
| | | 192 | | { |
| | 918 | 193 | | var fields = targetDefinition.FieldsInternal; |
| | 918 | 194 | | var mergeIndex = targetDefinition.MergeIndex; |
| | 918 | 195 | | var uniqueKey = KeyGenerator.GenerateUniqueKey(mergeIndex, fields, incomingField._effectiveName); |
| | | 196 | | |
| | | 197 | | // Deep-clone so the target dictionary owns its subtree exclusively. Subsequent in-place |
| | | 198 | | // merges (MergeFieldsInPlace below) must not leak field additions back into the source |
| | | 199 | | // QueryBuilder that supplied incomingField. |
| | 918 | 200 | | var fieldToAdd = incomingField.DeepClone(); |
| | 918 | 201 | | if (!string.Equals(uniqueKey, originalFieldKey, StringComparison.OrdinalIgnoreCase)) |
| | | 202 | | { |
| | 711 | 203 | | fieldToAdd = fieldToAdd with { Alias = uniqueKey }; |
| | | 204 | | } |
| | | 205 | | |
| | 918 | 206 | | fields[uniqueKey] = fieldToAdd; |
| | 918 | 207 | | mergeIndex.IndexAdd(uniqueKey, fieldToAdd); |
| | 918 | 208 | | queryMap.SetMapping(queryName, uniqueKey); |
| | 918 | 209 | | } |
| | | 210 | | |
| | | 211 | | private static (string Key, FieldDefinition Field)? FindMergeTarget( |
| | | 212 | | QueryDefinition targetDefinition, |
| | | 213 | | Dictionary<string, FieldDefinition> existingFields, |
| | | 214 | | FieldDefinition incomingField) |
| | | 215 | | { |
| | | 216 | | // Deep (subtree) fingerprint — strictly refines the old own-arguments-only fingerprint by |
| | | 217 | | // also folding in every descendant whose subtree carries an argument anywhere (see |
| | | 218 | | // FieldDefinitionExtensions.ComputeDeepFingerprint for the full conservatism proof). This is |
| | | 219 | | // what lets the production shape (a root field with zero arguments, whose only discriminator |
| | | 220 | | // is a filter several levels down) get bucketed correctly instead of every candidate |
| | | 221 | | // colliding into one fingerprint and falling through to a full CanMergeFields scan. |
| | 1764 | 222 | | var incomingFingerprint = FieldDefinitionExtensions.ComputeDeepFingerprint(incomingField); |
| | | 223 | | |
| | | 224 | | // Sub-bucketed by (Name, fingerprint): narrows the candidate set to just the handful of |
| | | 225 | | // existing fields that could actually match, instead of every field sharing incomingField's |
| | | 226 | | // Name (which is exactly what makes the production shape — N fragments, one shared root |
| | | 227 | | // Name, N distinct deep filters — quadratic under Name-only bucketing). The index |
| | | 228 | | // self-heals any entry whose live fingerprint has drifted since it was bucketed (see |
| | | 229 | | // FieldMergeIndex remarks), so this can never produce a false split: every candidate |
| | | 230 | | // returned here is confirmed, from its LIVE field, to share incomingFingerprint. |
| | 1764 | 231 | | var candidateKeys = targetDefinition.MergeIndex.GetMergeCandidatesByFingerprint( |
| | 1764 | 232 | | existingFields, incomingField.Name, incomingFingerprint); |
| | 2511 | 233 | | if (candidateKeys is null) return null; |
| | | 234 | | |
| | 3108 | 235 | | foreach (var key in candidateKeys) |
| | | 236 | | { |
| | 1026 | 237 | | if (!existingFields.TryGetValue(key, out var existingField)) |
| | | 238 | | continue; |
| | | 239 | | |
| | 1026 | 240 | | if (existingField.IsNeverMerge) |
| | | 241 | | continue; |
| | | 242 | | |
| | | 243 | | // CanMergeFields remains the sole source of truth — the fingerprint match above is a |
| | | 244 | | // conservative pre-filter only. A match does NOT prove equality (a hash collision costs |
| | | 245 | | // one extra, unmodified CanMergeFields call here; it is never treated as sufficient on |
| | | 246 | | // its own). |
| | 1014 | 247 | | if (FieldDefinitionExtensions.CanMergeFields(existingField, incomingField)) |
| | 978 | 248 | | return (key, existingField); |
| | | 249 | | } |
| | | 250 | | |
| | 39 | 251 | | return null; |
| | 978 | 252 | | } |
| | | 253 | | |
| | | 254 | | private static FieldDefinition MarkAsNeverMerge(FieldDefinition field) |
| | 132 | 255 | | => field.IsNeverMerge ? field : field with { IsNeverMerge = true }; |
| | | 256 | | } |