prestodb/presto · error · SemanticException

MULTIPLE_FIELDS_FROM_SUBQUERY

MULTIPLE_FIELDS_FROM_SUBQUERY

Error message

Multiple columns returned by subquery are not yet supported. Found %s

What it means

Semantic analysis error for a subquery used in a scalar context: the subquery's SELECT list returns more than one column, but only a single-column (scalar) subquery is supported there. The %s lists the multi-column row type that was found.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/ExpressionAnalyzer.java:1539

            setExpressionType(node, type);
            return type; // TODO: this really should a be relation type
        }

        @Override
        protected Type visitSubqueryExpression(SubqueryExpression node, StackableAstVisitorContext<Context> context)
        {
            if (context.getContext().isInLambda()) {
                throw new SemanticException(NOT_SUPPORTED, node, "Lambda expression cannot contain subqueries");
            }
            StatementAnalyzer analyzer = statementAnalyzerFactory.apply(node);
            Scope subqueryScope = Scope.builder()
                    .withParent(context.getContext().getScope())
                    .build();
            Scope queryScope = analyzer.analyze(node.getQuery(), subqueryScope);

            // Subquery should only produce one column
            if (queryScope.getRelationType().getVisibleFieldCount() != 1) {
                throw new SemanticException(MULTIPLE_FIELDS_FROM_SUBQUERY,
                        node,
                        "Multiple columns returned by subquery are not yet supported. Found %s",
                        queryScope.getRelationType().getVisibleFieldCount());
            }

            Node previousNode = context.getPreviousNode().orElse(null);
            if (previousNode instanceof InPredicate && ((InPredicate) previousNode).getValue() != node) {
                subqueryInPredicates.add(NodeRef.of((InPredicate) previousNode));
            }
            else if (previousNode instanceof QuantifiedComparisonExpression) {
                quantifiedComparisons.add(NodeRef.of((QuantifiedComparisonExpression) previousNode));
            }
            else {
                scalarSubqueries.add(NodeRef.of(node));
            }
            sourceFields.add(queryScope.getRelationType().getFieldByIndex(0));
            Type type = queryScope.getRelationType().getVisibleFields().stream().collect(onlyElement()).getType();
            return setExpressionType(node, type);

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Reduce the subquery to a single column, e.g. (SELECT a FROM ...) instead of (SELECT a, b FROM ...)
  2. Use a JOIN against the subquery when multiple columns are needed
  3. Wrap multi-column needs in a row() or restructure the predicate
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/ExpressionAnalyzer.java:1539 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/a6dc1bd2c36edd54. Report an issue: GitHub.