Browse Source

Throw IllegalArgumentException if parse time placeholder error (#15514)

(cherry picked from commit 5fa31e036d77698f3732a42664bfac3e7edcb8e4)
3.2.1-prepare
Wenjun Ruan 10 months ago committed by GitHub
parent
commit
5a7deb28f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 26
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java
  2. 1
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtils.java
  3. 46
      dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java

26
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java

@ -338,10 +338,10 @@ public class TimePlaceholderUtils {
*/ */
public static String getPlaceHolderTime(String expression, Date date) { public static String getPlaceHolderTime(String expression, Date date) {
if (StringUtils.isBlank(expression)) { if (StringUtils.isBlank(expression)) {
return null; throw new IllegalArgumentException("expression is null");
} }
if (null == date) { if (null == date) {
return null; throw new IllegalArgumentException("date is null");
} }
return calculateTime(expression, date); return calculateTime(expression, date);
} }
@ -354,8 +354,9 @@ public class TimePlaceholderUtils {
*/ */
private static String calculateTime(String expression, Date date) { private static String calculateTime(String expression, Date date) {
// After N years: $[add_months(yyyyMMdd,12*N)], the first N months: $[add_months(yyyyMMdd,-N)], etc // After N years: $[add_months(yyyyMMdd,12*N)], the first N months: $[add_months(yyyyMMdd,-N)], etc
String value; if (date == null) {
throw new IllegalArgumentException("Cannot parse the expression: " + expression + ", date is null");
}
try { try {
if (expression.startsWith(TIMESTAMP)) { if (expression.startsWith(TIMESTAMP)) {
String timeExpression = expression.substring(TIMESTAMP.length() + 1, expression.length() - 1); String timeExpression = expression.substring(TIMESTAMP.length() + 1, expression.length() - 1);
@ -366,19 +367,16 @@ public class TimePlaceholderUtils {
Date timestamp = DateUtils.parse(dateStr, PARAMETER_FORMAT_TIME); Date timestamp = DateUtils.parse(dateStr, PARAMETER_FORMAT_TIME);
value = String.valueOf(timestamp.getTime() / 1000); return String.valueOf(timestamp.getTime() / 1000);
} else if (expression.startsWith(YEAR_WEEK)) {
value = calculateYearWeek(expression, date);
} else {
Map.Entry<Date, String> entry = calcTimeExpression(expression, date);
value = DateUtils.format(entry.getKey(), entry.getValue());
} }
if (expression.startsWith(YEAR_WEEK)) {
return calculateYearWeek(expression, date);
}
Map.Entry<Date, String> entry = calcTimeExpression(expression, date);
return DateUtils.format(entry.getKey(), entry.getValue());
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); throw new IllegalArgumentException("Unsupported placeholder expression: " + expression, e);
throw e;
} }
return value;
} }
/** /**

1
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtils.java

@ -301,7 +301,6 @@ public class ParameterUtils {
continue; continue;
} }
String value = TimePlaceholderUtils.getPlaceHolderTime(key, date); String value = TimePlaceholderUtils.getPlaceHolderTime(key, date);
assert value != null;
matcher.appendReplacement(newValue, value); matcher.appendReplacement(newValue, value);
} }

46
dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java

@ -17,6 +17,8 @@
package org.apache.dolphinscheduler.plugin.task.api.parser; package org.apache.dolphinscheduler.plugin.task.api.parser;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.placeholder.BusinessTimeUtils; import org.apache.dolphinscheduler.common.utils.placeholder.BusinessTimeUtils;
@ -30,9 +32,9 @@ import org.junit.jupiter.api.Test;
public class TimePlaceholderUtilsTest { public class TimePlaceholderUtilsTest {
Date date = DateUtils.parse("2022-08-26 00:00:00", "yyyy-MM-dd HH:mm:ss"); private Date date = DateUtils.parse("2022-08-26 00:00:00", "yyyy-MM-dd HH:mm:ss");
Map<String, String> timeParams = BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA, date, null); private Map<String, String> timeParams = BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA, date, null);
@Test @Test
public void timePlaceHolderForThisDay() { public void timePlaceHolderForThisDay() {
@ -40,10 +42,10 @@ public class TimePlaceholderUtilsTest {
String thisDate = "$[this_day(yyyyMMdd)]"; String thisDate = "$[this_day(yyyyMMdd)]";
String thisDayTime = ParameterUtils.convertParameterPlaceholders(thisDay, timeParams); String thisDayTime = ParameterUtils.convertParameterPlaceholders(thisDay, timeParams);
Assertions.assertEquals(thisDayTime, "2022-08-26"); assertEquals(thisDayTime, "2022-08-26");
String thisDateTime = ParameterUtils.convertParameterPlaceholders(thisDate, timeParams); String thisDateTime = ParameterUtils.convertParameterPlaceholders(thisDate, timeParams);
Assertions.assertEquals(thisDateTime, "20220826"); assertEquals(thisDateTime, "20220826");
} }
@Test @Test
@ -51,10 +53,10 @@ public class TimePlaceholderUtilsTest {
String lastDay = "$[last_day(yyyy-MM-dd)]"; String lastDay = "$[last_day(yyyy-MM-dd)]";
String lastDate = "$[last_day(yyyyMMdd)]"; String lastDate = "$[last_day(yyyyMMdd)]";
String lastDayTime = ParameterUtils.convertParameterPlaceholders(lastDay, timeParams); String lastDayTime = ParameterUtils.convertParameterPlaceholders(lastDay, timeParams);
Assertions.assertEquals(lastDayTime, "2022-08-25"); assertEquals(lastDayTime, "2022-08-25");
String lastDateTime = ParameterUtils.convertParameterPlaceholders(lastDate, timeParams); String lastDateTime = ParameterUtils.convertParameterPlaceholders(lastDate, timeParams);
Assertions.assertEquals(lastDateTime, "20220825"); assertEquals(lastDateTime, "20220825");
} }
@Test @Test
@ -64,20 +66,20 @@ public class TimePlaceholderUtilsTest {
String yearWeekDay = "$[year_week(yyyyMMdd)]"; String yearWeekDay = "$[year_week(yyyyMMdd)]";
String yearWeekDateTime = ParameterUtils.convertParameterPlaceholders(yearWeekDate, timeParams); String yearWeekDateTime = ParameterUtils.convertParameterPlaceholders(yearWeekDate, timeParams);
Assertions.assertEquals(yearWeekDateTime, "2022-34"); assertEquals(yearWeekDateTime, "2022-34");
String yearWeekDayTime = ParameterUtils.convertParameterPlaceholders(yearWeekDay, timeParams); String yearWeekDayTime = ParameterUtils.convertParameterPlaceholders(yearWeekDay, timeParams);
Assertions.assertEquals(yearWeekDayTime, "202234"); assertEquals(yearWeekDayTime, "202234");
// Start the week on Friday // Start the week on Friday
String yearWeekDateAny = "$[year_week(yyyy-MM-dd,5)]"; String yearWeekDateAny = "$[year_week(yyyy-MM-dd,5)]";
String yearWeekDayAny = "$[year_week(yyyyMMdd,5)]"; String yearWeekDayAny = "$[year_week(yyyyMMdd,5)]";
String yearWeekDateAnyTime = ParameterUtils.convertParameterPlaceholders(yearWeekDateAny, timeParams); String yearWeekDateAnyTime = ParameterUtils.convertParameterPlaceholders(yearWeekDateAny, timeParams);
Assertions.assertEquals(yearWeekDateAnyTime, "2022-35"); assertEquals(yearWeekDateAnyTime, "2022-35");
String yearWeekDayAnyTime = ParameterUtils.convertParameterPlaceholders(yearWeekDayAny, timeParams); String yearWeekDayAnyTime = ParameterUtils.convertParameterPlaceholders(yearWeekDayAny, timeParams);
Assertions.assertEquals(yearWeekDayAnyTime, "202235"); assertEquals(yearWeekDayAnyTime, "202235");
} }
@Test @Test
@ -86,10 +88,10 @@ public class TimePlaceholderUtilsTest {
String monthFirstDay = "$[month_first_day(yyyyMMdd,-1)]"; String monthFirstDay = "$[month_first_day(yyyyMMdd,-1)]";
String monthFirstDateTime = ParameterUtils.convertParameterPlaceholders(monthFirstDate, timeParams); String monthFirstDateTime = ParameterUtils.convertParameterPlaceholders(monthFirstDate, timeParams);
Assertions.assertEquals(monthFirstDateTime, "2022-07-01"); assertEquals(monthFirstDateTime, "2022-07-01");
String monthFirstDayTime = ParameterUtils.convertParameterPlaceholders(monthFirstDay, timeParams); String monthFirstDayTime = ParameterUtils.convertParameterPlaceholders(monthFirstDay, timeParams);
Assertions.assertEquals(monthFirstDayTime, "20220701"); assertEquals(monthFirstDayTime, "20220701");
} }
@Test @Test
@ -98,10 +100,10 @@ public class TimePlaceholderUtilsTest {
String monthLastDay = "$[month_last_day(yyyyMMdd,-1)]"; String monthLastDay = "$[month_last_day(yyyyMMdd,-1)]";
String monthLastDateTime = ParameterUtils.convertParameterPlaceholders(monthLastDate, timeParams); String monthLastDateTime = ParameterUtils.convertParameterPlaceholders(monthLastDate, timeParams);
Assertions.assertEquals(monthLastDateTime, "2022-07-31"); assertEquals(monthLastDateTime, "2022-07-31");
String monthLastDayTime = ParameterUtils.convertParameterPlaceholders(monthLastDay, timeParams); String monthLastDayTime = ParameterUtils.convertParameterPlaceholders(monthLastDay, timeParams);
Assertions.assertEquals(monthLastDayTime, "20220731"); assertEquals(monthLastDayTime, "20220731");
} }
@Test @Test
@ -110,10 +112,10 @@ public class TimePlaceholderUtilsTest {
String weekFirstDay = "$[week_first_day(yyyyMMdd,0)]"; String weekFirstDay = "$[week_first_day(yyyyMMdd,0)]";
String weekFirstDateTime = ParameterUtils.convertParameterPlaceholders(weekFirstDate, timeParams); String weekFirstDateTime = ParameterUtils.convertParameterPlaceholders(weekFirstDate, timeParams);
Assertions.assertEquals(weekFirstDateTime, "2022-08-22"); assertEquals(weekFirstDateTime, "2022-08-22");
String weekFirstDayTime = ParameterUtils.convertParameterPlaceholders(weekFirstDay, timeParams); String weekFirstDayTime = ParameterUtils.convertParameterPlaceholders(weekFirstDay, timeParams);
Assertions.assertEquals(weekFirstDayTime, "20220822"); assertEquals(weekFirstDayTime, "20220822");
} }
@Test @Test
@ -122,9 +124,17 @@ public class TimePlaceholderUtilsTest {
String weekLastDay = "$[week_last_day(yyyyMMdd,0)]"; String weekLastDay = "$[week_last_day(yyyyMMdd,0)]";
String weekLastDateTime = ParameterUtils.convertParameterPlaceholders(weekLastDate, timeParams); String weekLastDateTime = ParameterUtils.convertParameterPlaceholders(weekLastDate, timeParams);
Assertions.assertEquals(weekLastDateTime, "2022-08-28"); assertEquals(weekLastDateTime, "2022-08-28");
String weekLastDayTime = ParameterUtils.convertParameterPlaceholders(weekLastDay, timeParams); String weekLastDayTime = ParameterUtils.convertParameterPlaceholders(weekLastDay, timeParams);
Assertions.assertEquals(weekLastDayTime, "20220828"); assertEquals(weekLastDayTime, "20220828");
}
@Test
void getPlaceHolderTime() {
IllegalArgumentException illegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class,
() -> TimePlaceholderUtils.getPlaceHolderTime("$[week_last_day(yyyy-MM-dd,0) - 1]", new Date()));
assertEquals("Unsupported placeholder expression: $[week_last_day(yyyy-MM-dd,0) - 1]",
illegalArgumentException.getMessage());
} }
} }

Loading…
Cancel
Save