Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit c13e21b

Browse files
committed
add tests for checking precision
1 parent 986c861 commit c13e21b

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

data_diff/databases/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class BaseDialect(abc.ABC):
201201
SUPPORTS_INDEXES: ClassVar[bool] = False
202202
PREVENT_OVERFLOW_WHEN_CONCAT: ClassVar[bool] = False
203203
TYPE_CLASSES: ClassVar[Dict[str, Type[ColType]]] = {}
204+
DEFAULT_NUMERIC_PRECISION: ClassVar[int] = 0 # effective precision when type is just "NUMERIC"
204205

205206
PLACEHOLDER_TABLE = None # Used for Oracle
206207

data_diff/databases/bigquery.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class Dialect(BaseDialect):
7575
}
7676
TYPE_ARRAY_RE = re.compile(r"ARRAY<(.+)>")
7777
TYPE_STRUCT_RE = re.compile(r"STRUCT<(.+)>")
78+
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#parameterized_decimal_type
79+
# The default scale is 9, which means a number can have up to 9 digits after the decimal point.
80+
DEFAULT_NUMERIC_PRECISION = 9
7881

7982
def random(self) -> str:
8083
return "RAND()"

data_diff/databases/duckdb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class Dialect(BaseDialect):
4545
SUPPORTS_PRIMARY_KEY = True
4646
SUPPORTS_INDEXES = True
4747

48+
# https://duckdb.org/docs/sql/data_types/numeric#fixed-point-decimals
49+
# The default WIDTH and SCALE is DECIMAL(18, 3), if none are specified.
50+
DEFAULT_NUMERIC_PRECISION = 3
51+
4852
TYPE_CLASSES = {
4953
# Timestamps
5054
"TIMESTAMP WITH TIME ZONE": TimestampTZ,

data_diff/databases/postgresql.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class PostgresqlDialect(BaseDialect):
4545
SUPPORTS_PRIMARY_KEY: ClassVar[bool] = True
4646
SUPPORTS_INDEXES = True
4747

48+
# https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL
49+
# without any precision or scale creates an “unconstrained numeric” column
50+
# in which numeric values of any length can be stored, up to the implementation limits.
51+
# https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-NUMERIC-TABLE
52+
DEFAULT_NUMERIC_PRECISION = 16383
53+
4854
TYPE_CLASSES: ClassVar[Dict[str, Type[ColType]]] = {
4955
# Timestamps
5056
"timestamp with time zone": TimestampTZ,

tests/test_database.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,36 @@ def test_three_part_support(self):
134134
d = db.query_table_schema(part.path)
135135
assert len(d) == 1
136136
db.query(part.drop())
137+
138+
139+
@test_each_database
140+
class TestNumericPrecisionParsing(unittest.TestCase):
141+
def test_specified_precision(self):
142+
name = "tbl_" + random_table_suffix()
143+
db = get_conn(self.db_cls)
144+
tbl = table(name, schema={"value": "NUMERIC(10, 2)"})
145+
db.query(tbl.create())
146+
t = table(name)
147+
raw_schema = db.query_table_schema(t.path)
148+
schema = db._process_table_schema(t.path, raw_schema)
149+
self.assertEqual(schema["value"].precision, 2)
150+
151+
def test_specified_zero_precision(self):
152+
name = "tbl_" + random_table_suffix()
153+
db = get_conn(self.db_cls)
154+
tbl = table(name, schema={"value": "NUMERIC(10)"})
155+
db.query(tbl.create())
156+
t = table(name)
157+
raw_schema = db.query_table_schema(t.path)
158+
schema = db._process_table_schema(t.path, raw_schema)
159+
self.assertEqual(schema["value"].precision, 0)
160+
161+
def test_default_precision(self):
162+
name = "tbl_" + random_table_suffix()
163+
db = get_conn(self.db_cls)
164+
tbl = table(name, schema={"value": "NUMERIC"})
165+
db.query(tbl.create())
166+
t = table(name)
167+
raw_schema = db.query_table_schema(t.path)
168+
schema = db._process_table_schema(t.path, raw_schema)
169+
self.assertEqual(schema["value"].precision, db.dialect.DEFAULT_NUMERIC_PRECISION)

0 commit comments

Comments
 (0)