Trying to create a nested map in Clojure from ltree data causes Posgres to throw misleading errors

I am trying to create a list of maps with :node_name, :children and :is_leaf keys. When children is not nil, it would also create a nested map with the same keys. Things work if the :children key is just a simple list of names of the nodes where the nesting level is nlevel(current_node) + 1, but things start to break when I try to recursively create a nested map of children for each node that is not a leaf node instead. On a side note I’m also looking for ways to reduce the number of times the query is evaluated within the recursive calls. My data flow that works is the following:

; read category relationships from a materialized view holding all categories
(def category-tree
  (let [query "SELECT id::integer, "name"::varchar, path_string::text 
              FROM public.category_tree_full;"
        result (future (jdbc/execute! @connect-database [query]))]
    @result))

; get the human-readable ltree paths, which can be something like Europe -> Germany -> Bavaria -> Munich
(def path-strs (mapv #(s/split % #" -> ") (mapv #(:path_string %) category-tree)))

(def leaf-nodes
  "returns categories with no child categories"
  (let [intermediaries (set (mapcat butlast path-strs))
        last-elems (set (mapv last path-strs))]
    (difference last-elems intermediaries)))


(defn get-immediate-children [node-name]
  "will return a vector of all nodes where nesting level is the 
  nlevel of the current node and where the path starts with it's ID"
  (let [node-id (get-category-id node-name)
        node-path (:pv (jdbc/execute-one! @connect-database
                                          ["SELECT path::text AS pv FROM category c 
                                                 WHERE c.id = ?" node-id]))
        node-level (:nlevel
                    (jdbc/execute-one!
                     @connect-database
                     ["SELECT nlevel(path) FROM category c WHERE c.id = ?" node-id]))
        children (or (map #(:subcategory_name %) (jdbc/execute! @connect-database
                                                                [(s/replace (format "WITH subcategories AS (SELECT path, "name"::varchar as subcategory_name
FROM category
WHERE nlevel(path) = %d AND path ~ '%s.*')
SELECT s.subcategory_name
FROM subcategories s
JOIN category c2 ON subpath(s.path, 0, nlevel(s.path) - 1) = c2.path;" (inc node-level) node-path) #"n" " ")] {:builder-fn as-unqualified-maps})) nil)]
    children))

; pull the data from one of the materialized views for primary roots
(defn from-view [view-name] (jdbc/execute! @connect-database
                                           [(format "SELECT name::varchar, path_string::varchar
                                           FROM %s" view-name)] {:builder-fn as-unqualified-maps}))

(defn process-data [from-view]
  (let [split-path (fn [s] (clojure.string/split s #" -> "))
        path-map (zipmap (map :name from-view) (map #(split-path (:path_string %)) from-view))
        is-leaf (fn [node] (some #(= node %) leaf-nodes))
        children (fn [node] (when-not (is-leaf node) (get-immediate-children node)))]
    (map (fn [[node]]
           {:node_name node
            :children (when-not (is-leaf node) (children node))
            :is_leaf (or (is-leaf node) false)})
         path-map)))

But changing the children let binding in the process-data function to

        children (fn [node] (let [cvalues (get-immediate-children node)]
                              (if (seq cvalues)
                                (map (fn [[child]]
                                       {:leaf (is-leaf child)
                                        :name child
                                        :children (when-not (is-leaf child) (get-immediate-children child))}) cvalues)
                                nil)))

causes postgresql to yell at me about the hstore extension not being installed. I cannot clearly understand how would hstore be related to this. Is my recursive map anonymous function wrong? As stated, I’m looking for ways to further minimize the number of times the query from get-immediate-children would be run just to create a nested structure. Maybe some ltree and json functions can be useful here, but then there’d be the overhead of the (de)serialization. Sorry if this feels noobish but I have only been programming in Clojure for a couple months or so after working mostly with imperative solutions.

New contributor

Marcelina Holub is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật