pentaho/pentaho-kettle · error · KettleDatabaseException

Error getting the first long value from the max value…

Error message

Error getting the first long value from the max value returned from table : " + schemaTable

What it means

In Database.getNextValue(), after reading MAX(key) from the table, converting that value to a long failed with a KettleValueException, wrapped in this KettleDatabaseException. It means the max value returned from the table was not usable as a numeric key.

Solutions

  1. Verify the key column is a numeric type (INTEGER/BIGINT), not text
  2. Handle empty tables: seed the counter manually or ensure the table has at least one row
  3. Check the value metadata Kettle derives — force the correct type in the SQL (e.g. CAST(MAX(id) AS BIGINT))
  4. Inspect the row returned by the MAX query with debug logging

Example fix

// before
String sql = "SELECT MAX(" + keyfield + ") FROM " + schemaTable;
// after
String sql = "SELECT CAST(MAX(" + keyfield + ") AS BIGINT) FROM " + schemaTable;
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the key column is numeric
RowMetaAndData chk = db.getOneRow("SELECT MAX(" + keyfield + ") FROM " + schemaTable);
if (chk != null && chk.getRowMeta().getFieldMeta(0).getType() != ValueMetaInterface.TYPE_INTEGER) {
  throw new IllegalStateException("Key column must be numeric: " + keyfield);
}

Type guard

boolean isNumericMax(RowMetaAndData r) {
  try { return r != null && r.getNumber(0, 0) >= 0; } catch (Exception e) { return false; }
}

Try / catch

try {
  Long next = db.getNextValue(counters, schemaTable, keyField);
} catch (KettleDatabaseException e) {
  // seed counter manually
  counters.put(schemaTable + "." + keyField, new Counter(1, 1));
  next = db.getNextValue(counters, schemaTable, keyField);
}

Prevention

When it happens

Trigger: getNextValue(...) reads a row containing MAX(keyval); calling getNumber/getInteger on that value throws KettleValueException because the value is null or non-numeric, so this exception is thrown with the schema.table in the message.

Common situations: Empty result row whose MAX value is null, a key column defined as VARCHAR containing non-numeric data, or a value meta type mismatch after changing the column type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

    }

    if ( counter == null ) {
      RowMetaAndData rmad =
        getOneRow( "SELECT MAX(" + databaseMeta.quoteField( valKey ) + ") FROM " + schemaTable );
      if ( rmad != null ) {
        long previous;
        try {
          Long tmp = rmad.getRowMeta().getInteger( rmad.getData(), 0 );

          // 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

View on GitHub (pinned to f3058517a1)