Skip to content

Commit 728f1b9

Browse files
fix LCA query
1 parent ef02077 commit 728f1b9

File tree

3 files changed

+46
-56
lines changed

3 files changed

+46
-56
lines changed

codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,32 +2863,45 @@ before x y =
28632863
selectAncestorsOfY = ancestorSql y
28642864

28652865
lca :: CausalHashId -> CausalHashId -> Transaction (Maybe CausalHashId)
2866-
lca x y =
2867-
queryStreamCol (ancestorSql x) \nextX ->
2868-
queryStreamCol (ancestorSql y) \nextY -> do
2869-
let getNext = (,) <$> nextX <*> nextY
2870-
loop2 seenX seenY =
2871-
getNext >>= \case
2872-
(Just px, Just py) ->
2873-
let seenX' = Set.insert px seenX
2874-
seenY' = Set.insert py seenY
2875-
in if Set.member px seenY'
2876-
then pure (Just px)
2877-
else
2878-
if Set.member py seenX'
2879-
then pure (Just py)
2880-
else loop2 seenX' seenY'
2881-
(Nothing, Nothing) -> pure Nothing
2882-
(Just px, Nothing) -> loop1 nextX seenY px
2883-
(Nothing, Just py) -> loop1 nextY seenX py
2884-
loop1 getNext matches v =
2885-
if Set.member v matches
2886-
then pure (Just v)
2887-
else
2888-
getNext >>= \case
2889-
Just v -> loop1 getNext matches v
2890-
Nothing -> pure Nothing
2891-
loop2 (Set.singleton x) (Set.singleton y)
2866+
lca alice bob =
2867+
queryMaybeCol
2868+
[sql|
2869+
WITH RECURSIVE history_one (causal_id) AS (
2870+
SELECT :alice
2871+
UNION
2872+
SELECT causal_parent.parent_id
2873+
FROM history_one
2874+
JOIN causal_parent ON history_one.causal_id = causal_parent.causal_id
2875+
),
2876+
history_two (causal_id) AS (
2877+
SELECT :bob
2878+
UNION
2879+
SELECT causal_parent.parent_id
2880+
FROM history_two
2881+
JOIN causal_parent ON history_two.causal_id = causal_parent.causal_id
2882+
),
2883+
common_ancestors (causal_id) AS (
2884+
SELECT causal_id
2885+
FROM history_one
2886+
INTERSECT
2887+
SELECT causal_id
2888+
FROM history_two
2889+
ORDER BY causal_id DESC
2890+
)
2891+
SELECT causal_id
2892+
FROM common_ancestors
2893+
WHERE NOT EXISTS (
2894+
SELECT 1
2895+
FROM causal_parent
2896+
WHERE causal_parent.parent_id = common_ancestors.causal_id
2897+
AND EXISTS (
2898+
SELECT 1
2899+
FROM common_ancestors c
2900+
WHERE c.causal_id = causal_parent.causal_id
2901+
)
2902+
)
2903+
LIMIT 1
2904+
|]
28922905

28932906
ancestorSql :: CausalHashId -> Sql
28942907
ancestorSql h =

unison-src/transcripts/fix-5326.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ F - D - C - B - A
105105
foo
106106
```
107107

108-
```ucm:error
108+
```ucm
109109
scratch/main> merge /bar
110110
```
111111

112-
This should be a fast-forward, but we get this shape instead (which fails due to conflicts), because we incorrectly
113-
compute `LCA(main, bar)` as `A`, not `B`.
112+
This should be a fast-forward, but we used to get this shape instead (which fails due to conflicts), because we
113+
incorrectly computed `LCA(main, bar)` as `A`, not `B`.
114114

115115
```
116116
main

unison-src/transcripts/fix-5326.output.md

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -214,36 +214,13 @@ F - D - C - B - A
214214
``` ucm
215215
scratch/main> merge /bar
216216
217-
I couldn't automatically merge scratch/bar into scratch/main.
218-
However, I've added the definitions that need attention to the
219-
top of scratch.u.
217+
😶
220218
221-
When you're done, you can run
222-
223-
merge.commit
224-
225-
to merge your changes back into main and delete the temporary
226-
branch. Or, if you decide to cancel the merge instead, you can
227-
run
228-
229-
delete.branch /merge-bar-into-main
230-
231-
to delete the temporary branch and switch back to main.
232-
233-
```
234-
``` unison:added-by-ucm scratch.u
235-
-- scratch/main
236-
x : Nat
237-
x = 4
238-
239-
-- scratch/bar
240-
x : Nat
241-
x = 2
219+
scratch/main was already up-to-date with scratch/bar.
242220
243221
```
244-
245-
This should be a fast-forward, but we get this shape instead (which fails due to conflicts), because we incorrectly
246-
compute `LCA(main, bar)` as `A`, not `B`.
222+
This should be a fast-forward, but we used to get this shape instead (which fails due to conflicts), because we
223+
incorrectly computed `LCA(main, bar)` as `A`, not `B`.
247224

248225
```
249226
main

0 commit comments

Comments
 (0)