{"record":{"id":"72ce3a488158342a","repo":"pentaho/pentaho-kettle","slug":"error-getting-the-first-long-value-from-the-max-value","errorCode":null,"errorMessage":"Error getting the first long value from the max value returned from table : \" + schemaTable","messagePattern":"Error getting the first long value from the max value returned from table : \" \\+ schemaTable","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":3974,"sourceCode":"    }\n\n    if ( counter == null ) {\n      RowMetaAndData rmad =\n        getOneRow( \"SELECT MAX(\" + databaseMeta.quoteField( valKey ) + \") FROM \" + schemaTable );\n      if ( rmad != null ) {\n        long previous;\n        try {\n          Long tmp = rmad.getRowMeta().getInteger( rmad.getData(), 0 );\n\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","sourceCodeStart":3956,"sourceCodeEnd":3992,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L3956-L3992","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the key column is a numeric type (INTEGER/BIGINT), not text","Handle empty tables: seed the counter manually or ensure the table has at least one row","Check the value metadata Kettle derives — force the correct type in the SQL (e.g. CAST(MAX(id) AS BIGINT))","Inspect the row returned by the MAX query with debug logging"],"exampleFix":"// before\nString sql = \"SELECT MAX(\" + keyfield + \") FROM \" + schemaTable;\n// after\nString sql = \"SELECT CAST(MAX(\" + keyfield + \") AS BIGINT) FROM \" + schemaTable;","handlingStrategy":"try-catch","validationCode":"// ensure the key column is numeric\nRowMetaAndData chk = db.getOneRow(\"SELECT MAX(\" + keyfield + \") FROM \" + schemaTable);\nif (chk != null && chk.getRowMeta().getFieldMeta(0).getType() != ValueMetaInterface.TYPE_INTEGER) {\n  throw new IllegalStateException(\"Key column must be numeric: \" + keyfield);\n}","typeGuard":"boolean isNumericMax(RowMetaAndData r) {\n  try { return r != null && r.getNumber(0, 0) >= 0; } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n  Long next = db.getNextValue(counters, schemaTable, keyField);\n} catch (KettleDatabaseException e) {\n  // seed counter manually\n  counters.put(schemaTable + \".\" + keyField, new Counter(1, 1));\n  next = db.getNextValue(counters, schemaTable, keyField);\n}","preventionTips":["Define key columns as INTEGER/BIGINT, never VARCHAR","Prefer DB sequences or auto-increment columns over Kettle counters","CAST MAX() in custom SQL to guarantee numeric metadata","Handle the empty-table case explicitly in your schema bootstrap"],"tags":["jdbc","sequence","type-mismatch"],"backgroundTag":"type-mismatch","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"}