I created a simple view to show users with their respective group membership and group roles. I added the following relationships, marking each of them as required:

OG membership: OG membership from User
(OG membership from user) OG membership: Group Node from OG membership
(OG membership from user) OG membership: OG Roles from membership

This resulted in the following query:

SELECT ... FROM users AS users
INNER JOIN og_membership AS og_membership_users 
    ON users.uid = og_membership_users.etid AND og_membership_users.entity_type = 'user'
INNER JOIN node AS node_og_membership 
    ON og_membership_users.gid = node_og_membership.nid AND og_membership_users.group_type = 'node'
INNER JOIN og_users_roles AS og_users_roles_og_membership 
    ON og_membership_users.etid = og_users_roles_og_membership.uid

Note the join between og_users_roles and og_membership. It joins based on the users ID ("og_membership_users.etid = og_users_roles_og_membership.uid"). That's good, but I believe it is inadequate. I believe the join of og_users_roles should look more like this which also includes the group ID:

INNER JOIN og_users_roles AS og_users_roles_og_membership 
    ON og_membership_users.etid = og_users_roles_og_membership.uid
    AND og_membership_users.gid = og_users_roles_og_membership.gid

Without the extra restriction that I propose ("AND og_membership_users.gid = og_users_roles_og_membership.gid"), the original query finds roles from any other groups and includes them in rows. So for example, if a user is an admin of one group but not the admin of 10 other groups, this query will still return the og_users_roles with the admin specification and associate it with all 11 rows being returned for that user. Anyone should be able to paste the query above (adding in whatever columns you want) and run it against any database you have to see that.

Having said that, when I add a field "OG user roles in group" to the Drupal view of and use the relationship "OG membership from user" when defining that field, the view itself seems to properly show the proper roles associated with the respective group. This seems to indicate that something else is happening in code which is not happening in the query. On the surface this would seem fine *UNTIL* I add another relationship. As soon as I add any other relationship which adds another join to the query, the problem becomes visible even in views and I see the repeating roles in the wrong rows.

Would someone please help me understand how and/or why the og_users_roles join does not include the group/gid restriction? Should og views include the restriction that I propose? Without modifying og views source (or should I?), is there a way to incorporate the extra join restriction that I believe should exist?

It feels like this could even be a bug, except that the field display of "OG user roles in group" works in the simple case.