Skip to content
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 1 commit into from
Feb 24, 2019

Conversation

AchilleAsh
Copy link
Contributor

ClickHouse 19.3.3 added IPv4 and IPv6 data types (not yet documented), see ClickHouse/ClickHouse#3669

This PR adds IPv4 and IPv6 columns to match these new types (implemented as domains in ClickHouse):

  • IPv4 uses UINT32
  • IPv6 uses FixedString(16)

The matching error code (generic domain error in ClickHouse, not specific to IP Addresses) is also added.

For convenience, the IPv4 column here supports input as integer, string or IPv4Address; the IPv6 column supports raw bytes, string or IPv6Address.

Both testcases are passing on Python 3 and Python 2.7, although the ipaddress module was added in Python 3.3 so I had to add the backport for Python 2.7 in the dependencies.

I thought about adding checks on the server revision number to ensure it supports IP types, but it will already properly return a type not supported error if needed, so I don't think this is necessary.

ch_type = "IPv4"
py_types = compat.string_types + (IPv4Address, int)

def check_item_type(self, value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems than clickhouse_driver.columns.base.Column has the same check_item_type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have removed it. However IntColumn and UIntColumn override the types_check methods, so I've added a condition in the init to properly handle types_check=True on IP columns (added matching tests)

from .intcolumn import UInt32Column


class IPv4(UInt32Column):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add "Column" suffix to class name: class IPv4Column

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

from ipaddress import IPv6Address, IPv4Address


class IPv4TestCase(BaseTestCase):
Copy link
Member

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

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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.

Copy link
Contributor Author

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


class IPv6(ByteFixedString):
ch_type = "IPv6"
py_types = IPv6Address, bytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

py_types = compat.string_types + (IPv6Address, bytes)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, fixed (the test now enforce the type check to ensure the type is in py_types)

ch_type = "IPv6"
py_types = IPv6Address, bytes

def check_item_type(self, value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is also redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

@coveralls
Copy link

coveralls commented Feb 23, 2019

Coverage Status

Coverage decreased (-1.5%) to 94.475% when pulling dd2aaa7 on AchilleAsh:master into 384391d on mymarilyn:master.

@xzkostyan xzkostyan merged commit 2beeac0 into mymarilyn:master Feb 24, 2019
@xzkostyan
Copy link
Member

Great. Do you want to add IP support to clickhouse-sqlalchemy?

@AchilleAsh
Copy link
Contributor Author

Yes, I'm already working on it. Hopefully I will be able to open a PR for this by the end of the month

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants