-
Notifications
You must be signed in to change notification settings - Fork 30
Resolve nested sourced_from fields into update targets on the root indexed type #1247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
myronmarston
merged 5 commits into
block:main
from
ellisandrews-toast:nested-update-target-resolver
Jun 12, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0072a04
Resolve nested sourced_from fields into update targets on the root in…
ellisandrews-toast 781f597
Address PR review: share update-target resolution logic and standardi…
ellisandrews-toast 754a054
Add nested sourced_from tests for nested source paths and name_in_ind…
ellisandrews-toast 5c16de3
Merge branch 'main' into nested-update-target-resolver
ellisandrews-toast a2e964d
Address PR feedback
ellisandrews-toast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
134 changes: 134 additions & 0 deletions
134
..._definition/lib/elastic_graph/schema_definition/indexing/nested_update_target_resolver.rb
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # Copyright 2024 - 2026 Block, Inc. | ||
| # | ||
| # Use of this source code is governed by an MIT-style | ||
| # license that can be found in the LICENSE file or at | ||
| # https://opensource.org/licenses/MIT. | ||
| # | ||
| # frozen_string_literal: true | ||
|
|
||
| require "elastic_graph/schema_artifacts/runtime_metadata/params" | ||
| require "elastic_graph/schema_artifacts/runtime_metadata/sourced_from_nested_params" | ||
| require "elastic_graph/schema_definition/indexing/update_target_factory" | ||
| require "elastic_graph/schema_definition/indexing/update_target_resolver_support" | ||
|
|
||
| module ElasticGraph | ||
| module SchemaDefinition | ||
| module Indexing | ||
| # Resolves a relationship and a set of `sourced_from` fields into an `UpdateTarget` that instructs the | ||
| # indexer how to update a type from the related type's source events. This handles the *nested* case, | ||
| # where the `sourced_from` fields live on a type embedded within an indexed type (reached via a | ||
| # `parent_relationship` chain) and the target updates the root indexed type the embedded type nests | ||
| # within. (The *top-level* case—`sourced_from` fields directly on an indexed type—is handled by | ||
| # `TopLevelUpdateTargetResolver`.) | ||
| # | ||
| # @private | ||
| class NestedUpdateTargetResolver | ||
| def initialize( | ||
| object_type:, | ||
| sourced_fields:, | ||
| resolved_chain:, | ||
| field_path_resolver:, | ||
| schema_def_state: | ||
| ) | ||
| @object_type = object_type | ||
| @sourced_fields = sourced_fields | ||
| @resolved_chain = resolved_chain | ||
| @field_path_resolver = field_path_resolver | ||
| @schema_def_state = schema_def_state | ||
| end | ||
|
|
||
| # Resolves the chain and `sourced_fields` into an `UpdateTarget` on the root indexed type, | ||
| # validating everything along the way. | ||
| # | ||
| # Returns a tuple of the `update_target` (if valid) and a list of errors. | ||
| def resolve | ||
| relationship_errors = validate_relationships | ||
| field_params, field_params_errors = UpdateTargetResolverSupport.resolve_sourced_field_params( | ||
| object_type: object_type, | ||
| related_type: related_type, | ||
| sourced_fields: sourced_fields, | ||
| field_path_resolver: field_path_resolver | ||
| ) | ||
| routing_value_source, routing_error = resolve_field_source(UpdateTargetResolverSupport::RoutingSourceAdapter) | ||
| rollover_timestamp_value_source, rollover_timestamp_error = resolve_field_source(UpdateTargetResolverSupport::RolloverTimestampSourceAdapter) | ||
| # Routing/rollover values resolve from `equivalent_field`s on the root relationship, so they are | ||
| # validated there (matching how `TopLevelUpdateTargetResolver` validates its own relationship). | ||
| equivalent_field_errors = root_relationship.validate_equivalent_fields(field_path_resolver) | ||
| has_had_multiple_sources_errors = UpdateTargetResolverSupport.validate_has_had_multiple_sources(root_index, root_type, relationship) | ||
|
|
||
| all_errors = relationship_errors + field_params_errors + equivalent_field_errors + has_had_multiple_sources_errors + | ||
| [routing_error, rollover_timestamp_error].compact | ||
|
|
||
| if all_errors.empty? | ||
| update_target = UpdateTargetFactory.new_normal_indexing_update_target( | ||
| type: root_type.name, | ||
| relationship: resolved_chain.qualified_relationship, | ||
| id_source: root_relationship.foreign_key, | ||
| sourced_from_nested_params: SchemaArtifacts::RuntimeMetadata::SourcedFromNestedParams.new( | ||
| field_params: field_params, | ||
| path_identifier_params: resolved_chain.path_identifier_params | ||
| ), | ||
| routing_value_source: routing_value_source, | ||
| rollover_timestamp_value_source: rollover_timestamp_value_source | ||
| ) | ||
| end | ||
|
|
||
| [update_target, all_errors] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @dynamic object_type, sourced_fields, resolved_chain, field_path_resolver, schema_def_state | ||
| attr_reader :object_type, :sourced_fields, :resolved_chain, :field_path_resolver, :schema_def_state | ||
|
|
||
| # The leaf relationship the chain was resolved from — the one backing this type's `sourced_from` fields. | ||
| def relationship | ||
| resolved_chain.leaf_relationship | ||
| end | ||
|
|
||
| def root_relationship | ||
| resolved_chain.root_relationship | ||
| end | ||
|
|
||
| def root_type | ||
| root_relationship.parent_type | ||
| end | ||
|
|
||
| def root_index | ||
| resolved_chain.root_index | ||
| end | ||
|
|
||
| def related_type | ||
| @related_type ||= schema_def_state.object_types_by_name.fetch(relationship.related_type.unwrap_non_null.name) | ||
| end | ||
|
|
||
| # Applies validations on the relationships backing nested `sourced_from` fields. Only the leaf must be | ||
| # `relates_to_one` (it's where a value is sourced through), but every relationship in the chain joins on | ||
| # a foreign key that routes the source event, so each must be routable (inbound foreign key, no filter). | ||
| def validate_relationships | ||
| leaf_prefix = UpdateTargetResolverSupport.relationship_error_prefix(relationship, sourced_fields) | ||
|
|
||
| UpdateTargetResolverSupport.validate_relationship_cardinality(relationship, error_prefix: leaf_prefix) + | ||
| resolved_chain.relationships.flat_map do |chain_relationship| | ||
| error_prefix = UpdateTargetResolverSupport.relationship_error_prefix(chain_relationship, sourced_fields) | ||
| UpdateTargetResolverSupport.validate_relationship_routability(chain_relationship, error_prefix: error_prefix) | ||
| end | ||
| end | ||
|
|
||
| # Resolves a routing/rollover field source via the shared helper, supplying the root type, index, and | ||
| # relationship — the update target updates the root indexed type via the root relationship, so routing | ||
| # and rollover (and the `equivalent_field` config) are resolved there. | ||
| def resolve_field_source(adapter) | ||
| UpdateTargetResolverSupport.resolve_field_source( | ||
| adapter, | ||
| relationship: root_relationship, | ||
| index_def: root_index, | ||
| related_type: related_type, | ||
| field_path_resolver: field_path_resolver, | ||
| updated_type: root_type | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
85 changes: 85 additions & 0 deletions
85
...ma_definition/lib/elastic_graph/schema_definition/indexing/resolved_relationship_chain.rb
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright 2024 - 2026 Block, Inc. | ||
| # | ||
| # Use of this source code is governed by an MIT-style | ||
| # license that can be found in the LICENSE file or at | ||
| # https://opensource.org/licenses/MIT. | ||
| # | ||
| # frozen_string_literal: true | ||
|
|
||
| require "elastic_graph/schema_artifacts/runtime_metadata/params" | ||
| require "elastic_graph/schema_artifacts/runtime_metadata/sourced_from_nested_path_segment" | ||
| require "elastic_graph/support/memoizable_data" | ||
|
|
||
| module ElasticGraph | ||
| module SchemaDefinition | ||
| module Indexing | ||
| # The result of resolving a relationship chain. | ||
| # | ||
| # @private | ||
| class ResolvedRelationshipChain < Support::MemoizableData.define( | ||
| :relationships, # Array<Relationship> - every relationship in the chain, ordered root-to-leaf | ||
| :path_segments # Array<PathSegment> - the embedding fields to descend, ordered root-to-leaf | ||
| ) | ||
| # The relationship the chain terminated at on the root indexed type. | ||
| def root_relationship | ||
| relationships.first | ||
| end | ||
|
|
||
| # The relationship the chain was resolved from — backs the `sourced_from` field(s). | ||
| def leaf_relationship | ||
| relationships.last | ||
| end | ||
|
|
||
| # The index the chain terminates at — where the root indexed type's documents (and their nested | ||
| # elements) live, and where the chain's navigation path is registered. The chain always terminates at | ||
| # an indexed type (enforced when it is resolved), so this is never `nil`. | ||
| def root_index | ||
| root_relationship.parent_type.index_def # : Index | ||
| end | ||
|
|
||
| # Records this chain's navigation path on its root index, so the painless script can locate the | ||
| # nested element to update at index time. | ||
| def register_on_root_index | ||
| root_index.register_sourced_from_nested_paths(qualified_relationship, sourced_from_nested_paths) | ||
| end | ||
|
|
||
| # The leaf relationship name qualified by its embedding-field path (hence unique per resolved chain) | ||
| def qualified_relationship | ||
| @qualified_relationship ||= | ||
| (path_segments.map { |segment| segment.field.name_in_index } + [leaf_relationship.name_in_index]).join(".") | ||
| end | ||
|
|
||
| # The runtime-metadata segments the painless script uses to navigate this chain: a `ListPathSegment` for | ||
| # each list embedding field (carrying the source field that matches the element) and an `ObjectPathSegment` | ||
| # for each object embedding field. | ||
| def sourced_from_nested_paths | ||
| @sourced_from_nested_paths ||= path_segments.map do |segment| | ||
| if (source_field = segment.source_field_name) | ||
| SchemaArtifacts::RuntimeMetadata::ListPathSegment.new( | ||
| field: segment.field.name_in_index, | ||
| source_field: source_field | ||
| ) | ||
| else | ||
| SchemaArtifacts::RuntimeMetadata::ObjectPathSegment.new( | ||
| field: segment.field.name_in_index | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # The params identifying which nested element to update at each level: one entry per list segment, | ||
| # pulling the matching value from the segment's foreign key on the source event. Object segments have no | ||
| # ambiguity, so they contribute no identifier. | ||
| def path_identifier_params | ||
| @path_identifier_params ||= path_segments.filter_map do |segment| | ||
| source_field = segment.source_field_name | ||
| next unless source_field | ||
|
|
||
| param = SchemaArtifacts::RuntimeMetadata::DynamicParam.new(source_path: source_field, cardinality: :one) | ||
| [source_field, param] | ||
| end.to_h | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.