Skip to content

make grep() always treat its pattern as a regular expression - #7723

Open
philrz wants to merge 1 commit into
mainfrom
grep-always-regexp
Open

philrz wants to merge 1 commit into
mainfrom
grep-always-regexp

Conversation

@philrz

@philrz philrz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

As discussed in #7718, grep() now always treats its pattern as a regular expression, as #6115 described. A pattern with no metacharacters, like "BAR", used to be a case-insensitive substring search, as in a keyword search. It's now case-sensitive, like any other regular expression, regexp() and LIKE. To ignore case, start the pattern with (?i). A pattern computed from the input, as in grep(p, this), used to be a case-insensitive substring search even when it had metacharacters, so a p of "foo|baz" didn't match "baz". It's now a regular expression too, and an invalid one gives an error value. Keyword search (search foo or ? foo) still ignores case, so the search docs now give its grep equivalent as grep("(?i)foo", this).

Fixes #6200

Trying it out

On current tip of main, commit 431a0c4:

$ super -version && super -c 'values "foobar" | values {plain:grep("BAR", this), ignore_case:grep("(?i)BAR", this)}'
Version: v0.4.0-121-g431a0c470
{plain:true,ignore_case:true}

$ echo '{p:"foo|baz",s:"baz"}' | super -c 'values grep(p, s)' -
false

On this branch, commit 74c90c9:

$ super -version && super -c 'values "foobar" | values {plain:grep("BAR", this), ignore_case:grep("(?i)BAR", this)}'
Version: v0.4.0-122-g74c90c99a
{plain:false,ignore_case:true}

$ echo '{p:"foo|baz",s:"baz"}' | super -c 'values grep(p, s)' -
true

Performance

A plain-string pattern, or one that is just (?i) and a plain string, now skips the regexp engine, and both still let a BSUP scan skip frames that can't match, as a plain-string grep() did before. Other regular expressions and LIKE work as before. Median wall times, MacBook M5 Pro, the gha dataset (4.4M records) as BSUP:

Query main this branch
gha Search benchmark: grep('in case you have any feedback 😊', payload.pull_request.body) 0.32 s 0.17 s
gha Search+ benchmark: the same pattern over this 3.90 s 1.63 s
grep('github', this) 3.72 s 3.19 s
grep('(?i)github', this), vs. main's case-insensitive grep('github', this) 3.73 s 3.28 s
grep('feedback.*😊', this) (unchanged path) 3.18 s 3.18 s

This also fixes three wrong results. A constant pattern like "^foo$" was treated as a substring search for foo, so it matched "xfooy". A plain-string grep() over a computed value that includes this, e.g., grep('zzz', [this, 'zzz']), gave no results on a BSUP file, because the scan skipped every frame that didn't contain zzz; the frame filter now applies only when grep() searches stored data. And an anchored case-insensitive pattern, e.g., grep('(?i)^bar', s), could drop matching rows on a BSUP file, because its min/max filter compared case-sensitively.

A computed pattern is now compiled as a regular expression whenever it differs from the previous row's. When every row has a different pattern, that costs about 0.45 µs per row (200,000 rows: 21 ms on main, 109 ms here).

Testing

  • New stanzas in grep.yaml cover constant and computed patterns, case sensitivity, anchors, a non-ASCII letter in a (?i) pattern and invalid computed patterns. The new bsup/ztests/grep-pushdown.yaml covers frame skipping for plain and (?i) patterns, and none for a computed value. A new case in pruner-regexp.yaml covers the (?i)^ min/max filter. All of them fail on main. A LIKE case without wildcards in like.yaml and the new grep-nfc.yaml (a decomposed pattern read from CSV) guard behavior main already has. In switch.yaml, the DAG output now shows regexp_search(...) for grep().
  • make test-unit, make test-system and the book's mdtests pass.

grep() treated a pattern with no regular expression metacharacters as
a case-insensitive substring search, and a computed pattern as one
even when it had metacharacters. Treat every pattern as a regular
expression, so matching is case-sensitive unless the pattern starts
with (?i). Plain and (?i) literal patterns skip the regexp engine and
keep the BSUP search filter.

Fixes #6200
@philrz philrz self-assigned this Nov 25, 2026
@philrz
philrz requested a review from a team November 25, 2026 01:06
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.

grep still seems to be doing case insensitive substring searches

1 participant