I wanted to combine two advanced queries, both of which had results on their own. The intent was the joined query combine the results of both. I wasn’t sure how given my limited understanding of Datalog. So I asked ChatGPT.
It had little issue combining the queries. I continued to prompt it to limit nesting and other things, but in the end, although all the queries it generated looked correct, they wouldn’t work in Logseq. The result, returning nothing, would just have the block drop out of the view.
Query #1
{
:title [:h4 "☀️ Today"]
:query [:find (pull ?block [*])
:in $ ?day
:where
[?block :block/marker ?marker]
(or
[?block :block/scheduled ?d]
[?block :block/deadline ?d])
[(contains? #{"TODO","DOING","WAITING"} ?marker)]
[(<= ?d ?day)]]
:result-transform
(fn [result]
(sort-by (fn [h] [(get h :block/priority "Z") (get h :block/created-at)]) result))
:inputs [:today]
:collapsed? false
}
Query #2
{
:title [:h4 "🪣 Next"]
:query [:find (pull ?block [*])
:where
[?block :block/marker ?marker]
[(contains? #{"DOING","WAITING"} ?marker)]]
:result-transform
(fn [result]
(sort-by (fn [h] [(get h :block/priority "Z") (get h :block/created-at)]) result))
:collapsed? false
}
ChatGPT Attempts
{
:title [:h4 "Merged Query"]
:query [:find (pull ?block [*])
:in $ ?day
:where
(or
(and
[?block :block/marker ?marker]
(or
[?block :block/scheduled ?d]
[?block :block/deadline ?d])
[(contains? #{"TODO","DOING","WAITING"} ?marker)]
[(<= ?d ?day)])
(and
[?block :block/marker ?marker]
[(contains? #{"DOING","WAITING"} ?marker)]))]
:result-transform
(fn [result]
(sort-by (fn [h] [(get h :block/priority "Z") (get h :block/created-at)]) result))
:inputs [:today]
:collapsed? false
}
{
:title [:h4 "Merged Query"]
:query [:find (pull ?block [*])
:in $ ?day
:where
(or-join
[?block :block/marker ?marker]
[(contains? #{"TODO","DOING","WAITING"} ?marker)]
[(<= ?d ?day)]
(or
[?block :block/scheduled ?d]
[?block :block/deadline ?d])
[(contains? #{"DOING","WAITING"} ?marker)])]
:result-transform
(fn [result]
(sort-by (fn [h] [(get h :block/priority "Z") (get h :block/created-at)]) result))
:inputs [:today]
:collapsed? false
}
Why would I not be able to simply wrap the two queries effectively in parantheses as below? I’m not sure why ChatGPT’s many valid attempt all seem to fail, as do mine. I don’t understand why nesting a couple levels deeper would be any more difficult in Datalog than in other languages like JavaScript.
{
:title [:h4 "Merged Query"]
:query [:find (pull ?block [*])
:in $ ?day
:where
(or
(and ;Query 1 -- comment
[?block :block/marker ?marker]
(or
[?block :block/scheduled ?d]
[?block :block/deadline ?d])
[(contains? #{"TODO","DOING","WAITING"} ?marker)]
[(<= ?d ?day)])
(and ;Query 2 -- comment
[?block :block/marker ?marker]
[(contains? #{"DOING","WAITING"} ?marker)]))
]
:result-transform
(fn [result]
(sort-by (fn [h] [(get h :block/priority "Z") (get h :block/created-at)]) result))
:inputs [:today]
:collapsed? false
}