From aa1caa61313dff34dd272d56ba90feb15542c36c Mon Sep 17 00:00:00 2001 From: tidwall Date: Tue, 5 Nov 2024 13:58:55 -0700 Subject: [PATCH] Use zero for undefined fields in where expressions This commit fixes an issue where expressions were silently failing when encountering undefined fields. Instead they should be treating the undefined field like the number zero. This is required to stay compatible with how Tile38 handles zeros. https://tile38.com/commands/intersects/#fields Fixes #754 --- internal/server/expr.go | 3 ++- tests/keys_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/server/expr.go b/internal/server/expr.go index dc1e75f3..dd03d158 100644 --- a/internal/server/expr.go +++ b/internal/server/expr.go @@ -91,6 +91,7 @@ func newExprPool(s *Server) *exprPool { return resultToValue(r), nil } } + return expr.Number(0), nil } else { switch v := info.Value.Value().(type) { case gjson.Result: @@ -102,8 +103,8 @@ func newExprPool(s *Server) *exprPool { return expr.Function("match"), nil } } + return expr.Undefined, nil } - return expr.Undefined, nil }, // call func(info expr.CallInfo, ctx *expr.Context) (expr.Value, error) { diff --git a/tests/keys_test.go b/tests/keys_test.go index 759c0077..1b14329d 100644 --- a/tests/keys_test.go +++ b/tests/keys_test.go @@ -600,6 +600,17 @@ func keys_FIELDS_test(mc *mockServer) error { Do("SCAN", "fleet", "WHERE", "props.speed > 53", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`), Do("SCAN", "fleet", "WHERE", "Props.speed > 53", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`), Do("SCAN", "fleet", "WHERE", "Props.Speed > 53", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`), + + Do("DROP", "fleet").JSON().OK(), + Do("SET", "fleet", "1", "field", "teamId", "1", "field", "optionalId", "999", "point", "0", "0").JSON().OK(), + Do("SET", "fleet", "2", "field", "teamId", "1", "point", "0", "0").JSON().OK(), + Do("SCAN", "fleet", "COUNT").JSON().Str(`{"ok":true,"count":2,"cursor":0}`), + Do("SCAN", "fleet", "WHEREIN", "teamId", "1", "1", "COUNT").JSON().Str(`{"ok":true,"count":2,"cursor":0}`), + + Do("SCAN", "fleet", "WHEREIN", "teamId", "1", "1", "WHERE", "!optionalId || optionalId == 999", "count").JSON().Str(`{"ok":true,"count":2,"cursor":0}`), + Do("SCAN", "fleet", "WHEREIN", "teamId", "1", "1", "WHERE", "!!!optionalId || optionalId == 999", "count").JSON().Str(`{"ok":true,"count":2,"cursor":0}`), + Do("SCAN", "fleet", "WHEREIN", "teamId", "1", "1", "WHERE", "optionalId == 0 || optionalId == 999", "count").JSON().Str(`{"ok":true,"count":2,"cursor":0}`), + Do("SCAN", "fleet", "WHEREIN", "teamId", "1", "1", "WHERE", "1 == 1 || optionalId == 999", "count").JSON().Str(`{"ok":true,"count":2,"cursor":0}`), ) }