NEW

Pulse 2025 Product Roundup: From Monitoring to AI-Native Control Plane

ClickHouse DB::Exception: Cannot compile code

The "DB::Exception: Cannot compile code" error in ClickHouse indicates that the JIT (Just-In-Time) compilation subsystem failed to compile a query expression into native machine code. The error code is CANNOT_COMPILE_CODE. ClickHouse uses LLVM-based JIT compilation to accelerate certain operations -- when this compilation step fails, the server raises this exception.

Impact

This error is generally limited in scope:

  • The specific query that triggered JIT compilation fails
  • Other queries that do not trigger compilation, or that compile successfully, are unaffected
  • No data is corrupted or lost
  • The server remains fully operational for non-JIT workloads

Common Causes

  1. Unsupported expression for JIT compilation -- Not all ClickHouse functions and data types are supported by the JIT compiler. Attempting to compile an unsupported combination triggers the error.
  2. LLVM internal error -- Bugs or limitations in the LLVM compilation backend can cause failures for certain expression patterns.
  3. Insufficient memory for compilation -- JIT compilation requires memory for the LLVM pipeline. Under heavy memory pressure, compilation can fail.
  4. ClickHouse build without full JIT support -- Some ClickHouse builds or distributions may have incomplete or experimental JIT support.
  5. Complex nested expressions -- Deeply nested expressions or very long expression chains may exceed internal limits of the compilation pipeline.

Troubleshooting and Resolution Steps

  1. Disable JIT compilation for the failing query to confirm it works without compilation:

    SET compile_expressions = 0;
    SET compile_aggregate_expressions = 0;
    
    -- Now retry your query
    SELECT ...;
    

    If the query succeeds with JIT disabled, the issue is specific to the JIT compiler.

  2. Identify which expression triggers the failure by simplifying the query. Remove expressions one by one until the query compiles successfully, isolating the problematic function or type combination.

  3. Check ClickHouse version and known issues. JIT compilation has improved significantly across releases. Check if a newer version resolves the compilation failure:

    SELECT version();
    
  4. Disable JIT compilation server-wide if it is causing persistent issues:

    <!-- users.xml or profiles -->
    <profiles>
        <default>
            <compile_expressions>0</compile_expressions>
            <compile_aggregate_expressions>0</compile_aggregate_expressions>
        </default>
    </profiles>
    
  5. Check server memory if compilation failures coincide with high memory usage:

    SELECT
        metric,
        formatReadableSize(value) AS value
    FROM system.metrics
    WHERE metric = 'MemoryTracking';
    
  6. Report the issue if you believe the expression should be compilable. Include the full query, ClickHouse version, and the complete error message when filing a bug report.

Best Practices

  • JIT compilation is an optimization, not a requirement. Disabling it results in slightly slower execution for certain queries but does not affect correctness.
  • If you encounter CANNOT_COMPILE_CODE in production, disable JIT for the affected query or user profile and investigate later.
  • Keep ClickHouse updated to benefit from JIT compiler improvements and bug fixes.
  • Monitor for this error in logs to identify patterns -- if specific expression types consistently fail, disable JIT compilation for those workloads.
  • Test JIT compilation behavior in staging before enabling it broadly in production.

Frequently Asked Questions

Q: Is JIT compilation enabled by default in ClickHouse?
A: In recent ClickHouse versions, compile_expressions defaults to 1 (enabled). The min_count_to_compile_expression setting (default 3) controls how many times an expression must be evaluated before JIT compilation is triggered, so not every query uses JIT.

Q: Does disabling JIT compilation significantly impact performance?
A: For most queries, the difference is minimal. JIT compilation provides the largest benefit for queries that evaluate simple expressions over very large numbers of rows (e.g., arithmetic or comparisons in WHERE clauses). Complex analytical queries often spend more time on I/O and aggregation than on expression evaluation.

Q: Can CANNOT_COMPILE_CODE cause data corruption?
A: No. The error occurs during the compilation phase, before any data processing takes place. The query simply fails, and no data is written or modified.

Q: Does this error relate to user-defined functions (UDFs)?
A: JIT compilation does not apply to executable UDFs. However, SQL-based UDFs that expand to expressions can trigger JIT compilation. If a SQL UDF produces an expression the JIT compiler cannot handle, you may see this error.

Subscribe to the Pulse Newsletter

Get early access to new Pulse features, insightful blogs & exclusive events , webinars, and workshops.

We use cookies to provide an optimized user experience and understand our traffic. To learn more, read our use of cookies; otherwise, please choose 'Accept Cookies' to continue using our website.