   (when-not (and (== 1 (count in))
                  (= "$" (name (-> in first :variable :symbol))))
     (raise (str "Complex `in` not supported: " (print-str in)))))
+
+(defn find->prepared-context [find]
+  ;; There's some confusing use of 'where' and friends here. That's because
+  ;; the parsed Datalog includes :where, and it's also input to honeysql's
+  ;; SQL formatter.
+  (let [{:keys [find in with where]} find]  ; Destructure the Datalog query.
+    (validate-with with)
+    (validate-in in)
+    (apply-elements-to-context
+      (expand-where-from-bindings
+        (patterns->context where))    ; 'where' here is the Datalog :where clause.
+      (:elements find))))
 
 (defn find->sql-clause
   "Take a parsed `find` expression and turn it into a structured SQL

  ;; There's some confusing use of 'where' and friends here. That's because
  ;; the parsed Datalog includes :where, and it's also input to honeysql's
  ;; SQL formatter.
  (let [{:keys [find in with where]} find]  ; Destructure the Datalog query.
    (validate-with with)
    (validate-in in)
    (context->sql-clause
      (expand-where-from-bindings
        (patterns->context where))    ; 'where' here is the Datalog :where clause.
      (:elements find))))

(defn find->sql-string
  "Take a parsed `find` expression and turn it into SQL."

;; `:bindings` is a map from var to qualified columns.
;; `:wheres` is a list of fragments that can be joined by `:and`.
;;
(defrecord Context [from bindings wheres elements attribute-transform constant-transform])

(defn attribute-in-context [context attribute]
  ((:attribute-transform context) attribute))
