The "DB::Exception: Unknown type of query" error in ClickHouse is raised when the server encounters a SQL statement type it does not recognize. ClickHouse supports a specific set of statement types (SELECT, INSERT, CREATE, ALTER, DROP, etc.), and any statement outside this set is rejected with the UNKNOWN_TYPE_OF_QUERY error code.
Impact
The statement fails immediately during parsing. ClickHouse does not attempt to interpret or partially execute the query. This error often appears when migrating SQL scripts from other databases or when using tools that generate SQL for a different dialect.
Common Causes
- Using statements from other SQL dialects -- for example,
MERGE,CALL,EXECUTE,DECLARE,FETCH CURSOR, orGRANT OPTION FOR. - Stored procedure syntax -- ClickHouse does not support stored procedures (
CREATE PROCEDURE,BEGIN...ENDblocks, etc.). - Transaction control statements used incorrectly -- while ClickHouse has limited transaction support, some transaction syntax from other databases is not recognized.
- Driver or ORM sending unsupported commands -- some database drivers issue statements like
SET NAMESorSET CHARACTER SETthat ClickHouse does not support. - Corrupted or truncated query -- a query that was cut off mid-stream may start with unrecognizable tokens.
Troubleshooting and Resolution Steps
Identify the unsupported statement. Check the error message or query log for the exact statement that failed:
SELECT query, exception FROM system.query_log WHERE exception LIKE '%UNKNOWN_TYPE_OF_QUERY%' ORDER BY event_time DESC LIMIT 5;Find the ClickHouse equivalent. Common replacements:
Unsupported Statement ClickHouse Alternative MERGE INTOUse INSERT ... SELECTwithALTER TABLE ... UPDATEor ReplacingMergeTreeCREATE PROCEDUREUse UDFs ( CREATE FUNCTION) or application logicCALL procedure()Not supported; use application code UPDATE ... SET ... FROMALTER TABLE ... UPDATEVACUUMNot needed; ClickHouse handles compaction automatically ANALYZE TABLENot needed; ClickHouse manages statistics internally Check your database driver configuration. If the driver sends initialization commands:
-- Some MySQL drivers send: SET NAMES utf8mb4; -- Configure the driver for ClickHouse mode or disable init commandsReview ORM compatibility. Make sure your ORM has a ClickHouse adapter:
# Example: SQLAlchemy needs the clickhouse-sqlalchemy driver # Django needs django-clickhouse or clickhouse-backendSplit multi-statement scripts. If you are running a SQL file that contains multiple statements, some may be unsupported. Run them individually to find the problematic ones.
Best Practices
- When migrating from another database, audit your SQL scripts for ClickHouse compatibility before running them.
- Use a ClickHouse-specific database driver rather than a generic MySQL or PostgreSQL driver.
- Avoid relying on stored procedures; move that logic to your application layer or use ClickHouse UDFs for simple reusable expressions.
- Test SQL scripts in a development environment before applying them to production.
- Keep a migration guide documenting the mappings between your source database's SQL and ClickHouse equivalents.
Frequently Asked Questions
Q: Does ClickHouse support stored procedures?
A: No. ClickHouse does not have stored procedures, triggers, or PL/SQL-style procedural code. Use application-level logic or simple UDFs created with CREATE FUNCTION for reusable expressions.
Q: What statement types does ClickHouse support?
A: The main statement types are: SELECT, INSERT, CREATE (TABLE, VIEW, MATERIALIZED VIEW, DATABASE, FUNCTION, etc.), ALTER, DROP, RENAME, ATTACH, DETACH, DESCRIBE, EXPLAIN, SHOW, USE, SET, SYSTEM, KILL, OPTIMIZE, TRUNCATE, EXCHANGE, GRANT, REVOKE, and CHECK.
Q: My MySQL connector sends SET NAMES on connect. How do I fix it?
A: Configure your connector to skip initialization queries, or use a ClickHouse-native connector instead. For JDBC, use the official ClickHouse JDBC driver. For Python, use clickhouse-connect or clickhouse-driver.
Q: Can I use BEGIN/COMMIT for transactions in ClickHouse?
A: ClickHouse has limited implicit transaction support for certain operations, and recent versions added explicit BEGIN TRANSACTION/COMMIT/ROLLBACK support. However, the semantics differ significantly from traditional RDBMS transactions. Check your ClickHouse version's documentation for the current state of transaction support.