Skip to content

Commit 2a0607e

Browse files
committed
SNOW-2346552: added more coverage and removed redundant code
1 parent d5f3f54 commit 2a0607e

2 files changed

Lines changed: 100 additions & 42 deletions

File tree

src/snowflake/snowpark/_internal/type_utils.py

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,8 +1490,6 @@ def format_day_time_interval_for_display(
14901490
elif isinstance(cell, str):
14911491
# Raw string that needs to be formatted (e.g., "1 01:01:01.7878")
14921492
interval_str = cell
1493-
else:
1494-
return str(cell).replace("\n", "\\n")
14951493

14961494
field_names = {
14971495
DayTimeIntervalType.DAY: "DAY",
@@ -1557,48 +1555,26 @@ def format_seconds_with_precision(
15571555
seconds_value: Union[float, decimal.Decimal]
15581556
) -> str:
15591557
"""Format seconds with full precision, preserving trailing zeros for proper padding"""
1560-
if isinstance(seconds_value, decimal.Decimal):
1561-
# Use Decimal precision formatting
1562-
if seconds_value == int(seconds_value):
1563-
return f"{int(seconds_value):02d}"
1564-
else:
1565-
# For fractional seconds, ensure proper leading zero padding
1566-
integer_part = int(seconds_value)
1567-
if integer_part < 10:
1568-
# Format with leading zero for the integer part
1569-
formatted = f"{seconds_value:.6f}".rstrip("0")
1570-
if formatted.endswith("."):
1571-
return f"{integer_part:02d}"
1572-
# Replace the integer part with zero-padded version
1573-
decimal_part = formatted.split(".", 1)[1]
1574-
return f"{integer_part:02d}.{decimal_part}"
1575-
else:
1576-
# For >= 10, use normal formatting
1577-
formatted = f"{seconds_value:.6f}".rstrip("0")
1578-
if formatted.endswith("."):
1579-
return f"{integer_part}"
1580-
return formatted
1558+
# Unified formatting logic for both Decimal and float types
1559+
if seconds_value == int(seconds_value):
1560+
return f"{int(seconds_value):02d}"
15811561
else:
1582-
# Float precision formatting (original logic)
1583-
if seconds_value == int(seconds_value):
1584-
return f"{int(seconds_value):02d}"
1562+
# For fractional seconds, ensure proper leading zero padding
1563+
integer_part = int(seconds_value)
1564+
if integer_part < 10:
1565+
# Format with leading zero for the integer part
1566+
formatted = f"{seconds_value:.6f}".rstrip("0")
1567+
if formatted.endswith("."):
1568+
return f"{integer_part:02d}"
1569+
# Replace the integer part with zero-padded version
1570+
decimal_part = formatted.split(".", 1)[1]
1571+
return f"{integer_part:02d}.{decimal_part}"
15851572
else:
1586-
# For fractional seconds, ensure proper leading zero padding
1587-
integer_part = int(seconds_value)
1588-
if integer_part < 10:
1589-
# Format with leading zero for the integer part
1590-
formatted = f"{seconds_value:.6f}".rstrip("0")
1591-
if formatted.endswith("."):
1592-
return f"{integer_part:02d}"
1593-
# Replace the integer part with zero-padded version
1594-
decimal_part = formatted.split(".", 1)[1]
1595-
return f"{integer_part:02d}.{decimal_part}"
1596-
else:
1597-
# For >= 10, use normal formatting
1598-
formatted = f"{seconds_value:.6f}".rstrip("0")
1599-
if formatted.endswith("."):
1600-
return f"{integer_part}"
1601-
return formatted
1573+
# For >= 10, use normal formatting
1574+
formatted = f"{seconds_value:.6f}".rstrip("0")
1575+
if formatted.endswith("."):
1576+
return f"{integer_part}"
1577+
return formatted
16021578

16031579
# For single field intervals, extract just that component
16041580
if start_field == end_field:

tests/integ/test_dataframe.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,88 @@ def test_show_interval_formatting(session):
33733373
"""
33743374
)
33753375

3376+
# === Targeted Tests to Hit Remaining Missing Lines ===
3377+
3378+
# Very large interval to trigger Decimal path with integer seconds
3379+
df = session.sql("SELECT INTERVAL '2000000' DAY as decimal_large_int")
3380+
assert df._show_string_spark(truncate=False) == dedent(
3381+
"""\
3382+
+----------------------+
3383+
|"DECIMAL_LARGE_INT" |
3384+
+----------------------+
3385+
|INTERVAL '2000000' DAY|
3386+
+----------------------+
3387+
"""
3388+
)
3389+
3390+
# Very large interval with fractional seconds < 10 to trigger Decimal path
3391+
df = session.sql(
3392+
"SELECT INTERVAL '2000000 00:00:05.123456' DAY TO SECOND as decimal_small_frac"
3393+
)
3394+
assert df._show_string_spark(truncate=False) == dedent(
3395+
"""\
3396+
+------------------------------------------------+
3397+
|"DECIMAL_SMALL_FRAC" |
3398+
+------------------------------------------------+
3399+
|INTERVAL '2000000 00:00:05.123456' DAY TO SECOND|
3400+
+------------------------------------------------+
3401+
"""
3402+
)
3403+
3404+
# Very large interval with fractional seconds >= 10 to trigger Decimal path
3405+
df = session.sql(
3406+
"SELECT INTERVAL '2000000 00:00:15.123456' DAY TO SECOND as decimal_large_frac"
3407+
)
3408+
assert df._show_string_spark(truncate=False) == dedent(
3409+
"""\
3410+
+------------------------------------------------+
3411+
|"DECIMAL_LARGE_FRAC" |
3412+
+------------------------------------------------+
3413+
|INTERVAL '2000000 00:00:15.123456' DAY TO SECOND|
3414+
+------------------------------------------------+
3415+
"""
3416+
)
3417+
3418+
# Normal interval with integer seconds to trigger float path
3419+
df = session.sql("SELECT INTERVAL '00:00:05' HOUR TO SECOND as float_int_test")
3420+
assert df._show_string_spark(truncate=False) == dedent(
3421+
"""\
3422+
+----------------------------------+
3423+
|"FLOAT_INT_TEST" |
3424+
+----------------------------------+
3425+
|INTERVAL '00:00:05' HOUR TO SECOND|
3426+
+----------------------------------+
3427+
"""
3428+
)
3429+
3430+
# Normal interval with fractional seconds < 10 for float path
3431+
df = session.sql(
3432+
"SELECT INTERVAL '00:00:05.123456' HOUR TO SECOND as float_small_frac"
3433+
)
3434+
assert df._show_string_spark(truncate=False) == dedent(
3435+
"""\
3436+
+-----------------------------------------+
3437+
|"FLOAT_SMALL_FRAC" |
3438+
+-----------------------------------------+
3439+
|INTERVAL '00:00:05.123456' HOUR TO SECOND|
3440+
+-----------------------------------------+
3441+
"""
3442+
)
3443+
3444+
# Normal interval with fractional seconds >= 10 for float path
3445+
df = session.sql(
3446+
"SELECT INTERVAL '00:00:15.123456' HOUR TO SECOND as float_large_frac"
3447+
)
3448+
assert df._show_string_spark(truncate=False) == dedent(
3449+
"""\
3450+
+-----------------------------------------+
3451+
|"FLOAT_LARGE_FRAC" |
3452+
+-----------------------------------------+
3453+
|INTERVAL '00:00:15.123456' HOUR TO SECOND|
3454+
+-----------------------------------------+
3455+
"""
3456+
)
3457+
33763458

33773459
@pytest.mark.parametrize("data", [[0, 1, 2, 3], ["", "a"], [False, True], [None]])
33783460
def test_create_dataframe_with_single_value(session, data):

0 commit comments

Comments
 (0)