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

Inconsistent OPA number handling #5585

Closed
alvarogomez93 opened this issue Jan 23, 2023 · 2 comments · Fixed by #5604
Closed

Inconsistent OPA number handling #5585

alvarogomez93 opened this issue Jan 23, 2023 · 2 comments · Fixed by #5604
Labels

Comments

@alvarogomez93
Copy link
Contributor

Short description

When using aliases or variables from a path, number type coercion doesn't work consistently. You get into a scenario where 1.0 == 1 evals to false. This can be avoided assigning the aliased value to an intermediary rule, however this behavior is not predictable and should get fixed.

Example:

Input:

{
    "oneDotZero": 1.0
}

Rule:

default bug := false
bug {
	input.oneDotZero == 1.0 # if you set this to 1 it returns true
}

Output:

{
    "bug": false,
}

Steps To Reproduce

Playground where this is reproduced and workaround is provided https://play.openpolicyagent.org/p/RbcP7xO0eW (replace 1 for 1.0 in oneDotZero, rego playground changes this automatically when publishing)

Expected behavior

Using aliases should behave similarly to other expressions

@anderseknert
Copy link
Member

anderseknert commented Jan 23, 2023

Adding to this, evaluation differes between rule head and rule body in this regard: https://play.openpolicyagent.org/p/DujkIZh56k

With input as {"i":1.0}:

package play

# true
x := input.i == 1.0

# not true
allow {
	input.i == 1.0
}

The compiler rewrites the former to use equal, and the latter to use unification (=):

package play

x := __local0__ { true; __local1__ = input.i; equal(__local1__, 1.0, __local0__) }
allow = true { input.i = 1.0 }

@srenatus
Copy link
Contributor

The compiler rewrites the former to use equal, and the latter to use unification (=)

Huh that's surprising. I would have expected the stage that rewrites equal to unification to cover both cases.

ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Jan 27, 2023
ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Jan 27, 2023
Earlier the trie's scalar used map[ast.Value]trieNode.
This resulted in unexpected key comparison results such
as 1 != 1.0 when key types were ast.Number for example.
This change updates the scalar to use a util.HashMap instead
that will utilize ast.Compare to perfom key comparisons.

Fixes: open-policy-agent#5585

Signed-off-by: Ashutosh Narkar <[email protected]>
ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Jan 30, 2023
Earlier the trie's scalar used map[ast.Value]trieNode.
This resulted in unexpected key comparison results such
as 1 != 1.0 when key types were ast.Number for example.
This change updates the scalar to use a util.HashMap instead
that will utilize ast.Compare to perfom key comparisons.

Fixes: open-policy-agent#5585

Signed-off-by: Ashutosh Narkar <[email protected]>
ashutosh-narkar added a commit that referenced this issue Feb 1, 2023
Earlier the trie's scalar used map[ast.Value]trieNode.
This resulted in unexpected key comparison results such
as 1 != 1.0 when key types were ast.Number for example.
This change updates the scalar to use a util.HashMap instead
that will utilize ast.Compare to perfom key comparisons.

Fixes: #5585

Signed-off-by: Ashutosh Narkar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants