Uploaded image for project: 'Couchbase Server'
  1. Couchbase Server
  2. MB-38564

ANSI JOIN: wrong result possible with left join and IS MISSING predicate is present on inner table

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • Major
    • 6.6.0, 7.0.0
    • 6.0.4, 6.5.0, Cheshire-Cat
    • query
    • Untriaged
    • No

    Description

      ANSI LEFT JOIN may return incorrect result:

      insert into default (key, value) values("K01", { "c11": 1, "c12": 11, "type": "left" });
      insert into default (key, value) values("K02", { "c11": 2, "c12": 12, "type": "left" });
       
      insert into default (key, value) values("K03", { "c21": 1, "c22": 21, "type": "right" });
       
      CREATE INDEX ix_right ON default(c21, c22) WHERE type = "right";
       
      SELECT l.c12, r.c22
      FROM default l LEFT JOIN default r
                  ON l.c11 = r.c21 AND r.type = "right"
      WHERE l.type = "left" AND r.c22 IS MISSING;
      
      

      This query should return a single document:

       "results": [
          {
              "c11": 2
          }
          ],
      
      

      However with the above index defined it returned two documents:

        "results": [
          {
              "c11": 1
          },
          {
              "c11": 2
          }
          ],
      

      The wrong result happens when:

      • ANSI LEFT JOIN is used (l LEFT JOIN r)
      • there is a IS MISSING predicate in the WHERE clause for r (r.c22 IS MISSING)
      • the field with IS MISSING predicate (c22) is present in the index used for the right-hand side

      Work around:

      use a field that is not present in the index keys in the IS MISSING predicate, or use the entire document in the IS MISSING predicate (r IS MISSING instead of r.c22 IS MISSING). This makes right side not covered:

      SELECT l.c12, r.c22
      FROM default l LEFT JOIN default r
                  ON l.c11 = r.c21 AND r.type = "right"
      WHERE l.type = "left" AND r IS MISSING;
      
      

      Attachments

        Issue Links

          Activity

            People

              evgeny.makarenko Evgeny Makarenko (Inactive)
              bingjie.miao Bingjie Miao
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                PagerDuty