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

Can't insert UInt128 value #251

Closed
fisher85 opened this issue Sep 14, 2021 · 1 comment
Closed

Can't insert UInt128 value #251

fisher85 opened this issue Sep 14, 2021 · 1 comment

Comments

@fisher85
Copy link

Describe the bug
An error occurs when trying to insert a value of UInt128 type.

To Reproduce

from clickhouse_driver import connect

with connect(dsn='clickhouse://localhost') as connection:
    with connection.cursor() as cursor:
        cursor.execute('DROP TABLE IF EXISTS test')
        cursor.execute('CREATE TABLE IF NOT EXISTS test (`a` UInt128) ENGINE = MergeTree() ORDER BY (a)')
        data = [
            {'a': 2**127-1},
            {'a': 2**127},
        ]
        cursor.executemany('INSERT INTO test (a) VALUES', data)
        cursor.execute('SELECT * FROM test')
        print(cursor.fetchall())

Throws

Traceback (most recent call last):
  File "/home/xxx/clickhouse.py", line 11, in <module>
    cursor.executemany('INSERT INTO test (a) VALUES', data)
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/dbapi/cursor.py", line 138, in executemany
    response = execute(
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/client.py", line 261, in execute
    rv = self.process_insert_query(
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/client.py", line 478, in process_insert_query
    rv = self.send_data(sample_block, data,
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/client.py", line 531, in send_data
    self.connection.send_data(block)
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/connection.py", line 566, in send_data
    self.block_out.write(block)
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/streams/native.py", line 38, in write
    write_column(self.context, col_name, col_type, items,
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/columns/service.py", line 113, in write_column
    column.write_data(items, buf)
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/columns/base.py", line 77, in write_data
    self._write_data(items, buf)
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/columns/base.py", line 81, in _write_data
    self.write_items(prepared, buf)
  File "/usr/local/lib64/python3.9/site-packages/clickhouse_driver/columns/intcolumn.py", line 115, in write_items
    buf.write(s.pack(*uint_64_pairs))
struct.error: pack expected 4 items for packing (got 8)

Expected behavior
The UInt128 value is expected to be inserted successfully into the table.

If you change the UInt128 type to Int128

from clickhouse_driver import connect

with connect(dsn='clickhouse://localhost') as connection:
    with connection.cursor() as cursor:
        cursor.execute('DROP TABLE IF EXISTS test')
        cursor.execute('CREATE TABLE IF NOT EXISTS test (`a` Int128) ENGINE = MergeTree() ORDER BY (a)')
        data = [
            {'a': 2**127-1},
            {'a': 2**127},
        ]
        cursor.executemany('INSERT INTO test (a) VALUES', data)
        cursor.execute('SELECT * FROM test')
        print(cursor.fetchall())

Everything works well

[(-170141183460469231731687303715884105728,), (170141183460469231731687303715884105727,)]

Versions

  • clickhouse-driver 0.2.1
  • Python 3.9.6
xzkostyan added a commit that referenced this issue Sep 19, 2021
xzkostyan added a commit that referenced this issue Sep 20, 2021
@xzkostyan
Copy link
Member

Fix was merged into master branch.

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

No branches or pull requests

2 participants