-
-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add native IPv4 and IPv6 types support #73
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
env: | ||
- VERSION=19.3.3 | ||
- VERSION=18.12.17 | ||
- VERSION=18.12.13 | ||
- VERSION=18.10.3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ Features | |
* Nullable(T) | ||
* UUID | ||
* Decimal | ||
* IPv4/IPv6 | ||
|
||
- Query progress information. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from ipaddress import IPv4Address, IPv6Address, AddressValueError | ||
|
||
from .. import errors | ||
from ..util import compat | ||
from .exceptions import ColumnTypeMismatchException | ||
from .stringcolumn import ByteFixedString | ||
from .intcolumn import UInt32Column | ||
|
||
|
||
class IPv4Column(UInt32Column): | ||
ch_type = "IPv4" | ||
py_types = compat.string_types + (IPv4Address, int) | ||
|
||
def __init__(self, types_check=False, **kwargs): | ||
# UIntColumn overrides before_write_item and check_item | ||
# in its __init__ when types_check is True so we force | ||
# __init__ without it then add the appropriate check method for IPv4 | ||
super(UInt32Column, self).__init__(types_check=False, **kwargs) | ||
|
||
self.types_check_enabled = types_check | ||
if types_check: | ||
|
||
def check_item(value): | ||
if isinstance(value, int) and value < 0: | ||
raise ColumnTypeMismatchException(value) | ||
|
||
if not isinstance(value, IPv4Address): | ||
try: | ||
value = IPv4Address(value) | ||
except AddressValueError: | ||
# Cannot parse input in a valid IPv4 | ||
raise ColumnTypeMismatchException(value) | ||
|
||
self.check_item = check_item | ||
|
||
def before_write_item(self, value): | ||
# allow Ipv4 in integer, string or IPv4Address object | ||
try: | ||
if isinstance(value, int): | ||
return value | ||
|
||
if not isinstance(value, IPv4Address): | ||
value = IPv4Address(value) | ||
|
||
return int(value) | ||
except AddressValueError: | ||
raise errors.CannotParseDomainError( | ||
"Cannot parse IPv4 '{}'".format(value) | ||
) | ||
|
||
def after_read_item(self, value): | ||
return IPv4Address(value) | ||
|
||
|
||
class IPv6Column(ByteFixedString): | ||
ch_type = "IPv6" | ||
py_types = compat.string_types + (IPv6Address, bytes) | ||
|
||
def __init__(self, types_check=False, **kwargs): | ||
super(IPv6Column, self).__init__(16, types_check=types_check, **kwargs) | ||
|
||
if types_check: | ||
|
||
def check_item(value): | ||
if isinstance(value, bytes) and len(value) != 16: | ||
raise ColumnTypeMismatchException(value) | ||
|
||
if not isinstance(value, IPv6Address): | ||
try: | ||
value = IPv6Address(value) | ||
except AddressValueError: | ||
# Cannot parse input in a valid IPv6 | ||
raise ColumnTypeMismatchException(value) | ||
|
||
self.check_item = check_item | ||
|
||
def before_write_item(self, value): | ||
# allow Ipv6 in bytes or python IPv6Address | ||
# this is raw bytes (not encoded) in order to fit FixedString(16) | ||
try: | ||
if isinstance(value, bytes): | ||
return value | ||
|
||
if not isinstance(value, IPv6Address): | ||
value = IPv6Address(value) | ||
return value.packed | ||
except AddressValueError: | ||
raise errors.CannotParseDomainError( | ||
"Cannot parse IPv6 '{}'".format(value) | ||
) | ||
|
||
def after_read_item(self, value): | ||
return IPv6Address(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
from __future__ import unicode_literals | ||
|
||
from clickhouse_driver import errors | ||
from ipaddress import IPv6Address, IPv4Address | ||
|
||
from tests.testcase import BaseTestCase | ||
from tests.util import require_server_version | ||
|
||
|
||
class IPv4TestCase(BaseTestCase): | ||
@require_server_version(19, 3, 3) | ||
def test_simple(self): | ||
with self.create_table('a IPv4'): | ||
data = [ | ||
(IPv4Address("10.0.0.1"),), | ||
(IPv4Address("192.168.253.42"),) | ||
] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, ( | ||
'10.0.0.1\n' | ||
'192.168.253.42\n' | ||
)) | ||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, [ | ||
(IPv4Address("10.0.0.1"),), | ||
(IPv4Address("192.168.253.42"),) | ||
]) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_from_int(self): | ||
with self.create_table('a IPv4'): | ||
data = [ | ||
(167772161,), | ||
] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, ( | ||
'10.0.0.1\n' | ||
)) | ||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, [ | ||
(IPv4Address("10.0.0.1"),), | ||
]) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_from_str(self): | ||
with self.create_table('a IPv4'): | ||
data = [ | ||
("10.0.0.1",), | ||
] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, ( | ||
'10.0.0.1\n' | ||
)) | ||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, [ | ||
(IPv4Address("10.0.0.1"),), | ||
]) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_type_mismatch(self): | ||
data = [(1025.2147,)] | ||
with self.create_table('a IPv4'): | ||
with self.assertRaises(errors.TypeMismatchError): | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_bad_ipv4(self): | ||
data = [('985.512.12.0',)] | ||
with self.create_table('a IPv4'): | ||
with self.assertRaises(errors.CannotParseDomainError): | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data | ||
) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_bad_ipv4_with_type_check(self): | ||
data = [('985.512.12.0',)] | ||
with self.create_table('a IPv4'): | ||
with self.assertRaises(errors.TypeMismatchError): | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_nullable(self): | ||
with self.create_table('a Nullable(IPv4)'): | ||
data = [(IPv4Address('10.10.10.10'),), (None,)] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, | ||
'10.10.10.10\n\\N\n') | ||
|
||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, data) | ||
|
||
|
||
class IPv6TestCase(BaseTestCase): | ||
@require_server_version(19, 3, 3) | ||
def test_simple(self): | ||
with self.create_table('a IPv6'): | ||
data = [ | ||
(IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),), | ||
(IPv6Address('a22:cc64:cf47:1653:4976:3c0c:ff8d:417c'),), | ||
(IPv6Address('12ff:0000:0000:0000:0000:0000:0000:0001'),) | ||
] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, ( | ||
'79f4:e698:45de:a59b:2765:28e3:8d3a:35ae\n' | ||
'a22:cc64:cf47:1653:4976:3c0c:ff8d:417c\n' | ||
'12ff::1\n' | ||
)) | ||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, [ | ||
(IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),), | ||
(IPv6Address('a22:cc64:cf47:1653:4976:3c0c:ff8d:417c'),), | ||
(IPv6Address('12ff::1'),) | ||
]) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_from_str(self): | ||
with self.create_table('a IPv6'): | ||
data = [ | ||
('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae',), | ||
] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, ( | ||
'79f4:e698:45de:a59b:2765:28e3:8d3a:35ae\n' | ||
)) | ||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, [ | ||
(IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),), | ||
]) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_from_bytes(self): | ||
with self.create_table('a IPv6'): | ||
data = [ | ||
(b"y\xf4\xe6\x98E\xde\xa5\x9b'e(\xe3\x8d:5\xae",), | ||
] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, ( | ||
'79f4:e698:45de:a59b:2765:28e3:8d3a:35ae\n' | ||
)) | ||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, [ | ||
(IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),), | ||
]) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_type_mismatch(self): | ||
data = [(1025.2147,)] | ||
with self.create_table('a IPv6'): | ||
with self.assertRaises(errors.TypeMismatchError): | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_bad_ipv6(self): | ||
data = [("ghjk:e698:45de:a59b:2765:28e3:8d3a:zzzz",)] | ||
with self.create_table('a IPv6'): | ||
with self.assertRaises(errors.CannotParseDomainError): | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data | ||
) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_bad_ipv6_with_type_check(self): | ||
data = [("ghjk:e698:45de:a59b:2765:28e3:8d3a:zzzz",)] | ||
with self.create_table('a IPv6'): | ||
with self.assertRaises(errors.TypeMismatchError): | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data, types_check=True | ||
) | ||
|
||
@require_server_version(19, 3, 3) | ||
def test_nullable(self): | ||
with self.create_table('a Nullable(IPv6)'): | ||
data = [ | ||
(IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae'),), | ||
(None,)] | ||
self.client.execute( | ||
'INSERT INTO test (a) VALUES', data | ||
) | ||
|
||
query = 'SELECT * FROM test' | ||
inserted = self.emit_cli(query) | ||
self.assertEqual(inserted, | ||
'79f4:e698:45de:a59b:2765:28e3:8d3a:35ae\n\\N\n') | ||
|
||
inserted = self.client.execute(query) | ||
self.assertEqual(inserted, data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to add new ClickHouse release with IP addresses support to travis matrix: https://github.com/mymarilyn/clickhouse-driver/blob/master/.travis.yml#L2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To skip tests for versions that do not support IP addresses use
require_server_version
decorator: https://github.com/mymarilyn/clickhouse-driver/blob/master/tests/columns/test_decimal.py#L12.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check travis for build status: https://travis-ci.org/mymarilyn/clickhouse-driver/pull_requests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just pushed branch https://github.com/mymarilyn/clickhouse-driver/tree/feature-version-dependent-settings-in-test where Decimal tests run without errors with latest server version. You can use it as branchpoint for making all tests green.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the decorator on all tests, this should work now. Note that tests for the Decimal column will fail on 19.3.3 because the --allow_experimental_decimal_type flag was removed in 18.14.9 (it is enabled by default now), so we may want to add an upper version check decorator