{"record":{"id":"fc10f3e32bdea53d","repo":"prestodb/presto","slug":"not-supported-fc10f3","errorCode":"NOT_SUPPORTED","errorMessage":"statement is too large (stack overflow during analysis)","messagePattern":"statement is too large \\(stack overflow during analysis\\)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/execution/SqlQueryExecution.java","lineNumber":635,"sourceCode":"            // extract output\n            Optional<Output> output = new OutputExtractor().extractOutput(plan.getRoot());\n            stateMachine.setOutput(output);\n\n            // fragment the plan\n            // the variableAllocator is finally passed to SqlQueryScheduler for runtime cost-based optimizations\n            variableAllocator.set(new VariableAllocator(plan.getTypes().allVariables()));\n            SubPlan fragmentedPlan = getSession().getRuntimeStats().recordWallAndCpuTime(\n                    FRAGMENT_PLAN_TIME_NANOS,\n                    () -> planFragmenter.createSubPlans(stateMachine.getSession(), plan, false, idAllocator, variableAllocator.get(), stateMachine.getWarningCollector()));\n\n            // record analysis time\n            stateMachine.endAnalysis();\n\n            boolean explainAnalyze = queryAnalysis.isExplainAnalyzeQuery();\n            return new PlanRoot(fragmentedPlan, !explainAnalyze, queryAnalysis.extractConnectors());\n        }\n        catch (StackOverflowError e) {\n            throw new PrestoException(NOT_SUPPORTED, \"statement is too large (stack overflow during analysis)\", e);\n        }\n        catch (InvalidFunctionArgumentException e) {\n            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getMessage(), e);\n        }\n    }\n\n    private PlanRoot runCreateLogicalPlanAsync()\n    {\n        try {\n            // Check if creating plan async has been cancelled\n            if (planFutureLocked.compareAndSet(false, true)) {\n                return createLogicalPlanAndOptimize();\n            }\n            return null;\n        }\n        catch (Throwable e) {\n            fail(e);\n            throw e;","sourceCodeStart":617,"sourceCodeEnd":653,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/execution/SqlQueryExecution.java#L617-L653","documentation":"When analyzing/planning a query, deep recursion over the expression tree exhausted the JVM stack. Presto catches the StackOverflowError during logical plan creation and rethrows it as a NOT_SUPPORTED PrestoException indicating the statement is too large.","triggerScenarios":"doCreateLogicalPlanAndOptimize() in SqlQueryExecution hits StackOverflowError while analyzing a statement with an extremely deep expression tree — e.g. thousands of nested OR/AND conditions, huge IN lists, or deeply nested subqueries.","commonSituations":"ORMs or query builders generating giant predicates, dynamically generated WHERE clauses with thousands of terms, machine-generated SQL, or very long chained CASE expressions.","solutions":["Split the query into smaller queries and combine results client-side.","Rewrite huge OR chains into an IN list or a temporary table + JOIN.","Reduce subquery nesting depth by flattening or using CTEs.","Increase -Xss thread stack size as a workaround, though this only delays the limit."],"exampleFix":"-- before\nSELECT * FROM t WHERE a=1 OR a=2 OR ... (10000 terms);\n-- after\nSELECT * FROM t WHERE a IN (1,2,...); -- or load values into a temp table and JOIN","handlingStrategy":"validation","validationCode":"// client-side: approximate expression depth before sending\ncountNestedOps(sql) > 1000 ? reject(\"query too deep\") : send(sql);","typeGuard":null,"tryCatchPattern":"// catch PrestoException with errorCode NOT_SUPPORTED and message containing 'stack overflow during analysis'; split the query and retry","preventionTips":["Limit generated predicate size; batch large IN lists into temp tables","Cap OR/AND chain length in query builders","Avoid deeply nested subqueries; flatten with CTEs"],"tags":["sql","planner","stack-overflow"],"backgroundTag":"query-too-large","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"}