|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.runtime.planner; |
| 16 | + |
| 17 | +import static java.util.Objects.requireNonNull; |
| 18 | + |
| 19 | +import dev.cel.runtime.RuntimeEquality; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +/** |
| 27 | + * Unique cache key for an asynchronous function invocation at a given AST expression node. |
| 28 | + * |
| 29 | + * <p>Equality delegates to {@link RuntimeEquality}, except that {@link Double#NaN} and {@link |
| 30 | + * Float#NaN} argument values compare equal so that re-evaluating a node with a NaN argument hits |
| 31 | + * its existing call record. Map keys do not get this override because lookup goes through {@link |
| 32 | + * RuntimeEquality#findInMap}. |
| 33 | + */ |
| 34 | +final class AsyncCallKey { |
| 35 | + private final long exprId; |
| 36 | + private final String functionName; |
| 37 | + private final String overloadId; |
| 38 | + private final Object[] args; |
| 39 | + private final RuntimeEquality runtimeEquality; |
| 40 | + private final int hashCode; |
| 41 | + |
| 42 | + static AsyncCallKey create( |
| 43 | + long exprId, |
| 44 | + String functionName, |
| 45 | + String overloadId, |
| 46 | + Object[] args, |
| 47 | + RuntimeEquality runtimeEquality) { |
| 48 | + return new AsyncCallKey(exprId, functionName, overloadId, args, runtimeEquality); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean equals(Object o) { |
| 53 | + if (this == o) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + if (!(o instanceof AsyncCallKey)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + AsyncCallKey other = (AsyncCallKey) o; |
| 60 | + if (exprId != other.exprId |
| 61 | + || !functionName.equals(other.functionName) |
| 62 | + || !overloadId.equals(other.overloadId) |
| 63 | + || args.length != other.args.length) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + for (int i = 0; i < args.length; i++) { |
| 67 | + if (!argEquals(args[i], other.args[i], runtimeEquality)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int hashCode() { |
| 76 | + return hashCode; |
| 77 | + } |
| 78 | + |
| 79 | + private static boolean argEquals(Object a, Object b, RuntimeEquality runtimeEquality) { |
| 80 | + if (a == b) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + if (a == null || b == null) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + if (a instanceof Number && b instanceof Number) { |
| 87 | + double da = ((Number) a).doubleValue(); |
| 88 | + double db = ((Number) b).doubleValue(); |
| 89 | + // CEL defines NaN != NaN; override so a node re-evaluated with NaN hits its existing record. |
| 90 | + if (Double.isNaN(da) && Double.isNaN(db)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + if (da == 0.0d && db == 0.0d) { |
| 94 | + return celEquals(normalizeSignedZero(a, da), normalizeSignedZero(b, db), runtimeEquality); |
| 95 | + } |
| 96 | + return celEquals(a, b, runtimeEquality); |
| 97 | + } |
| 98 | + if (a instanceof List && b instanceof List) { |
| 99 | + List<?> listA = (List<?>) a; |
| 100 | + List<?> listB = (List<?>) b; |
| 101 | + if (listA.size() != listB.size()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + Iterator<?> iterA = listA.iterator(); |
| 105 | + Iterator<?> iterB = listB.iterator(); |
| 106 | + while (iterA.hasNext() && iterB.hasNext()) { |
| 107 | + if (!argEquals(iterA.next(), iterB.next(), runtimeEquality)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + return true; |
| 112 | + } |
| 113 | + if (a instanceof Map && b instanceof Map) { |
| 114 | + Map<?, ?> mapA = (Map<?, ?>) a; |
| 115 | + Map<?, ?> mapB = (Map<?, ?>) b; |
| 116 | + if (mapA.size() != mapB.size()) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + for (Map.Entry<?, ?> entry : mapA.entrySet()) { |
| 120 | + Optional<Object> valB = findInMap(mapB, entry.getKey(), runtimeEquality); |
| 121 | + if (valB.isPresent()) { |
| 122 | + if (!argEquals(entry.getValue(), valB.get(), runtimeEquality)) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + } else { |
| 126 | + if (!mapB.containsKey(entry.getKey()) |
| 127 | + || entry.getValue() != null |
| 128 | + || mapB.get(entry.getKey()) != null) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + return true; |
| 134 | + } |
| 135 | + if (a instanceof byte[] && b instanceof byte[]) { |
| 136 | + return Arrays.equals((byte[]) a, (byte[]) b); |
| 137 | + } |
| 138 | + if (a instanceof Object[] && b instanceof Object[]) { |
| 139 | + return Arrays.deepEquals((Object[]) a, (Object[]) b); |
| 140 | + } |
| 141 | + return celEquals(a, b, runtimeEquality); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Applies CEL heterogeneous equality, treating incomparable argument pairs (which throw unchecked |
| 146 | + * exceptions from {@link RuntimeEquality#objectEquals}) as unequal. |
| 147 | + */ |
| 148 | + private static boolean celEquals(Object a, Object b, RuntimeEquality runtimeEquality) { |
| 149 | + try { |
| 150 | + return runtimeEquality.objectEquals(a, b); |
| 151 | + } catch (RuntimeException e) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Normalizes {@code -0.0} to {@code 0.0} before comparison, because {@link |
| 158 | + * RuntimeEquality#objectEquals} returns {@code false} for cross-type pairs such as {@code (0L, |
| 159 | + * -0.0d)}. |
| 160 | + */ |
| 161 | + private static Object normalizeSignedZero(Object value, double asDouble) { |
| 162 | + if (asDouble == 0.0d && (value instanceof Double || value instanceof Float)) { |
| 163 | + return 0.0d; |
| 164 | + } |
| 165 | + return value; |
| 166 | + } |
| 167 | + |
| 168 | + private static Optional<Object> findInMap( |
| 169 | + Map<?, ?> map, Object key, RuntimeEquality runtimeEquality) { |
| 170 | + try { |
| 171 | + return runtimeEquality.findInMap(map, key); |
| 172 | + } catch (RuntimeException e) { |
| 173 | + return Optional.empty(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private static int computeHashCode( |
| 178 | + long exprId, |
| 179 | + String functionName, |
| 180 | + String overloadId, |
| 181 | + Object[] args, |
| 182 | + RuntimeEquality runtimeEquality) { |
| 183 | + int result = (int) (exprId ^ (exprId >>> 32)); |
| 184 | + result = 31 * result + functionName.hashCode(); |
| 185 | + result = 31 * result + overloadId.hashCode(); |
| 186 | + for (Object arg : args) { |
| 187 | + result = 31 * result + hashArg(arg, runtimeEquality); |
| 188 | + } |
| 189 | + return result; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Hashes a single argument consistently with {@link #argEquals}. |
| 194 | + * |
| 195 | + * <p>Does not delegate to {@link RuntimeEquality#hashCode} because that method hashes {@code |
| 196 | + * -0.0} and {@code 0.0} differently, which would break the {@link Object#hashCode} contract for |
| 197 | + * keys containing signed zero. |
| 198 | + */ |
| 199 | + private static int hashArg(Object arg, RuntimeEquality runtimeEquality) { |
| 200 | + if (arg == null) { |
| 201 | + return 0; |
| 202 | + } |
| 203 | + if (arg instanceof Number) { |
| 204 | + double d = ((Number) arg).doubleValue(); |
| 205 | + // Normalize -0.0d to +0.0d so that values CEL considers equal hash identically. |
| 206 | + if (d == 0.0d) { |
| 207 | + d = 0.0d; |
| 208 | + } |
| 209 | + return Double.hashCode(d); |
| 210 | + } |
| 211 | + if (arg instanceof Iterable) { |
| 212 | + int h = 1; |
| 213 | + for (Object elem : (Iterable<?>) arg) { |
| 214 | + h = h * 31 + hashArg(elem, runtimeEquality); |
| 215 | + } |
| 216 | + return h; |
| 217 | + } |
| 218 | + if (arg instanceof Map) { |
| 219 | + int h = 0; |
| 220 | + for (Map.Entry<?, ?> entry : ((Map<?, ?>) arg).entrySet()) { |
| 221 | + h += hashArg(entry.getKey(), runtimeEquality) ^ hashArg(entry.getValue(), runtimeEquality); |
| 222 | + } |
| 223 | + return h; |
| 224 | + } |
| 225 | + if (arg instanceof byte[]) { |
| 226 | + return Arrays.hashCode((byte[]) arg); |
| 227 | + } |
| 228 | + if (arg instanceof Object[]) { |
| 229 | + return Arrays.deepHashCode((Object[]) arg); |
| 230 | + } |
| 231 | + return runtimeEquality.hashCode(arg); |
| 232 | + } |
| 233 | + |
| 234 | + private AsyncCallKey( |
| 235 | + long exprId, |
| 236 | + String functionName, |
| 237 | + String overloadId, |
| 238 | + Object[] args, |
| 239 | + RuntimeEquality runtimeEquality) { |
| 240 | + this.exprId = exprId; |
| 241 | + this.functionName = requireNonNull(functionName); |
| 242 | + this.overloadId = requireNonNull(overloadId); |
| 243 | + this.args = args.clone(); |
| 244 | + this.runtimeEquality = requireNonNull(runtimeEquality); |
| 245 | + this.hashCode = computeHashCode(exprId, functionName, overloadId, this.args, runtimeEquality); |
| 246 | + } |
| 247 | +} |
0 commit comments