fix: use index on jsonb/jsonb arrow filter/order
This commit is contained in:
committed by
Steve Chavez
parent
e332f038ef
commit
28d5278d62
@@ -103,7 +103,7 @@ spec actualPgVersion = describe "json and jsonb operators" $ do
|
||||
|
||||
it "can get array of objects" $ do
|
||||
get "/json_arr?select=data->0->>a&id=in.(5,6)" `shouldRespondWith`
|
||||
[json| [{"a":"A"}, {"a":"[1, 2, 3]"}] |]
|
||||
[json|[{"a":"A"}, {"a":"[1,2,3]"}]|]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/json_arr?select=data->0->a->>2&id=in.(5,6)" `shouldRespondWith`
|
||||
[json| [{"a":null}, {"a":"3"}] |]
|
||||
@@ -275,7 +275,7 @@ spec actualPgVersion = describe "json and jsonb operators" $ do
|
||||
[json| [{"data":8}, {"data":7}] |]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
get "/json_arr?select=data->-2->>a&id=in.(5,6)" `shouldRespondWith`
|
||||
[json| [{"a":"A"}, {"a":"[1, 2, 3]"}] |]
|
||||
[json| [{"a":"A"}, {"a":"[1,2,3]"}] |]
|
||||
{ matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "can filter with negative indexes" $ do
|
||||
|
||||
@@ -348,6 +348,43 @@ spec actualPgVersion = do
|
||||
liftIO $ do
|
||||
resBody `shouldSatisfy` (\t -> not $ T.isInfixOf "getitemrange" (decodeUtf8 $ LBS.toStrict t))
|
||||
|
||||
context "index usage" $ do
|
||||
it "should use an index for a json arrow operator filter" $ do
|
||||
r <- request methodGet "/bets?data_json->>contractId=eq.1"
|
||||
[(hAccept, "application/vnd.pgrst.plan")] ""
|
||||
|
||||
let resBody = simpleBody r
|
||||
|
||||
liftIO $ do
|
||||
resBody `shouldSatisfy` (\t -> T.isInfixOf "Index Cond" (decodeUtf8 $ LBS.toStrict t))
|
||||
|
||||
it "should use an index for a jsonb arrow operator filter" $ do
|
||||
r <- request methodGet "/bets?data_jsonb->>contractId=eq.1"
|
||||
[(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 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))
|
||||
|
||||
disabledSpec :: SpecWith ((), Application)
|
||||
disabledSpec =
|
||||
it "doesn't work if db-plan-enabled=false(the default)" $ do
|
||||
|
||||
Vendored
+9
@@ -3293,3 +3293,12 @@ create table evil_friends(
|
||||
id devil_int
|
||||
, name text
|
||||
);
|
||||
|
||||
create table bets (
|
||||
id int
|
||||
, data_json json
|
||||
, data_jsonb jsonb
|
||||
);
|
||||
|
||||
create index bets_data_json on bets ((data_json ->>'contractId'));
|
||||
create index bets_data_jsonb on bets ((data_jsonb ->>'contractId'));
|
||||
|
||||
Reference in New Issue
Block a user