Skip to content

Commit

Permalink
fix(parse_sql): parse IN clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRook authored and cpcloud committed Nov 25, 2023
1 parent a465bee commit 8b1f7b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ibis/expr/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ def convert_sum(reduction, catalog):
return getattr(this, method)()


@convert.register(sge.In)
def convert_in(in_, catalog):
this = convert(in_.this, catalog=catalog)
candidates = [convert(expression, catalog) for expression in in_.expressions]
return this.isin(candidates)


@public
@experimental
def parse_sql(sqlstring, catalog, dialect=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ibis


employee = ibis.table(
name="employee",
schema={"first_name": "string", "last_name": "string", "id": "int64"},
)

result = employee.select(employee.first_name).filter(
employee.first_name.isin(
(
ibis.literal("Graham"),
ibis.literal("John"),
ibis.literal("Terry"),
ibis.literal("Eric"),
ibis.literal("Michael"),
)
)
)
10 changes: 10 additions & 0 deletions ibis/expr/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,13 @@ def test_parse_sql_join_with_filter(snapshot):
expr = ibis.parse_sql(sql, catalog)
code = ibis.decompile(expr, format=True)
snapshot.assert_match(code, "decompiled.py")


def test_parse_sql_in_clause(snapshot):
sql = """
SELECT first_name FROM employee
WHERE first_name IN ('Graham', 'John', 'Terry', 'Eric', 'Michael')"""

expr = ibis.parse_sql(sql, catalog)
code = ibis.decompile(expr, format=True)
snapshot.assert_match(code, "decompiled.py")

0 comments on commit 8b1f7b5

Please sign in to comment.