Query failure with "All clauses in 'or' must use same set of free vars"

I asked chatgpt to create an advanced Logseq query that basically grabs all the blocks in the journal page with some stated conditions, but the code it gives always fail at some point or doesn’t show what I want. Here are the conditions (note: they are OR conditions):

  • Condition 1: Captures tasks with the marker DOING in journal pages.
  • Condition 2: Captures tasks with TODO that have a SCHEDULED property where the scheduled date is earlier/less than today.
  • Condition 3: Captures tasks with DOING that also have a SCHEDULED property where the scheduled date is earlier/less than today

And here is the code it created:

#+BEGIN_QUERY
{:title "📝 Filtered Tasks"
 :query [:find (pull ?b [*])
         :in $ ?today
         :where
         (or
          ;; Condition 1: Tasks with DOING marker
          (and
           [?b :block/marker "DOING"]
           [?b :block/page ?page]
           [?page :block/journal? true])
          ;; Condition 2: Tasks with TODO and SCHEDULED dates less than today
          (and
           [?b :block/marker "TODO"]
           [?b :block/scheduled ?scheduled]
           [(< ?scheduled ?today)]
           [?b :block/page ?page]
           [?page :block/journal? true])
          ;; Condition 3: Tasks with DOING and SCHEDULED dates less than today
          (and
           [?b :block/marker "DOING"]
           [?b :block/scheduled ?scheduled]
           [(< ?scheduled ?today)]
           [?b :block/page ?page]
           [?page :block/journal? true]))]
 :inputs [:today]
 :result-transform (fn [results]
                    (sort-by (fn [h]
                      (get h :block/priority "Z")) results))
 :collapsed? false
 :breadcrumb-show? false}

#+END_QUERY

And here is the error the code gave on Logseq:

I tried troubleshooting it with chatgpt but I don’t know much about Datalog. Can someone help me? Thanks.

  • Should generally replace or with or-join
    • In this case, or-join [?b ?today]
  • The query will still be far from optimal, but at least it will work.
    • For example, should move out of the or the common parts:
      • [?b :block/marker ?marker]
      • [?b :block/page ?page]
        [?page :block/journal? true])
        
1 Like

I fed your comment to ChatGPT and it worked. Here is the produced code for anyone wondering:

#+BEGIN_QUERY
{:title "📝 Filtered Tasks"
 :query [:find (pull ?b [*])
         :in $ ?today
         :where
         ;; Common conditions moved out of or-join
         [?b :block/marker ?marker]
         [?b :block/page ?page]
         [?page :block/journal? true]
         ;; Main conditions using or-join
         (or-join [?b ?marker ?today]
           ;; Condition 1: Tasks with DOING marker
           (and
             [(= ?marker "DOING")])
           ;; Condition 2: Tasks with TODO and SCHEDULED dates less than today
           (and
             [(= ?marker "TODO")]
             [?b :block/scheduled ?scheduled]
             [(< ?scheduled ?today)])
           ;; Condition 3: Tasks with DOING and SCHEDULED dates less than today
           (and
             [(= ?marker "DOING")]
             [?b :block/scheduled ?scheduled]
             [(< ?scheduled ?today)]))]
 :inputs [:today]
 :result-transform (fn [results]
                     (sort-by (fn [h]
                                (or (get h :block/scheduled)
                                    ;; Default to nil if no scheduled date
                                    (get h :block/deadline))) results))
 :collapsed? false
 :breadcrumb-show? false}

#+END_QUERY

The produced code is still redundant. The optimal code is closer to this one:

(or-join [?b ?marker ?today]
  [(= ?marker "DOING")]
  (and
    [(= ?marker "TODO")]
    [?b :block/scheduled ?scheduled]
    [(< ?scheduled ?today)]))
  • From your description, if a task is in DOING, its scheduled date doesn’t matter.
  • If it somehow matters, should rather use [(contains? #{"TODO" "DOING"} ?marker)]
1 Like

MODERATION POST

  • We discuss topics, not persons.
    • This includes persons’ behaviors.
    • Any issues about behaviors should go to private messages.
      • Furthermore, every post can be flagged as problematic, which informs moderators.
  • Stay on topic.
    • The topic here is a query.
    • Topics of general interest (e.g. LLMs) should go to category General.
      • When helping, relevant topics can easily get linked from there.
  • All posts are subject to moderation, including deletion.
    • Moderation is rare.