Skip to content

Commit

Permalink
Fix numeric tests
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Jun 2, 2024
1 parent 23f7838 commit bef272c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 4 additions & 2 deletions py-tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def test_arrays(self):
"""(1, Array['a', 'b'], Array[1, 2], Array['\\x01'::bytea, '\\x02'::bytea], Array[1.1, 2.2]),
(2, NULL, NULL, NULL, NULL),
(3, Array[NULL, 'a', NULL, 'b'], Array[]::int[], Array[NULL::bytea], Array[1.1, 'NaN'::numeric, 1000010])
"""
""",
options=["--numeric-handling=decimal"]
)
duckdb_table = duckdb.read_parquet(file).fetchall()
self.assertEqual(duckdb_table[0], (1, ["a", "b"], [1, 2], [ b"\x01", b"\x02" ], [Decimal("1.10"), Decimal("2.20")]))
Expand Down Expand Up @@ -63,7 +64,8 @@ def test_ranges(self):
(1, '[1,2)', ARRAY['[1,2)'::int4range,'[2,3)'], ARRAY['[1.1,2.2)'::numrange,'[2.2,3.3)'], ARRAY['[2020-01-01 00:00:00,2020-01-02 00:00:00)'::tsrange,'[2020-01-02 00:00:00,2020-01-03 00:00:00)'::tsrange]),
(2, NULL, NULL, NULL, NULL),
(3, '(,2]', ARRAY[NULL::int4range, '(2,)', 'empty'], ARRAY['(1.1,)'::numrange,'(,)'], ARRAY[]::tsrange[])
"""
""",
options=["--numeric-handling=decimal"]
)
def r(low, up, low_inc=True, up_inc=False, is_empty=False):
return {'lower': low, 'upper': up, 'lower_inclusive': low_inc, 'upper_inclusive': up_inc, 'is_empty': is_empty}
Expand Down
17 changes: 9 additions & 8 deletions py-tests/test_basic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ def test_floats(self):
self.assertEqual(list(polars_df["f32"].cast(str)), ['1.1', 'NaN', 'inf', '-inf', '-0.0'])
self.assertEqual(list(polars_df["f64"].cast(str)), ['2.2', 'NaN', 'inf', '-inf', '-0.0'])

def test_numeric(self):
def test_decimal(self):
file = wrappers.create_and_export(
"numeric_types", "id",
"id int, normal numeric(10, 5), high_precision numeric(140, 100)",
"(1, 1000.0001, 1.00000000000000000000000000000000000000000001), (2, 'NaN', 'NaN')"
"(1, 1000.0001, 1.00000000000000000000000000000000000000000001), (2, 'NaN', 'NaN')",
options=["--numeric-handling=decimal"]
)
duckdb_table = duckdb.read_parquet(file).fetchall()
self.assertEqual(duckdb_table, [
Expand All @@ -122,31 +123,31 @@ def test_numeric(self):
# "high_precision": pl.Binary
# })

file2 = wrappers.run_export("numeric_types_higher_prec", "select * from numeric_types order by id", options=["--decimal-precision=76", "--decimal-scale=50"])
file2 = wrappers.run_export("numeric_types_higher_prec", "select * from numeric_types order by id", options=["--decimal-precision=76", "--decimal-scale=50", "--numeric-handling=decimal"])
# only PyArrow supports precision=76
pd_df = pd.read_parquet(file2, engine="pyarrow")
self.assertEqual(list(pd_df["normal"]), [Decimal('1000.000100000000000000'), None])
self.assertEqual(list(pd_df["high_precision"]), [Decimal('1.00000000000000000000000000000000000000000001'), None])

def test_numeric_i32(self):
def test_decimal_i32(self):
file = wrappers.create_and_export(
"numeric_types", "id",
"id int, normal numeric(10, 5), high_precision numeric(140, 100)",
"(1, 1000.0001, 1.00000000000000000000000000000000000000000001), (2, 'NaN', 'NaN')",
options=["--decimal-precision=9", "--decimal-scale=4"]
options=["--decimal-precision=9", "--decimal-scale=4", "--numeric-handling=decimal"]
)
duckdb_table = duckdb.read_parquet(file).fetchall()
self.assertEqual(duckdb_table, [
(1, Decimal('1000.000100000000000000'), Decimal('1.000000000000000000')),
(2, None, None )
])

def test_numeric_i64(self):
def test_decimal_i64(self):
file = wrappers.create_and_export(
"numeric_types", "id",
"id int, normal numeric(10, 5), high_precision numeric(140, 100)",
"(1, 1000.0001, 1.00000000000000000000000000000000000000000001), (2, 'NaN', 'NaN')",
options=["--decimal-precision=18", "--decimal-scale=9"]
options=["--decimal-precision=18", "--decimal-scale=9", "--numeric-handling=decimal"]
)
duckdb_table = duckdb.read_parquet(file).fetchall()
self.assertEqual(duckdb_table, [
Expand All @@ -158,7 +159,7 @@ def test_numeric_f64(self):
"numeric_types", "id",
"id int, normal numeric(10, 5), high_precision numeric(140, 100)",
"(1, 1000.0001, 1.00000000000000000000000000000000000000000001), (2, 'NaN', 'NaN')",
options=["--numeric-handling=double"]
#options=["--numeric-handling=double"]
)
duckdb_table = duckdb.read_parquet(file).fetchall()
self.assertEqual(duckdb_table[0], (1, 1000.0001, 1))
Expand Down

0 comments on commit bef272c

Please sign in to comment.