cockroachdb / cockroachdb/cockroach

sql: discrepancy in SUBSTRING Regex Handling Between CockroachDB and PostgreSQL

Open
#140,383 6 comments 0 reactions 0 assignees View on GitHub
A-sql-pgcompat branch-release-24.3 C-bug O-community T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Problem**

When using SUBSTRING with the regex pattern ^(?:a*){1,1}?(.*){1,1}(?:g*)$ on the input 'abcdefg', CockroachDB returns 'bcdefg', while PostgreSQL returns the entire substring 'abcdefg'. This indicates a discrepancy in how the regex engines handle trailing quantifiers and captured groups.

**To Reproduce**

1. Run the following SQL query in CockroachDB:

```sql
SELECT SUBSTRING('abcdefg' FROM '^(?:a*){1,1}?(.*){1,1}(?:g*)$');
```
2. Observe the output:
```sql
substring
-------------
bcdefg
(1 row)
```
3. Run the same query in PostgreSQL.

4. PostgreSQL returns:
```sql
substring
-------------
abcdefg
(1 row)
```

**Expected behavior**
```sql
abcdefg
```

**Environment:**
- CockroachDB version 24.3.4
- Server OS: Linux/Ubuntu-22.04
- Client app cockroach sql

Jira issue: CRDB-47140

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.