{"record":{"id":"337a0d7b6acf2118","repo":"apache/dolphinscheduler","slug":"sql-column-name-conflict-duplicate-column-name","errorCode":null,"errorMessage":"SQL column name conflict: duplicate column name '\" + label + \"'. Please use aliases to ensure unique column names.","messagePattern":"SQL column name conflict: duplicate column name '\" \\+ label \\+ \"'\\. Please use aliases to ensure unique column names\\.","errorType":"exception","errorClass":"TaskException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java","lineNumber":267,"sourceCode":"     * result process\n     *\n     * @param resultSet resultSet\n     * @throws Exception Exception\n     */\n    private String resultProcess(ResultSet resultSet) throws Exception {\n        ArrayNode resultJSONArray = JSONUtils.createArrayNode();\n        if (resultSet != null) {\n            ResultSetMetaData md = resultSet.getMetaData();\n            int num = md.getColumnCount();\n            String[] columnLabels = new String[num];\n\n            // Check for duplicates in column definitions (across all columns)\n            Set<String> uniqueLabels = new HashSet<>(num);\n            for (int i = 1; i <= num; i++) {\n                String label = md.getColumnLabel(i);\n                columnLabels[i - 1] = label;\n                if (!uniqueLabels.add(label)) {\n                    throw new TaskException(\"SQL column name conflict: duplicate column name '\" + label\n                            + \"'. Please use aliases to ensure unique column names.\");\n                }\n            }\n\n            while (resultSet.next()) {\n                ObjectNode mapOfColValues = JSONUtils.createObjectNode();\n                for (int i = 1; i <= num; i++) {\n                    mapOfColValues.set(columnLabels[i - 1], JSONUtils.toJsonNode(resultSet.getObject(i)));\n                }\n                resultJSONArray.add(mapOfColValues);\n            }\n\n            int displayRows = sqlParameters.getDisplayRows() > 0 ? sqlParameters.getDisplayRows()\n                    : TaskConstants.DEFAULT_DISPLAY_ROWS;\n            displayRows = Math.min(displayRows, resultJSONArray.size());\n            log.info(\"display sql result {} rows as follows:\", displayRows);\n            for (int i = 0; i < displayRows; i++) {\n                String row = JSONUtils.toJsonString(resultJSONArray.get(i));","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java#L249-L285","documentation":"When processing a SELECT result set, SqlTask builds a JSON object per row keyed by column label. If two or more columns share the same label, keys would overwrite each other, so the task fails fast with this error asking for unique column aliases.","triggerScenarios":"Executing a SQL query whose result set contains duplicate column labels, e.g. `SELECT a.id, b.id FROM a JOIN b` or `SELECT *` from a join of tables with same-named columns, without aliases.","commonSituations":"Join queries selecting unaliased id/name columns from multiple tables; `SELECT *` over joined tables; stored procedures or views emitting duplicate labels; UNION queries with identical column names.","solutions":["Add unique aliases to the duplicated columns: `SELECT a.id AS a_id, b.id AS b_id ...`","Restrict the select list to only needed columns instead of `SELECT *`","Rename columns in the underlying view/table if duplicates come from a shared view"],"exampleFix":"// before\nString sql = \"SELECT a.id, b.id FROM t_a a JOIN t_b b ON a.k = b.k\";\n// after\nString sql = \"SELECT a.id AS a_id, b.id AS b_id FROM t_a a JOIN t_b b ON a.k = b.k\";","handlingStrategy":"validation","validationCode":"String validateUniqueLabels(String sql) {\n    // run 'SELECT ... LIMIT 0' via the same connection and check md.getColumnLabel(i) uniqueness\n    // or statically: ensure every selected column in a JOIN has an alias\n    return sql.matches(\"(?i).*select\\s+\\*.*from.*join.*\") ? \"expand SELECT * and alias duplicates\" : null;\n}","typeGuard":null,"tryCatchPattern":"try {\n    sqlTask.execute();\n} catch (TaskException e) {\n    if (e.getMessage().contains(\"SQL column name conflict\")) {\n        // fix SQL: add aliases, then retry\n    }\n}","preventionTips":["Always alias every column in JOIN queries","Avoid SELECT * in production SQL task definitions","Test queries with a LIMIT 1 preview before scheduling"],"tags":["sql","jdbc","duplicate-columns","result-processing"],"backgroundTag":"schema-validation-failed","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}