// ClickHouse error reference

20: NUMBER_OF_COLUMNS_DOESNT_MATCH (code 20)

The rows being inserted have a different column count than the statement or the target table expects.

錯誤碼
20
名稱
NUMBER_OF_COLUMNS_DOESNT_MATCH
類別
Formats and input data
錯誤訊息
Number of columns doesn't match

它的意義

NUMBER_OF_COLUMNS_DOESNT_MATCH (20) fires while INSERT data is parsed, when a row's value count disagrees with the column list or the table: a VALUES tuple with the wrong arity, a CSV or JSONEachRow row with missing or extra fields, or an INSERT ... SELECT projection that differs from the target list.

常見原因

  • A hand written INSERT has a different number of values per tuple.
  • A CSV or JSONEachRow row gained or lost fields, often after a schema edit on either side.
  • INSERT INTO t (a, b) SELECT ... selects a different number of columns.

Find the cause

`DESCRIBE TABLE` for the target arity, `system.query_log` for the failed statement, and for file formats the exception names the first bad row, which is usually enough.

如何修正

  1. Compare the target schema against the file header before importing.

    DESCRIBE TABLE my_table;
  2. Name every column explicitly so positional drift becomes an error you can read.

    INSERT INTO my_table (id, name) VALUES (1, 'a');
  3. Eyeball the first rows of a suspect file with the same format the import uses.

    SELECT * FROM file('data.csv', CSVWithNames) LIMIT 5;

Other ways to handle it

Reading a file through file() with an explicit format isolates parse errors from schema errors. INSERT with an explicit column list beats INSERT without one.

安全地重現問題

VALUES binds positionally, so a single missing value is enough; file formats report the first bad row instead.

CREATE TABLE scratch_h (a UInt32, b String) ENGINE = MergeTree ORDER BY a; then INSERT INTO scratch_h VALUES (1) returns code 20.

問題

A CSV row has too many fields?

An extra CSV field raises 117 INCORRECT_DATA (Expected end of line). A short row raises 27 CANNOT_PARSE_INPUT_ASSERTION_FAILED. Neither is code 20. A trailing comma on a two-column row was accepted silently in 26.9, so check the loaded rows.

Why did this break after we added a column?

INSERT without a column list binds values positionally to every table column. Appending a column with ADD COLUMN makes an old-width VALUES tuple fail with code 62. A column added AFTER a position can shift values silently when the tuple width still matches. Always list columns explicitly.

相關錯誤

程式碼和訊息文字來自 ClickHouse documentation (system.errors)。瀏覽完整的 ClickHouse error list 或 搜尋每個資料庫錯誤.

// 錯誤更少,修復更快

Work on ClickHouse in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for ClickHouse and the other databases you use.

本頁文字與資料: CC BY 4.0,請註明出處 SQLly (sqlly.app)。 程式碼與 SQL 片段: MIT. 授權詳情 引用的錯誤訊息文字歸其資料庫廠商所有。