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 support for decimal256 and allow rounding decimal to scale #439

Merged
merged 6 commits into from
Jan 3, 2025
Merged
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 deltacat/tests/utils/data/overflowing_decimal_precision.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Y 322236.65
"" 32.33
N 0.40
3 changes: 3 additions & 0 deletions deltacat/tests/utils/data/overflowing_decimal_scale.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Y 322236.65883738
"" 32.33
N 0.40
255 changes: 255 additions & 0 deletions deltacat/tests/utils/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
s3_file_to_table,
ReadKwargsProviderPyArrowSchemaOverride,
RAISE_ON_EMPTY_CSV_KWARG,
RAISE_ON_DECIMAL_OVERFLOW,
)
import decimal
from deltacat.types.media import ContentEncoding, ContentType
from deltacat.types.partial_download import PartialParquetParameters
from pyarrow.parquet import ParquetFile
Expand All @@ -16,6 +18,12 @@
PARQUET_FILE_PATH = "deltacat/tests/utils/data/test_file.parquet"
EMPTY_UTSV_PATH = "deltacat/tests/utils/data/empty.csv"
NON_EMPTY_VALID_UTSV_PATH = "deltacat/tests/utils/data/non_empty_valid.csv"
OVERFLOWING_DECIMAL_PRECISION_UTSV_PATH = (
"deltacat/tests/utils/data/overflowing_decimal_precision.csv"
)
OVERFLOWING_DECIMAL_SCALE_UTSV_PATH = (
"deltacat/tests/utils/data/overflowing_decimal_scale.csv"
)
GZIP_COMPRESSED_FILE_UTSV_PATH = "deltacat/tests/utils/data/non_empty_compressed.gz"
BZ2_COMPRESSED_FILE_UTSV_PATH = "deltacat/tests/utils/data/non_empty_compressed.bz2"

Expand Down Expand Up @@ -407,6 +415,253 @@ def test_read_csv_when_empty_csv_and_raise_on_empty_passed(self):
),
)

def test_read_csv_when_decimal_precision_overflows_and_raise_kwarg_specified(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(4, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)
read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)
self.assertRaises(
pa.lib.ArrowInvalid,
lambda: pyarrow_read_csv(
OVERFLOWING_DECIMAL_PRECISION_UTSV_PATH,
**{**kwargs, RAISE_ON_DECIMAL_OVERFLOW: True}
),
)

