Skip to content

Commit 705a9f3

Browse files
Use COALESCE instead of OR
Co-authored-by: Koen Bollen <[email protected]>
1 parent 9cfb666 commit 705a9f3

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

filter/converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
137137
paramIndex += len(innerValues)
138138
// Just putting a NOT around the condition is not enough, a non existing jsonb field will for example
139139
// make the whole inner condition NULL. And NOT NULL is still a falsy value, so we need to check for NULL explicitly.
140-
conditions = append(conditions, fmt.Sprintf("(NOT %s OR %s IS NULL)", innerConditions, innerConditions))
140+
conditions = append(conditions, fmt.Sprintf("(NOT COALESCE(%s, FALSE))", innerConditions))
141141
values = append(values, innerValues...)
142142
default:
143143
if !isValidPostgresIdentifier(key) {

filter/converter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func TestConverter_Convert(t *testing.T) {
228228
"$not operator",
229229
nil,
230230
`{"$not": {"name": "John"}}`,
231-
`(NOT ("name" = $1) OR ("name" = $1) IS NULL)`,
231+
`(NOT COALESCE(("name" = $1), FALSE))`,
232232
[]any{"John"},
233233
nil,
234234
},

integration/postgres_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,17 @@ func TestIntegration_BasicOperators(t *testing.T) {
310310
nil,
311311
},
312312
{
313-
`jsonb equal to null`,
313+
`$not on jsonb column`,
314314
`{"$not": {"pet": "cat"}}`,
315315
[]int{1, 3, 5, 7, 9, 10},
316316
nil,
317317
},
318+
{
319+
`$not on normal column`,
320+
`{"$not": {"mount": "horse"}}`,
321+
[]int{3, 4, 5, 6, 7, 8, 9, 10},
322+
nil,
323+
},
318324
}
319325

320326
for _, tt := range tests {

0 commit comments

Comments
 (0)