def PatternResolution(
                      quad, cursor, BRPs, orderByTriple=True, fetchall=True,
                      fetchContexts=False, select_modifier=''):
    """
    This function implements query pattern resolution against a list of
    partition objects and 3 parameters specifying whether to sort the result
    set (in order to group identical triples by the contexts in which they
    appear), whether to fetch the entire result set or one at a time, and
    whether to fetch the matching contexts only or the assertions.  This
    function uses BinaryRelationPartitionCoverage to whittle out the
    partitions that don't need to be searched, generateHashIntersections /
    generateWhereClause to generate the SQL query and the parameter fill-ins
    and creates a single UNION query against the relevant partitions.
    
    Note the use of UNION syntax requires that the literal properties
    partition is first (since it uses the first select to determine the
    column types for the resulting rows from the subsequent SELECT queries)
    
    see: http://dev.mysql.com/doc/refman/5.0/en/union.html
    """
    subject,predicate,object_,context = quad
    targetBRPs = BinaryRelationPartitionCoverage(
                            (subject,predicate,object_,context),BRPs)
    unionQueries = []
    unionQueriesParams = []
    for brp in targetBRPs:
        first = targetBRPs.index(brp) == 0
        if fetchContexts:
            query = "SELECT DISTINCT %s FROM %s %s WHERE " % \
                (','.join(brp.selectContextFields(first)),
                 brp,
                 brp._intersectionSQL)
        else:
            query = CROSS_BRP_QUERY_SQL % \
                (select_modifier,
                 ','.join(brp.selectFields(first)),
                 brp,
                 brp._intersectionSQL)
        whereClause,whereParameters = brp.generateWhereClause(
                                (subject,predicate,object_,context))
        unionQueries.append(query+whereClause)
        unionQueriesParams.extend(whereParameters)
    
    if fetchContexts:
        orderBySuffix = ''
    else:
        orderBySuffix = orderByTriple and ' ORDER BY %s,%s,%s' % \
                (SlotPrefixes[SUBJECT], 
                 SlotPrefixes[PREDICATE],
                 SlotPrefixes[OBJECT]) or ''
    if len(unionQueries) == 1:
        query = unionQueries[0] + orderBySuffix
    else:
        query = ' union all '.join(['('+q+')' 
                        for q in unionQueries]) + orderBySuffix
    try:
        if EXPLAIN_INFO:
            cursor.execute("EXPLAIN "+query,tuple(unionQueriesParams))
            print query
            from pprint import pprint
            pprint(cursor.fetchall())
        cursor.execute(query,tuple(unionQueriesParams))
    except __HOLE__,e:
        print "## Query ##\n",query
        print "## Parameters ##\n",unionQueriesParams
        raise e
    if fetchall:
        qRT = cursor.fetchall()
    else:
        qRT = cursor.fetchone()
    return qRT
dataset/ETHPy150Open RDFLib/rdfextras/rdfextras/store/FOPLRelationalModel/BinaryRelationPartition.py/PatternResolution