{"record":{"id":"f4e595e0606e4bb8","repo":"prestodb/presto","slug":"mismatched-set-column-types","errorCode":"MISMATCHED_SET_COLUMN_TYPES","errorMessage":"Mismatch at column %d","messagePattern":"Mismatch at column (.+?)","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java","lineNumber":634,"sourceCode":"            if (expectedColumns.size() != queryColumnTypes.size()) {\n                errorMessage = format(\"Insert query has %d expression(s) but expected %d target column(s). \",\n                        queryColumnTypes.size(), expectedColumns.size());\n            }\n\n            for (int i = 0; i < Math.max(expectedColumns.size(), queryColumnTypes.size()); i++) {\n                Node node = insert;\n                QueryBody queryBody = insert.getQuery().getQueryBody();\n                if (queryBody instanceof Values) {\n                    List<Expression> rows = ((Values) queryBody).getRows();\n                    checkState(!rows.isEmpty(), \"Missing column values\");\n                    node = rows.get(0);\n                    if (node instanceof Row) {\n                        int columnIndex = Math.min(i, queryColumnTypes.size() - 1);\n                        node = ((Row) rows.get(0)).getFields().get(columnIndex).getExpression();\n                    }\n                }\n                if (i == expectedColumns.size()) {\n                    throw new SemanticException(MISMATCHED_SET_COLUMN_TYPES,\n                            node,\n                            errorMessage + \"Mismatch at column %d\",\n                            i + 1);\n                }\n                if (i == queryColumnTypes.size()) {\n                    throw new SemanticException(MISMATCHED_SET_COLUMN_TYPES,\n                            node,\n                            errorMessage + \"Mismatch at column %d: '%s'\",\n                            i + 1,\n                            expectedColumns.get(i).getName());\n                }\n                if (!functionAndTypeResolver.canCoerce(\n                        queryColumnTypes.get(i),\n                        expectedColumns.get(i).getType())) {\n                    if (queryColumnTypes.get(i) instanceof RowType && expectedColumns.get(i).getType() instanceof RowType) {\n                        String fieldName = expectedColumns.get(i).getName();\n                        List<Type> columnRowTypes = queryColumnTypes.get(i).getTypeParameters();\n                        List<RowType.Field> expectedRowFields = ((RowType) expectedColumns.get(i).getType()).getFields();","sourceCodeStart":616,"sourceCodeEnd":652,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java#L616-L652","documentation":"checkTypesMatchForInsert compares the number and types of the query output columns against the target table columns for an INSERT. If the query returns fewer columns than the target table (the loop index i reached expectedColumns.size() while iterating), it throws MISMATCHED_SET_COLUMN_TYPES with 'Mismatch at column %d' (1-based index i+1).","triggerScenarios":"INSERT INTO t (col list or implicit full list) SELECT/VALUES providing fewer expressions than the number of target columns — e.g. INSERT INTO t (a,b,c) VALUES (1,2) or SELECT of two columns into a three-column table.","commonSituations":"Target table gained a column (schema evolution) so bare INSERT ... SELECT no longer lines up; writing VALUES rows with too few entries; SELECT list shortened after edits; forgetting that an explicit column list must still be matched by an equal count of query columns.","solutions":["Make the SELECT/VALUES produce exactly as many expressions as there are target columns (or entries in the explicit column list).","Add explicit DEFAULT literals or NULLs for missing columns in the SELECT/VALUES rows.","Use an explicit column list covering only the columns you actually supply, ensuring its size equals the query's column count.","Check the target table schema with DESCRIBE after recent DDL changes and update the statement."],"exampleFix":"// before\nINSERT INTO orders (orderkey, custkey, orderdate) VALUES (1, 2);\n// after\nINSERT INTO orders (orderkey, custkey, orderdate) VALUES (1, 2, DATE '2026-01-01');","handlingStrategy":"validation","validationCode":"-- Compare counts before inserting:\nSELECT count(*) FROM (SELECT ... ) q; -- must equal target column count\n-- Or check explicitly:\nSELECT count(*) FROM information_schema.columns WHERE table_schema='s' AND table_name='target_table';","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid SELECT * in INSERT ... SELECT; name columns explicitly.","Always write an explicit INSERT column list so arity mismatches fail at design time, not runtime.","Provide explicit DEFAULT/NULL placeholders for columns you do not supply.","Re-verify arity after any DDL change to the target table."],"tags":["sql","insert","type-mismatch","arity"],"backgroundTag":"column-count-mismatch","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}