I was looking code due to MDL-12897 when, in cache_correlated_tags(), this query:
$query = "SELECT tb.tagid , COUNT nr
FROM {$CFG->prefix}tag_instance ta
INNER JOIN {$CFG->prefix}tag_instance tb ON ta.itemid = tb.itemid
WHERE ta.tagid = {$tag_id}
GROUP BY tb.tagid
ORDER BY nr DESC";
seems to be missing one extra condition, in order to compare only same itemtype tags (or else we'll be counting unrelated items), so this join seems necessary:
INNER JOIN {$CFG->prefix}tag_instance tb ON ta.itemid = tb.itemid AND ta.itemtype = tb.itemtype
instead of the current one.
Ciao