Skip to content
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

planner: fix ref heads processing #5418

Merged

Conversation

srenatus
Copy link
Contributor

With the introduction of ref heads in #4660, the planned IR still mostly worked, but it was bypassing the CallDynamic optimization when it shouldn't have.

This commit re-works some of the rule planning to more robustly handle ref heads.

Also adds a few test cases to get a grip on what should and should not happen.

@srenatus srenatus force-pushed the sr/planner/fix-planning-ref-heads branch from 7214708 to b5a4a5c Compare November 28, 2022 11:16
@srenatus srenatus marked this pull request as ready for review November 28, 2022 11:57
@srenatus srenatus force-pushed the sr/planner/fix-planning-ref-heads branch from b5a4a5c to c1ab3b0 Compare November 28, 2022 12:01
With the introduction of ref heads in open-policy-agent#4660, the planned IR
still mostly worked, but it was bypassing the CallDynamic
optimization when it shouldn't have.

This commit re-works some of the rule planning to more robustly
handle ref heads.

Also adds a few test cases to get a grip on what should and
should not happen.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
@srenatus srenatus force-pushed the sr/planner/fix-planning-ref-heads branch from c1ab3b0 to 4834c9a Compare November 28, 2022 12:06
Copy link
Contributor Author

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 comments

default: // cut off
r = r[:len(r)-1]
}
val := p.rules.LookupOrInsert(r)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ We now take care of the last ref segment when builtin ruletrie, not when retrieving something from it.

This is what the changes comes down to:

  1. putting the rules into the rule trie without any non-string pieces:
    • so a.b.c = 1 is a different node than a.b[1] = 2
  2. taking care of building objects or not when planning any given rule set, not by passing a boolean to planRules()
  3. the optimizeLookup code flow had to be adjusted to not make the change in (1.) avoid optimizations that are desired.

default:
// Needs to be fixed if we allow non-string ref pieces, like `p.q[3][4].r = x`
pathPieces = append(pathPieces, q.String())
panic("impossible")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If our functions had "path pieces" that are not strings, they'll fail in the call_dynamic mapping lookup. For #4660, we've started out with string prefixes (not generic refs), so this should be OK.

@srenatus srenatus added this to In Progress in Open Policy Agent via automation Nov 28, 2022
Copy link
Contributor

@philipaconrad philipaconrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the battery of testcases for this PR. It looks like there was some nice refactoring done, especially in internal/planner/planner.go

@srenatus
Copy link
Contributor Author

Thanks for the review!

It looks like there was some nice refactoring done, [...]

Mostly just reverting a shoddy first try 😅 Hope this one is more robust 🤞

@srenatus srenatus merged commit 0e6cb88 into open-policy-agent:main Nov 28, 2022
Open Policy Agent automation moved this from In Progress to Done Nov 28, 2022
@srenatus srenatus deleted the sr/planner/fix-planning-ref-heads branch November 28, 2022 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants