Skip to content

Fix Oracle regexp newline matching semantics - #1489

Merged
gaoxueyu merged 4 commits into
IvorySQL:masterfrom
ai-yang:fix/1481-regexp-count-newlines
Aug 14, 2026
Merged

gaoxueyu merged 4 commits into
IvorySQL:masterfrom
ai-yang:fix/1481-regexp-count-newlines

Conversation

@ai-yang

@ai-yang ai-yang commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Stop changing REGEXP_COUNT totals based on newlines outside the matched
    substrings.
  • Add REG_NLDOT, which excludes newline from . without excluding it from
    negated bracket expressions.
  • Handle Oracle's documented m and n behaviors centrally in
    ora_parse_re_flags(): m enables multiline anchors, while n allows dot
    to match newline.
  • Share the behavior across REGEXP_COUNT, REGEXP_SUBSTR, REGEXP_INSTR,
    and REGEXP_REPLACE, with regression coverage for default, c, n, and
    m modes.

Root cause

ora_regexp_count() counted every newline in the complete source and adjusted
the regex engine's result when the pattern began with . or ^. Newlines
outside the matched substrings therefore changed the returned count.

PostgreSQL's newline-sensitive regex mode combines dot, bracket-expression,
and anchor behavior. Oracle's m and n options control multiline anchors
and dot-newline matching separately. The existing REG_NLSTOP flag could not
provide the required dot-only behavior because it also prevents negated
bracket expressions from matching newline.

This change adds a dot-specific regex flag and applies the Oracle option
semantics in the shared Oracle flag parser used by all four functions.

Review update

  • Removed the unsupported last-option-wins interpretation and the associated
    combined-option assertions and regression cases.
  • Kept m and n as independent controls for their documented behaviors.
  • Rechecked all callers and the adjacent regex-engine flag handling; no other
    PostgreSQL, HBA, JSONPath, or REGEXP_LIKE path is changed by the new flag.

Validation

Run in the Docker development environment:

  • make -j2 -C contrib/ivorysql_ora oracle-check ORA_REGRESS=ora_character_datatype_functions
    • 1/1 test passed
  • make -j2 -C contrib/ivorysql_ora oracle-check
    • 24/24 tests passed
  • make -j2 -C src/test/modules/test_regex check
    • 2/2 tests passed
  • make -j2 check
    • 244/244 tests passed
  • make -j2 oracle-check ORA_REGRESS=alter_index EXTRA_INSTALL=contrib/amcheck
    • 254/254 tests passed
  • Manual Oracle-mode checks covered Unicode text, unrelated leading/middle/
    trailing newlines, negated classes, regex-cache isolation, the original
    issue reproduction, and a 1,000-match stress case.
  • git diff --check

Closes #1481

Compile Oracle REGEXP_COUNT patterns with independent newline controls
instead of adjusting match totals using every newline in the source. This
keeps dot matching, negated character classes, and multiline anchors
consistent with Oracle behavior.

Add regression coverage for unrelated newlines, the m/n option matrix,
negated bracket expressions, and multiline anchors.

Assisted-by: OpenAI:gpt-5
Percentage of AI-generated code: 100%

Signed-off-by: Rui <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Oracle regular-expression handling now separates dot-newline matching from multiline anchors. REGEXP_COUNT handles trailing empty matches. Regression tests cover newline behavior and option ordering across Oracle regular-expression functions.

Changes

Oracle regular-expression newline semantics

Layer / File(s) Summary
Implement independent newline flags
src/include/regex/regex.h, src/backend/regex/regcomp.c, src/backend/regex/README, src/backend/utils/adt/regexp.c
Adds REG_NLDOT. Oracle m and n options independently control anchors and dot-newline matching. The last option takes precedence.
Apply newline semantics in REGEXP_COUNT
contrib/ivorysql_ora/src/builtin_functions/character_datatype_functions.c
Tracks trailing empty matches, parses flags before null-input handling, and excludes a trailing empty line from bare multiline-anchor matches.
Validate newline behavior across functions
contrib/ivorysql_ora/sql/ora_character_datatype_functions.sql, contrib/ivorysql_ora/expected/ora_character_datatype_functions.out
Adds regression coverage for newline placement, dot matching, multiline anchors, negated classes, and m/n ordering across Oracle regular-expression functions.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SQL caller
  participant Oracle regexp flags
  participant Regex compiler
  participant Oracle regexp function
  SQL caller->>Oracle regexp flags: Supply pattern and match parameters
  Oracle regexp flags->>Regex compiler: Set REG_NLDOT and REG_NLANCH
  Regex compiler->>Oracle regexp function: Produce newline-aware matches
  Oracle regexp function->>SQL caller: Return count, position, substring, or replacement
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes fix REGEXP_COUNT results affected by unrelated newlines and add regression coverage for issue #1481.
Out of Scope Changes check ✅ Passed The core regex, flag parsing, and regression test changes directly support the stated Oracle newline semantics objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: fixing Oracle regular expression newline matching semantics.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ai-yang
ai-yang marked this pull request as ready for review August 1, 2026 03:51
@coderabbitai coderabbitai Bot mentioned this pull request Aug 2, 2026
@bigplaice bigplaice self-assigned this Aug 3, 2026
@bigplaice

Copy link
Copy Markdown
Collaborator

Thanks for contribution. We'll schedule a review for the patch.

Handle Oracle m and n match parameters in ora_parse_re_flags so
REGEXP_COUNT, REGEXP_SUBSTR, REGEXP_INSTR, and REGEXP_REPLACE share the
same dot and multiline-anchor semantics.

Add regression coverage for the shared default, c, n, m, and mn behavior.

Assisted-by: OpenAI:gpt-5
Percentage of AI-generated code: 100%

Signed-off-by: Rui <[email protected]>
@ai-yang ai-yang changed the title Fix REGEXP_COUNT newline matching Fix Oracle regexp newline matching semantics Aug 4, 2026
Treat m and n as mutually overriding Oracle newline modes, with the
last occurrence controlling dot and anchor behavior. This makes mn use
n mode and nm use m mode across all shared regexp callers.

Extend REGEXP_COUNT, REGEXP_SUBSTR, REGEXP_INSTR, and REGEXP_REPLACE
regression matrices with default, c, n, m, mn, and nm coverage.

Assisted-by: OpenAI:gpt-5
Percentage of AI-generated code: 100%

Signed-off-by: Rui <[email protected]>
Comment thread src/backend/utils/adt/regexp.c Resolved
Remove the unsupported last-option-wins interpretation for combined m and n match parameters. Keep m and n as independent controls for multiline anchors and dot-newline matching, and drop combined-mode expectations because Oracle behavior is not documented consistently.

Retain default, c, n, and m coverage across REGEXP_COUNT, REGEXP_SUBSTR, REGEXP_INSTR, and REGEXP_REPLACE.

Assisted-by: OpenAI:gpt-5
Percentage of AI-generated code: 100%

Signed-off-by: Rui <[email protected]>
@gaoxueyu
gaoxueyu merged commit c3529ba into IvorySQL:master Aug 14, 2026
9 checks passed
hs-liuxh pushed a commit to hs-liuxh/IvorySQL that referenced this pull request Sep 2, 2026
…ewlines

Fix Oracle regexp newline matching semantics

(cherry picked from commit c3529ba)
hs-liuxh pushed a commit to hs-liuxh/IvorySQL that referenced this pull request Sep 2, 2026
…ewlines

Fix Oracle regexp newline matching semantics

(cherry picked from commit c3529ba)
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.

REGEXP_COUNT returns wrong value

3 participants