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);
}
Aggregations