bug: CSV/TSV `COPY INTO <table>` stores the NULL marker (`\N`, or the `NULL_DISPLAY` text) as a string in a `STRING NOT NULL` column instead of rejecting the row

Open
#20,783 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@youngsofun is already working on this.

Since Oct 23, 2026.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
databases

Research direction

Start with src/query/formats/src/field_decoder/separated_text.rs, especially SeparatedTextDecoder::read_field and read_nullable, to trace how null_if is checked for nullable and non-nullable columns. Add or locate focused CSV and TSV load tests for STRING NOT NULL with the default \N marker and a custom NULL_DISPLAY; done means those rows are rejected while valid string rows still load.

Written by the indexing model from the issue text.

Description

C-bug
Search before asking
  • I had searched in the issues and found no similar issues.
Version

main e9b49fd (source build, v1.2.953-nightly-e9b49fde); also v1.2.952-nightly-8862a6285b.

What's Wrong?

When a CSV or TSV file holds a NULL (written by Databend's own unload as \N, or as the configured NULL_DISPLAY) and
the target column is STRING NOT NULL, the load succeeds and stores the two-character text \N (or NULL). The
result reports 0 errors. The NOT NULL constraint the user declared is bypassed, and a value that was never in the source
appears in the table.

Every other NOT NULL type rejects the same field (e.g. INT: Invalid value '\N' for column 2 (n Int32): invalid text for number), and so do NDJSON loads and INSERT … VALUES (1, NULL). Even an empty CSV field for the same
STRING NOT NULL column is rejected (EMPTY_FIELD_AS=NULL). Only the explicit NULL marker gets through. A column
DEFAULT does not change this.

Cause: SeparatedTextDecoder::read_field (src/query/formats/src/field_decoder/separated_text.rs:96) checks the field
against null_if (built from NULL_DISPLAY) only in read_nullable (line 170), i.e. only for nullable columns. For a
non-nullable column it goes straight to the type's parser. Every parser except STRING fails on \N, but
ColumnBuilder::String (line 113) accepts it as data. The NULL check (with an error like "null value is not allowed for
non-nullable field", as the NDJSON reader gives) should come before the per-type dispatch for unquoted fields.

Related but different: #20611 (TSV reads the escaped literal \\N as NULL in a nullable column).

How to Reproduce?
CREATE OR REPLACE STAGE s116;
CREATE OR REPLACE TABLE src (id INT, name STRING NULL, n INT NULL);
INSERT INTO src VALUES (1, NULL, 1), (2, 'bob', 2);
COPY INTO @s116/csv/ FROM src FILE_FORMAT = (TYPE = CSV);                         -- file: 1,\N,1 / 2,"bob",2
COPY INTO @s116/tsv/ FROM src FILE_FORMAT = (TYPE = TSV);
COPY INTO @s116/csvn/ FROM src FILE_FORMAT = (TYPE = CSV NULL_DISPLAY = 'NULL');

CREATE OR REPLACE TABLE dst (id INT, name STRING NOT NULL, n INT NULL);
COPY INTO dst FROM @s116/csv/ FILE_FORMAT = (TYPE = CSV);                         -- 2 rows loaded, 0 errors
COPY INTO dst FROM @s116/tsv/ FILE_FORMAT = (TYPE = TSV);                         -- 2 rows loaded, 0 errors
COPY INTO dst FROM @s116/csvn/ FILE_FORMAT = (TYPE = CSV NULL_DISPLAY = 'NULL');  -- 2 rows loaded, 0 errors
SELECT id, name, length(name) FROM dst ORDER BY id, name;
-- 1 | NULL | 4        <-- the string 'NULL'
-- 1 | \N   | 2        <-- the string '\N'
-- 1 | \N   | 2
-- 2 | bob  | 3  (×3)

For comparison, the same NULL into an INT NOT NULL column:

CREATE OR REPLACE TABLE dst_int (id INT, name STRING NULL, n INT NOT NULL);
INSERT INTO src VALUES (3, 'carl', NULL);
COPY INTO @s116/csv2/ FROM src FILE_FORMAT = (TYPE = CSV);
COPY INTO dst_int FROM @s116/csv2/ FILE_FORMAT = (TYPE = CSV);
-- error: Invalid value '\N' for column 2 (n Int32): invalid text for number

Expected: the row with the NULL marker is rejected for the STRING NOT NULL column (an error, or a counted error under
ON_ERROR = CONTINUE), as for every other type.

Single node, default settings, HTTP handler.

Are you willing to submit PR?
  • Yes I am willing to submit a PR!
Dominant language
Rust
Stars
9.5k
Forks
903
Avg merge
2d 13h
Merged PRs (30d)
204

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from databendlabs/databend

All issues in databendlabs/databend

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.