Skip to content

Commit

Permalink
Fix return type priority and update transcripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Jul 29, 2024
1 parent 4951476 commit 5ea5613
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/Share/Postgres/Search/DefinitionSearch/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Share.Postgres.Search.DefinitionSearch.Queries
defNameSearch,
definitionSearch,
DefnNameSearchFilter (..),
-- Exported for logging/debugging
searchTokensToTsQuery,
)
where

Expand Down Expand Up @@ -195,7 +197,7 @@ searchTokenToText shouldAddWildcards = \case
TypeTagToken typTag -> makeSearchToken tagType (typeTagText typTag) Nothing
TypeModToken mod -> makeSearchToken typeModType (typeModText mod) Nothing
where
addWildCard token = if shouldAddWildcards then (token <> ".:*") else token
addWildCard token = if shouldAddWildcards then (token <> ":*") else token
typeModText = \case
DD.Structural -> "structural"
DD.Unique {} -> "unique"
Expand Down Expand Up @@ -317,8 +319,8 @@ definitionSearch mayCaller mayFilter limit searchTokens preferredArity = do
rows <-
queryListRows @(ProjectId, ReleaseId, Name, Hasql.Jsonb)
[sql|
WITH matches_deduped_by_project(project_id, release_id, name, arity, metadata, num_search_tokens) AS (
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
WITH matches_deduped_by_project(project_id, release_id, name, arity, metadata, search_tokens) AS (
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
JOIN projects p ON p.id = doc.project_id
JOIN project_releases r ON r.id = doc.release_id
WHERE
Expand All @@ -330,11 +332,11 @@ definitionSearch mayCaller mayFilter limit searchTokens preferredArity = do
) SELECT m.project_id, m.release_id, m.name, m.metadata
FROM matches_deduped_by_project m
-- Score matches by:
-- * Whether arity is equal or greater than the preferred arity
-- * Whether the return type of the query matches the type signature
-- * Prefer shorter arities
-- * Prefer less complex type signatures (by number of tokens)
ORDER BY (m.arity >= #{preferredArity}) DESC, tsquery(#{returnTokensText}) @@ doc.search_tokens DESC, m.arity ASC, m.num_search_tokens ASC
-- - Whether arity is equal or greater than the preferred arity
-- - Whether the return type of the query matches the type signature
-- - Prefer shorter arities
-- - Prefer less complex type signatures (by number of tokens)
ORDER BY (m.arity >= #{preferredArity}) DESC, tsquery(#{returnTokensText}) @@ m.search_tokens DESC, m.arity ASC, length(m.search_tokens) ASC
LIMIT #{limit}
|]
rows & traverseOf (traversed . _4) \(Hasql.Jsonb v) -> do
Expand Down
7 changes: 5 additions & 2 deletions src/Share/Web/Share/DefinitionSearch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ type P = MP.Parsec QueryError Text
-- Right (fromList [TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Optional"} :| []))) (Count 1),TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Text"} :| []))) (Count 1)],Nothing)
--
-- >>> queryToTokens "e -> abilities.Exception"
-- Right (fromList [TypeMentionToken (Left (Name Relative (NameSegment {toUnescapedText = "Exception"} :| [NameSegment {toUnescapedText = "abilities"}]))) (Count 1),TypeVarToken 0 (Count 1)],Just 1)
-- 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)
--
-- >>> queryToTokens "Json.Text"
-- Right (fromList [NameToken (Name Relative (NameSegment {toUnescapedText = "Text"} :| [NameSegment {toUnescapedText = "Json"}]))],Nothing)
--
-- >>> queryToTokens "Nat -> Text"
-- 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)
queryToTokens :: Text -> Either Text (Set (DefnSearchToken (Either Name ShortHash)), Maybe Arity)
queryToTokens query =
let cleanQuery =
Expand Down Expand Up @@ -230,7 +233,7 @@ typeQueryP = do
pure $
-- If we've got at least one well-formed argument, then assume that the last segment is
-- meant to be the return type.
if arity > 1
if arity >= 1
then (arity, foldedTokens, foldOf (_last . _2 . to MonMap.keysSet) topLevelTypeSegments)
else (arity, foldedTokens, mempty)
where
Expand Down
2 changes: 2 additions & 0 deletions src/Share/Web/Share/Impl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Share.Postgres.Projects.Queries qualified as PQ
import Share.Postgres.Queries qualified as Q
import Share.Postgres.Releases.Queries qualified as RQ
import Share.Postgres.Search.DefinitionSearch.Queries qualified as DDQ
import Share.Postgres.Search.DefinitionSearch.Queries qualified as DSQ
import Share.Postgres.Users.Queries qualified as UsersQ
import Share.Prelude
import Share.Project (Project (..))
Expand Down Expand Up @@ -419,6 +420,7 @@ searchDefinitionsEndpoint callerUserId (Query query) mayLimit userFilter project
Logging.logErrorText $ "Failed to parse query: " <> query
pure $ DefinitionSearchResults []
Right (searchTokens, mayArity) -> do
Logging.logInfoText $ "definition-search-tokens: " <> DSQ.searchTokensToTsQuery searchTokens
matches <-
PG.runTransactionMode PG.ReadCommitted PG.Read $
DDQ.definitionSearch callerUserId filter limit searchTokens mayArity
Expand Down
4 changes: 2 additions & 2 deletions transcripts/share-apis/search/create-release.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"body": {
"causalHashSquashed": "#ih4vtflqotqkapjcmcfsbn7ohs94rp5mr4lo4tddlbja0hf6n0bp964dbqumfli49jbvu40uu9i3257aq406kt3v0nt6jrei9ajf9j0",
"causalHashUnsquashed": "#ih4vtflqotqkapjcmcfsbn7ohs94rp5mr4lo4tddlbja0hf6n0bp964dbqumfli49jbvu40uu9i3257aq406kt3v0nt6jrei9ajf9j0",
"causalHashSquashed": "#k64a0e0j4goec5ktrql2q8knot22dakbks05kno7jvr5fha3c894o6hhbkusi7tqegk024d9trmh0v7hikbjn6rrsura5tuecg1bt8o",
"causalHashUnsquashed": "#1d13nals2i51pbupql9vm16qqsvr9svld305f5uijrslk26vuqunq9sd3mqqsn8lsecgocdmn7fr7cujgppsp3lie9j1fc68mfeua28",
"createdAt": "<TIMESTAMP>",
"createdBy": "@transcripts",
"projectRef": "@transcripts/search",
Expand Down
2 changes: 1 addition & 1 deletion transcripts/share-apis/search/name-search-suffix.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"tag": "data-constructor",
"token": "List.Cons"
"token": "data.List.Cons"
}
],
"status": [
Expand Down
22 changes: 20 additions & 2 deletions transcripts/share-apis/search/prelude.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
```ucm
scratch/main> alias.type ##Nat data.Nat
scratch/main> alias.type ##Text data.Text
```

```unison
structural type List a = Nil | Cons a (List a)
structural type data.List a = Nil | Cons a (List a)
-- Type with a shared prefix to List
structural type ListLike a = ListLike
function.const : a -> b -> a
function.const a b = a
Expand All @@ -11,8 +19,18 @@ List.map : (a -> {g} b) -> List a -> {g} List b
List.map f = cases
(Cons a rest) -> Cons (f a) (List.map f rest)
Nil -> Nil
```
-- Stubs for types which have the same type mentions, but different return types.
Nat.toText : Nat -> Text
Nat.toText _n = "natText"
Nat.fromText : Text -> Nat
Nat.fromText _t = 0
-- A type which mentions a type that has a shared prefix with List
usesListLike : ListLike a -> ListLike b
usesListLike _ = ListLike
```

```ucm
scratch/main> add
Expand Down
99 changes: 99 additions & 0 deletions transcripts/share-apis/search/return-type-sorting-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"body": {
"results": [
{
"branchRef": "releases/1.2.3",
"definition": {
"displayName": "Nat.toText",
"hash": "#9da94h7t7rhg55vdur11jle4b1r8tn8ptpqc4m4hg8tve9vb57g64av66ghc0dpoc4r5e4f7ippu9qja7nqcq1c73cvd5fuvl0o6efo",
"summary": {
"contents": [
{
"annotation": {
"contents": "##Nat",
"tag": "TypeReference"
},
"segment": "Nat"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"tag": "TypeOperator"
},
"segment": "->"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"contents": "##Text",
"tag": "TypeReference"
},
"segment": "Text"
}
],
"tag": "UserObject"
},
"tag": "Plain"
},
"fqn": "Nat.toText",
"kind": "term",
"projectRef": "@transcripts/search"
},
{
"branchRef": "releases/1.2.3",
"definition": {
"displayName": "Nat.fromText",
"hash": "#he2h9hm6c2obcmnvp2g17dcg1l65h9t0q5mamjlrl2lk0flmjioc5j350l0386r88ho1u1ncqklh31qdqgrmbltrincivfcjcsmdeo0",
"summary": {
"contents": [
{
"annotation": {
"contents": "##Text",
"tag": "TypeReference"
},
"segment": "Text"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"tag": "TypeOperator"
},
"segment": "->"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"contents": "##Nat",
"tag": "TypeReference"
},
"segment": "Nat"
}
],
"tag": "UserObject"
},
"tag": "Plain"
},
"fqn": "Nat.fromText",
"kind": "term",
"projectRef": "@transcripts/search"
}
]
},
"status": [
{
"status_code": 200
}
]
}
99 changes: 99 additions & 0 deletions transcripts/share-apis/search/return-type-sorting-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"body": {
"results": [
{
"branchRef": "releases/1.2.3",
"definition": {
"displayName": "Nat.fromText",
"hash": "#he2h9hm6c2obcmnvp2g17dcg1l65h9t0q5mamjlrl2lk0flmjioc5j350l0386r88ho1u1ncqklh31qdqgrmbltrincivfcjcsmdeo0",
"summary": {
"contents": [
{
"annotation": {
"contents": "##Text",
"tag": "TypeReference"
},
"segment": "Text"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"tag": "TypeOperator"
},
"segment": "->"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"contents": "##Nat",
"tag": "TypeReference"
},
"segment": "Nat"
}
],
"tag": "UserObject"
},
"tag": "Plain"
},
"fqn": "Nat.fromText",
"kind": "term",
"projectRef": "@transcripts/search"
},
{
"branchRef": "releases/1.2.3",
"definition": {
"displayName": "Nat.toText",
"hash": "#9da94h7t7rhg55vdur11jle4b1r8tn8ptpqc4m4hg8tve9vb57g64av66ghc0dpoc4r5e4f7ippu9qja7nqcq1c73cvd5fuvl0o6efo",
"summary": {
"contents": [
{
"annotation": {
"contents": "##Nat",
"tag": "TypeReference"
},
"segment": "Nat"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"tag": "TypeOperator"
},
"segment": "->"
},
{
"annotation": null,
"segment": " "
},
{
"annotation": {
"contents": "##Text",
"tag": "TypeReference"
},
"segment": "Text"
}
],
"tag": "UserObject"
},
"tag": "Plain"
},
"fqn": "Nat.toText",
"kind": "term",
"projectRef": "@transcripts/search"
}
]
},
"status": [
{
"status_code": 200
}
]
}
12 changes: 12 additions & 0 deletions transcripts/share-apis/search/run.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ fetch "$transcript_user" GET 'type-var-search' '/search-definitions?query=b%20-%

# (a -> b) -> List a -> List b
fetch "$transcript_user" GET 'complex-type-mention-search' '/search-definitions?query=(a%20-%3E%20b)%20-%3E%20List%20a%20-%3E%20List%20b'

# Similar type search, should find 'map' but not 'usesListLike'
# List a -> List b
fetch "$transcript_user" GET 'similar-type-search' '/search-definitions?query=List%20a%20-%3E%20List%20b'

# Return-type sorting. Should sort Nat.toText first
# Nat -> Text
fetch "$transcript_user" GET 'return-type-sorting-1' '/search-definitions?query=Nat%20-%3E%20Text'

# Return-type sorting. Should sort Nat.fromText first
# Text -> Nat
fetch "$transcript_user" GET 'return-type-sorting-2' '/search-definitions?query=Text%20-%3E%20Nat'
Loading

0 comments on commit 5ea5613

Please sign in to comment.