cloudflare / sqlalchemy-clickhouse

Apache License 2.0
306 stars 105 forks source link

Issue with Decimal field #35

Closed artbeglaryan closed 5 years ago

artbeglaryan commented 5 years ago

I have table in Clickhouse, which contain Decimal64(8) field. When I try to run simple query to select it via sqlalchemy-clickhouse, code throws an error.

Here is a code DB create and insert

CREATE TABLE sample_table
(
    field1 Decimal64(8), 
    field2 Decimal64(8)
)
ENGINE = MergeTree()
ORDER BY field1

Python code for getting data

import sqlalchemy as sa
a = sa.create_engine('clickhouse://default:@localhost:8123/')
from sqlalchemy.orm import sessionmaker
DBSession = sessionmaker(bind=a)
dbSession = DBSession()
c = dbSession.execute('Select * from sample_table').fetchone()
print c

I think issue can be fixed by adding following in connector.py line 90

    # db_type for Deimal comes like 'Decimal(P, S) string where P is precision and S is scale'
    if db_type.startswith('Decimal'):
        nums = [int(n) for n in db_type[8:-1].split(',')]
        return orm_fields.DecimalField(nums[0], nums[1])
artbeglaryan commented 5 years ago

38 Closing