opa icon indicating copy to clipboard operation
opa copied to clipboard

Partial Evaluation : wrong local names

Open fab29p opened this issue 3 years ago • 8 comments

Short description

Wrong local names in partial evaluation

Steps To Reproduce

Using partial evaluation with this Rego Rules in a file rules.rego

package data.play

u[{"a":a}] { some {"a":a} in data.tables.x }

b[{"a":a}] { some {"a":a} in data.tables.x films := [1 | u[{"a":a}]] }

test[{"a":a}] { b[{"a":a}] }

Command line :

./opa eval --data rules.rego -f source -u data.tables -p 'data.play.test'

Output

package partial.play

test[{"a": a1}] { {"a": a1} = data.tables.x[__local74__2] __local76__2 = [1 | data.play.u[{"a": __local75__2}]; __local75__2 = a12]

Expected behavior

a12 should be a1

fab29p avatar Oct 09 '22 22:10 fab29p

I've been trying to investigate this, but I'm stuck fairly early on:

With this policy (I've added a future keyword import for in),

package data.play

import future.keywords.in

u[{"a": a}] {
	some {"a": a} in data.tables.x
}

b[{"a": a}] {
	some {"a": a} in data.tables.x
	films := [1 | u[{"a": a}]]
}

test[{"a": a}] {
	b[{"a": a}]
}

I only ever seem to get empty results, using any OPA 0.4x.y version.

$ opa eval --data scratch/issue-5226.rego -fsource -u data.tables -p 'data.play.test' 

Which OPA version are you using? Is there more to your policy that is missing here...?

srenatus avatar Oct 10 '22 07:10 srenatus

Hi.

Try with « package play » :-)

Le 10 oct. 2022 à 09:38, Stephan Renatus @.***> a écrit :

I've been trying to investigate this, but I'm stuck fairly early on:

With this policy (I've added a future keyword import for in),

package data.play

import future.keywords.in

u[{"a": a}] { some {"a": a} in data.tables.x }

b[{"a": a}] { some {"a": a} in data.tables.x films := [1 | u[{"a": a}]] }

test[{"a": a}] { b[{"a": a}] } I only ever seem to get empty results, using any OPA 0.4x.y version.

$ opa eval --data scratch/issue-5226.rego -fsource -u data.tables -p 'data.play.test' Which OPA version are you using? Is there more to your policy that is missing here...?

— Reply to this email directly, view it on GitHub https://github.com/open-policy-agent/opa/issues/5226#issuecomment-1272898816, or unsubscribe https://github.com/notifications/unsubscribe-auth/A3QVXXKE7NPK3CTRFQUXWRTWCPBWJANCNFSM6AAAAAARA4DV7I. You are receiving this because you authored the thread.

fab29p avatar Oct 10 '22 08:10 fab29p

And sorry for the wrong copy/pasting in my original post regarding the package name.

fab29plou avatar Oct 10 '22 08:10 fab29plou

🤦 OK it's been early. On a Monday.

srenatus avatar Oct 10 '22 08:10 srenatus

ℹ️ This bug goes waaaay back. The oldest version I've tried is 0.29.4 and it is there, too.

srenatus avatar Oct 10 '22 09:10 srenatus

Example can be made simpler

package play

import future.keywords.in

b[a] {
  some a in data.tables.x
  f := [1 | a]
}
test[a] {
     b[a]
}

fab29plou avatar Oct 10 '22 11:10 fab29plou

Do you even need the some .. in ...? I also think it's sufficient that a is unknown 👇

package play

b[a] {
  a := input
  f := [1 | a]
}
test[a] {
     b[a]
}
open-policy-agent/opa % opa-0.29.4 eval --data 5226.rego -fsource -p 'data.play.test'            
# Query 1
data.partial.play.test

# Module 1
package partial.play

test[a1] {
	a1 = input
	__local1__2 = [1 |
		__local0__2
		__local0__2 = a12
	]

	a1
}

srenatus avatar Oct 10 '22 11:10 srenatus

This issue (or at least a flavor of it) might have been solved in OPA 0.57.0, where changes have been made to partial eval to accommodate general refs in rule heads:

# Query 1
data.partial.play.test

# Module 1
package partial.play

test[__local0__2] {
	__local0__2 = input
	__local1__2 = [1 | __local0__2]
	__local0__2
}

johanfylling avatar Oct 02 '23 14:10 johanfylling