Skip to content

Commit 8c0af01

Browse files
committed
SNOW-3027686: Fix spark string format for interval year-month
1 parent 03d045e commit 8c0af01

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/snowflake/snowpark/_internal/type_utils.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,18 +1462,25 @@ def format_year_month_interval_for_display(
14621462
# Default initialization
14631463
years = "0"
14641464
months = "0"
1465-
is_negative = False
1465+
is_negative = cell.startswith("-")
1466+
# Remove the sign prefix and parse the remaining part
1467+
remaining = cell[1:] # Remove the "+" or "-" prefix: "1-6"
14661468

14671469
if has_internal_dash:
14681470
# Format like "+1-03" or "-1-03" or "-1-6" (compound year-month)
1469-
is_negative = cell.startswith("-")
1470-
1471-
# Remove the sign prefix and parse the remaining "year-month" part
1472-
remaining = cell[1:] # Remove the "+" or "-" prefix: "1-6"
14731471
if "-" in remaining:
14741472
parts = remaining.split("-", 1) # Split only on first dash: ["1", "6"]
14751473
years = str(int(parts[0]))
14761474
months = str(int(parts[1]))
1475+
else:
1476+
# Format like "+2" or "-3"
1477+
if (
1478+
start_field == YearMonthIntervalType.YEAR
1479+
and end_field == YearMonthIntervalType.YEAR
1480+
):
1481+
years = str(int(remaining))
1482+
else:
1483+
months = str(int(remaining))
14771484

14781485
# Format based on start/end field
14791486
sign_prefix = "-" if is_negative else ""
@@ -1496,9 +1503,7 @@ def format_year_month_interval_for_display(
14961503
):
14971504
# Months only: MONTH - calculate total months
14981505
total_months = int(years) * 12 + int(months)
1499-
if is_negative:
1500-
total_months = -total_months
1501-
return f"INTERVAL '{total_months}' MONTH"
1506+
return f"INTERVAL '{sign_prefix}{total_months}' MONTH"
15021507

15031508

15041509
def format_day_time_interval_for_display(

tests/integ/scala/test_dataframe_suite.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,13 +3308,13 @@ def test_year_month_interval_type_dataframe(session):
33083308
test_result = df.collect()
33093309

33103310
assert len(test_result) == 2
3311-
assert test_result[0][0] == "+3-00"
3312-
assert test_result[0][1] == "+1-06"
3313-
assert test_result[0][2] == "+1-03"
3311+
assert test_result[0][0] == "+3" # interval year
3312+
assert test_result[0][1] == "+1-06" # interval year to month
3313+
assert test_result[0][2] == "+15" # interval month
33143314

3315-
assert test_result[1][0] == "-1-00"
3316-
assert test_result[1][1] == "-0-03"
3317-
assert test_result[1][2] == "+0-07"
3315+
assert test_result[1][0] == "-1" # interval year
3316+
assert test_result[1][1] == "-0-03" # interval year to month
3317+
assert test_result[1][2] == "+7" # interval month
33183318

33193319
assert isinstance(df.schema.fields[0].datatype, YearMonthIntervalType)
33203320
assert df.schema.fields[0].datatype.start_field == 0

0 commit comments

Comments
 (0)