Skip to content

Commit

Permalink
feat(compiler): restore intersect_class and difference_class over…
Browse files Browse the repository at this point in the history
…rides in base SQL backend

This allows BigQuery and other backends that use a different keyword for
interection or difference to override it in the backend implementation. See:
ibis-project/ibis-bigquery#88 for an example.
  • Loading branch information
tswast authored and cpcloud committed Jun 2, 2022
1 parent ceffc9a commit 2c46a15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ibis/backends/base/sql/compiler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from ibis.backends.base.sql.compiler.base import DDL, DML
from ibis.backends.base.sql.compiler.query_builder import (
Compiler,
Difference,
Intersection,
Select,
SelectBuilder,
TableSetFormatter,
Expand All @@ -16,6 +18,8 @@
'Select',
'SelectBuilder',
'Union',
'Intersection',
'Difference',
'TableSetFormatter',
'ExprTranslator',
'QueryContext',
Expand Down
34 changes: 26 additions & 8 deletions ibis/backends/base/sql/compiler/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,17 @@ def _get_keyword_list(self):


class Intersection(SetOp):
_keyword = "INTERSECT"

def _get_keyword_list(self):
return ["INTERSECT"] * (len(self.tables) - 1)
return [self._keyword] * (len(self.tables) - 1)


class Difference(SetOp):
_keyword = "EXCEPT"

def _get_keyword_list(self):
return ["EXCEPT"] * (len(self.tables) - 1)
return [self._keyword] * (len(self.tables) - 1)


def flatten_union(table: ir.Table):
Expand Down Expand Up @@ -523,6 +527,8 @@ class Compiler:
table_set_formatter_class = TableSetFormatter
select_class = Select
union_class = Union
intersect_class = Intersection
difference_class = Difference

@classmethod
def make_context(cls, params=None):
Expand Down Expand Up @@ -551,11 +557,11 @@ def to_ast(cls, expr, context=None):
# TODO: any setup / teardown DDL statements will need to be done prior
# to building the result set-generating statements.
if isinstance(op, ops.Union):
query = cls._make_union(cls.union_class, expr, context)
query = cls._make_union(expr, context)
elif isinstance(op, ops.Intersection):
query = Intersection(flatten(expr), expr, context=context)
query = cls._make_intersect(expr, context)
elif isinstance(op, ops.Difference):
query = Difference(flatten(expr), expr, context=context)
query = cls._make_difference(expr, context)
else:
query = cls.select_builder_class().to_select(
select_class=cls.select_class,
Expand Down Expand Up @@ -611,8 +617,8 @@ def _generate_setup_queries(expr, context):
def _generate_teardown_queries(expr, context):
return []

@staticmethod
def _make_union(union_class, expr, context):
@classmethod
def _make_union(cls, expr, context):
# flatten unions so that we can codegen them all at once
union_info = list(flatten_union(expr))

Expand All @@ -628,6 +634,18 @@ def _make_union(union_class, expr, context):
# 2. every other object starting from 1 is a bool indicating the type
# of union (distinct or not distinct)
table_exprs, distincts = union_info[::2], union_info[1::2]
return union_class(
return cls.union_class(
table_exprs, expr, distincts=distincts, context=context
)

@classmethod
def _make_intersect(cls, expr, context):
# flatten intersections so that we can codegen them all at once
table_exprs = list(flatten(expr))
return cls.intersect_class(table_exprs, expr, context=context)

@classmethod
def _make_difference(cls, expr, context):
# flatten differences so that we can codegen them all at once
table_exprs = list(flatten(expr))
return cls.difference_class(table_exprs, expr, context=context)
2 changes: 1 addition & 1 deletion ibis/expr/datatypes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ibis.expr.datatypes.core import * # noqa: F403
from ibis.expr.datatypes.core import * # noqa: F401,F403
from ibis.expr.datatypes.core import _normalize, _WellKnownText # noqa: F401

halffloat = float16 # noqa: F405
Expand Down

0 comments on commit 2c46a15

Please sign in to comment.