-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
fix: unused index on jsonb/jsonb arrow filter #2859
Conversation
it "should use an index for ordering on a json arrow operator" $ do | ||
r <- request methodGet "/bets?order=data_json->>contractId" | ||
[(hAccept, "application/vnd.pgrst.plan")] "" | ||
|
||
let resBody = simpleBody r | ||
|
||
liftIO $ do | ||
resBody `shouldSatisfy` (\t -> T.isInfixOf "Index" (decodeUtf8 $ LBS.toStrict t)) | ||
|
||
it "should use an index for ordering on a jsonb arrow operator" $ do | ||
r <- request methodGet "/bets?order=data_jsonb->>contractId" | ||
[(hAccept, "application/vnd.pgrst.plan")] "" | ||
|
||
let resBody = simpleBody r | ||
|
||
liftIO $ do | ||
resBody `shouldSatisfy` (\t -> T.isInfixOf "Index" (decodeUtf8 $ LBS.toStrict t)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To fix these I had to add a new CoercibleOrderTerm type. This is so we know the type of the order field and not apply to_jsonb
unnecessarily.
resolveTableField table (fieldName, jp) = | ||
case resolveTableFieldName table fieldName of | ||
cf@CoercibleField{cfIRType=""} -> cf{cfJsonPath=jp} | ||
cf -> cf{cfJsonPath=jp, cfIRType="json"} | ||
-- types that are already json/jsonb don't need to be converted with `to_jsonb` for using arrow operators `data->attr` | ||
-- this prevents indexes not applying https://github.com/PostgREST/postgrest/issues/2594 | ||
cf@CoercibleField{cfIRType="json"} -> cf{cfJsonPath=jp} | ||
cf@CoercibleField{cfIRType="jsonb"} -> cf{cfJsonPath=jp} | ||
-- other types will get converted `to_jsonb(col)->attr` | ||
cf -> cf{cfJsonPath=jp, cfToJson=True} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the main logic for the fix. Now is in only one place.
Closes #2594
Before:
Now: