def test_rewrite_substitute_distinct_tables(con): t = con.table('test1') tt = con.table('test1') expr = t[t.c > 0] expr2 = tt[tt.c > 0] metric = t.f.sum().name('metric') expr3 = expr.aggregate(metric) result = L.sub_for(expr3, [(expr2, t)]) expected = t.aggregate(metric) assert_equal(result, expected)
def test_rewrite_substitute_distinct_tables(self): t = self.con.table('test1') tt = self.con.table('test1') expr = t[t.c > 0] expr2 = tt[tt.c > 0] metric = t.f.sum().name('metric') expr3 = expr.aggregate(metric) result = L.sub_for(expr3, [(expr2, t)]) expected = t.aggregate(metric) assert_equal(result, expected)
def _pushdown_exprs(self, exprs): from ibis.expr.analysis import shares_all_roots, sub_for subbed_exprs = [] for expr in util.promote_list(exprs): expr = self.op.table._ensure_expr(expr) subbed = sub_for(expr, [(self.parent, self.op.table)]) subbed_exprs.append(subbed) if subbed_exprs: valid = shares_all_roots(subbed_exprs, self.op.table) else: valid = True return valid, subbed_exprs
def _pushdown_exprs(self, exprs): import ibis.expr.analysis as L # exit early if there's nothing to push down if not exprs: return True, [] resolved = self.op.table._resolve(exprs) subbed_exprs = [] valid = False if resolved: for x in util.promote_list(resolved): subbed = L.sub_for(x, [(self.parent, self.op.table)]) subbed_exprs.append(subbed) valid = self.op.table._is_valid(subbed_exprs) else: valid = False return valid, subbed_exprs
def _make_distinct_join_predicates(left, right, predicates): import ibis.expr.analysis as L if left.equals(right): # GH #667: If left and right table have a common parent expression, # e.g. they have different filters, we need to add a self-reference and # make the appropriate substitution in the join predicates right = right.view() elif isinstance(right.op(), Join): # for joins with joins on the right side we turn the right side into a # view, otherwise the join tree is incorrectly flattened and tables on # the right are incorrectly scoped old = right new = right = right.view() predicates = [ L.sub_for(pred, [(old, new)]) if isinstance(pred, ir.Expr) else pred for pred in predicates ] predicates = _clean_join_predicates(left, right, predicates) return left, right, predicates
def test_join_table_choice(): # GH807 x = ibis.table(ibis.schema([('n', 'int64')]), 'x') t = x.aggregate(cnt=x.n.count()) predicate = t.cnt > 0 assert L.sub_for(predicate, [(t, t.op().table)]).equals(predicate)