Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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,7 +84,7 @@ public int hashCode() {

@Override
public String toString() {
StringBuilder sb = new StringBuilder("interval");
StringBuilder sb = new StringBuilder();

if (months != 0) {
appendUnit(sb, months / 12, "year");
Expand All @@ -101,18 +101,19 @@ public String toString() {
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.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('s').append(' ');
Copy link
Member

Choose a reason for hiding this comment

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

nit: Less .append() calls:

Suggested change
sb.append(value).append(' ').append(unit).append('s').append(' ');
sb.append(value).append(' ').append(unit).append("s ");

}
}

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 microseconds", 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
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"
)
}
}
70 changes: 35 additions & 35 deletions sql/core/src/test/resources/sql-tests/results/ansi/interval.sql.out

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions sql/core/src/test/resources/sql-tests/results/cast.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ SELECT CAST('interval 3 month 1 hour' AS interval)
-- !query 33 schema
struct<CAST(interval 3 month 1 hour AS INTERVAL):interval>
-- !query 33 output
interval 3 months 1 hours
3 months 1 hours


-- !query 34
SELECT CAST(interval 3 month 1 hour AS string)
-- !query 34 schema
struct<CAST(interval 3 months 1 hours AS STRING):string>
struct<CAST(3 months 1 hours AS STRING):string>
-- !query 34 output
interval 3 months 1 hours
3 months 1 hours
14 changes: 7 additions & 7 deletions sql/core/src/test/resources/sql-tests/results/datetime.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -128,44 +128,44 @@ select date '2001-10-01' - date '2001-09-28'
-- !query 14 schema
struct<subtractdates(DATE '2001-10-01', DATE '2001-09-28'):interval>
-- !query 14 output
interval 3 days
3 days


-- !query 15
select date'2020-01-01' - timestamp'2019-10-06 10:11:12.345678'
-- !query 15 schema
struct<subtracttimestamps(CAST(DATE '2020-01-01' AS TIMESTAMP), TIMESTAMP('2019-10-06 10:11:12.345678')):interval>
-- !query 15 output
interval 2078 hours 48 minutes 47.654322 seconds
2078 hours 48 minutes 47.654322 seconds


-- !query 16
select timestamp'2019-10-06 10:11:12.345678' - date'2020-01-01'
-- !query 16 schema
struct<subtracttimestamps(TIMESTAMP('2019-10-06 10:11:12.345678'), CAST(DATE '2020-01-01' AS TIMESTAMP)):interval>
-- !query 16 output
interval -2078 hours -48 minutes -47.654322 seconds
-2078 hours -48 minutes -47.654322 seconds


-- !query 17
select 3 * (timestamp'2019-10-15 10:11:12.001002' - date'2019-10-15')
-- !query 17 schema
struct<multiply_interval(subtracttimestamps(TIMESTAMP('2019-10-15 10:11:12.001002'), CAST(DATE '2019-10-15' AS TIMESTAMP)), CAST(3 AS DOUBLE)):interval>
-- !query 17 output
interval 30 hours 33 minutes 36.003006 seconds
30 hours 33 minutes 36.003006 seconds


-- !query 18
select interval 4 month 2 weeks 3 microseconds * 1.5
-- !query 18 schema
struct<multiply_interval(interval 4 months 14 days 0.000003 seconds, CAST(1.5 AS DOUBLE)):interval>
struct<multiply_interval(4 months 14 days 0.000003 seconds, CAST(1.5 AS DOUBLE)):interval>
-- !query 18 output
interval 6 months 21 days 0.000005 seconds
6 months 21 days 0.000005 seconds


-- !query 19
select (timestamp'2019-10-15' - timestamp'2019-10-14') / 1.5
-- !query 19 schema
struct<divide_interval(subtracttimestamps(TIMESTAMP('2019-10-15 00:00:00'), TIMESTAMP('2019-10-14 00:00:00')), CAST(1.5 AS DOUBLE)):interval>
-- !query 19 output
interval 16 hours
16 hours
18 changes: 9 additions & 9 deletions sql/core/src/test/resources/sql-tests/results/group-by.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -596,31 +596,31 @@ select sum(cast(v as interval)) from VALUES ('1 seconds'), ('2 seconds'), (null)
-- !query 58 schema
struct<sum(CAST(v AS INTERVAL)):interval>
-- !query 58 output
interval 3 seconds
3 seconds


-- !query 59
select sum(cast(v as interval)) from VALUES ('-1 seconds'), ('2 seconds'), (null) t(v)
-- !query 59 schema
struct<sum(CAST(v AS INTERVAL)):interval>
-- !query 59 output
interval 1 seconds
1 seconds


-- !query 60
select sum(cast(v as interval)) from VALUES ('-1 seconds'), ('-2 seconds'), (null) t(v)
-- !query 60 schema
struct<sum(CAST(v AS INTERVAL)):interval>
-- !query 60 output
interval -3 seconds
-3 seconds


-- !query 61
select sum(cast(v as interval)) from VALUES ('-1 weeks'), ('2 seconds'), (null) t(v)
-- !query 61 schema
struct<sum(CAST(v AS INTERVAL)):interval>
-- !query 61 output
interval -7 days 2 seconds
-7 days 2 seconds


-- !query 62
Expand All @@ -632,8 +632,8 @@ group by i
-- !query 62 schema
struct<i:int,sum(CAST(v AS INTERVAL)):interval>
-- !query 62 output
1 interval -2 days
2 interval 2 seconds
1 -2 days
2 2 seconds
3 NULL


Expand All @@ -645,7 +645,7 @@ having sv is not null
-- !query 63 schema
struct<sv:interval>
-- !query 63 output
interval -2 days 2 seconds
-2 days 2 seconds


-- !query 64
Expand All @@ -656,7 +656,7 @@ FROM VALUES(1,'1 seconds'),(1,'2 seconds'),(2,NULL),(2,NULL) t(i,v)
-- !query 64 schema
struct<i:int,sum(CAST(v AS INTERVAL)) OVER (ORDER BY i ASC NULLS FIRST ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING):interval>
-- !query 64 output
1 interval 2 seconds
1 interval 3 seconds
1 2 seconds
1 3 seconds
2 NULL
2 NULL
Loading