{"record":{"id":"4dd383c3587adfa2","repo":"prestodb/presto","slug":"generic-internal-error-4dd383","errorCode":"GENERIC_INTERNAL_ERROR","errorMessage":"Expect exactly 1 child PlanNode","messagePattern":"Expect exactly 1 child PlanNode","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-spi/src/main/java/com/facebook/presto/spi/plan/TopNNode.java","lineNumber":162,"sourceCode":"\n    @Override\n    public PlanNode replaceChildren(List<PlanNode> newChildren)\n    {\n        checkCondition(newChildren != null && newChildren.size() == 1, GENERIC_INTERNAL_ERROR, \"Expect exactly 1 child PlanNode\");\n        return new TopNNode(getSourceLocation(), getId(), getStatsEquivalentPlanNode(), newChildren.get(0), count, orderingScheme, step);\n    }\n\n    private static void checkArgument(boolean condition, String message)\n    {\n        if (!condition) {\n            throw new IllegalArgumentException(message);\n        }\n    }\n\n    private static void checkCondition(boolean condition, ErrorCodeSupplier errorCode, String formatString, Object... args)\n    {\n        if (!condition) {\n            throw new PrestoException(errorCode, format(formatString, args));\n        }\n    }\n}\n","sourceCodeStart":144,"sourceCodeEnd":166,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-spi/src/main/java/com/facebook/presto/spi/plan/TopNNode.java#L144-L166","documentation":"TopNNode validates its plan structure invariants during construction and child replacement via checkCondition. When a TopNNode is built or rewritten with a number of children other than exactly one (a TopN operator always consumes a single input plan), the SPI throws GENERIC_INTERNAL_ERROR. This signals a broken optimizer rule or connector-provided plan, not a user query problem per se.","triggerScenarios":"Calling TopNNode constructor or replaceChildren(...) with a child list of size 0 or >= 2, e.g. a custom optimizer/PlanRewriter that removes the source child or wraps TopN in multiple inputs.","commonSituations":"Custom PlanRewriter/Optimizer rules that mis-handle TopNNode; connector or engine version mismatch where a rule produces an unexpected plan shape; bugs in plan serialization/deserialization round-trips.","solutions":["Inspect the optimizer/rewriter rule that produced the TopNNode and ensure replaceChildren is called with exactly one child","Verify the query works without the custom plugin/rule to isolate the faulty component","Check for version mismatches between presto-spi and custom plugins and rebuild against the matching SPI","If reproducible in stock Presto, file a bug with the query and EXPLAIN (VERBOSE) output"],"exampleFix":"// before\nnode.replaceChildren(rewrittenChildren);\n// after\ncom.google.common.collect.ImmutableList<PlanNode> children = rewrittenChildren.stream().limit(1).collect(ImmutableList.toImmutableList());\nnode.replaceChildren(children); // TopNNode requires exactly one child","handlingStrategy":"validation","validationCode":"if (children == null || children.size() != 1) {\n    throw new IllegalStateException(\"TopNNode requires exactly 1 child, got \" + (children == null ? \"null\" : children.size()));\n}","typeGuard":"static boolean hasSingleChild(PlanNode node) {\n    return node.getSources().size() == 1;\n}","tryCatchPattern":"try {\n    planRewriter.rewrite(topnNode);\n} catch (PrestoException e) {\n    if (e.getErrorCode().getCode() == StandardErrorCode.GENERIC_INTERNAL_ERROR.toErrorCode().getCode()) {\n        LOG.error(\"Plan rewrite produced invalid TopNNode arity\", e);\n    }\n    throw e;\n}","preventionTips":["Always build TopNNode with a single source child; assert list size in custom rewriters","Add unit tests for custom optimizer rules covering TopNNode replaceChildren","Keep plugins compiled against the same presto-spi version as the server"],"tags":["plan-structure","internal-error","optimizer"],"backgroundTag":"invalid-plan-node-arity","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"}