Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,38 @@ public int hashCode() {

@Override
public String toString() {
StringBuilder sb = new StringBuilder("interval");
if (months == 0 && days == 0 && microseconds == 0) {
return "0 seconds";
}

StringBuilder sb = new StringBuilder();

if (months != 0) {
appendUnit(sb, months / 12, "year");
appendUnit(sb, months % 12, "month");
appendUnit(sb, months / 12, "years");
appendUnit(sb, months % 12, "months");
}

appendUnit(sb, days, "day");
appendUnit(sb, days, "days");

if (microseconds != 0) {
long rest = microseconds;
appendUnit(sb, rest / MICROS_PER_HOUR, "hour");
appendUnit(sb, rest / MICROS_PER_HOUR, "hours");
rest %= MICROS_PER_HOUR;
appendUnit(sb, rest / MICROS_PER_MINUTE, "minute");
appendUnit(sb, rest / MICROS_PER_MINUTE, "minutes");
rest %= MICROS_PER_MINUTE;
if (rest != 0) {
String s = BigDecimal.valueOf(rest, 6).stripTrailingZeros().toPlainString();
sb.append(' ').append(s).append(" seconds");
sb.append(s).append(" seconds ");
}
} else if (months == 0 && days == 0) {
sb.append(" 0 microseconds");
}

sb.setLength(sb.length() - 1);
return sb.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important but you could leave the code as is, and just do trim() on the result.

}

private void appendUnit(StringBuilder sb, long value, String unit) {
if (value != 0) {
sb.append(' ').append(value).append(' ').append(unit).append('s');
sb.append(value).append(' ').append(unit).append(' ');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ public void toStringTest() {
CalendarInterval i;

i = new CalendarInterval(0, 0, 0);
assertEquals("interval 0 microseconds", i.toString());
assertEquals("0 seconds", i.toString());

i = new CalendarInterval(34, 0, 0);
assertEquals("interval 2 years 10 months", i.toString());
assertEquals("2 years 10 months", i.toString());

i = new CalendarInterval(-34, 0, 0);
assertEquals("interval -2 years -10 months", i.toString());
assertEquals("-2 years -10 months", i.toString());

i = new CalendarInterval(0, 31, 0);
assertEquals("interval 31 days", i.toString());
assertEquals("31 days", i.toString());

i = new CalendarInterval(0, -31, 0);
assertEquals("interval -31 days", i.toString());
assertEquals("-31 days", i.toString());

i = new CalendarInterval(0, 0, 3 * MICROS_PER_HOUR + 13 * MICROS_PER_MINUTE + 123);
assertEquals("interval 3 hours 13 minutes 0.000123 seconds", i.toString());
assertEquals("3 hours 13 minutes 0.000123 seconds", i.toString());

i = new CalendarInterval(0, 0, -3 * MICROS_PER_HOUR - 13 * MICROS_PER_MINUTE - 123);
assertEquals("interval -3 hours -13 minutes -0.000123 seconds", i.toString());
assertEquals("-3 hours -13 minutes -0.000123 seconds", i.toString());

i = new CalendarInterval(34, 31, 3 * MICROS_PER_HOUR + 13 * MICROS_PER_MINUTE + 123);
assertEquals("interval 2 years 10 months 31 days 3 hours 13 minutes 0.000123 seconds",
assertEquals("2 years 10 months 31 days 3 hours 13 minutes 0.000123 seconds",
i.toString());
}

Expand Down
2 changes: 2 additions & 0 deletions docs/sql-migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ license: |

- Since Spark 3.0, the interval literal syntax does not allow multiple from-to units anymore. For example, `SELECT INTERVAL '1-1' YEAR TO MONTH '2-2' YEAR TO MONTH'` throws parser exception.

- Since Spark 3.0, when casting interval values to string type, there is no "interval" prefix, e.g. `1 days 2 hours`. In Spark version 2.4 and earlier, the string contains the "interval" prefix like `interval 1 days 2 hours`.

## Upgrading from Spark SQL 2.4 to 2.4.1

- The value of `spark.executor.heartbeatInterval`, when specified without units like "30" rather than "30s", was
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.apache.spark.sql.types._
> SELECT _FUNC_(col) FROM VALUES (NULL), (NULL) AS tab(col);
NULL
> SELECT _FUNC_(cast(col as interval)) FROM VALUES ('1 seconds'), ('2 seconds'), (null) tab(col);
interval 3 seconds
3 seconds
""",
since = "1.0.0")
// scalastyle:on line.size.limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Cast(Literal.create(
new CalendarInterval(15, 9, -3 * CalendarInterval.MICROS_PER_HOUR), CalendarIntervalType),
StringType),
"interval 1 years 3 months 9 days -3 hours")
"1 years 3 months 9 days -3 hours")
checkEvaluation(Cast(Literal("INTERVAL 1 Second 1 microsecond"), CalendarIntervalType),
new CalendarInterval(0, 0, 1000001))
checkEvaluation(Cast(Literal("1 MONTH 1 Microsecond"), CalendarIntervalType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ class ExpressionSQLBuilderSuite extends SparkFunSuite {

checkSQL(
TimeAdd('a, interval),
"`a` + interval 1 hours"
"`a` + 1 hours"
)

checkSQL(
TimeSub('a, interval),
"`a` - interval 1 hours"
"`a` - 1 hours"
)
}
}
Loading