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 Decimal type support #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ def visit_float32(self, type_, **kw):
def visit_float64(self, type_, **kw):
return 'Float64'

def visit_numeric(self, type_, **kw):
return 'Decimal(%s,%s)' % (type_.precision, type_.scale)

def _render_enum(self, db_type, type_, **kw):
choices = (
"'%s' = %d" %
Expand Down
43 changes: 41 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import datetime

from sqlalchemy import Column
from decimal import Decimal
from sqlalchemy import Column, Numeric
from sqlalchemy.sql.ddl import CreateTable
from clickhouse_sqlalchemy import types, engines, Table
from clickhouse_sqlalchemy.exceptions import DatabaseException

from tests.testcase import TypesTestCase

Expand All @@ -26,3 +27,41 @@ def test_create_table(self):
self.compile(CreateTable(self.table)),
'CREATE TABLE test (x DateTime) ENGINE = Memory'
)


class NumericTypeTestCase(TypesTestCase):
table = Table(
'test', TypesTestCase.metadata(),
Column('x', Numeric(10, 2)),
engines.Memory()
)

def test_create_table(self):
self.assertEqual(
self.compile(CreateTable(self.table)),
'CREATE TABLE test (x Decimal(10,2)) ENGINE = Memory'
)

def test_select_insert(self):
x = Decimal('123456789.12')

with self.create_table(self.table):
self.session.execute(self.table.insert(), [{'x': x}])
self.assertEqual(self.session.query(self.table.c.x).scalar(), x)

def test_insert_truncate(self):
value = Decimal('123.129999')
expected = Decimal('123.12')

with self.create_table(self.table):
self.session.execute(self.table.insert(), [{'x': value}])
self.assertEqual(self.session.query(self.table.c.x).scalar(),
expected)

def test_insert_overflow(self):
value = Decimal('12345678901234567890.1234567890')

with self.create_table(self.table):
with self.assertRaisesRegex(DatabaseException,
'Column x: argument out of range$'):
self.session.execute(self.table.insert(), [{'x': value}])