{"record":{"id":"c5082b3a4c9c3b7c","repo":"pentaho/pentaho-kettle","slug":"unable-to-get-queryfields-for-sql","errorCode":null,"errorMessage":"Unable to get queryfields for SQL: ","messagePattern":"Unable to get queryfields for SQL: ","errorType":"exception","errorClass":"KettleStepException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/tableinput/TableInputMeta.java","lineNumber":233,"sourceCode":"    boolean param = false;\n\n    Database db = getDatabase();\n    super.databases = new Database[] { db }; // keep track of it for canceling purposes...\n\n    // First try without connecting to the database... (can be S L O W)\n    String sNewSQL = sql;\n    if ( isVariableReplacementActive() ) {\n      sNewSQL = db.environmentSubstitute( sql );\n      if ( space != null ) {\n        sNewSQL = space.environmentSubstitute( sNewSQL );\n      }\n    }\n\n    RowMetaInterface add = null;\n    try {\n      add = db.getQueryFields( sNewSQL, param );\n    } catch ( KettleDatabaseException dbe ) {\n      throw new KettleStepException( \"Unable to get queryfields for SQL: \" + Const.CR + sNewSQL, dbe );\n    }\n\n    if ( add != null ) {\n      attachOrigin( add, origin );\n      row.addRowMeta( add );\n    } else {\n      try {\n        db.connect();\n\n        RowMetaInterface paramRowMeta = null;\n        Object[] paramData = null;\n\n        StreamInterface infoStream = getStepIOMeta().getInfoStreams().get( 0 );\n        if ( !Utils.isEmpty( infoStream.getStepname() ) ) {\n          param = true;\n          if ( info.length > 0 && info[ 0 ] != null ) {\n            paramRowMeta = info[ 0 ];\n            paramData = RowDataUtil.allocateRowData( paramRowMeta.size() );","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/tableinput/TableInputMeta.java#L215-L251","documentation":"In TableInputMeta.getFields() (impact-analysis path), the library previews the step's SQL via db.getQueryFields(sNewSQL, param) to derive the output row fields. When the database engine/driver rejects the SQL (KettleDatabaseException), it is wrapped in KettleStepException with message 'Unable to get queryfields for SQL: ' plus the SQL text.","triggerScenarios":"analyseImpact -> getFields executes the SQL through Database.getQueryFields and the query fails: SQL syntax error, invalid variables (SQL with ${...} not yet substituted), connection failure, or schema/table referenced by the SQL does not exist.","commonSituations":"SQL containing unresolved Kettle variables or ?-parameters that getQueryFields cannot bind; typo in table/column names; database unreachable during impact analysis; dialect-specific SQL the driver's preview cannot parse.","solutions":["Validate the step's SQL directly (Preview / SQL editor) — the exception message contains the full failing SQL.","Resolve Kettle variables before analysis or set default values for ${var} placeholders.","Test the database connection used by the step.","If the SQL uses parameters (?), ensure they are supported in this path or restructure the query so getQueryFields can infer columns.","Check the wrapped KettleDatabaseException cause for the driver's specific error."],"exampleFix":"// before: unresolved variable breaks query-field inference\nmeta.setSQL(\"SELECT * FROM ${TABLE_NAME}\");\n// after: ensure the variable is defined before analyseImpact runs\ntransMeta.setVariable(\"TABLE_NAME\", \"customers\");\nmeta.setSQL(\"SELECT * FROM ${TABLE_NAME}\");","handlingStrategy":"try-catch","validationCode":"// Java: check connection and resolve variables before getFields/analyseImpact\nif (!databaseMeta.testConnectionSuccess()) throw new IllegalStateException(\"DB connection fails\");\nString resolved = transMeta.environmentSubstitute(meta.getSQL());\nif (resolved.contains(\"${\")) throw new IllegalStateException(\"Unresolved variables in SQL: \" + resolved);","typeGuard":null,"tryCatchPattern":"try {\n  meta.getFields(row, origin, info, target, databaseMeta, metaStore);\n} catch (KettleStepException e) {\n  log.error(\"Query field inference failed for SQL: \" + e.getMessage() + \", cause: \" + KettleUtil.getMessage(e));\n}","preventionTips":["Resolve all ${variables} or give them defaults before impact analysis.","Verify SQL syntax in the step's SQL editor / Preview before analysis.","Test the database connection used by the step.","Avoid dialect-specific constructs when you need automatic field inference."],"tags":["kettle","sql","database","step-fields"],"backgroundTag":"sql-query-failed","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"}