fix: apply per-candidate index when traversing arrays/maps by a streamed index#2750
Open
StressTestor wants to merge 1 commit into
Open
fix: apply per-candidate index when traversing arrays/maps by a streamed index#2750StressTestor wants to merge 1 commit into
StressTestor wants to merge 1 commit into
Conversation
…med index `$o[.]` over a streamed context (e.g. `keys[] | $o[.]`) only returned the first match. The index expression yields one index set per incoming candidate, but traverseArrayOperator used only the first set (rhs.MatchingNodes.Front()), dropping the rest. Pair each index set with its candidate: when the LHS has one node per candidate (e.g. `.[] | .[idx]`) each node is traversed with its own index set; when the LHS collapses to a single node (a variable) it is traversed against every index set. Covers both arrays and maps. Fixes mikefarah#2593.
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.
what
$o[.]over a streamed context only returned the first match. this fixes it.it hits maps the same way:
echo '{"x":1,"y":2}' | yq '. as $o | keys[] | $o[.]'returned only1.why
the index in
$o[.]is a collect expression that yields one index set per incoming candidate (forkeys[]streaming0then1, it yields[0]then[1]).traverseArrayOperatorevaluated it over the whole context but then used onlyrhs.MatchingNodes.Front(), the first index set, so every candidate but the first was dropped. the. as $k | $o[.]workaround worked becauseassplits the stream, so each call sees a single candidate andFront()is enough.fix
pair each index set with its candidate (
pkg/yqlib/operator_traverse_path.go):.[] | .[idx]), traverse each LHS node with its own index set$o), traverse it against every index setLHS and RHS are still each evaluated once over the whole context, so positional operators like
split_dockeep their document index.tests
regression scenarios for the array and map forms in
operator_traverse_path_test.go. fullgo test ./...passes, and the existing traverse and split-doc scenarios are unchanged.Fixes #2593.