Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

LEFT JOIN with WHERE doesn't filter the result as expected #124

Open
penghuo opened this issue Jul 19, 2019 · 1 comment
Open

LEFT JOIN with WHERE doesn't filter the result as expected #124

penghuo opened this issue Jul 19, 2019 · 1 comment
Assignees
Labels
bug Something isn't working SQL

Comments

@penghuo
Copy link
Contributor

penghuo commented Jul 19, 2019

Issue
The WHERE clause doesn't filter out the null value from right table after LEFT JOIN.

LEFT JOIN query

GET /_opendistro/_sql/?
{
  "query": "SELECT * FROM dog D LEFT JOIN account A ON D.holdersName = A.firstname WHERE A.firstname = 'Hattie'"
}

Sample Data

// dog index
{"index":{"_type": "dog", "_id":"1"}}
{"dog_name":"rex","holdersName":"Daenerys","age":2}
{"index":{"_type": "dog", "_id":"6"}}
{"dog_name":"snoopy","holdersName":"Hattie","age":4}

// account index
{"index":{"_type": "account", "_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36}

Actual Result

    "hits": [
      {
        "_type": "dog|account",
        "_id": "6|6",
        "_score": "-Infinity",
        "_source": {
          "D.age": 4,
          "D.holdersName": "Hattie",
          "A.lastname": "Bond",
          "D.dog_name": "snoopy",
          "A.balance": 5686,
          "A.firstname": "Hattie",
          "A.age": 36,
        }
      },
      {
        "_type": "dog|null",
        "_id": "1|0",
        "_score": "-Infinity",
        "_source": {
          "D.dog_name": "rex",
          "D.age": 2,
          "D.holdersName": "Daenerys"
        }
      }
    ]

Expected Result

    "hits": [
      {
        "_type": "dog|account",
        "_id": "6|6",
        "_score": "-Infinity",
        "_source": {
          "D.age": 4,
          "D.holdersName": "Hattie",
          "A.lastname": "Bond",
          "D.dog_name": "snoopy",
          "A.balance": 5686,
          "A.firstname": "Hattie",
          "A.age": 36,
        }
      }
    ]
@penghuo penghuo added the bug Something isn't working label Jul 19, 2019
@dai-chen dai-chen self-assigned this Jul 19, 2019
@dai-chen
Copy link
Member

dai-chen commented Jul 19, 2019

Thanks for raising the issue! The root cause is there is no physical operator for WHERE conditions on top of Join operator to perform the post-processing for now. We missed this semantic error in the testing because we used and assumed existing Nested Loop join is correct (which actually has the same issue) and didn't realized the difference in other RDBMS.

To fix the issue, a new operator should be included in physical plan in which WHERE conditions be translated into in-memory operation in Java code. To make it simple, we can start with support for very basic operation such as =, <, > etc.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working SQL
Projects
None yet
Development

No branches or pull requests

2 participants