Skip to content

Commit c9591a8

Browse files
Use COALESCE instead of OR
Co-authored-by: Koen Bollen <[email protected]>
1 parent b5d11ff commit c9591a8

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
@@ -133,7 +133,7 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
133133
paramIndex += len(innerValues)
134134
// Just putting a NOT around the condition is not enough, a non existing jsonb field will for example
135135
// make the whole inner condition NULL. And NOT NULL is still a falsy value, so we need to check for NULL explicitly.
136-
conditions = append(conditions, fmt.Sprintf("(NOT %s OR %s IS NULL)", innerConditions, innerConditions))
136+
conditions = append(conditions, fmt.Sprintf("(NOT COALESCE(%s, FALSE))", innerConditions))
137137
values = append(values, innerValues...)
138138
default:
139139
if !isValidPostgresIdentifier(key) {

filter/converter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func TestConverter_Convert(t *testing.T) {
220220
"$not operator",
221221
nil,
222222
`{"$not": {"name": "John"}}`,
223-
`(NOT ("name" = $1) OR ("name" = $1) IS NULL)`,
223+
`(NOT COALESCE(("name" = $1), FALSE))`,
224224
[]any{"John"},
225225
nil,
226226
},

integration/postgres_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,17 @@ func TestIntegration_BasicOperators(t *testing.T) {
304304
nil,
305305
},
306306
{
307-
`jsonb equal to null`,
307+
`$not on jsonb column`,
308308
`{"$not": {"pet": "cat"}}`,
309309
[]int{1, 3, 5, 7, 9, 10},
310310
nil,
311311
},
312+
{
313+
`$not on normal column`,
314+
`{"$not": {"mount": "horse"}}`,
315+
[]int{3, 4, 5, 6, 7, 8, 9, 10},
316+
nil,
317+
},
312318
}
313319

314320
for _, tt := range tests {

0 commit comments

Comments
 (0)