Skip to content

Commit

Permalink
Remove decimal precision check (#295)
Browse files Browse the repository at this point in the history
* Remove decimal precision check
  • Loading branch information
joelynch committed Mar 4, 2022
1 parent c11bedc commit 378bc3a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
4 changes: 0 additions & 4 deletions clickhouse_driver/columns/decimalcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ def __init__(self, precision, scale, types_check=False, **kwargs):
def check_item(value):
parts = str(value).split('.')
int_part = parts[0]
frac_part = parts[1] if len(parts) > 1 else ''

if len(int_part) > precision:
raise ColumnTypeMismatchException(value)

if len(frac_part) > scale:
raise ColumnTypeMismatchException(value)

self.check_item = check_item

def after_read_items(self, items, nulls_map=None):
Expand Down
31 changes: 13 additions & 18 deletions tests/columns/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,6 @@ def test_type_mismatch(self):

self.assertIn('Column a', str(e.exception))

def test_type_mismatch_scale(self):
data = [(1.234,)]
with self.create_table('a Decimal32(2)'):
with self.assertRaises(errors.TypeMismatchError) as e:
self.client.execute(
'INSERT INTO test (a) VALUES', data, types_check=True
)

self.assertIn('1.234 for column "a"', str(e.exception))

# Without types_check decimal will be cropped.
self.client.execute('INSERT INTO test (a) VALUES', data)
query = 'SELECT * FROM test'
inserted = self.emit_cli(query)
self.assertEqual(inserted, '1.23\n')
inserted = self.client.execute(query)
self.assertEqual(inserted, [(Decimal('1.23'), )])

def test_preserve_precision(self):
data = [(1.66, ), (1.15, )]

Expand Down Expand Up @@ -239,6 +221,19 @@ def test_precision_one_sign_after_point(self):
(Decimal('999999.6'),)
])

def test_truncates_scale(self):
with self.create_table('a Decimal(9, 4)'):
data = [(3.14159265358,), (2.7182,)]
expected = [(Decimal('3.1415'),), (Decimal('2.7182'),)]
self.client.execute(
'INSERT INTO test (a) VALUES',
data,
types_check=True,
)
query = 'SELECT * FROM test'
inserted = self.client.execute(query)
self.assertEqual(inserted, expected)


class Decimal256TestCase(BaseTestCase):
required_server_version = (18, 12, 13)
Expand Down

0 comments on commit 378bc3a

Please sign in to comment.