Skip to content

Commit 5ea5613

Browse files
committed
Fix return type priority and update transcripts
1 parent 4951476 commit 5ea5613

File tree

10 files changed

+409
-15
lines changed

10 files changed

+409
-15
lines changed

src/Share/Postgres/Search/DefinitionSearch/Queries.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Share.Postgres.Search.DefinitionSearch.Queries
99
defNameSearch,
1010
definitionSearch,
1111
DefnNameSearchFilter (..),
12+
-- Exported for logging/debugging
13+
searchTokensToTsQuery,
1214
)
1315
where
1416

@@ -195,7 +197,7 @@ searchTokenToText shouldAddWildcards = \case
195197
TypeTagToken typTag -> makeSearchToken tagType (typeTagText typTag) Nothing
196198
TypeModToken mod -> makeSearchToken typeModType (typeModText mod) Nothing
197199
where
198-
addWildCard token = if shouldAddWildcards then (token <> ".:*") else token
200+
addWildCard token = if shouldAddWildcards then (token <> ":*") else token
199201
typeModText = \case
200202
DD.Structural -> "structural"
201203
DD.Unique {} -> "unique"
@@ -317,8 +319,8 @@ definitionSearch mayCaller mayFilter limit searchTokens preferredArity = do
317319
rows <-
318320
queryListRows @(ProjectId, ReleaseId, Name, Hasql.Jsonb)
319321
[sql|
320-
WITH matches_deduped_by_project(project_id, release_id, name, arity, metadata, num_search_tokens) AS (
321-
SELECT DISTINCT ON (doc.project_id, doc.name) doc.project_id, doc.release_id, doc.name, doc.arity, doc.metadata, length(doc.search_tokens) FROM global_definition_search_docs doc
322+
WITH matches_deduped_by_project(project_id, release_id, name, arity, metadata, search_tokens) AS (
323+
SELECT DISTINCT ON (doc.project_id, doc.name) doc.project_id, doc.release_id, doc.name, doc.arity, doc.metadata, doc.search_tokens FROM global_definition_search_docs doc
322324
JOIN projects p ON p.id = doc.project_id
323325
JOIN project_releases r ON r.id = doc.release_id
324326
WHERE
@@ -330,11 +332,11 @@ definitionSearch mayCaller mayFilter limit searchTokens preferredArity = do
330332
) SELECT m.project_id, m.release_id, m.name, m.metadata
331333
FROM matches_deduped_by_project m
332334
-- Score matches by:
333-
-- * Whether arity is equal or greater than the preferred arity
334-
-- * Whether the return type of the query matches the type signature
335-
-- * Prefer shorter arities
336-
-- * Prefer less complex type signatures (by number of tokens)
337-
ORDER BY (m.arity >= #{preferredArity}) DESC, tsquery(#{returnTokensText}) @@ doc.search_tokens DESC, m.arity ASC, m.num_search_tokens ASC
335+
-- - Whether arity is equal or greater than the preferred arity
336+
-- - Whether the return type of the query matches the type signature
337+
-- - Prefer shorter arities
338+
-- - Prefer less complex type signatures (by number of tokens)
339+
ORDER BY (m.arity >= #{preferredArity}) DESC, tsquery(#{returnTokensText}) @@ m.search_tokens DESC, m.arity ASC, length(m.search_tokens) ASC
338340
LIMIT #{limit}
339341
|]
340342
rows & traverseOf (traversed . _4) \(Hasql.Jsonb v) -> do

src/Share/Web/Share/DefinitionSearch.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ type P = MP.Parsec QueryError Text
109109
-- Right (fromList [TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Optional"} :| []))) (Count 1),TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Text"} :| []))) (Count 1)],Nothing)
110110
--
111111
-- >>> queryToTokens "e -> abilities.Exception"
112-
-- Right (fromList [TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Exception"} :| [NameSegment {toUnescapedText = "abilities"}]))) (Count 1),TypeVarToken 0 (Count 1)],Just 1)
112+
-- Right (fromList [TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Exception"} :| [NameSegment {toUnescapedText = "abilities"}]))) ReturnPosition,TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Exception"} :| [NameSegment {toUnescapedText = "abilities"}]))) (Count 1),TypeVarToken 0 (Count 1)],Just 1)
113113
--
114114
-- >>> queryToTokens "Json.Text"
115115
-- Right (fromList [NameToken (Name Relative (NameSegment {toUnescapedText = "Text"} :| [NameSegment {toUnescapedText = "Json"}]))],Nothing)
116+
--
117+
-- >>> queryToTokens "Nat -> Text"
118+
-- Right (fromList [TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Nat"} :| []))) (Count 1),TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Text"} :| []))) ReturnPosition,TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Text"} :| []))) (Count 1)],Just 1)
116119
queryToTokens :: Text -> Either Text (Set (DefnSearchToken (Either Name ShortHash)), Maybe Arity)
117120
queryToTokens query =
118121
let cleanQuery =
@@ -230,7 +233,7 @@ typeQueryP = do
230233
pure $
231234
-- If we've got at least one well-formed argument, then assume that the last segment is
232235
-- meant to be the return type.
233-
if arity > 1
236+
if arity >= 1
234237
then (arity, foldedTokens, foldOf (_last . _2 . to MonMap.keysSet) topLevelTypeSegments)
235238
else (arity, foldedTokens, mempty)
236239
where

