diff --git a/test/fuzz/fuzz_test.go b/test/fuzz/fuzz_test.go index 9616b1ea..e12c4d8e 100644 --- a/test/fuzz/fuzz_test.go +++ b/test/fuzz/fuzz_test.go @@ -65,6 +65,7 @@ func FuzzExpr(f *testing.F) { regexp.MustCompile(`sortBy order argument must be a string`), regexp.MustCompile(`invalid order .*, expected asc or desc`), regexp.MustCompile(`unknown order, use asc or desc`), + regexp.MustCompile(`cannot use .* as a key for groupBy: type is not comparable`), } env := NewEnv() diff --git a/vm/vm.go b/vm/vm.go index 29ceb51f..ba3b5386 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -578,6 +578,9 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) { case OpGroupBy: scope := vm.currScope key := vm.pop() + if key != nil && !reflect.TypeOf(key).Comparable() { + panic(fmt.Sprintf("cannot use %T as a key for groupBy: type is not comparable", key)) + } scope.Acc.(groupBy)[key] = append(scope.Acc.(groupBy)[key], scope.Item()) case OpSortBy: diff --git a/vm/vm_test.go b/vm/vm_test.go index a9b09697..c86183ca 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -487,6 +487,11 @@ func TestVM_GroupAndSortOperations(t *testing.T) { "odd": {1, 3, 5}, }, }, + { + name: "group by with non-comparable key", + expr: `groupBy([1, 2, 3], [#, # + 1])`, // predicate returns a slice, which is not comparable + expectError: "not comparable", + }, { name: "invalid sort order", expr: `sortBy([1, 2, 3], #, "invalid")`,