@Test
@FixFor( "MODE-1900" )
public void shouldSelectDistinctNodesWhenJoiningMultiValueReferenceProperties() throws Exception {

    Node nodeA = session.getRootNode().addNode("A", "test:node");
    nodeA.setProperty("test:name", "A");
    Node nodeB = session.getRootNode().addNode("B", "test:node");
    nodeB.setProperty("test:name", "B");
    JcrValue nodeBRef = session.getValueFactory().createValue(nodeB);
    Node nodeC = session.getRootNode().addNode("C", "test:node");
    nodeC.setProperty("test:name", "C");
    JcrValue nodeCRef = session.getValueFactory().createValue(nodeC);
    try {
        Node relationship = nodeA.addNode("relationship", "test:relationship");
        relationship.setProperty("test:target", new JcrValue[] {nodeBRef, nodeCRef});
        session.save();
        QueryManager queryManager = session.getWorkspace().getQueryManager();
        // Find the reference in the node ...
        String queryString = "SELECT relationship.[test:target] FROM [test:relationship] AS relationship";
        Query query = queryManager.createQuery(queryString, Query.JCR_SQL2);
        validateQuery().rowCount(1).validate(query, query.execute());
        // Find the UUIDs of the 'test:node' (which are all 'mix:referenceable') ...
        queryString = "SELECT [jcr:uuid], [jcr:path] FROM [test:node]";
        query = queryManager.createQuery(queryString, Query.JCR_SQL2);
        validateQuery().rowCount(3).onEachRow(new Predicate() {
            @Override
            public void validate( int rowNumber,
                                  Row row ) throws RepositoryException {
                assertNotNull(row.getValue("jcr:uuid"));
            }
        }).validate(query, query.execute());
        queryString = "SELECT relationship.[test:target], target.[jcr:path] "
                      + "   FROM [test:relationship] AS relationship "
                      + "   JOIN [test:node] AS target ON relationship.[test:target] = target.[jcr:uuid] ";
        query = queryManager.createQuery(queryString, Query.JCR_SQL2);
        validateQuery().rowCount(2).validate(query, query.execute());
        queryString = "SELECT node.[jcr:path], relationship.[test:target], target.[jcr:path] FROM [test:node] AS node "
                      + "   JOIN [test:relationship] AS relationship ON ISCHILDNODE(relationship, node) "
                      + "   JOIN [test:node] AS target ON relationship.[test:target] = target.[jcr:uuid] "
                      + "   WHERE node.[test:name] = 'A'";
        query = queryManager.createQuery(queryString, Query.JCR_SQL2);
        validateQuery().rowCount(2).validate(query, query.execute());
        queryString = "SELECT DISTINCT target.* FROM [test:node] AS node "
                      + "   JOIN [test:relationship] AS relationship ON ISCHILDNODE(relationship, node) "
                      + "   JOIN [test:node] AS target ON relationship.[test:target] = target.[jcr:uuid] "
                      + "   WHERE node.[test:name] = 'A'";
        query = queryManager.createQuery(queryString, Query.JCR_SQL2);
        validateQuery().rowCount(2).validate(query, query.execute());
    } finally {
        nodeA.remove();
        nodeB.remove();
        nodeC.remove();
        session.save();
    }
}