Skip to content

Commit d3fddaf

Browse files
committed
fix(compiler): make decimal arguments optional
1 parent 0589171 commit d3fddaf

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

clickhouse_sqlalchemy/drivers/compilers/typecompiler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ def visit_float64(self, type_, **kw):
8282
return 'Float64'
8383

8484
def visit_numeric(self, type_, **kw):
85-
return 'Decimal(%s, %s)' % (type_.precision, type_.scale)
85+
if type_.precision is None:
86+
return "Decimal"
87+
elif type_.scale is None:
88+
return "Decimal(%(precision)s)" % {"precision": type_.precision}
89+
else:
90+
return ("Decimal(%(precision)s, %(scale)s)" %
91+
{"precision": type_.precision, "scale": type_.scale})
8692

8793
def visit_boolean(self, type_, **kw):
8894
return 'Bool'

tests/types/test_numeric.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from decimal import Decimal
22

3-
from sqlalchemy import Column, Numeric
3+
from sqlalchemy import Column, Numeric, select, cast, column
44
from sqlalchemy.sql.ddl import CreateTable
55

66
from clickhouse_sqlalchemy import types, engines, Table
@@ -36,6 +36,30 @@ def test_create_table_decimal_symlink(self):
3636
'CREATE TABLE test (x Decimal(10, 2)) ENGINE = Memory'
3737
)
3838

39+
def test_scale_and_precision(self):
40+
self.assertEqual(
41+
self.compile(cast(10, Numeric(10, 2))),
42+
"CAST(%(param_1)s AS Decimal(10, 2))"
43+
)
44+
45+
def test_no_scale(self):
46+
self.assertEqual(
47+
self.compile(cast(10, Numeric(10))),
48+
"CAST(%(param_1)s AS Decimal(10))"
49+
)
50+
51+
def test_no_precision(self):
52+
self.assertEqual(
53+
self.compile(cast(10, Numeric())),
54+
"CAST(%(param_1)s AS Decimal)"
55+
)
56+
57+
def test_division_works(self):
58+
self.assertEqual(
59+
self.compile(select(column('x') / column('y'))),
60+
"SELECT x / CAST(y AS Decimal) AS anon_1"
61+
)
62+
3963

4064
class NumericTestCase(BaseTestCase):
4165
table = Table(

0 commit comments

Comments
 (0)