[SERVER-26536] Regex containing an escaped | pipe symbol are not handled correctly Created: 09/Oct/16  Updated: 11/Oct/16  Resolved: 11/Oct/16

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Kai Orend Assignee: Kelsey Schubert
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-4431 mongo shell doesn't escape characters... Closed
Related
is related to SERVER-20432 $regex prefix search with escaped "|"... Closed
is related to SERVER-16622 RegEx query predicates using the | (v... Backlog
Operating System: ALL
Steps To Reproduce:

Setup test data:

for(var i=0; i < 100; i++) { db.regexTest.insertOne({name:"test|"+i})}

The follwoing query should only return a single document, but instead it returns all:

db.regexTest.find({name: { $regex: "^test\|18"}})

If a double escape is being used, then it matches only a single document:

db.regexTest.find({name: { $regex: "^test\\|18"}})

The results are the same if an index is being used.

However because of the related bug SERVER-16622 in the classification of the regex, the regex is classified as complex and the wrong index bounds are being used:

db.regexTest.createIndex({name:1})
db.regexTest.find({name: { $regex: "^test\\|18"}}).explain()
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "test.regexTest",
    "indexFilterSet": false,
    "parsedQuery": {
      "name": /^test\|18/
    },
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "filter": {
          "name": /^test\|18/
        },
        "keyPattern": {
          "name": 1
        },
        "indexName": "name_1",
        "isMultiKey": false,
        "multiKeyPaths": {
          "name": [ ]
        },
        "isUnique": false,
        "isSparse": false,
        "isPartial": false,
        "indexVersion": 2,
        "direction": "forward",
        "indexBounds": {
          "name": [
            "[\"\", {})",
            "[/^test\\|18/, /^test\\|18/]"
          ]
        }
      }
    },
    "rejectedPlans": [ ]
  },

Participants:

 Description   

If a regex query contains an escaped pipe symbol it is not handled as an escaped | but as an OR. Only if the pipe is double escaped ( \ \ | ) it is being handle as an escaped |.


Generated at Thu Feb 08 04:12:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.