Skip to content

Commit

Permalink
fix(clickhouse): add IPv4/IPv6 literal inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul Pwanson authored and cpcloud committed Aug 31, 2022
1 parent 26c891d commit 0a2f315
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ibis/backends/clickhouse/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ def _literal(translator, expr):
return _null_literal(translator, expr)
if isinstance(expr, ir.BooleanValue):
return '1' if value else '0'
elif isinstance(expr, ir.INETValue):
v = str(value)
return f"toIPv6({v!r})" if ':' in v else f"toIPv4({v!r})"
elif isinstance(expr, ir.StringValue):
return "'{!s}'".format(value.replace("'", "\\'"))
elif isinstance(expr, ir.NumericValue):
Expand Down
8 changes: 8 additions & 0 deletions ibis/expr/datatypes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime
import enum
import functools
import ipaddress
import numbers
import re
import typing
Expand Down Expand Up @@ -1371,6 +1372,13 @@ def infer_null(value: Null | None) -> Null:
return null


@infer.register((ipaddress.IPv4Address, ipaddress.IPv6Address))
def infer_ipaddr(
_: ipaddress.IPv4Address | ipaddress.IPv6Address | None,
) -> INET:
return inet


if IS_SHAPELY_AVAILABLE:

@infer.register(shapely.geometry.Point)
Expand Down
3 changes: 3 additions & 0 deletions ibis/expr/operations/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import decimal
import enum
import ipaddress
import itertools
import uuid
from operator import attrgetter
Expand Down Expand Up @@ -255,6 +256,8 @@ class Literal(Value):
frozenset,
int,
frozendict,
ipaddress.IPv4Address,
ipaddress.IPv6Address,
np.generic,
np.ndarray,
pd.Timedelta,
Expand Down
5 changes: 5 additions & 0 deletions ibis/tests/expr/test_value_exprs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ipaddress
import operator
import uuid
from collections import OrderedDict
Expand Down Expand Up @@ -57,6 +58,8 @@ def test_null():
('foo', 'string'),
(b'fooblob', 'bytes'),
([1, 2, 3], 'array<int8>'),
(ipaddress.ip_address('1.2.3.4'), 'inet'),
(ipaddress.ip_address('::1'), 'inet'),
],
)
def test_literal_with_implicit_type(value, expected_type):
Expand Down Expand Up @@ -115,6 +118,8 @@ def test_listeral_with_unhashable_values(value, expected_type, expected_value):
(-2147483649, 'float64'),
(1.5, 'float64'),
('foo', 'string'),
(ipaddress.ip_address('1.2.3.4'), 'inet'),
(ipaddress.ip_address('::1'), 'inet'),
(list(pointA), 'point'),
(tuple(pointA), 'point'),
(list(lineAB), 'linestring'),
Expand Down

0 comments on commit 0a2f315

Please sign in to comment.