pdeva opened a new issue #8643: incorrect result when using `NULLIF` in sql
URL: https://github.com/apache/incubator-druid/issues/8643
 
 
   
   
   ### Affected Version
   
   0.16
   
   using default settings for sql (that is no sql specific settings applied)
   
   ### Description
   
   This expression:
   
   ```sql
   SELECT (SUM(totalTime)/ SUM(thp)) as avgTime
   FROM "xx"
   ```
   
   results in:
   
   ```
   Unknown exception / / by zero / java.lang.ArithmeticException
   ```
   
   However, this expression:
   ```sql
   SELECT (SUM(totalTime)/ NULLIF(SUM(thp),0)) as avgTime
   FROM "xx"
   ```
   
   results in `0`.
   
   In the second expression, either:
   1. the divisor is being replaced to `0` due to the `nullif`, so dividing by 
`0` should result in an exception
   2. druid is replacing the `null` with a `0`, which should still result in a 
`divide by zero exception` like the first expression above.
   
   either way, this expression should be throwing an exception, but it somehow 
returns `0`.
   
   this is the `explain` for the latter sql:
   
   ```js
   {
     "queryType": "timeseries",
     "dataSource": {
       "type": "table",
       "name": "xx"
     },
     "intervals": {
       "type": "intervals",
       "intervals": [
         "-146136543-09-08T08:23:32.096Z/146140482-04-24T15:36:27.903Z"
       ]
     },
     "descending": false,
     "virtualColumns": [],
     "filter": null,
     "granularity": {
       "type": "all"
     },
     "aggregations": [
       {
         "type": "longSum",
         "name": "a0",
         "fieldName": "totalTime",
         "expression": null
       },
       {
         "type": "longSum",
         "name": "a1",
         "fieldName": "thp",
         "expression": null
       }
     ],
     "postAggregations": [
       {
         "type": "expression",
         "name": "p0",
         "expression": "(\"a0\" / case_searched((\"a1\" == 0),null,\"a1\"))",
         "ordering": null
       }
     ],
     "limit": 2147483647,
     "context": {
       "skipEmptyBuckets": true,
       "sqlQueryId": "6e5ac469-3b69-40e2-b84a-d5d0c873b9ef"
     }
   }
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to