Skip to content

Fix CSV Parsing of Quoted Fields, Escaped Quotes, Embedded Delimiters, and Multi-Line Records - #5080

Merged
ajpotts merged 24 commits into
Bears-R-Us:mainfrom
ShreyasKhandekar:fix_csv_quoted_read
Dec 1, 2025
Merged

ajpotts merged 24 commits into
Bears-R-Us:mainfrom
ShreyasKhandekar:fix_csv_quoted_read

Conversation

@ShreyasKhandekar

@ShreyasKhandekar ShreyasKhandekar commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

Brief

This PR introduces a substantial improvement to Arkouda’s CSV parsing logic, bringing it closer to full RFC 4180 compatibility. Specifically, this PR fixes incorrect handling of:

  • column delimiters inside quoted strings,
  • escaped quotes ("") inside fields and headers,
  • multi-line CSV records inside quoted fields, and
  • quoted or escaped column headers that become mis-escaped when serialized through JSON.

These changes address multiple long-standing issues in Arkouda’s CSV reader and enable correct behavior for real-world CSV files.

Fixes:

Overview of the Fixes

1. Correct handling of delimiters inside quoted strings

Arkouda previously treated any delimiter (e.g., ,) as a field separator—even when it appeared inside a quoted field. This PR rewrites the low-level delimiter scanning logic so that:

  • when inside a quoted field, delimiters are treated as literal characters,
  • field boundaries are only recognized when the closing quote is reached.
    This ensures correct behavior for fields such as:
"Hello, World!"
"Nested, commas, inside"

2. Proper parsing of escaped quotes ("")

RFC 4180 requires that "" inside a quoted field be interpreted as a literal ".
This PR adds correct recognition of escaped quotes and rewrites them appropriately during parsing.

For example:

"Quote ""Text"""

now parses to:

Quote "Text"

3. Support for multi-line records inside quoted fields

Arkouda previously treated physical newlines as record terminators regardless of quoting state. This PR:

  • tracks quoting state across physical lines,
  • continues reading until the closing quote is found,
  • treats embedded newlines as part of the field value.

Important

#5083 is still left unresolved by this PR.

Example now works correctly:

"id","comment"
1,"Hello
World"
2,"Another
multi-line
comment"

Tests Added / Updated

This PR adds several targeted tests to cover CSV edge cases that were previously unsupported.

Remaining Work / Future Improvements

While this PR significantly improves compliance and correctness, future enhancements may include:

  • Streaming CSV parsing for extremely large multi-line quoted fields.
  • A full RFC 4180-compliance validation mode.
  • Optional type inference similar to Pandas (e.g., auto-convert true/false, numeric fields, etc.).
  • A full revamp of the CSV reader because we still read each files once per column which is terrible for performance
  • Adding parallel IO features to CSV readers for even faster reads

Performance

The performance is mixed bag unfortunately. As mentioned in the issues related to this PR, there is a challenge to maintain correctness without sacrificing performance. Advancing through the column delimiter in Chapel is fast and efficient, but when we try to take into account maintain the "state" of being in quotes and managing them to ignore column delimiters makes this slower.
This becomes even slower when escaped quotes can exist which require marking and reverting the file pointer to manage reads.

I have added many "fast paths" where if we can determine that there are no quotes, or no multi line fields, we can be less careful and avoid doing some expensive operations. In one of the steps we also have the option to completely revert to the old implementation if we can determine that there are no quotes at all in the entire CSV file.

All the performance numbers here are collected by running:

❯ python3 -m pytest -c benchmark.ini benchmark_v2/csv_io_benchmark.py --size=1000000 

I have removed the write performance from the output for brevity

1. Here is what the performance looks like for the implementation in the PR:

----------------------------------------------------------------------------------- benchmark 'CSV_IO': 8 tests -----------------------------------------------------------------------------------
Name (time in ms)                    Min                 Max                Mean            StdDev              Median               IQR            Outliers      OPS            Rounds  Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bench_csv_io[read-uint64]       188.0938 (3.60)     189.4901 (3.37)     188.8971 (3.49)     0.5729 (1.0)      188.9925 (3.52)     0.9302 (1.15)          2;0   5.2939 (0.29)          5           1
bench_csv_io[read-int64]        188.1897 (3.60)     190.8679 (3.39)     190.0001 (3.51)     1.0423 (1.82)     190.3515 (3.55)     0.8081 (1.0)           1;1   5.2632 (0.28)          5           1
bench_csv_io[read-float64]      188.4753 (3.61)     190.0816 (3.38)     189.1288 (3.49)     0.7034 (1.23)     189.0157 (3.52)     1.2325 (1.53)          1;0   5.2874 (0.29)          5           1
bench_csv_io[read-str]          963.9651 (18.45)    970.8573 (17.27)    967.8028 (17.88)    3.1459 (5.49)     969.4045 (18.07)    5.4923 (6.80)          1;0   1.0333 (0.06)          5           1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. What happens if you remove the "revert back to old implementation" fast path"?

