Skip to content

Implement $not #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions filter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
conditions = append(conditions, strings.Join(inner, " "+op+" "))
}
}
case "$not":
vv, ok := value.(map[string]any)
if !ok {
return "", nil, fmt.Errorf("invalid value for $not operator (must be object): %v", value)
}
innerConditions, innerValues, err := c.convertFilter(vv, paramIndex)
if err != nil {
return "", nil, err
}
paramIndex += len(innerValues)
// Just putting a NOT around the condition is not enough, a non existing jsonb field will for example
// make the whole inner condition NULL. And NOT NULL is still a falsy value, so we need to check for NULL explicitly.
conditions = append(conditions, fmt.Sprintf("(NOT COALESCE(%s, FALSE))", innerConditions))
values = append(values, innerValues...)
default:
if !isValidPostgresIdentifier(key) {
return "", nil, fmt.Errorf("invalid column name: %s", key)
Expand All @@ -148,6 +162,8 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
return "", nil, fmt.Errorf("$or as scalar operator not supported")
case "$and":
return "", nil, fmt.Errorf("$and as scalar operator not supported")
case "$not":
return "", nil, fmt.Errorf("$not as scalar operator not supported")
case "$in":
if !isScalarSlice(v[operator]) {
return "", nil, fmt.Errorf("invalid value for $in operator (must array of primatives): %v", v[operator])
Expand Down
24 changes: 24 additions & 0 deletions filter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ func TestConverter_Convert(t *testing.T) {
nil,
fmt.Errorf("invalid column name: \"bla = 1 --"),
},
{
"$not operator",
nil,
`{"$not": {"name": "John"}}`,
`(NOT COALESCE(("name" = $1), FALSE))`,
[]any{"John"},
nil,
},
{
"$not in the wrong place",
nil,
`{"name": {"$not": {"$eq": "John"}}}`,
``,
nil,
fmt.Errorf("$not as scalar operator not supported"),
},
{
"$not with a scalar",
nil,
`{"$not": "John"}`,
``,
nil,
fmt.Errorf("invalid value for $not operator (must be object): John"),
},
}

for _, tt := range tests {
Expand Down
2 changes: 2 additions & 0 deletions fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func FuzzConverter(f *testing.F) {
`{"status": {"$in": []}}`,
`{"$or": [{}, {}]}`,
`{"\"bla = 1 --": 1}`,
`{"$not": {"name": "John"}}`,
`{"name": {"$not": {"$eq": "John"}}}`,
}
for _, tc := range tcs {
f.Add(tc, true)
Expand Down
12 changes: 12 additions & 0 deletions integration/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,18 @@ func TestIntegration_BasicOperators(t *testing.T) {
[]int{3, 4, 5, 6, 7, 8, 9, 10},
nil,
},
{
`$not on jsonb column`,
`{"$not": {"pet": "cat"}}`,
[]int{1, 3, 5, 7, 9, 10},
nil,
},
{
`$not on normal column`,
`{"$not": {"mount": "horse"}}`,
[]int{3, 4, 5, 6, 7, 8, 9, 10},
nil,
},
}

for _, tt := range tests {
Expand Down
Loading