Skip to content

Commit 8b1f7b5

Browse files
AndrewRookcpcloud
authored andcommitted
fix(parse_sql): parse IN clauses
1 parent a465bee commit 8b1f7b5

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

ibis/expr/sql.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ def convert_sum(reduction, catalog):
260260
return getattr(this, method)()
261261

262262

263+
@convert.register(sge.In)
264+
def convert_in(in_, catalog):
265+
this = convert(in_.this, catalog=catalog)
266+
candidates = [convert(expression, catalog) for expression in in_.expressions]
267+
return this.isin(candidates)
268+
269+
263270
@public
264271
@experimental
265272
def parse_sql(sqlstring, catalog, dialect=None):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ibis
2+
3+
4+
employee = ibis.table(
5+
name="employee",
6+
schema={"first_name": "string", "last_name": "string", "id": "int64"},
7+
)
8+
9+
result = employee.select(employee.first_name).filter(
10+
employee.first_name.isin(
11+
(
12+
ibis.literal("Graham"),
13+
ibis.literal("John"),
14+
ibis.literal("Terry"),
15+
ibis.literal("Eric"),
16+
ibis.literal("Michael"),
17+
)
18+
)
19+
)

ibis/expr/tests/test_sql.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,13 @@ def test_parse_sql_join_with_filter(snapshot):
139139
expr = ibis.parse_sql(sql, catalog)
140140
code = ibis.decompile(expr, format=True)
141141
snapshot.assert_match(code, "decompiled.py")
142+
143+
144+
def test_parse_sql_in_clause(snapshot):
145+
sql = """
146+
SELECT first_name FROM employee
147+
WHERE first_name IN ('Graham', 'John', 'Terry', 'Eric', 'Michael')"""
148+
149+
expr = ibis.parse_sql(sql, catalog)
150+
code = ibis.decompile(expr, format=True)
151+
snapshot.assert_match(code, "decompiled.py")

0 commit comments

Comments
 (0)