Skip to content

Postgres: Support SUBSTRING SIMILAR/ESCAPE syntax and reversed FOR/FROM order - #2494

Open
BenSatori wants to merge 7 commits into
apache:mainfrom
BenSatori:postgres-substring-similar-escape
Open

BenSatori wants to merge 7 commits into
apache:mainfrom
BenSatori:postgres-substring-similar-escape

Conversation

@BenSatori

Copy link
Copy Markdown
Contributor

Fixes parsing of two legal Postgres SUBSTRING forms:

  • SUBSTRING('hello world' FOR 5 FROM 1) (FOR before FROM)
  • SUBSTRING('hello world' SIMILAR '%#"o w#"%' ESCAPE '#')

See https://www.postgresql.org/docs/current/functions-string.html

Both previously failed with a parser error. Adds a similar flag to Expr::Substring to track the SIMILAR ... ESCAPE display form, and allows FOR to be parsed before FROM (normalized to FROM ... FOR on output).

Generated with AI assistance (GitHub Copilot).

@codecov-commenter

codecov-commenter commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.09%. Comparing base (5cad906) to head (ebab908).

Files with missing lines Patch % Lines
src/parser/mod.rs 72.00% 0 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2494      +/-   ##
==========================================
- Coverage   81.09%   81.09%   -0.01%     
==========================================
  Files          42       42              
  Lines       33684    33719      +35     
  Branches    33684    33719      +35     
==========================================
+ Hits        27316    27344      +28     
  Misses       2798     2798              
- Partials     3570     3577       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/parser/mod.rs Outdated
Comment thread src/parser/mod.rs
Comment thread tests/sqlparser_common.rs
@LucaCappelletti94 LucaCappelletti94 added the waiting on contributor The review needs further refinements by its author label Sep 24, 2026
@LucaCappelletti94 LucaCappelletti94 added waiting on reviewer PR needs to be reviewed and removed waiting on contributor The review needs further refinements by its author labels Sep 28, 2026
Comment thread src/ast/mod.rs
Comment on lines +1219 to +1222
/// true if the expression uses the `SUBSTRING(expr SIMILAR pattern ESCAPE escape)` syntax,
/// in which case `substring_from` holds the pattern and `substring_for` holds the escape.
/// This flag is used for formatting.
similar: bool,

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.

Would a single syntax enum be better here, replacing special and similar with one variant each for the comma, FROM/FOR, FOR/FROM and SIMILAR/ESCAPE forms? SUBSTRING('1' FOR 3 FROM 1) now prints as SUBSTRING('1' FROM 1 FOR 3), which breaks the round trip the README promises, and a third flag for the order would leave combinations such as special with similar representable.

Comment thread tests/sqlparser_common.rs
Comment on lines +8434 to +8437
one_statement_parses_to(
"SELECT SUBSTRING('1' FOR 3 FROM 1)",
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
);

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.

You may want to pin the reversed order as a round trip. On the current head it fails with left: "SELECT SUBSTRING('1' FOR 3 FROM 1)", right: "SELECT SUBSTRING('1' FROM 1 FOR 3)".

Suggested change
one_statement_parses_to(
"SELECT SUBSTRING('1' FOR 3 FROM 1)",
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
);
verified_stmt("SELECT SUBSTRING('1' FOR 3 FROM 1)");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PostgreSQL waiting on reviewer PR needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants