- Notifications
You must be signed in to change notification settings - Fork689
Fix backslash escape handling in SQL string literals with performance optimization#2335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:develop
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
mathiasrw left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@copilot Do you beleive this PR will truly represent a better alignment with SQL-99?
Yes, this PR improves SQL-99 alignment. The key changes: SQL-99 Compliance:
Deviations (intentional, common extensions):
The PR fixes the broken |
mathiasrw commentedDec 12, 2025
Relationship Analysis: PR#2306 and this PR (fix backslash escaping) arecomplementary but have potential conflicts: Overlap Areas:
Potential Conflicts:
Recommendation:
The changes are orthogonal (identifiers vs literals) but share code in the parser. |
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Pre-compile the escape sequence regex patterns (reEscapedQuote, reDoubleQuote, reEscapedBackslash) in the parser initialization section instead of creating them on every parse. This improves performance by avoiding regex compilation in the hot path.Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
The yarn.lock file was inadvertently modified when npm install was run, changing registry URLs from registry.yarnpkg.com to registry.npmjs.org. This change was unrelated to the backslash escaping fix. Reverted to keep yarn.lock clean and focused on the actual changes. esbuild is still being used as before.Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Added comprehensive tests to verify the parser correctly handles the edge case where an escaped backslash is followed by an escaped quote. The implementation correctly processes '\\' (backslash + quote using SQL quote doubling) and '\\\\' (two backslashes + quote).Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Reverted yarn.lock to the state from commit7d6394c to avoid including unintended dependency updates. The file was modified when running npm install for testing.Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Changed the backslash replacement in the parser from using a string literal "\\" to String.fromCharCode(92) to ensure proper unescaping. This fixes the issue where backslash escape sequences were not being correctly converted (e.g., '\\' in SQL now correctly produces a single backslash character instead of two).Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Added test section F with extensive coverage of:- Multiple consecutive backslashes (1-5 backslashes)- Backslashes followed by doubled quotes- Doubled quotes in various positions- Mixed backslash and quote combinations- Double-quoted strings with backslashes- Complex multi-character escape sequencesTests verify the parser correctly handles all combinations of SQL escape sequences including quote doubling ('') and backslash escaping (\\).Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>Added two new test sections:- Test G: Double quote escaping tests, documenting that AlaSQL treats double quotes in double-quoted strings as literals (not escape sequences), which is correct behavior since double quotes are typically for identifiers in SQL- Test H: Parameterized queries with escaped data, testing round-trip behavior with single quotes, backslashes, and combinations passed as parameters and stored/retrieved from tablesAll 8 test sections now pass, providing comprehensive coverage of string literal escaping.Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
3da66cf to472f3a8Compare
Uh oh!
There was an error while loading.Please reload this page.
Description
The SQL parser was not properly unescaping
\\sequences in string literals, causingSELECT ASCII('\\')to fail or return incorrect results. This PR fixes the escape handling by usingString.fromCharCode(92)for proper backslash replacement and optimizes the parser for better performance by pre-compiling regex patterns.SQL Standard Alignment: This PR improves alignment with common SQL database behavior. While backslash escaping (
\\) is not part of core SQL-99, it's a widely supported extension in major databases (MySQL, PostgreSQL). The implementation correctly handles SQL-99's quote doubling ('') for escaping single quotes and adds functional backslash escaping as a common extension, making AlaSQL behave more consistently with PostgreSQL/MySQL.Changes
Parser Grammar (
src/alasqlparser.jison)reEscapedQuote,reDoubleQuote,reEscapedBackslash) in the parser initialization section to avoid creating regex objects on every parse operation (performance optimization)\\escape sequencesString.fromCharCode(92)instead of string literal"\\"to ensure proper unescapingsrc/alasqlparser.jsfrom updated grammarTests
test/test489.jswith comprehensive test coverage (8 test sections):'\\'in SQL → single backslash character)test/test376.jsThe test suite systematically builds from simple to complex cases, ensuring robust coverage of all escape sequence combinations including static SQL strings and parameterized queries.
Example
Performance
The regex patterns are now compiled once during parser initialization rather than being created on every parse operation, improving parser performance.
Checklist
Original prompt
✨ Let Copilot coding agentset things up for you — coding agent works faster and does higher quality work when set up for your repo.