CsvReadOptions.null_regex has no effect: matching values are read as literal strings, and fail the read in a numeric column
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 76/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Bereich
- backend, data-engineering
Rechercherichtung
Beginne in crates/core/src/options.rs und in datafusion-datasource-csvs src/source.rs:187 und vergleiche dann den Reader-Builder mit der null_regex-Behandlung in src/file_format.rs:547. Füge Abdeckung zu python/tests/test_context.py hinzu, indem du einen übereinstimmenden Wert in einer numerischen Spalte verwendest, und führe die CSV-Optionstests aus. Als erledigt gilt die Aufgabe, wenn übereinstimmende Felder sowohl für String- als auch für numerische Spalten zu NULL werden und das dokumentierte Verhalten in docs/source/user-guide/io/csv.md weiterhin korrekt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Describe the bug
CsvReadOptions.null_regex is accepted everywhere it is offered, but the CSV
reader never applies it. Values matching the regex are read as literal strings,
and when a matching value sits in a column typed as an integer the read fails
outright instead of producing NULL.
This is not a bug in this crate — python/datafusion/options.py stores the
value and crates/core/src/options.rs copies it into DataFusion's
CsvReadOptions correctly. The root cause is in DataFusion itself; see below.
To Reproduce
from pathlib import Path
from datafusion import CsvReadOptions, SessionContext
p = Path("probe.csv")
p.write_text("id,name\n1,alice\n2,N/A\n3,carol\n")
ctx = SessionContext()
options = CsvReadOptions().with_has_header(True).with_null_regex(r"^(null|NULL|N/A)$")
ctx.read_csv(p, options=options).show()
+----+-------+
| id | name |
+----+-------+
| 1 | alice |
| 2 | N/A | <- expected NULL
| 3 | carol |
+----+-------+
Every entry point behaves the same way — the CsvReadOptions(null_regex=...)
constructor, with_null_regex(), read_csv(), register_csv(), and SQL:
ctx.sql("""
CREATE EXTERNAL TABLE t (id INT, name VARCHAR)
STORED AS CSV LOCATION 'probe.csv'
OPTIONS ('format.has_header' 'true', 'format.null_regex' '^(null|NULL|N/A)$')
""")
ctx.sql("select * from t").show() # same output, N/A not nulled
The SQL form does not reject the option, and giving an explicit schema does not
change anything, so this is not schema inference choosing Utf8.
When the matching value is in a numeric column, the read fails rather than
returning the wrong value:
p.write_text("id,value\n1,10\n2,N/A\n3,30\n")
ctx.sql("""CREATE EXTERNAL TABLE t2 (id INT, value BIGINT) STORED AS CSV
LOCATION 'probe.csv'
OPTIONS ('format.has_header' 'true', 'format.null_regex' '^(null|NULL|N/A)$')""")
ctx.sql("select * from t2").show()
DataFusion error: Arrow error: Parser error: Error while parsing value 'N/A' as
type 'Int64' for column 1 at line 2. Row data: '[2,N/A]'
This is the case that matters in practice: N/A, NULL and - placeholders
in otherwise numeric columns are the reason to reach for null_regex at all.
Expected behavior
A field matching null_regex is read as NULL, whatever the column's type.
Additional context
Why the existing coverage does not catch it: test_read_csv_with_options in
python/tests/test_context.py does set null_regex="[pP]+aris", but the only
Paris in its fixture is on the #Charlie;35;Paris line, which comment="#"
removes before the reader sees it. The None in that test's expected output
comes from truncated_rows=True on the Bob;25 row, not from null_regex. The
test pins that the option parses, which is what its comment says it is for — it
does not pin the behavior.
docs/source/user-guide/io/csv.md documents with_null_regex as "Treat these
as NULL", so the documented behavior and the actual behavior disagree.
Root cause (upstream). In datafusion-datasource-csv 55.0.0, the version
this repository pins, null_regex is applied during schema inference but never
to the reader that actually parses the rows:
src/file_format.rs:547sets it on thearrow::csv::reader::Formatused for
infer_schema.src/source.rs:187builds thecsv::ReaderBuilderthat reads the data, and
callswith_delimiter,with_header,with_quote,with_truncated_rows,
with_terminator,with_escapeandwith_comment— but not
with_null_regex.arrow_csv::reader::ReaderBuilder::with_null_regexexists
in arrow 59.2.0 and is simply never called.
CsvSource already holds the full CsvOptions, so self.options.null_regex is
in scope at that point — it looks like a few lines in builder(), mirroring the
escape and comment blocks just below it.
I could not find this reported in either this repository or apache/datafusion.
If you would rather track it upstream, I am happy to open it against
apache/datafusion and link it back here.
Found while working on #1728 / #1732, which is why the advanced-options example
there keeps with_null_regex set but puts its N/A in a string column, so the
example runs. Happy to take the fix if it turns out to be on this side.
- Vorherrschende Sprache
- Python
- Sterne
- 605
- Forks
- 176
- Ø Merge
- 1 T. 23 Std.
- Gemergte PRs (30 T.)
- 8
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus apache/datafusion-python
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
apache/datafusion-python#1726 ·
-
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 88/100
apache/datafusion-python#1691 ·
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
apache/datafusion-python#1644 ·
-
enhancement
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 30/100
apache/datafusion-python#1737 ·
-
bug good first issue
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 48/100
apache/datafusion-python#1728 ·
Alle Issues in apache/datafusion-python
Ähnliche Issues
-
documentation help wanted
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 90/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 90/100
simonw/sqlite-utils#872 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100