Skip to content

[BUG] SQL confusing type conversion for TIMESTAMP when using join #3204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dhvcc opened this issue Dec 17, 2024 · 3 comments
Open

[BUG] SQL confusing type conversion for TIMESTAMP when using join #3204

dhvcc opened this issue Dec 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@dhvcc
Copy link

dhvcc commented Dec 17, 2024

Describe the bug

So given 2 indexes, with a matching field (to be used in a JOIN) and a timestamp field. When querying without a join, a WHERE clause like WHERE t.timestamp > timestamp('2001-05-07 00:00:00') will work and WHERE t.timestamp > 0 won't work since it's a TIMESTAMP field, that's what opensearch tell you

{\n  \"error\": {\n    \"reason\": \"Invalid SQL query\",\n    \"details\": \"> function expected {[BYTE,BYTE],[SHORT,SHORT],[INTEGER,INTEGER],[LONG,LONG],[FLOAT,FLOAT],[DOUBLE,DOUBLE],[STRING,STRING],[BOOLEAN,BOOLEAN],[DATE,DATE],[TIME,TIME],[DATETIME,DATETIME],[TIMESTAMP,TIMESTAMP],[INTERVAL,INTERVAL],[STRUCT,STRUCT],[ARRAY,ARRAY]}, but get [TIMESTAMP,INTEGER]\",\n    \"type\": \"ExpressionEvaluationException\"\n  },\n  \"status\": 400\n}

But, if you're using a JOIN, the behavior reverses, while the error becomes even more confusing than just "can't match type"
WHERE t.timestamp > timestamp('2001-05-07 00:00:00') now will not work and WHERE t.timestamp > 0 will with error

{\n  \"error\": {\n    \"reason\": \"Invalid SQL query\",\n    \"details\": \"Cannot invoke \\\"String.startsWith(String)\\\" because \\\"fieldName\\\" is null\",\n    \"type\": \"NullPointerException\"\n  },\n  \"status\": 400\n}

Related component

Search

To Reproduce

  1. Go to 'Dashboard -> Management -> Dev Tools'
  2. Paste the example below
  3. Run those queries one by one and observe the behavior
PUT /a
{
  "mappings": {
    "properties": {
      "some_id": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "date"
      }
    }
  }
}

PUT /b
{
  "mappings": {
    "properties": {
      "some_id": {
        "type": "keyword"
      },
      "timestamp": {
        "type": "date"
      }
    }
  }
}

POST /a/_doc
{
  "some_id": "a1",
  "timestamp": "2024-12-15T10:00:00Z"
}

POST /b/_doc
{
  "some_id": "a1",
  "timestamp": "2024-12-15T11:00:00Z"
}

-- NO JOIN - TIMESTAMP works
POST /_plugins/_sql 
{
"query": "SELECT b.timestamp from b                                                WHERE b.timestamp > timestamp('2001-05-07 00:00:00');"
}
-- NO JOIN - INTEGER doesn't work
-- {\n  \"error\": {\n    \"reason\": \"Invalid SQL query\",\n    \"details\": \"> function expected {[BYTE,BYTE],[SHORT,SHORT],[INTEGER,INTEGER],[LONG,LONG],[FLOAT,FLOAT],[DOUBLE,DOUBLE],[STRING,STRING],[BOOLEAN,BOOLEAN],[DATE,DATE],[TIME,TIME],[DATETIME,DATETIME],[TIMESTAMP,TIMESTAMP],[INTERVAL,INTERVAL],[STRUCT,STRUCT],[ARRAY,ARRAY]}, but get [TIMESTAMP,INTEGER]\",\n    \"type\": \"ExpressionEvaluationException\"\n  },\n  \"status\": 400\n}
POST /_plugins/_sql 
{
"query": "SELECT b.timestamp from b                                                WHERE b.timestamp > 0;"
}

-- JOIN - INTEGER works
POST /_plugins/_sql 
{
"query": "SELECT b.timestamp from b JOIN a ON a.some_id = b.some_id WHERE b.timestamp > 0;"
}
-- JOIN - TIMESTAMP doesn't work
-- {\n  \"error\": {\n    \"reason\": \"Invalid SQL query\",\n    \"details\": \"Cannot invoke \\\"String.startsWith(String)\\\" because \\\"fieldName\\\" is null\",\n    \"type\": \"NullPointerException\"\n  },\n  \"status\": 400\n}
POST /_plugins/_sql 
{
"query": "SELECT b.timestamp from b JOIN a ON a.some_id = b.some_id WHERE b.timestamp > timestamp('2001-05-07 00:00:00');"
}

Expected behavior

Expected to see timestamp filtering work when using a join. Also, since it's a timestamp, i'd also expect INTEGER to work both ways

Additional Details

Plugins
AFAIK, sql, it's a default OpenSearch AWS installation

Screenshots
I've put the whole repro above with error results, so don't really need it

Host/Environment (please complete the following information):

  • Environment: AWS
  • Version: OpenSearch 2.17 (latest)
  • Service software version: OpenSearch_2_17_R20241112-P3 (latest)
@dhvcc dhvcc added bug Something isn't working untriaged labels Dec 17, 2024
@dhvcc
Copy link
Author

dhvcc commented Dec 17, 2024

Re-raised the same bug as here (opensearch-project/OpenSearch#16847), since it seems to be a more suitable place

@Swiddis
Copy link
Collaborator

Swiddis commented Dec 17, 2024

Duplicate of #1545 and #3159. #3160 holds the fix, it's set to be shipped in 2.19.

@Swiddis Swiddis closed this as completed Dec 17, 2024
@Swiddis Swiddis added the v2.19.0 Issues targeting release v2.19.0 label Dec 17, 2024
@Swiddis Swiddis reopened this May 6, 2025
@Swiddis Swiddis added calcite calcite migration releated and removed untriaged v2.19.0 Issues targeting release v2.19.0 labels May 7, 2025
@Swiddis
Copy link
Collaborator

Swiddis commented May 8, 2025

Turns out the other two issues aren't quite the same, the issue here is more to do with a difference in validation/supported function logic between the V1 and V2 engines. Out of scope for the current system, but this should be solved by the Calcite initiative.

@ Calcite team: I left a TODO for this test 4a83cd7 in #3613, remove the Ignore to resolve.

@LantaoJin LantaoJin removed the calcite calcite migration releated label May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants