The "DB::Exception: Invalid LIMIT expression" error in ClickHouse is thrown when the value provided to a LIMIT clause is not a valid non-negative integer. The error code is INVALID_LIMIT_EXPRESSION. ClickHouse expects LIMIT values to be non-negative integer constants or expressions that evaluate to non-negative integers.
Impact
This error prevents query execution entirely. The effects include:
- Immediate query failure with no data returned
- Application errors if dynamic LIMIT values are constructed incorrectly
- Blocked pagination or data export workflows that depend on LIMIT/OFFSET logic
Common Causes
- Negative LIMIT value -- Passing a negative number to LIMIT, such as
LIMIT -1. - Non-integer LIMIT value -- Using a float, string, or expression that does not resolve to an unsigned integer, like
LIMIT 10.5orLIMIT 'ten'. - Expression that evaluates to an invalid value -- A computed LIMIT using a subquery or variable that produces a null or non-integer result.
- Parameterized query with wrong type -- Passing a parameter of the wrong type to a LIMIT placeholder in a parameterized or prepared query.
- LIMIT value exceeds UInt64 range -- Extremely large values that overflow the unsigned 64-bit integer range.
- Empty or missing LIMIT value -- Syntax errors where the LIMIT keyword is present but no value follows it.
Troubleshooting and Resolution Steps
Verify the LIMIT value is a positive integer:
-- Wrong SELECT * FROM events LIMIT -1; SELECT * FROM events LIMIT 10.5; SELECT * FROM events LIMIT 'abc'; -- Correct SELECT * FROM events LIMIT 10; SELECT * FROM events LIMIT 0; -- valid, returns no rowsCheck dynamic or parameterized LIMIT values: If your application constructs the LIMIT dynamically, ensure the value is cast to a positive integer before being included in the query:
# Python example limit = max(0, int(user_input)) query = f"SELECT * FROM events LIMIT {limit}"Use toUInt64 for expression-based limits: If you need a computed LIMIT, ensure the expression returns the right type:
-- Using a setting or variable SET max_rows = 100; SELECT * FROM events LIMIT toUInt64(100);Check for null values in expressions: If the LIMIT value comes from a subquery or function that might return NULL:
-- This could fail if the subquery returns NULL SELECT * FROM events LIMIT (SELECT count FROM limits_table WHERE name = 'default'); -- Safer approach SELECT * FROM events LIMIT ifNull((SELECT count FROM limits_table WHERE name = 'default'), 100);Review OFFSET values as well: The same validation applies to OFFSET. Ensure both are valid:
-- Both LIMIT and OFFSET must be non-negative integers SELECT * FROM events LIMIT 10 OFFSET 20;
Best Practices
- Validate and sanitize LIMIT values in your application layer before sending queries to ClickHouse.
- Use parameterized queries with explicit type casting to prevent type mismatch issues.
- Set a sensible
max_result_rowsserver setting as a safety net to prevent unbounded result sets, rather than relying solely on client-side LIMIT values. - When using pagination, calculate LIMIT and OFFSET values carefully to avoid negative numbers from off-by-one errors.
- Prefer
LIMIT Nover omitting it entirely for production queries to prevent accidental full-table scans in client applications.
Frequently Asked Questions
Q: Is LIMIT 0 a valid expression in ClickHouse?
A: Yes. LIMIT 0 is valid and returns an empty result set. This can be useful for schema introspection or testing query syntax without returning data.
Q: Can I use a subquery as a LIMIT value?
A: ClickHouse does not support subqueries directly as LIMIT values in standard queries. You would need to compute the value separately and pass it as a constant, or use a parameterized query approach.
Q: What is the maximum LIMIT value ClickHouse supports?
A: The LIMIT value is stored as a UInt64, so the theoretical maximum is 18,446,744,073,709,551,615. In practice, your query will be constrained by available memory and the max_result_rows setting long before hitting this limit.
Q: Does this error also apply to LIMIT BY?
A: Yes. The LIMIT BY clause also requires a valid positive integer. The INVALID_LIMIT_EXPRESSION error can be triggered by invalid values in either LIMIT or LIMIT BY.