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

Commit ada754e

Browse files
committed
fix float value precision calculation
1 parent 1790a38 commit ada754e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

data_diff/databases/databricks.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
from typing import Dict, Sequence
23
import logging
34

@@ -61,11 +62,14 @@ def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
6162
return f"date_format({value}, 'yyyy-MM-dd HH:mm:ss.{precision_format}')"
6263

6364
def normalize_number(self, value: str, coltype: NumericType) -> str:
64-
return self.to_string(f"cast({value} as decimal(38, {coltype.precision}))")
65+
value = f"cast({value} as decimal(38, {coltype.precision}))"
66+
if coltype.precision > 0:
67+
value = f"format_number({value}, {coltype.precision})"
68+
return f"replace({self.to_string(value)}, ',', '')"
6569

6670
def _convert_db_precision_to_digits(self, p: int) -> int:
67-
# Subtracting 1 due to wierd precision issues
68-
return max(super()._convert_db_precision_to_digits(p) - 1, 0)
71+
# Subtracting 2 due to wierd precision issues
72+
return max(super()._convert_db_precision_to_digits(p) - 2, 0)
6973

7074

7175
class Databricks(ThreadedDatabase):
@@ -103,8 +107,6 @@ def query_table_schema(self, path: DbPath) -> Dict[str, tuple]:
103107
cursor.columns(catalog_name=self._args['catalog'], schema_name=schema, table_name=table)
104108
try:
105109
rows = cursor.fetchall()
106-
except:
107-
rows = None
108110
finally:
109111
conn.close()
110112
if not rows:
@@ -129,7 +131,7 @@ def _process_table_schema(
129131
row = (row[0], row_type, None, None, 0)
130132

131133
elif issubclass(type_cls, Float):
132-
numeric_precision = self._convert_db_precision_to_digits(row[2])
134+
numeric_precision = math.ceil(row[2] / math.log(2, 10))
133135
row = (row[0], row_type, None, numeric_precision, None)
134136

135137
elif issubclass(type_cls, Decimal):

0 commit comments

Comments
 (0)