sql icon indicating copy to clipboard operation
sql copied to clipboard

Cannot use the IN clause within the SUM method

Open idamerce opened this issue 1 year ago • 1 comments

Describe the bug Hello everyone, Is there anyone here familiar with using SQL on OpenSearch? We are encountering errors when running our queries and would greatly appreciate any assistance or insights you could provide.

To Reproduce Specifically, we are facing issues with queries of type :

this works:

SELECT count(*) AS fps_count
FROM `my-alias-db`
WHERE status IN ('AA','BB')

but this does not (needed for creating new metrics in superset)

SELECT SUM(CASE WHEN status IN ('AA', 'BB') THEN 1 ELSE 0 END) AS status_sum
FROM 'my-alias-db'

Expected behavior I'm expecting to have same results but I'm getting this error: odelasticsearch error: Error (NullPointerException): {'error': {'reason': 'Invalid SQL query', 'details': '', 'type': 'NullPointerException'}, 'status': 400}

Host/Environment (please complete the following information):

  • OS: [ opensearch on aws]

Additional context need to use my query for creating new metrics on superset

idamerce avatar Aug 06 '24 13:08 idamerce

[Catch All Triage - 1, 2, 3, 4, 5]

dblock avatar Aug 26 '24 16:08 dblock

@LantaoJin could you check whether this will be supported with the Calcite work

seankao-az avatar Apr 29 '25 17:04 seankao-az

@LantaoJin could you check whether this will be supported with the Calcite work

@seankao-az it works w/wo Calcite

POST /_plugins/_ppl
{
  "query" : """
source=state_country | stats sum(case(country in ('USA', 'England'), 1 else 0)) as status_sum
  """
}

returns

{
  "schema": [
    {
      "name": "status_sum",
      "type": "int"
    }
  ],
  "datarows": [
    [
      3
    ]
  ],
  "total": 1,
  "size": 1
}

Even I cannot reproduce in SQL with main branch.

POST /_plugins/_sql
{
  "query" : """
  SELECT SUM(CASE WHEN country IN ('USA', 'England') THEN 1 ELSE 0 END) AS status_sum
FROM state_country
  """
}

returns

{
  "schema": [
    {
      "name": "SUM(CASE WHEN country IN ('USA', 'England') THEN 1 ELSE 0 END)",
      "alias": "status_sum",
      "type": "integer"
    }
  ],
  "datarows": [
    [
      3
    ]
  ],
  "total": 1,
  "size": 1,
  "status": 200
}

LantaoJin avatar May 19 '25 02:05 LantaoJin

@idamerce what's the version of OpenSearch in your report?

LantaoJin avatar May 19 '25 02:05 LantaoJin

feel free to reopen the issue

penghuo avatar May 19 '25 18:05 penghuo