fix: handle Ecto.SubQuery in IN expressions #66
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When Ecto generates an
INclause with a subquery (e.g.,WHERE id IN (SELECT ...)), the adapter's catch-allINhandler wraps it inJSON_EACH(), producing invalid SQL:This causes a
malformed JSONerror at runtime because the subquery parameters (strings, integers, etc.) are not valid JSON arrays.Affected use case: Libraries like Oban with the
Oban.Engines.Liteengine useUPDATE ... WHERE id IN (subquery)patterns infetch_jobs/3, which triggers this bug.Root Cause
The
defp expr({:in, _, [left, right]}, sources, query)catch-all clause handles all non-listINexpressions by wrapping the right side inJSON_EACH(). This is correct for tagged arrays (e.g.,~w()sigils) but incorrect for%Ecto.SubQuery{}structs, which should generate inline SQL subqueries.The Postgres adapter in
ecto_sqlhas a specific pattern match for%Ecto.SubQuery{}(source) that was missing from this adapter.Before (broken)
After (fixed)
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.