pentaho/pentaho-kettle · error · RuntimeException

Argument of TRUNC of date has to be between 0 and 5

Error message

Argument of TRUNC of date has to be between 0 and 5

What it means

The static truncDate(Date, Integer) helper uses an intentional fall-through switch over the level; any level outside 0-5 hits the default branch and throws this RuntimeException. The level selects the truncation precision (0=millisecond, 1=second, 2=minute, 3=hour, 4=day, 5=month), and there is no year option despite what one might assume.

Solutions

  1. Use a level between 0 and 5 only (5=month is the coarsest supported)
  2. Clamp or validate the level: level = Math.max(0, Math.min(5, level))
  3. Map precision names explicitly: {0:'ms',1:'s',2:'min',3:'hour',4:'day',5:'month'}
  4. For year truncation, build it manually with new Date(y, 0, 1) logic

Example fix

// before
var td = truncDate(d, 6); // intended year
// after
if (level < 0 || level > 5) level = 5;
var td = truncDate(d, level); // 5 = month is the coarsest supported
Defensive patterns

Strategy: validation

Validate before calling

function safeLevel(level) {
  var L = parseInt(level, 10);
  if (isNaN(L) || L < 0 || L > 5) throw new Error('TRUNC date level must be 0-5');
  return L;
}
var td = truncDate(d, safeLevel(level));

Type guard

function isValidTruncLevel(v) { return Number.isInteger(v) && v >= 0 && v <= 5; }

Try / catch

try {
  var td = truncDate(d, level);
} catch (e) {
  if (String(e.message).indexOf('between 0 and 5') >= 0) {
    var td = truncDate(d, 0); // or correct the level
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling truncDate(d, 6), truncDate(d, -1), or any non-0..5 integer level from JavaScript script code.

Common situations: Assuming 6 means 'year' truncation (it is not supported); computing the level dynamically and producing an out-of-range value; confusing this API with ones where level counts up to year; off-by-one when mapping names like 'month' to 6 instead of 5.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

        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
      case 2:
        cal.set( Calendar.MINUTE, 0 );
        // SECONDS
      case 1:
        cal.set( Calendar.SECOND, 0 );
        // MILI-SECONDS
      case 0:
        cal.set( Calendar.MILLISECOND, 0 );
        break;
      default:
        throw new RuntimeException( "Argument of TRUNC of date has to be between 0 and 5" );
    }
    return cal.getTime();
  }


  public static void moveFile( Bowl bowl, ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {

    try {
      if ( ArgList.length == 3
        && !isNull( ArgList[0] ) && !isNull( ArgList[1] ) && !isUndefined( ArgList[0] )
        && !isUndefined( ArgList[1] ) ) {
        FileObject fileSource = null, fileDestination = null;

        try {
          // Source file to move
          fileSource = KettleVFS.getInstance( bowl ).getFileObject( (String) ArgList[0] );
          // Destination filename

View on GitHub (pinned to f3058517a1)