def test_read_csv_when_decimal_precision_overflows_sanity(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(4, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

self.assertRaises(
pa.lib.ArrowInvalid,
lambda: pyarrow_read_csv(OVERFLOWING_DECIMAL_PRECISION_UTSV_PATH, **kwargs),
)

def test_read_csv_when_decimal_scale_overflows_and_raise_kwarg_specified(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(20, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)
read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

self.assertRaises(
pa.lib.ArrowInvalid,
lambda: pyarrow_read_csv(
OVERFLOWING_DECIMAL_SCALE_UTSV_PATH,
**{**kwargs, RAISE_ON_DECIMAL_OVERFLOW: True}
),
)

def test_read_csv_when_decimal_scale_overflows_sanity(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(20, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

result = pyarrow_read_csv(OVERFLOWING_DECIMAL_SCALE_UTSV_PATH, **kwargs)

self.assertEqual(len(result), 3)
self.assertEqual(
result[1][0].as_py(), decimal.Decimal("322236.66")
) # rounding decimal
self.assertEqual(result[1][1].as_py(), decimal.Decimal("32.33")) # not rounded
self.assertEqual(len(result.column_names), 2)
result_schema = result.schema
self.assertEqual(result_schema.field(0).type, "string")
self.assertEqual(result_schema.field(1).type, pa.decimal128(20, 2))

def test_read_csv_when_decimal_scale_overflows_and_negative_scale(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(20, -2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

result = pyarrow_read_csv(OVERFLOWING_DECIMAL_SCALE_UTSV_PATH, **kwargs)

self.assertEqual(len(result), 3)
self.assertEqual(
result[1][0].as_py(),
decimal.Decimal("322200"), # consequence of negative scale
) # rounding decimal
self.assertEqual(result[1][1].as_py(), decimal.Decimal("00"))
self.assertEqual(len(result.column_names), 2)
result_schema = result.schema
self.assertEqual(result_schema.field(0).type, "string")
self.assertEqual(result_schema.field(1).type, pa.decimal128(20, -2))

def test_read_csv_when_decimal_scale_overflows_with_decimal256(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal256(20, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

result = pyarrow_read_csv(OVERFLOWING_DECIMAL_SCALE_UTSV_PATH, **kwargs)

self.assertEqual(len(result), 3)
self.assertEqual(
result[1][0].as_py(), decimal.Decimal("322236.66")
) # rounding decimal
self.assertEqual(result[1][1].as_py(), decimal.Decimal("32.33")) # not rounded
self.assertEqual(len(result.column_names), 2)
result_schema = result.schema
self.assertEqual(result_schema.field(0).type, "string")
self.assertEqual(result_schema.field(1).type, pa.decimal256(20, 2))

def test_read_csv_when_decimal_scale_overflows_with_decimal256_and_raise_on_overflow(
self,
):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal256(20, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

self.assertRaises(
pa.lib.ArrowNotImplementedError,
lambda: pyarrow_read_csv(
OVERFLOWING_DECIMAL_SCALE_UTSV_PATH,
**{**kwargs, RAISE_ON_DECIMAL_OVERFLOW: True}
),
)

def test_read_csv_when_decimal_scale_overflows_without_any_schema_then_infers(self):
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=None)
kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

result = pyarrow_read_csv(OVERFLOWING_DECIMAL_SCALE_UTSV_PATH, **kwargs)

# The default behavior of pyarrow is to invalid skip rows
self.assertEqual(len(result), 2)
self.assertEqual(result[1][0].as_py(), 32.33) # rounding decimal
self.assertEqual(result[1][1].as_py(), 0.4) # not rounded
self.assertEqual(len(result.column_names), 2)
result_schema = result.schema
self.assertEqual(result_schema.field(0).type, "string")
self.assertEqual(result_schema.field(1).type, pa.float64())

def test_read_csv_when_decimal_scale_and_precision_overflow_and_raise_on_overflow(
self,
):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(5, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

self.assertRaises(
pa.lib.ArrowInvalid,
lambda: pyarrow_read_csv(OVERFLOWING_DECIMAL_SCALE_UTSV_PATH, **kwargs),
)

def test_read_csv_when_decimal_scale_overflow_and_file_like_obj_passed(self):
schema = pa.schema(
[("is_active", pa.string()), ("decimal_value", pa.decimal128(15, 2))]
)
kwargs = content_type_to_reader_kwargs(ContentType.UNESCAPED_TSV.value)
_add_column_kwargs(
ContentType.UNESCAPED_TSV.value,
["is_active", "decimal_value"],
["is_active", "decimal_value"],
kwargs,
)

read_kwargs_provider = ReadKwargsProviderPyArrowSchemaOverride(schema=schema)

kwargs = read_kwargs_provider(ContentType.UNESCAPED_TSV.value, kwargs)

with open(OVERFLOWING_DECIMAL_SCALE_UTSV_PATH, "rb") as file:
result = pyarrow_read_csv(file, **kwargs)

self.assertEqual(len(result), 3)
self.assertEqual(
result[1][0].as_py(), decimal.Decimal("322236.66")
) # rounding decimal
self.assertEqual(
result[1][1].as_py(), decimal.Decimal("32.33")
) # not rounded
self.assertEqual(len(result.column_names), 2)
result_schema = result.schema
self.assertEqual(result_schema.field(0).type, "string")
self.assertEqual(result_schema.field(1).type, pa.decimal128(15, 2))


class TestS3FileToTable(TestCase):
def test_s3_file_to_table_identity_sanity(self):
Expand Down
Loading
Loading