# Buggy code
def is_ancestor(self, other):
    if isinstance(other, ir.Expr):
        other = other.op()

    if self.equals(other):
        return True

    table = self.table
    exist_layers = False
    op = table.op()
    while not (op.blocks() or isinstance(op, Join)):
        table = table.op().table
        exist_layers = True

    if exist_layers:
        reboxed = Selection(table, self.selections)
        return reboxed.is_ancestor(other)
    else:
        return False


# Traceback (ibis.common.RelationError)
Traceback (most recent call last):
File "ibis_fail.py", line 14, in <module>
t[t.columns]
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/types.py", line 607, in __getitem__
return self.projection(what)
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/api.py", line 2058, in projection
op = projector.get_result()
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 685, in get_result
fused_op = self._check_fusion(roots[0])
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 725, in _check_fusion
elif validator.validate(lifted_val):
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 764, in validate
if self._among_roots(op.table.op()):
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 777, in _among_roots
return self.roots_shared(node) > 0
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 782, in roots_shared
if root.is_ancestor(node):
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/operations.py", line 1746, in is_ancestor
reboxed = Selection(table, self.selections)
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/operations.py", line 1673, in __init__
self._validate(dependent_exprs)
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/operations.py", line 1694, in _validate
self.table._assert_valid(exprs)
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/types.py", line 576, in _assert_valid
ExprValidator([self]).validate_all(exprs)
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 805, in validate_all
self.assert_valid(expr)
File "/pipedream/local/venv/deepfield-env/lib/python2.7/site-packages/ibis/expr/analysis.py", line 810, in assert_valid
raise RelationError(msg)
ibis.common.RelationError: The expression ref_0
UnboundTable[table]
name: t
schema:
col : int32

ref_1
Selection[table]
table:
Table: ref_0
selections:
col = Column[array(int32)] 'col' from table
ref_0
filter = NullLiteral[null]
None

ref_2
Selection[table]
table:
Table: ref_1
predicates:
IsNull[array(boolean)]
filter = Column[array(null)] 'filter' from table
ref_1

col = Column[array(int32)] 'col' from table
ref_2 does not fully originate from dependencies of the table expression.

# Fixed code
def is_ancestor(self, other):
    if isinstance(other, ir.Expr):
        other = other.op()

    if self.equals(other):
        return True

    return False