Skip to content

Partial evaluation with a context object fails for folded expressions #1124

@dbuduev

Description

@dbuduev

Describe the bug
When partially evaluating (ContextEval) an expression like groups.exists(t, t == id) where id is declared as an unknown pattern, CEL fails with no such attribute(s): id.

To Reproduce
Check which components this affects:

  • parser
  • checker
  • interpreter

Sample expression and input that reproduces the issue:

groups.exists(t, t == id)

Test setup:

package main

import (
	"context"
	"log"

	"github.com/google/cel-go/cel"
	"github.com/google/cel-go/checker/decls"
)

func main() {
	env, err := cel.NewEnv(cel.Declarations(
		decls.NewVar("groups", decls.NewListType(decls.Int)),
		decls.NewVar("id", decls.Int),
	))
	exitOnErr(err)

	pvars, err := cel.PartialVars(
		map[string]any{
			"groups": []int{1, 2, 3},
		},
		cel.AttributePattern("id"))

	exitOnErr(err)

	ast, iss := env.Compile(`groups.exists(t, t == id)`)
	exitOnErr(iss.Err())

	prg, err := env.Program(ast, cel.EvalOptions(cel.OptTrackState, cel.OptPartialEval), cel.InterruptCheckFrequency(100))
	exitOnErr(err)

	log.Println("calling prg.Eval")
	_, _, err = prg.Eval(pvars)
	exitOnErr(err)
	log.Println("calling prg.ContextEval")
	_, _, err = prg.ContextEval(context.Background(), pvars)
	exitOnErr(err)
}

func exitOnErr(err error) {
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
}

Expected behavior
Both Eval and ContextEval should successfully evaluate the above program, but prg.ContextEval fails with no such attribute(s): id.

Additional context
It's only folded expressions that can't be partially evaluated by the ContextEval method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions