{"record":{"id":"7833baf4954f6851","repo":"pentaho/pentaho-kettle","slug":"couldn-t-find-maximum-key-value-from-table","errorCode":null,"errorMessage":"Couldn't find maximum key value from table ","messagePattern":"Couldn't find maximum key value from table ","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":3983,"sourceCode":"\n          // A \"select max(x)\" on a table with no matching rows will return\n          // null.\n          if ( tmp != null ) {\n            previous = tmp.longValue();\n          } else {\n            previous = 0L;\n          }\n        } catch ( KettleValueException e ) {\n          throw new KettleDatabaseException(\n            \"Error getting the first long value from the max value returned from table : \" + schemaTable );\n        }\n        counter = new Counter( previous + 1, 1 );\n        nextValue = Long.valueOf( counter.next() );\n        if ( counters != null ) {\n          counters.put( lookup, counter );\n        }\n      } else {\n        throw new KettleDatabaseException( \"Couldn't find maximum key value from table \" + schemaTable );\n      }\n    } else {\n      nextValue = Long.valueOf( counter.next() );\n    }\n\n    return nextValue;\n  }\n\n  @Override\n  public String toString() {\n    if ( databaseMeta != null ) {\n      return databaseMeta.getName();\n    } else {\n      return \"-\";\n    }\n  }\n\n  public boolean isSystemTable( String tableName ) {","sourceCodeStart":3965,"sourceCodeEnd":4001,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L3965-L4001","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Insert an initial row (or seed the key column) so MAX() returns a value","Verify schemaTable and keyfield resolve to the correct existing table/column","Prefer database-native sequences/auto-increment instead of Kettle's counter table lookup","Catch KettleDatabaseException and initialize the counter to 1 for known-empty tables"],"exampleFix":"// before\nLong id = db.getNextValue(counters, \"PUBLIC.MYTABLE\", \"ID\"); // empty table\n// after\n// seed the table first:\ndb.execStatement(\"INSERT INTO PUBLIC.MYTABLE (ID) VALUES (0)\");\nLong id = db.getNextValue(counters, \"PUBLIC.MYTABLE\", \"ID\");","handlingStrategy":"fallback","validationCode":"if (!db.checkTableExistsUnquoted(schema, tableName) || db.getCount(schemaTable) == 0) {\n  // seed table before calling getNextValue\n}","typeGuard":"null","tryCatchPattern":"Long next;\ntry {\n  next = db.getNextValue(counters, schemaTable, keyField);\n} catch (KettleDatabaseException e) {\n  next = 1L; // fallback: first key on empty table\n  counters.put(schemaTable + \".\" + keyField, new Counter(1, 1));\n}","preventionTips":["Seed tables with an initial row so MAX() returns a value","Use native sequences/auto-increment where the DB supports it","Catch KettleDatabaseException around getNextValue for empty-table scenarios","Verify schemaTable name with tableExists before generating keys"],"tags":["jdbc","sequence","empty-result"],"backgroundTag":"empty-result-set","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}