From 2c892ebd0bceb9562ee80cb078df5180590da36f Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Sat, 14 May 2022 07:57:49 -0400 Subject: [PATCH] fix(clickhouse): handle empty IN and NOT IN expressions --- ibis/backends/clickhouse/registry.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ibis/backends/clickhouse/registry.py b/ibis/backends/clickhouse/registry.py index 841ac3d0f594..95fc5d5b0d15 100644 --- a/ibis/backends/clickhouse/registry.py +++ b/ibis/backends/clickhouse/registry.py @@ -164,6 +164,30 @@ def formatter(translator, expr): return formatter +def _in(translator, expr): + op = expr.op() + + left, right = op.args + if isinstance(right, ir.ListExpr) and not right: + return "FALSE" + + left_ = _parenthesize(translator, left) + right_ = _parenthesize(translator, right) + return f"{left_} IN {right_}" + + +def _not_in(translator, expr): + op = expr.op() + + left, right = op.args + if isinstance(right, ir.ListExpr) and not right: + return "TRUE" + + left_ = _parenthesize(translator, left) + right_ = _parenthesize(translator, right) + return f"{left_} NOT IN {right_}" + + def _call(translator, func, *args): args_ = ', '.join(map(translator.translate, args)) return f'{func!s}({args_!s})' @@ -720,8 +744,8 @@ def _string_right(translator, expr): ops.Least: _varargs('least'), ops.Where: _fixed_arity('if', 3), ops.Between: _between, - ops.Contains: _binary_infix_op('IN'), - ops.NotContains: _binary_infix_op('NOT IN'), + ops.Contains: _in, + ops.NotContains: _not_in, ops.SimpleCase: _simple_case, ops.SearchedCase: _searched_case, ops.TableColumn: _table_column,