pentaho/pentaho-kettle · error · RuntimeException

The function call truncDate requires 2 arguments: a date…

Error message

The function call truncDate requires 2 arguments: a date and a level (int)

What it means

truncDate(date, level) truncates a Date to the precision given by level (0=ms, 1=s, 2=min, 3=hour, 4=day, 5=month). The library throws this RuntimeException when the function is not called with exactly 2 arguments, since both a date and a level are mandatory to compute the truncated value.

Solutions

  1. Supply exactly 2 arguments: truncDate(myDate, level)
  2. Add the missing level argument (0..5) appropriate to the desired precision
  3. If only the date may be missing/undefined, guard it first: if (d != null) td = truncDate(d, 2)
  4. Use trunc() instead if you intended numeric truncation

Example fix

// before
var td = truncDate(orderDate);
// after
var td = truncDate(orderDate, 4); // truncate to day
Defensive patterns

Strategy: validation

Validate before calling

function safeTruncDate(d, level) {
  if (arguments.length !== 2) throw new Error('truncDate requires date + level');
  if (d == null) return null;
  return truncDate(d, level);
}

Type guard

function hasTwoArgs(fn) { return function(a, b) { if (b === undefined) throw new Error('missing level'); return fn(a, b); }; }

Try / catch

try {
  var td = truncDate(d, 4);
} catch (e) {
  Logger.logError('truncDate misuse: ' + e.message);
  var td = null;
}

Prevention

When it happens

Trigger: Calling truncDate(d) with only the date, truncDate() with none, or truncDate(d, level, extra) with a third argument in JavaScript script steps.

Common situations: Copy-pasting a one-argument TRUNC(date) habit from SQL/Excel where the level defaults; an optional third argument added during refactoring; a variable holding the level being undefined and dropped by argument marshalling.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/075f9b3c82977c9c. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java:2433

        return null;
      } else if ( isUndefined( ArgList[0] ) ) {
        return undefinedValue;
      }

      // This is the truncation of a date...
      // The second argument specifies the level: ms, s, min, hour, day, month, year
      //
      java.util.Date dArg1 = null;
      Integer level = null;
      try {
        dArg1 = (java.util.Date) ArgList[0];
        level = (Integer) ArgList[1];
      } catch ( Exception e ) {
        throw new RuntimeException( e.toString() );
      }
      return ScriptAddedFunctions.truncDate( dArg1, level );
    } else {
      throw new RuntimeException( "The function call truncDate requires 2 arguments: a date and a level (int)" );
    }
  }

  @VisibleForTesting
  static Date truncDate( Date dArg1, Integer level ) {
    Calendar cal = Calendar.getInstance();
    cal.setTime( dArg1 );
    switch ( level.intValue() ) {
      // MONTHS
      case 5:
        cal.set( Calendar.MONTH, 0 );
        // DAYS
      case 4:
        cal.set( Calendar.DAY_OF_MONTH, 1 );
        // HOURS
      case 3:
        cal.set( Calendar.HOUR_OF_DAY, 0 );
        // MINUTES

View on GitHub (pinned to f3058517a1)