[3.15] gh-109638: Fix exponential time in csv.Sniffer for doubled quotes - #154868
Open
serhiy-storchaka wants to merge 1 commit into
Open
[3.15] gh-109638: Fix exponential time in csv.Sniffer for doubled quotes#154868serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
The regular expression which looks for a doubled quote character let its runs match the quote character too, so every quote could be taken either as a part of a run or as one of the matched quotes. Exclude the quote character from the runs and match them possessively. The runs no longer exclude the delimiter and the line break either, so a doubled quote is now also found in a field which contains them.
Member
Author
|
Note that the widened detection -- a doubled quote is now found in a field which contains the delimiter or a line break -- changes results on 42 of 560 files in the CSVsniffer corpora. Against that corpus's ground truth the change is positive on both sides: There are two alternatives. #154865 changes no results, but leaves the search polynomial (O(n^5) -> O(n^4), still four and a half minutes on a 2 KB sample). Or, since 3.15 is still in beta, the new engine can be backported from main, which fixes this issue along with seven others; it changes results on 122 of 560 files. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The regular expression which detects a doubled quote character lets its runs match the quote character too, so every quote can be taken either as part of a run or as one of the three matched quotes. With three unbounded quantifiers over a region of quotes and delimiters the search is O(n^5) -- 254 s at n=100 in the report.
The runs now exclude the quote character and are matched possessively, which leaves exactly one way to read any input.
The runs also no longer exclude the delimiter and the line break, which the original did for no reason a reader shares -- so a doubled quote is now found in a field containing them as well, e.g.
,"All-Weather Dining Table, Round 48""",. That is the bulk of the behaviour change: 42 of 560 corpus files, and against ground truthdoublequote=Trueimproves from 25.8% to 36.7% anddoublequote=Falsefrom 77.0% to 78.7%, with delimiter and quotechar unchanged.Note #109639 proposed an atomic group on the first run only. It removes the time but also the detection:
doublequotebecomes unconditionallyFalse(0 of 126 annotated files, vs 32 for 3.15).main is not affected: the sniffer was rewritten there in gh-83273.