Skip to content

Commit 87037fb

Browse files
Fix suggestions
1 parent 35b236e commit 87037fb

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

filter/converter.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,26 +237,22 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
237237
values = append(values, innerValues...)
238238
default:
239239
value := v[operator]
240-
isNumericOperatorMap := false
240+
isNumericOperator := false
241241
op, ok := textOperatorMap[operator]
242242
if !ok {
243243
op, ok = numericOperatorMap[operator]
244244
if !ok {
245245
return "", nil, fmt.Errorf("unknown operator: %s", operator)
246246
}
247-
isNumericOperatorMap = true
247+
isNumericOperator = true
248248
}
249249

250250
if !isScalar(value) {
251251
return "", nil, fmt.Errorf("invalid comparison value (must be a primitive): %v", value)
252252
}
253253

254-
if isNumericOperatorMap && isNumeric(value) {
255-
if c.isNestedColumn(key) {
256-
inner = append(inner, fmt.Sprintf("((%s)::numeric %s $%d)", c.columnName(key), op, paramIndex))
257-
} else {
258-
inner = append(inner, fmt.Sprintf("(%s %s $%d)", c.columnName(key), op, paramIndex))
259-
}
254+
if isNumericOperator && isNumeric(value) && c.isNestedColumn(key) {
255+
inner = append(inner, fmt.Sprintf("((%s)::numeric %s $%d)", c.columnName(key), op, paramIndex))
260256
} else {
261257
inner = append(inner, fmt.Sprintf("(%s %s $%d)", c.columnName(key), op, paramIndex))
262258
}

filter/converter_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,14 @@ func TestConverter_Convert(t *testing.T) {
366366
[]any{float64(0)},
367367
nil,
368368
},
369+
{
370+
"numeric comparison against null with jsonb column",
371+
filter.WithNestedJSONB("meta"),
372+
`{"foo": {"$gt": null}}`,
373+
`("meta"->>'foo' > $1)`,
374+
[]any{nil},
375+
nil,
376+
},
369377
}
370378

371379
for _, tt := range tests {

filter/util.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package filter
22

33
func isNumeric(v any) bool {
4-
if v == nil {
5-
return true
6-
}
7-
8-
switch v.(type) {
9-
case float64:
10-
return true
11-
default:
12-
return false
13-
}
4+
// json.Unmarshal returns float64 for all numbers
5+
// so we only need to check for float64.
6+
_, ok := v.(float64)
7+
return ok
148
}
159

1610
func isScalar(v any) bool {

integration/postgres_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ func TestIntegration_BasicOperators(t *testing.T) {
286286
nil,
287287
},
288288
{
289-
`invalid value type 1`,
289+
`invalid value type int`,
290290
`{"level": "town1"}`, // Level is an integer column, but the value is a string.
291291
nil,
292292
errors.New(`pq: invalid input syntax for type integer: "town1"`),
293293
},
294294
{
295-
`invalid value type 2`,
295+
`invalid value type string`,
296296
`{"name": 123}`, // Name is a string column, but the value is an integer.
297297
[]int{},
298298
nil,
@@ -388,9 +388,15 @@ func TestIntegration_BasicOperators(t *testing.T) {
388388
nil,
389389
},
390390
{
391-
"numeric comparison bug bug with jsonb column",
391+
`$lt bug with jsonb column`,
392392
`{"guild_id": {"$lt": 100}}`,
393-
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, // wrong
393+
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
394+
nil,
395+
},
396+
{
397+
`$lt with null and jsonb column`,
398+
`{"guild_id": {"$lt": null}}`,
399+
[]int{},
394400
nil,
395401
},
396402
}

0 commit comments

Comments
 (0)