pentaho/pentaho-kettle · error · KettleDatabaseException

Couldn't find maximum key value from table

Error message

Couldn't find maximum key value from table 

What it means

getNextValue() throws this when the MAX(key) lookup returned no row at all, so no previous counter value could be established. Without a max value the method cannot compute the next key, so it fails with the schema.table name in the message.

Solutions

  1. Insert an initial row (or seed the key column) so MAX() returns a value
  2. Verify schemaTable and keyfield resolve to the correct existing table/column
  3. Prefer database-native sequences/auto-increment instead of Kettle's counter table lookup
  4. Catch KettleDatabaseException and initialize the counter to 1 for known-empty tables

Example fix

// before
Long id = db.getNextValue(counters, "PUBLIC.MYTABLE", "ID"); // empty table
// after
// seed the table first:
db.execStatement("INSERT INTO PUBLIC.MYTABLE (ID) VALUES (0)");
Long id = db.getNextValue(counters, "PUBLIC.MYTABLE", "ID");
Defensive patterns

Strategy: fallback

Validate before calling

if (!db.checkTableExistsUnquoted(schema, tableName) || db.getCount(schemaTable) == 0) {
  // seed table before calling getNextValue
}

Type guard

null

Try / catch

Long next;
try {
  next = db.getNextValue(counters, schemaTable, keyField);
} catch (KettleDatabaseException e) {
  next = 1L; // fallback: first key on empty table
  counters.put(schemaTable + "." + keyField, new Counter(1, 1));
}

Prevention

When it happens

Trigger: Calling Database.getNextValue(counters, tableName, valKey) on a table whose MAX(key) query returns zero rows (no aggregate row produced) — the else-branch where 'maxValueRow' is null throws.

Common situations: Querying an empty or freshly created table where the driver returns no row for MAX(), a misspelled table/key column, or schemas where MAX() over an empty set yields a null row instead of a null column.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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

Appendix: source

Thrown at core/src/main/java/org/pentaho/di/core/database/Database.java:3983

          // A "select max(x)" on a table with no matching rows will return
          // null.
          if ( tmp != null ) {
            previous = tmp.longValue();
          } else {
            previous = 0L;
          }
        } catch ( KettleValueException e ) {
          throw new KettleDatabaseException(
            "Error getting the first long value from the max value returned from table : " + schemaTable );
        }
        counter = new Counter( previous + 1, 1 );
        nextValue = Long.valueOf( counter.next() );
        if ( counters != null ) {
          counters.put( lookup, counter );
        }
      } else {
        throw new KettleDatabaseException( "Couldn't find maximum key value from table " + schemaTable );
      }
    } else {
      nextValue = Long.valueOf( counter.next() );
    }

    return nextValue;
  }

  @Override
  public String toString() {
    if ( databaseMeta != null ) {
      return databaseMeta.getName();
    } else {
      return "-";
    }
  }

  public boolean isSystemTable( String tableName ) {

View on GitHub (pinned to f3058517a1)