If you remove that fast path, you can argue it more accurately demonstrates the performance of the new implementation. While this is partly true, the new implementation still has a few "fast paths" for when there are some quotes in the file, but not perhaps on every line.

Nevertheless, since it does represent the performance of just the new implementation (which is worse) I have included it here:

------------------------------------------------------------------------------------ benchmark 'CSV_IO': 8 tests ------------------------------------------------------------------------------------
Name (time in ms)                    Min                 Max                Mean             StdDev              Median                IQR            Outliers      OPS            Rounds  Iterations
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bench_csv_io[read-uint64]       414.5707 (7.49)     453.2336 (7.76)     423.7333 (7.44)     16.5427 (20.69)    416.9608 (7.33)     11.3597 (9.57)          1;1   2.3600 (0.13)          5           1
bench_csv_io[read-int64]        408.9339 (7.39)     410.8985 (7.04)     409.6753 (7.19)      0.7997 (1.0)      409.5734 (7.20)      1.1866 (1.0)           1;0   2.4410 (0.14)          5           1
bench_csv_io[read-float64]      451.7167 (8.16)     486.8253 (8.34)     459.3327 (8.06)     15.3819 (19.23)    452.8630 (7.96)      9.7451 (8.21)          1;1   2.1771 (0.12)          5           1
bench_csv_io[read-str]          957.3902 (17.30)    972.2829 (16.65)    964.4374 (16.93)     5.5556 (6.95)     965.3951 (16.98)     7.0409 (5.93)          2;0   1.0369 (0.06)          5           1
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. Compared to performance on main


---------------------------------------------------------------------------------------- benchmark 'CSV_IO': 8 tests ----------------------------------------------------------------------------------------
Name (time in ms)                      Min                   Max                  Mean             StdDev                Median                IQR            Outliers      OPS            Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bench_csv_io[read-uint64]         118.1343 (2.22)       119.1238 (2.13)       118.7053 (2.15)      0.3653 (1.02)       118.7932 (2.15)      0.4072 (1.0)           2;0   8.4242 (0.46)          5           1
bench_csv_io[read-int64]          119.2602 (2.24)       120.1037 (2.15)       119.7022 (2.17)      0.3596 (1.0)        119.6677 (2.16)      0.6308 (1.55)          2;0   8.3541 (0.46)          5           1
bench_csv_io[read-float64]        124.0562 (2.33)       125.7878 (2.25)       124.8458 (2.27)      0.6210 (1.73)       124.8347 (2.26)      0.5899 (1.45)          2;0   8.0099 (0.44)          5           1
bench_csv_io[read-str]          6,427.0379 (120.54)   6,478.8276 (115.86)   6,449.4896 (117.01)   20.9028 (58.12)    6,450.0977 (116.55)   32.9164 (80.84)         2;0   0.1551 (0.01)          5           1
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

So in general, performance is Much better for string reads (close to7x better), but worse for typed reads (about 1.5x worse).

Most CSV files are all "string reads" in Arkouda because if Arkouda doesn't see the type headers (only available if arkouda wrote the csv file), then all the reads are string reads.
Given the above, this should improve performance in all cases when reading a CSV file written by some other tool.

If you're writing to CSV from Arkouda and reading it back, and you care about performance, then you should be using parquet anyway.

Summary

This PR fixes several major issues with Arkouda's CSV parsing:

  • Corrects the interpretation of embedded commas inside quoted fields
  • Properly handles escaped quotes
  • Supports multi-line quoted records
  • Adds extensive tests to prevent regression

These improvements bring Arkouda's CSV behavior more in line with RFC 4180 and with tools such as Pandas, enabling more reliable ingestion of real-world CSV datasets.

Signed-off-by: Shreyas Khandekar <[email protected]>
This adds tests for columns delimters in quoted strings and escaped quotes

Signed-off-by: Shreyas Khandekar <[email protected]>
…ine fields; add tests for edge cases

Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Replace with normal cast

Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Enhance CSV reading by optimizing handling of quoted fields and adding fast path for single-line records

Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
@ShreyasKhandekar ShreyasKhandekar changed the title Fix csv quoted read Fix CSV Parsing of Quoted Fields, Escaped Quotes, Embedded Delimiters, and Multi-Line Records Nov 21, 2025
@ShreyasKhandekar
ShreyasKhandekar marked this pull request as ready for review November 21, 2025 19:51
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Comment thread src/CSVMsg.chpl Resolved
Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>

@ajpotts ajpotts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Love all the tests.

And reflow for readability

Signed-off-by: Shreyas Khandekar <[email protected]>
Signed-off-by: Shreyas Khandekar <[email protected]>

@jaketrookman jaketrookman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@ajpotts
ajpotts added this pull request to the merge queue Dec 1, 2025
Merged via the queue into Bears-R-Us:main with commit 821af11 Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants