'Check whether two queries are equivalent'

def equivalent(self, l, r):

    first = l.first
    second = l.second
    equivalent = self.equivalent
    if (l.op != r.op):
        return False
    if isinstance(first, Query):
        if isinstance(second, Query):
            return ((equivalent(l.first, r.first) and equivalent(l.second, r.second)) or (equivalent(l.second, r.first) and equivalent(l.first, r.second)))
        else:
            return equivalent(l.first, r.first)
    else:
        return ((l.first == r.first) and (l.second == r.second))
