From 8b1f7b5a299ae06259c4c15c7d02d772259bf6bc Mon Sep 17 00:00:00 2001 From: Andrew Schechtman-Rook Date: Sat, 25 Nov 2023 15:31:12 -0500 Subject: [PATCH] fix(parse_sql): parse IN clauses --- ibis/expr/sql.py | 7 +++++++ .../test_parse_sql_in_clause/decompiled.py | 19 +++++++++++++++++++ ibis/expr/tests/test_sql.py | 10 ++++++++++ 3 files changed, 36 insertions(+) create mode 100644 ibis/expr/tests/snapshots/test_sql/test_parse_sql_in_clause/decompiled.py diff --git a/ibis/expr/sql.py b/ibis/expr/sql.py index 5e5241561b8b..71ce9068efac 100644 --- a/ibis/expr/sql.py +++ b/ibis/expr/sql.py @@ -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): diff --git a/ibis/expr/tests/snapshots/test_sql/test_parse_sql_in_clause/decompiled.py b/ibis/expr/tests/snapshots/test_sql/test_parse_sql_in_clause/decompiled.py new file mode 100644 index 000000000000..cc4993250d02 --- /dev/null +++ b/ibis/expr/tests/snapshots/test_sql/test_parse_sql_in_clause/decompiled.py @@ -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"), + ) + ) +) diff --git a/ibis/expr/tests/test_sql.py b/ibis/expr/tests/test_sql.py index 9e2961401191..5ed847000304 100644 --- a/ibis/expr/tests/test_sql.py +++ b/ibis/expr/tests/test_sql.py @@ -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")