apache/dolphinscheduler · error · RuntimeException

expression not valid

Error message

expression not valid

What it means

calculateYearWeek wraps all failures while parsing the $[week(...)] expression — bad date format segment, non-numeric second parameter, or errors in transformYearWeek — in a bare RuntimeException("expression not valid") with no cause. In formatTimeExpression this RuntimeException is then caught and rethrown as 'Unsupported placeholder expression' (or swallowed when ignoreInvalidExpression=true), but if calculateYearWeek is reached through the try-block it surfaces as this generic message.

Source

Thrown at dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java:321

    private static String calculateYearWeek(final String expression, final Date date) {

        String dataFormat = expression.substring(YEAR_WEEK.length() + 1, expression.length() - 1);

        String targetDate = "";
        try {

            if (dataFormat.contains(COMMA)) {
                String param1 = dataFormat.split(COMMA)[0];
                String param2 = dataFormat.split(COMMA)[1];
                dataFormat = param1;

                targetDate = transformYearWeek(date, dataFormat, calculate(param2));

            } else {
                targetDate = transformYearWeek(date, dataFormat, 1);
            }
        } catch (Exception e) {
            throw new RuntimeException("expression not valid");
        }

        return targetDate;
    }

    /**
     * transform week of year
     * @param date date
     * @param format date_format,for example: yyyy-MM-dd / yyyyMMdd
     * @param weekDay day of week
     * @return date_string
     */
    private static String transformYearWeek(Date date, String format, int weekDay) {
        Calendar calendar = Calendar.getInstance();
        // Minimum number of days required for the first week of the year
        calendar.setMinimalDaysInFirstWeek(4);

        // By default ,Set Monday as the first day of the week

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Use the exact form $[week(fmt)] or $[week(fmt,n)] with no spaces and a numeric n, e.g. $[week(yyyyMMdd,1)]
  2. Wrap the cause: this RuntimeException discards the original exception, so reproduce locally with the same input to see the real failure inside calculate()/transformYearWeek
  3. Set ignoreInvalidExpression=true in formatTimeExpression so an invalid week expression returns the input unchanged rather than failing the task
  4. Check the substring bounds — the expression must end with ')' or substring will throw StringIndexOutOfBounds before parsing

Example fix

// before
$[week(yyyy-MM-dd, x2)]
// calculate("x2") throws -> RuntimeException("expression not valid")
// after
$[week(yyyy-MM-dd, 2)]
Defensive patterns

Strategy: validation

Validate before calling

// Validate $[week(...)] form before calling formatTimeExpression
private static final Pattern WEEK_EXPR = Pattern.compile("\\$\\[week\\([A-Za-z\-]+(,[0-9]+)?\\)\\]");
boolean isValidWeekExpression(String e) {
    return e != null && WEEK_EXPR.matcher(e).matches();
}

Try / catch

try {
    return TimePlaceholderUtils.formatTimeExpression(expr, date, false);
} catch (RuntimeException e) {
    if ("expression not valid".equals(e.getMessage())) {
        throw new IllegalArgumentException("Invalid week expression: " + expr, e);
    }
    throw e;
}

Prevention

When it happens

Trigger: An expression starting with the YEAR_WEEK prefix ($[week(...)]) whose content after the prefix has no enclosing parentheses bounds (substring fails), a format part that is not a valid pattern, or a second comma-separated parameter that calculate() cannot evaluate to an int (e.g. $[week(yyyyMMdd,N+abc)]).

Common situations: Users write $[week(yyyy-MM-dd, 2)] with a space or extra token that breaks the comma split, or use an unsupported week-day number, or omit the format so substring produces a malformed body.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06). Data as JSON: /api/errors/f175790494780043. Report an issue: GitHub.