From 62fa479e2e80fadb8e52244817d9310db861812c Mon Sep 17 00:00:00 2001 From: Tyler White <50381805+IndexSeek@users.noreply.github.com> Date: Sat, 28 Dec 2024 14:37:01 +0000 Subject: [PATCH] feat(polars): reuse existing ctx for set operations --- ibis/backends/polars/compiler.py | 34 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/ibis/backends/polars/compiler.py b/ibis/backends/polars/compiler.py index 13612de1ff21..534976997c51 100644 --- a/ibis/backends/polars/compiler.py +++ b/ibis/backends/polars/compiler.py @@ -1258,18 +1258,19 @@ def execute_union(op, **kw): @translate.register(ops.Intersection) def execute_intersection(op, **kw): - with pl.SQLContext( + ctx = kw.get("ctx") + ctx.register_many( frames={ "left": translate(op.left, **kw), "right": translate(op.right, **kw), } - ) as ctx: - sql = ( - sg.select(STAR) - .from_(sg.to_identifier("left", quoted=True)) - .intersect(sg.select(STAR).from_(sg.to_identifier("right", quoted=True))) - ) - result = ctx.execute(sql.sql()) + ) + sql = ( + sg.select(STAR) + .from_(sg.to_identifier("left", quoted=True)) + .intersect(sg.select(STAR).from_(sg.to_identifier("right", quoted=True))) + ) + result = ctx.execute(sql.sql(Polars), eager=False) if op.distinct is True: return result.unique() return result @@ -1277,18 +1278,19 @@ def execute_intersection(op, **kw): @translate.register(ops.Difference) def execute_difference(op, **kw): - with pl.SQLContext( + ctx = kw.get("ctx") + ctx.register_many( frames={ "left": translate(op.left, **kw), "right": translate(op.right, **kw), } - ) as ctx: - sql = ( - sg.select(STAR) - .from_(sg.to_identifier("left", quoted=True)) - .except_(sg.select(STAR).from_(sg.to_identifier("right", quoted=True))) - ) - result = ctx.execute(sql.sql()) + ) + sql = ( + sg.select(STAR) + .from_(sg.to_identifier("left", quoted=True)) + .except_(sg.select(STAR).from_(sg.to_identifier("right", quoted=True))) + ) + result = ctx.execute(sql.sql(Polars), eager=False) if op.distinct is True: return result.unique() return result