{"record":{"id":"41a01ff6be0ea3b6","repo":"prestodb/presto","slug":"column-type-unknown","errorCode":"COLUMN_TYPE_UNKNOWN","errorMessage":"Column type is unknown at position %s","messagePattern":"Column type is unknown at position (.+?)","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java","lineNumber":843,"sourceCode":"            node.getColumnAliases().ifPresent(analysis::setCreateTableColumnAliases);\n            analysis.setCreateTableComment(node.getComment());\n\n            analysis.addAccessControlCheckForTable(TABLE_CREATE, new AccessControlInfoForTable(accessControl, session.getIdentity(), session.getTransactionId(), session.getAccessControlContext(), targetTable));\n\n            analysis.setCreateTableAsSelectWithData(node.isWithData());\n\n            // analyze the query that creates the table\n            Scope queryScope = process(node.getQuery(), scope);\n\n            ImmutableList.Builder<OutputColumnMetadata> outputColumns = ImmutableList.builder();\n\n            if (node.getColumnAliases().isPresent()) {\n                validateColumnAliases(node.getColumnAliases().get(), queryScope.getRelationType().getVisibleFieldCount());\n                int aliasPosition = 0;\n                // analyze only column types in subquery if column alias exists\n                for (Field field : queryScope.getRelationType().getVisibleFields()) {\n                    if (field.getType().equals(UNKNOWN)) {\n                        throw new SemanticException(COLUMN_TYPE_UNKNOWN, node, \"Column type is unknown at position %s\", queryScope.getRelationType().indexOf(field) + 1);\n                    }\n                    String columnName = node.getColumnAliases().get().get(aliasPosition).getValue();\n                    outputColumns.add(OutputColumnMetadata.fromColumnLineage(columnName, field.getType().toString(), analysis.getColumnLineageForField(field)));\n                    aliasPosition++;\n                }\n            }\n            else {\n                validateColumns(node, queryScope.getRelationType());\n                queryScope.getRelationType().getVisibleFields().stream()\n                        .map(this::createOutputColumn)\n                        .forEach(outputColumns::add);\n            }\n            analysis.setUpdatedSourceColumns(Optional.of(outputColumns.build()));\n            return createAndAssignScope(node, scope, Field.newUnqualified(node.getLocation(), \"rows\", BIGINT));\n        }\n\n        private OutputColumnMetadata createOutputColumn(Field field)\n        {","sourceCodeStart":825,"sourceCodeEnd":861,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java#L825-L861","documentation":"CTAS with explicit column aliases requires every output column of the query to have a known (concrete) type. Presto's UNKNOWN type (e.g. from a NULL literal with no cast) cannot be used to create a table column, so visitCreateTableAsSelect throws COLUMN_TYPE_UNKNOWN at the 1-based position of the offending field.","triggerScenarios":"CREATE TABLE ... AS SELECT with column aliases where one output column is a bare NULL (or otherwise unknown-typed expression), e.g. SELECT CAST(NULL AS VARCHAR) is fine but SELECT NULL is not.","commonSituations":"Hand-written queries with untyped NULL placeholder columns; generated SQL from ORMs or BI tools emitting NULL for missing values; migrating queries that relied on another engine's type inference of NULL.","solutions":["CAST the NULL to an explicit type, e.g. CAST(NULL AS VARCHAR)","Replace the bare NULL with a typed expression or a typed placeholder","Set the column alias's type via the query expression rather than relying on inference"],"exampleFix":"-- before\nCREATE TABLE t (id, note) AS SELECT 1, NULL;\n-- after\nCREATE TABLE t (id, note) AS SELECT 1, CAST(NULL AS VARCHAR);","handlingStrategy":"validation","validationCode":"// before CTAS with aliases, ensure no untyped NULL columns:\n// if any expression is a bare NULL, wrap it: CAST(NULL AS <type>)","typeGuard":"boolean hasKnownType(Type t) { return t != UNKNOWN; }","tryCatchPattern":null,"preventionTips":["Never select bare NULL columns in CTAS; always CAST to a concrete type","Add a lint check rejecting untyped NULL in generated SQL","Review ORM/BI-generated queries for NULL placeholders"],"tags":["presto","ctas","null","unknown-type","column-type"],"backgroundTag":"unknown-column-type","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"}