src/Share/Web/Share/Impl.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Share.Postgres.Projects.Queries qualified as PQ
2323
import Share.Postgres.Queries qualified as Q
2424
import Share.Postgres.Releases.Queries qualified as RQ
2525
import Share.Postgres.Search.DefinitionSearch.Queries qualified as DDQ
26+
import Share.Postgres.Search.DefinitionSearch.Queries qualified as DSQ
2627
import Share.Postgres.Users.Queries qualified as UsersQ
2728
import Share.Prelude
2829
import Share.Project (Project (..))
@@ -419,6 +420,7 @@ searchDefinitionsEndpoint callerUserId (Query query) mayLimit userFilter project
419420
Logging.logErrorText $ "Failed to parse query: " <> query
420421
pure $ DefinitionSearchResults []
421422
Right (searchTokens, mayArity) -> do
423+
Logging.logInfoText $ "definition-search-tokens: " <> DSQ.searchTokensToTsQuery searchTokens
422424
matches <-
423425
PG.runTransactionMode PG.ReadCommitted PG.Read $
424426
DDQ.definitionSearch callerUserId filter limit searchTokens mayArity

transcripts/share-apis/search/create-release.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"body": {
3-
"causalHashSquashed": "#ih4vtflqotqkapjcmcfsbn7ohs94rp5mr4lo4tddlbja0hf6n0bp964dbqumfli49jbvu40uu9i3257aq406kt3v0nt6jrei9ajf9j0",
4-
"causalHashUnsquashed": "#ih4vtflqotqkapjcmcfsbn7ohs94rp5mr4lo4tddlbja0hf6n0bp964dbqumfli49jbvu40uu9i3257aq406kt3v0nt6jrei9ajf9j0",
3+
"causalHashSquashed": "#k64a0e0j4goec5ktrql2q8knot22dakbks05kno7jvr5fha3c894o6hhbkusi7tqegk024d9trmh0v7hikbjn6rrsura5tuecg1bt8o",
4+
"causalHashUnsquashed": "#1d13nals2i51pbupql9vm16qqsvr9svld305f5uijrslk26vuqunq9sd3mqqsn8lsecgocdmn7fr7cujgppsp3lie9j1fc68mfeua28",
55
"createdAt": "<TIMESTAMP>",
66
"createdBy": "@transcripts",
77
"projectRef": "@transcripts/search",

transcripts/share-apis/search/name-search-suffix.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
{
88
"tag": "data-constructor",
9-
"token": "List.Cons"
9+
"token": "data.List.Cons"
1010
}
1111
],
1212
"status": [

transcripts/share-apis/search/prelude.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
```ucm
2+
scratch/main> alias.type ##Nat data.Nat
3+
scratch/main> alias.type ##Text data.Text
4+
```
5+
16
```unison
2-
structural type List a = Nil | Cons a (List a)
7+
structural type data.List a = Nil | Cons a (List a)
8+
9+
-- Type with a shared prefix to List
10+
structural type ListLike a = ListLike
311
412
function.const : a -> b -> a
513
function.const a b = a
@@ -11,8 +19,18 @@ List.map : (a -> {g} b) -> List a -> {g} List b
1119
List.map f = cases
1220
(Cons a rest) -> Cons (f a) (List.map f rest)
1321
Nil -> Nil
14-
```
1522
23+
-- Stubs for types which have the same type mentions, but different return types.
24+
Nat.toText : Nat -> Text
25+
Nat.toText _n = "natText"
26+
27+
Nat.fromText : Text -> Nat
28+
Nat.fromText _t = 0
29+
30+
-- A type which mentions a type that has a shared prefix with List
31+
usesListLike : ListLike a -> ListLike b
32+
usesListLike _ = ListLike
33+
```
1634

1735
```ucm
1836
scratch/main> add
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"body": {
3+
"results": [
4+
{
5+
"branchRef": "releases/1.2.3",
6+
"definition": {
7+
"displayName": "Nat.toText",
8+
"hash": "#9da94h7t7rhg55vdur11jle4b1r8tn8ptpqc4m4hg8tve9vb57g64av66ghc0dpoc4r5e4f7ippu9qja7nqcq1c73cvd5fuvl0o6efo",
9+
"summary": {
10+
"contents": [
11+
{
12+
"annotation": {
13+
"contents": "##Nat",
14+
"tag": "TypeReference"
15+
},
16+
"segment": "Nat"
17+
},
18+
{
19+
"annotation": null,
20+
"segment": " "
21+
},
22+
{
23+
"annotation": {
24+
"tag": "TypeOperator"
25+
},
26+
"segment": "->"
27+
},
28+
{
29+
"annotation": null,
30+
"segment": " "
31+
},
32+
{
33+
"annotation": {
34+
"contents": "##Text",
35+
"tag": "TypeReference"
36+
},
37+
"segment": "Text"
38+
}
39+
],
40+
"tag": "UserObject"
41+
},
42+
"tag": "Plain"
43+
},
44+
"fqn": "Nat.toText",
45+
"kind": "term",
46+
"projectRef": "@transcripts/search"
47+
},
48+
{
49+
"branchRef": "releases/1.2.3",
50+
"definition": {
51+
"displayName": "Nat.fromText",
52+
"hash": "#he2h9hm6c2obcmnvp2g17dcg1l65h9t0q5mamjlrl2lk0flmjioc5j350l0386r88ho1u1ncqklh31qdqgrmbltrincivfcjcsmdeo0",
53+
"summary": {
54+
"contents": [
55+
{
56+
"annotation": {
57+
"contents": "##Text",
58+
"tag": "TypeReference"
59+
},
60+
"segment": "Text"
61+
},
62+
{
63+
"annotation": null,
64+
"segment": " "
65+
},
66+
{
67+
"annotation": {
68+
"tag": "TypeOperator"
69+
},
70+
"segment": "->"
71+
},
72+
{
73+
"annotation": null,
74+
"segment": " "
75+
},
76+
{
77+
"annotation": {
78+
"contents": "##Nat",
79+
"tag": "TypeReference"
80+
},
81+
"segment": "Nat"
82+
}
83+
],
84+
"tag": "UserObject"
85+
},
86+
"tag": "Plain"
87+
},
88+
"fqn": "Nat.fromText",
89+
"kind": "term",
90+
"projectRef": "@transcripts/search"
91+
}
92+
]
93+
},
94+
"status": [
95+
{
96+
"status_code": 200
97+
}
98+
]
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"body": {
3+
"results": [
4+
{
5+
"branchRef": "releases/1.2.3",
6+
"definition": {
7+
"displayName": "Nat.fromText",
8+
"hash": "#he2h9hm6c2obcmnvp2g17dcg1l65h9t0q5mamjlrl2lk0flmjioc5j350l0386r88ho1u1ncqklh31qdqgrmbltrincivfcjcsmdeo0",
9+
"summary": {
10+
"contents": [
11+
{
12+
"annotation": {
13+
"contents": "##Text",
14+
"tag": "TypeReference"
15+
},
16+
"segment": "Text"
17+
},
18+
{
19+
"annotation": null,
20+
"segment": " "
21+
},
22+
{
23+
"annotation": {
24+
"tag": "TypeOperator"
25+
},
26+
"segment": "->"
27+
},
28+
{
29+
"annotation": null,
30+
"segment": " "
31+
},
32+
{
33+
"annotation": {
34+
"contents": "##Nat",
35+
"tag": "TypeReference"
36+
},
37+
"segment": "Nat"
38+
}
39+
],
40+
"tag": "UserObject"
41+
},
42+
"tag": "Plain"
43+
},
44+
"fqn": "Nat.fromText",
45+
"kind": "term",
46+
"projectRef": "@transcripts/search"
47+
},
48+
{
49+
"branchRef": "releases/1.2.3",
50+
"definition": {
51+
"displayName": "Nat.toText",
52+
"hash": "#9da94h7t7rhg55vdur11jle4b1r8tn8ptpqc4m4hg8tve9vb57g64av66ghc0dpoc4r5e4f7ippu9qja7nqcq1c73cvd5fuvl0o6efo",
53+
"summary": {
54+
"contents": [
55+
{
56+
"annotation": {
57+
"contents": "##Nat",
58+
"tag": "TypeReference"
59+
},
60+
"segment": "Nat"
61+
},
62+
{
63+
"annotation": null,
64+
"segment": " "
65+
},
66+
{
67+
"annotation": {
68+
"tag": "TypeOperator"
69+
},
70+
"segment": "->"
71+
},
72+
{
73+
"annotation": null,
74+
"segment": " "
75+
},
76+
{
77+
"annotation": {
78+
"contents": "##Text",
79+
"tag": "TypeReference"
80+
},
81+
"segment": "Text"
82+
}
83+
],
84+
"tag": "UserObject"
85+
},
86+
"tag": "Plain"
87+
},
88+
"fqn": "Nat.toText",
89+
"kind": "term",
90+
"projectRef": "@transcripts/search"
91+
}
92+
]
93+
},
94+
"status": [
95+
{
96+
"status_code": 200
97+
}
98+
]
99+
}

transcripts/share-apis/search/run.zsh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ fetch "$transcript_user" GET 'type-var-search' '/search-definitions?query=b%20-%
4545

4646
# (a -> b) -> List a -> List b
4747
fetch "$transcript_user" GET 'complex-type-mention-search' '/search-definitions?query=(a%20-%3E%20b)%20-%3E%20List%20a%20-%3E%20List%20b'
48+
49+
# Similar type search, should find 'map' but not 'usesListLike'
50+
# List a -> List b
51+
fetch "$transcript_user" GET 'similar-type-search' '/search-definitions?query=List%20a%20-%3E%20List%20b'
52+
53+
# Return-type sorting. Should sort Nat.toText first
54+
# Nat -> Text
55+
fetch "$transcript_user" GET 'return-type-sorting-1' '/search-definitions?query=Nat%20-%3E%20Text'
56+
57+
# Return-type sorting. Should sort Nat.fromText first
58+
# Text -> Nat
59+
fetch "$transcript_user" GET 'return-type-sorting-2' '/search-definitions?query=Text%20-%3E%20Nat'

0 commit comments

Comments
 (0)