{"record":{"id":"e6f127a59914fc53","repo":"prestodb/presto","slug":"clickhouse-query-generator-failure-e6f127","errorCode":"CLICKHOUSE_QUERY_GENERATOR_FAILURE","errorMessage":"Invalid limit: ","messagePattern":"Invalid limit: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"warning","filePath":"presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/optimization/ClickHouseQueryGeneratorContext.java","lineNumber":154,"sourceCode":"    public ClickHouseQueryGeneratorContext withProject(Map<VariableReferenceExpression, Selection> newSelections)\n    {\n        return new ClickHouseQueryGeneratorContext(\n                newSelections,\n                from,\n                schema,\n                filter,\n                limit,\n                aggregations,\n                groupByColumns,\n                variablesInAggregation,\n                hiddenColumnSet,\n                tableScanNodeId);\n    }\n\n    public ClickHouseQueryGeneratorContext withLimit(long limit)\n    {\n        if (limit <= 0 || limit > Long.MAX_VALUE) {\n            throw new PrestoException(CLICKHOUSE_QUERY_GENERATOR_FAILURE, \"Invalid limit: \" + limit);\n        }\n        checkState(!hasLimit(), \"Limit already exists. ClickHouse doesn't support limit on top of another limit\");\n        return new ClickHouseQueryGeneratorContext(\n                selections,\n                from,\n                schema,\n                filter,\n                OptionalLong.of(limit),\n                aggregations,\n                groupByColumns,\n                variablesInAggregation,\n                hiddenColumnSet,\n                tableScanNodeId);\n    }\n\n    public ClickHouseQueryGeneratorContext withAggregation(\n            Map<VariableReferenceExpression, Selection> newSelections,\n            Map<VariableReferenceExpression, Selection> newGroupByColumns,","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/optimization/ClickHouseQueryGeneratorContext.java#L136-L172","documentation":"The pushdown LIMIT clause must be a positive value representable as a long; withLimit rejects limit <= 0 or limit > Long.MAX_VALUE. This guards the generated ClickHouse SQL from receiving a LIMIT 0, negative LIMIT, or an overflowed value. The exception code is CLICKHOUSE_QUERY_GENERATOR_FAILURE because it indicates the generator cannot produce valid SQL for this plan, and pushdown is abandoned.","triggerScenarios":"visitLimit calls withLimit(long) with a value that is <= 0 or (defensively) greater than Long.MAX_VALUE — e.g. a LimitNode with count 0 or a plan carrying an unbounded/placeholder limit value.","commonSituations":"Queries with LIMIT 0 (often from EXPLAIN probes or optimizer probing), plans where the limit was computed to 0 by prior optimizations, or internal plan nodes that materialize as limit values outside the valid range.","solutions":["Remove LIMIT 0 from the query or let the optimizer eliminate the empty-limit plan node before pushdown.","Use a positive limit value in the query.","If the limit comes from generated/dynamic SQL, validate the limit > 0 before building the query.","Report it if a valid query produces limit 0 at this point — it may be an upstream planner issue; pushdown will otherwise be skipped and the query should still run in Presto."],"exampleFix":"// before\nSELECT * FROM clickhouse_table LIMIT 0;\n// after\nSELECT * FROM clickhouse_table LIMIT 10;","handlingStrategy":"validation","validationCode":"if (limit <= 0 || limit == Long.MAX_VALUE) {\n    throw new IllegalArgumentException(\"Limit must be a positive long before querying ClickHouse: \" + limit);\n}","typeGuard":"boolean isValidLimit(long limit) {\n    return limit > 0 && limit < Long.MAX_VALUE;\n}","tryCatchPattern":"try {\n    execute(query);\n} catch (PrestoException e) {\n    if (\"CLICKHOUSE_QUERY_GENERATOR_FAILURE\".equals(e.getErrorCode().getName()) && e.getMessage().startsWith(\"Invalid limit\")) {\n        executeWithLimit(query, Math.max(1, sanitizedLimit));\n    } else {\n        throw e;\n    }\n}","preventionTips":["Never issue LIMIT 0 queries expecting pushdown; the optimizer may probe with 0","Clamp/validate user-supplied page sizes to >= 1 before building SQL","Avoid computing limits via arithmetic that can overflow or underflow to non-positive values"],"tags":["clickhouse","pushdown","limit","validation"],"backgroundTag":"invalid-limit-value","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"}