'Split this query into a real query and a virtual one (AND)
@param resource: the S3Resource
@return: tuple (DAL-translatable sub-query, virtual filter),
both S3ResourceQuery instances'

def split(self, resource):

    op = self.op
    l = self.left
    r = self.right
    if (op == self.AND):
        (lq, lf) = (l.split(resource) if isinstance(l, S3ResourceQuery) else (l, None))
        (rq, rf) = (r.split(resource) if isinstance(r, S3ResourceQuery) else (r, None))
        q = lq
        if (rq is not None):
            if (q is not None):
                q &= rq
            else:
                q = rq
        f = lf
        if (rf is not None):
            if (f is not None):
                f &= rf
            else:
                f = rf
        return (q, f)
    elif (op == self.OR):
        (lq, lf) = (l.split(resource) if isinstance(l, S3ResourceQuery) else (l, None))
        (rq, rf) = (r.split(resource) if isinstance(r, S3ResourceQuery) else (r, None))
        if ((lf is not None) or (rf is not None)):
            return (None, self)
        else:
            q = lq
            if (rq is not None):
                if (q is not None):
                    q |= rq
                else:
                    q = rq
            return (q, None)
    elif (op == self.NOT):
        if isinstance(l, S3ResourceQuery):
            if (l.op == self.OR):
                i = ((~ l.left) & (~ l.right))
                return i.split(resource)
            else:
                (q, f) = l.split(resource)
                if ((q is not None) and (f is not None)):
                    return (None, self)
                elif (q is not None):
                    return ((~ q), None)
                elif (f is not None):
                    return (None, (~ f))
        else:
            return ((~ l), None)
    l = self.left
    try:
        if isinstance(l, S3FieldSelector):
            lfield = l.resolve(resource)
        else:
            lfield = S3ResourceField(resource, l)
    except:
        lfield = None
    if ((not lfield) or (lfield.field is None)):
        return (None, self)
    else:
        return (self, None)
