Skip to content

Commit

Permalink
Use zero for undefined fields in where expressions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tidwall committed Nov 5, 2024
1 parent 1ef5394 commit aa1caa6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/server/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions tests/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`),
)
}

Expand Down

0 comments on commit aa1caa6

Please sign in to comment.