Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ cel_android_library(
exports = ["//common/src/main/java/dev/cel/common/internal:comparison_functions_android"],
)

java_library(
name = "map_keys",
exports = ["//common/src/main/java/dev/cel/common/internal:map_keys"],
)

cel_android_library(
name = "map_keys_android",
exports = ["//common/src/main/java/dev/cel/common/internal:map_keys_android"],
)

java_library(
name = "converter",
# used_by_android
Expand Down
24 changes: 24 additions & 0 deletions common/src/main/java/dev/cel/common/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ cel_android_library(
],
)

java_library(
name = "map_keys",
srcs = ["MapKeys.java"],
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

cel_android_library(
name = "map_keys_android",
srcs = ["MapKeys.java"],
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
)

java_library(
name = "converter",
srcs = [
Expand Down
47 changes: 47 additions & 0 deletions common/src/main/java/dev/cel/common/internal/MapKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.internal;

import com.google.common.primitives.UnsignedLong;
import com.google.errorprone.annotations.CheckReturnValue;
import dev.cel.common.annotations.Internal;

/**
* Checks for CEL map keys.
*
* <p>CEL Library Internals. Do Not Use.
*/
@CheckReturnValue
@Internal
public final class MapKeys {

/** Whether {@code key} has a type CEL allows for map keys: int, uint, bool or string. */
public static boolean isValidKeyType(Object key) {
return key instanceof Long
|| key instanceof UnsignedLong
|| key instanceof Boolean
|| key instanceof String;
}

/**
* Returns {@code key}, with an int and a uint of the same value, which are the same CEL map key,
* made equal.
*/
public static Object normalize(Object key) {
return key instanceof Long && (Long) key >= 0 ? UnsignedLong.valueOf((Long) key) : key;
}

private MapKeys() {}
}
2 changes: 2 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ java_library(
"//common/ast",
"//common/exceptions:duplicate_key",
"//common/exceptions:invalid_argument",
"//common/internal:map_keys",
"//runtime:accumulated_unknowns",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down Expand Up @@ -863,6 +864,7 @@ cel_android_library(
"//common/ast:ast_android",
"//common/exceptions:duplicate_key",
"//common/exceptions:invalid_argument",
"//common/internal:map_keys_android",
"//runtime:interpretable_android",
"//runtime/src/main/java/dev/cel/runtime:accumulated_unknowns_android",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand Down
27 changes: 5 additions & 22 deletions runtime/src/main/java/dev/cel/runtime/planner/EvalCreateMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.common.primitives.UnsignedLong;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.exceptions.CelDuplicateKeyException;
import dev.cel.common.exceptions.CelInvalidArgumentException;
import dev.cel.common.internal.MapKeys;
import dev.cel.runtime.AccumulatedUnknowns;
import dev.cel.runtime.GlobalResolver;
import java.util.HashSet;
Expand Down Expand Up @@ -77,9 +77,8 @@ Object evalInternal(GlobalResolver resolver, ExecutionFrame frame) {

Optional<?> opt = (Optional<?>) val;
if (!opt.isPresent()) {
// This is a no-op currently but will be semantically correct when extended proto
// support allows proto mutation.
keysSeen.remove(key);
// The key isn't set, so a later entry may use it.
keysSeen.remove(MapKeys.normalize(key));
continue;
}
val = opt.get();
Expand All @@ -97,29 +96,13 @@ Object evalInternal(GlobalResolver resolver, ExecutionFrame frame) {

private static void validateAndTrackKey(
Set<Object> keysSeen, PlannedInterpretable keyInterpretable, Object key) {
if (!(key instanceof String
|| key instanceof Long
|| key instanceof UnsignedLong
|| key instanceof Boolean)) {
if (!MapKeys.isValidKeyType(key)) {
throw new LocalizedEvaluationException(
new CelInvalidArgumentException("Unsupported key type: " + key),
keyInterpretable.expr().id());
}

boolean isDuplicate = !keysSeen.add(key);
if (!isDuplicate) {
if (key instanceof Long) {
long longVal = (Long) key;
if (longVal >= 0) {
isDuplicate = keysSeen.contains(UnsignedLong.valueOf(longVal));
}
} else if (key instanceof UnsignedLong) {
UnsignedLong ulongVal = (UnsignedLong) key;
isDuplicate = keysSeen.contains(ulongVal.longValue());
}
}

if (isDuplicate) {
if (!keysSeen.add(MapKeys.normalize(key))) {
throw new LocalizedEvaluationException(
CelDuplicateKeyException.of(key), keyInterpretable.expr().id());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,26 @@ public void plan_createMap_containsDuplicateKey_throws() throws Exception {
.contains("evaluation error at <input>:20: duplicate map key [true]");
}

@Test
public void plan_createMap_negativeIntAndLargeUintKeys_distinct() throws Exception {
CelAbstractSyntaxTree ast = compile("{-1: 'a', 18446744073709551615u: 'b'}");
Program program = PLANNER.plan(ast);

Map<?, ?> result = (Map<?, ?>) program.eval();

assertThat(result).containsExactly(-1L, "a", UnsignedLong.MAX_VALUE, "b");
}

@Test
public void plan_createMap_absentOptionalEntry_keyNotTaken() throws Exception {
CelAbstractSyntaxTree ast = CEL_COMPILER.parse("{?1: opt_var, 1u: 'a'}").getAst();
Program program = PLANNER.plan(ast);

Map<?, ?> result = (Map<?, ?>) program.eval(ImmutableMap.of("opt_var", Optional.empty()));

assertThat(result).containsExactly(UnsignedLong.ONE, "a");
}

@Test
public void plan_createMap_unsupportedKeyType_throws() throws Exception {
CelAbstractSyntaxTree ast = compile("{1.0: 'foo'}");
Expand Down
Loading