The Unknown Identifier error in ClickHouse (Code 47) means the query parser couldn’t find a column or function with the name you used. This is usually because you’ve misspelled something, are referencing a column that doesn’t exist in the table, or are trying to use a function that isn’t available.
Here are the most common culprits and how to fix them:
1. Typo in Column Name This is by far the most frequent reason. You simply mistyped a column name.
- Diagnosis: Carefully review your query, paying close attention to every letter in the column names you’re referencing. Compare them against your table schema.
-- Example: Suppose your table has a column 'user_id' but you typed 'userid' SELECT userid, COUNT(*) FROM my_table GROUP BY userid; - Fix: Correct the spelling.
SELECT user_id, COUNT(*) FROM my_table GROUP BY user_id; - Why it works: ClickHouse requires exact matches for identifiers. Correcting the typo makes the identifier known to the parser.
2. Column Does Not Exist in the Table You might be trying to select or filter on a column that was never added to the table, or has been dropped.
- Diagnosis: Check your table’s schema.
Look for the column name in the output.DESCRIBE TABLE my_table; -- or SHOW COLUMNS FROM my_table; - Fix: If the column truly doesn’t exist, either add it if it’s a legitimate requirement, or correct your query to use an existing column.
-- If you intended to use 'account_id' but typed 'user_id' SELECT account_id, COUNT(*) FROM my_table GROUP BY account_id; -- If the column is missing and you need it: ALTER TABLE my_table ADD COLUMN new_column String; - Why it works: The
DESCRIBE TABLEcommand reveals the actual structure of the table. If the column isn’t there, you must either query what is there or alter the table to include it.
3. Case Sensitivity Mismatch (Less Common, Depends on Configuration)
By default, ClickHouse identifiers are case-insensitive. However, if your ClickHouse server was started with specific settings that enforce case sensitivity for identifiers (e.g., identifier_case_sensitive = 1), then MyColumn is different from mycolumn.
- Diagnosis: Check your server configuration or session settings.
IfSELECT name, value FROM system.settings WHERE name = 'identifier_case_sensitive';identifier_case_sensitiveis 1, case matters. - Fix: Ensure the case in your query exactly matches the case in the table definition.
-- If table has 'UserID' and case sensitivity is ON SELECT UserID FROM my_table; - Why it works: When
identifier_case_sensitiveis enabled, the parser treatsUserIDandUserIDas distinct entities, requiring an exact match.
4. Incorrectly Qualified Table/Column Names
If you’re using JOINs or referencing columns from specific tables in a complex query, you might have a typo in the table name prefix or the fully qualified column name.
- Diagnosis: Examine your
JOINclauses andSELECTlist for qualified names liketable_name.column_nameoralias.column_name.SELECT t1.user_id, t2.order_date FROM users AS t1 JOIN orders AS t2 ON t1.user_id = t2.user_id; -- Suppose you mistyped 'orders' as 'orderz' - Fix: Correct the table name or alias.
SELECT t1.user_id, t2.order_date FROM users AS t1 JOIN orders AS t2 ON t1.user_id = t2.user_id; - Why it works: The parser needs to resolve
t2.order_dateby first identifyingt2as an alias forordersand then findingorder_datewithin theorderstable schema. A typo breaks this resolution.
5. Using a Reserved Keyword as an Identifier (Without Backticks)
While less common for "Unknown Identifier" and more for syntax errors, if you use a word that is a ClickHouse reserved keyword (like SELECT, FROM, WHERE, GROUP, ORDER) as a column name, and you don’t quote it, it can cause parsing issues.
- Diagnosis: Review your query for any column names that might also be ClickHouse keywords.
- Fix: Enclose the identifier in backticks (
``).-- If you have a column named 'order' SELECT `order` FROM my_table; - Why it works: Backticks tell the parser to treat the enclosed string as a literal identifier, overriding its keyword interpretation.
6. Typo in Function Name or Incorrect Function Usage You might be trying to use a function that doesn’t exist, or you’ve misspelled a valid function name.
- Diagnosis: Consult the ClickHouse documentation for available functions.
-- Suppose you meant to use 'uniq' but typed 'unique' SELECT unique(user_id) FROM my_table; - Fix: Correct the function name.
SELECT uniq(user_id) FROM my_table; - Why it works: ClickHouse has a specific set of built-in functions. The parser only recognizes these predefined function names.
7. Column in GROUP BY or ORDER BY Clause Not Present in SELECT List (and not aggregated)
This is a specific case, often leading to an Unknown Identifier error if the identifier is not resolvable in the context of the SELECT list or aggregate functions. While ClickHouse is flexible, certain combinations can trip it up.
- Diagnosis: Check your
GROUP BYandORDER BYclauses. Ensure all non-aggregated columns referenced in these clauses are also present in theSELECTlist.-- Suppose 'event_type' is not in the SELECT list and not aggregated SELECT COUNT(*) FROM events GROUP BY event_type; - Fix: Either add the column to the
SELECTlist or aggregate it.SELECT event_type, COUNT(*) FROM events GROUP BY event_type; -- or SELECT COUNT(*), any(event_type) FROM events GROUP BY event_type; - Why it works: The
GROUP BYclause defines the dimensions by which data is aggregated. Columns used for grouping must be made available in the output, either directly or via an aggregate function that collapses them.
After fixing the Unknown Identifier error, the next most common problem you’ll encounter is likely a Syntax error if your query structure itself is malformed, or potentially a Column not found in nested table if you’re dealing with nested data structures and misidentified a field within them.