Skip to content

Commit 0a2f315

Browse files
Saul Pwansoncpcloud
Saul Pwanson
authored andcommitted
fix(clickhouse): add IPv4/IPv6 literal inference
1 parent 26c891d commit 0a2f315

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

ibis/backends/clickhouse/registry.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ def _literal(translator, expr):
363363
return _null_literal(translator, expr)
364364
if isinstance(expr, ir.BooleanValue):
365365
return '1' if value else '0'
366+
elif isinstance(expr, ir.INETValue):
367+
v = str(value)
368+
return f"toIPv6({v!r})" if ':' in v else f"toIPv4({v!r})"
366369
elif isinstance(expr, ir.StringValue):
367370
return "'{!s}'".format(value.replace("'", "\\'"))
368371
elif isinstance(expr, ir.NumericValue):

ibis/expr/datatypes/core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datetime
66
import enum
77
import functools
8+
import ipaddress
89
import numbers
910
import re
1011
import typing
@@ -1371,6 +1372,13 @@ def infer_null(value: Null | None) -> Null:
13711372
return null
13721373

13731374

1375+
@infer.register((ipaddress.IPv4Address, ipaddress.IPv6Address))
1376+
def infer_ipaddr(
1377+
_: ipaddress.IPv4Address | ipaddress.IPv6Address | None,
1378+
) -> INET:
1379+
return inet
1380+
1381+
13741382
if IS_SHAPELY_AVAILABLE:
13751383

13761384
@infer.register(shapely.geometry.Point)

ibis/expr/operations/generic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datetime
55
import decimal
66
import enum
7+
import ipaddress
78
import itertools
89
import uuid
910
from operator import attrgetter
@@ -255,6 +256,8 @@ class Literal(Value):
255256
frozenset,
256257
int,
257258
frozendict,
259+
ipaddress.IPv4Address,
260+
ipaddress.IPv6Address,
258261
np.generic,
259262
np.ndarray,
260263
pd.Timedelta,

ibis/tests/expr/test_value_exprs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ipaddress
12
import operator
23
import uuid
34
from collections import OrderedDict
@@ -57,6 +58,8 @@ def test_null():
5758
('foo', 'string'),
5859
(b'fooblob', 'bytes'),
5960
([1, 2, 3], 'array<int8>'),
61+
(ipaddress.ip_address('1.2.3.4'), 'inet'),
62+
(ipaddress.ip_address('::1'), 'inet'),
6063
],
6164
)
6265
def test_literal_with_implicit_type(value, expected_type):
@@ -115,6 +118,8 @@ def test_listeral_with_unhashable_values(value, expected_type, expected_value):
115118
(-2147483649, 'float64'),
116119
(1.5, 'float64'),
117120
('foo', 'string'),
121+
(ipaddress.ip_address('1.2.3.4'), 'inet'),
122+
(ipaddress.ip_address('::1'), 'inet'),
118123
(list(pointA), 'point'),
119124
(tuple(pointA), 'point'),
120125
(list(lineAB), 'linestring'),

0 commit comments

Comments
 (0)