prestodb/presto · warning · PrestoException

CLICKHOUSE_QUERY_GENERATOR_FAILURE

CLICKHOUSE_QUERY_GENERATOR_FAILURE

Error message

Invalid limit: 

What it means

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.

Source

Thrown at presto-clickhouse/src/main/java/com/facebook/presto/plugin/clickhouse/optimization/ClickHouseQueryGeneratorContext.java:154

    public ClickHouseQueryGeneratorContext withProject(Map<VariableReferenceExpression, Selection> newSelections)
    {
        return new ClickHouseQueryGeneratorContext(
                newSelections,
                from,
                schema,
                filter,
                limit,
                aggregations,
                groupByColumns,
                variablesInAggregation,
                hiddenColumnSet,
                tableScanNodeId);
    }

    public ClickHouseQueryGeneratorContext withLimit(long limit)
    {
        if (limit <= 0 || limit > Long.MAX_VALUE) {
            throw new PrestoException(CLICKHOUSE_QUERY_GENERATOR_FAILURE, "Invalid limit: " + limit);
        }
        checkState(!hasLimit(), "Limit already exists. ClickHouse doesn't support limit on top of another limit");
        return new ClickHouseQueryGeneratorContext(
                selections,
                from,
                schema,
                filter,
                OptionalLong.of(limit),
                aggregations,
                groupByColumns,
                variablesInAggregation,
                hiddenColumnSet,
                tableScanNodeId);
    }

    public ClickHouseQueryGeneratorContext withAggregation(
            Map<VariableReferenceExpression, Selection> newSelections,
            Map<VariableReferenceExpression, Selection> newGroupByColumns,

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Remove LIMIT 0 from the query or let the optimizer eliminate the empty-limit plan node before pushdown.
  2. Use a positive limit value in the query.
  3. If the limit comes from generated/dynamic SQL, validate the limit > 0 before building the query.
  4. 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.

Example fix

// before
SELECT * FROM clickhouse_table LIMIT 0;
// after
SELECT * FROM clickhouse_table LIMIT 10;
Defensive patterns

Strategy: validation

Validate before calling

if (limit <= 0 || limit == Long.MAX_VALUE) {
    throw new IllegalArgumentException("Limit must be a positive long before querying ClickHouse: " + limit);
}

Type guard

boolean isValidLimit(long limit) {
    return limit > 0 && limit < Long.MAX_VALUE;
}

Try / catch

try {
    execute(query);
} catch (PrestoException e) {
    if ("CLICKHOUSE_QUERY_GENERATOR_FAILURE".equals(e.getErrorCode().getName()) && e.getMessage().startsWith("Invalid limit")) {
        executeWithLimit(query, Math.max(1, sanitizedLimit));
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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