Lucas Hoepner
Lucas Hoepner

Reputation: 1447

The unexpected selection and merging behavior of clojure.set/join

I wanted to overwrite some default value in a relation with some specific values and stumbled upon some behavior I don't quite understand.

(clojure.set/join 
#{
  {:a 1 :b nil :c "2"}
  {:a 2 :b "2" :c nil}
  {:a 3 :b 1 :c 5}
} 
#{
  {:a 1 :b 44}
  {:a 3 :b 11 :c 55}
} 
{:a :a})

resolves to #{{:a 3, :c 5, :b 1} {:c "2", :a 1, :b nil}}.

If I flip the arguments I get the same result.

(clojure.set/join 
#{
  {:a 1 :b 44}
  {:a 3 :b 11 :c 55}
} 
#{
  {:a 1 :b nil :c "2"}
  {:a 2 :b "2" :c nil}
  {:a 3 :b 1 :c 5}
} 
{:a :a}) 

also resolves to #{{:a 3, :c 5, :b 1} {:c "2", :a 1, :b nil}}.

What I expected (at least from the first statement) was #{{:a 3, :c 55, :b 11} {:c "2", :a 1, :b 44}}.

First question: Why don't I get what I expect?

Second question: Why do I get the same result regardless of argument order?


Just in case someone wants my solution to my problem stated in the beginning

(
  (fn [default, data, key] 
    (->> default 
      (map (fn [defaultEntry] 
        (merge 
          defaultEntry 
          (->> data (filter (fn [dataEntry] (= (key defaultEntry) (key dataEntry)))) first)
        )
      ))
    )
  )
  #{
    {:a 1 :b nil :c "2"}
    {:a 2 :b "2" :c nil}
    {:a 3 :b 1 :c 5}
  } 
  #{
    {:a 1 :b 44}
    {:a 3 :b 11 :c 55}
  } 
  :a
)

(Warning: Assumes that key is unique!)

Upvotes: 1

Views: 184

Answers (1)

nberger
nberger

Reputation: 3659

From the clojure.set/join code we can see that the set with fewer elements is used as an index and the set with more elements is reduced by looking for each element in the index and keeping the merged version for each found item.

(defn join
  "When passed 2 rels, returns the rel corresponding to the natural
  join. When passed an additional keymap, joins on the corresponding
  keys."
  ; 2-arity version removed for brevity
  ([xrel yrel km] ;arbitrary key mapping
   (let [[r s k] (if (<= (count xrel) (count yrel))
                   [xrel yrel (map-invert km)]
                   [yrel xrel km])
         idx (index r (vals k))]
     (reduce (fn [ret x]
               (let [found (idx (rename-keys (select-keys x (keys k)) k))]
                 (if found
                   (reduce #(conj %1 (merge %2 x)) ret found)
                   ret)))
             #{} s))))

That's why you'll the get same result with (set/join a b) or (set/join b a), except if both sets are of the same length, which is not your case.

It also explains why you get #{{:a 3, :c 5, :b 1} {:c "2", :a 1, :b nil}} instead of #{{:a 3, :c 55, :b 11} {:c "2", :a 1, :b 44}}: The values in the maps from the longer set take precedence over the values from the shorter set when there's a match.

Upvotes: 2

Related Questions