Search in sources :

Example 1 with EvalSetMembership

use of org.projectnessie.cel.interpreter.Interpretable.EvalSetMembership in project cel-java by projectnessie.

the class InterpretableDecorator method maybeOptimizeSetMembership.

/**
 * maybeOptimizeSetMembership may convert an 'in' operation against a list to map key membership
 * test if the following conditions are true:
 *
 * <ul>
 *   <li>the list is a constant with homogeneous element types.
 *   <li>the elements are all of primitive type.
 * </ul>
 */
static Interpretable maybeOptimizeSetMembership(Interpretable i, InterpretableCall inlist) {
    Interpretable[] args = inlist.args();
    Interpretable lhs = args[0];
    Interpretable rhs = args[1];
    if (!(rhs instanceof InterpretableConst)) {
        return i;
    }
    InterpretableConst l = (InterpretableConst) rhs;
    // When the incoming binary call is flagged with as the InList overload, the value will
    // always be convertible to a `traits.Lister` type.
    Lister list = (Lister) l.value();
    if (list.size() == IntZero) {
        return newConstValue(inlist.id(), False);
    }
    IteratorT it = list.iterator();
    Type typ = null;
    Set<Val> valueSet = new HashSet<>();
    while (it.hasNext() == True) {
        Val elem = it.next();
        if (!Util.isPrimitiveType(elem)) {
            // Note, non-primitive type are not yet supported.
            return i;
        }
        if (typ == null) {
            typ = elem.type();
        } else if (!typ.typeName().equals(elem.type().typeName())) {
            return i;
        }
        valueSet.add(elem);
    }
    return new EvalSetMembership(inlist, lhs, typ.typeName(), valueSet);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) IteratorT(org.projectnessie.cel.common.types.IteratorT) Type(org.projectnessie.cel.common.types.ref.Type) Lister(org.projectnessie.cel.common.types.traits.Lister) EvalSetMembership(org.projectnessie.cel.interpreter.Interpretable.EvalSetMembership) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 IteratorT (org.projectnessie.cel.common.types.IteratorT)1 Type (org.projectnessie.cel.common.types.ref.Type)1 Val (org.projectnessie.cel.common.types.ref.Val)1 Lister (org.projectnessie.cel.common.types.traits.Lister)1 EvalSetMembership (org.projectnessie.cel.interpreter.Interpretable.EvalSetMembership)1 InterpretableConst (org.projectnessie.cel.interpreter.Interpretable.InterpretableConst)1