zapr-oss / druidry

Java based Druid Query Generator library
Apache License 2.0
193 stars 89 forks source link

Added expression support to some aggregators #135

Closed tunix closed 4 years ago

tunix commented 4 years ago
SELECT AVG(sum_sessionDurationInMs / "count")
FROM events
WHERE __time >= '2020-02-27' and __time < '2020-02-28'

When above query is translated to native JSON query, it converts sum_sessionDurationInMs / "count" into an expression and sets it into an aggregator:

{
  "queryType": "timeseries",
  "dataSource": {
    "type": "table",
    "name": "events"
  },
  "intervals": {
    "type": "intervals",
    "intervals": [
      "2020-02-27T00:00:00.000Z/2020-02-28T00:00:00.000Z"
    ]
  },
  "descending": false,
  "virtualColumns": [],
  "filter": null,
  "granularity": {
    "type": "all"
  },
  "aggregations": [
    {
      "type": "longSum",
      "name": "a0:sum",
      "fieldName": null,
      "expression": "(\"sum_sessionDurationInMs\" / \"count\")"
    },
    {
      "type": "count",
      "name": "a0:count"
    }
  ],
  "postAggregations": [
    {
      "type": "arithmetic",
      "name": "a0",
      "fn": "quotient",
      "fields": [
        {
          "type": "fieldAccess",
          "name": null,
          "fieldName": "a0:sum"
        },
        {
          "type": "fieldAccess",
          "name": null,
          "fieldName": "a0:count"
        }
      ],
      "ordering": null
    }
  ],
  "limit": 2147483647,
  "context": {
    "skipEmptyBuckets": true,
    "sqlOuterLimit": 100,
    "sqlQueryId": "5f8a13ff-1863-44fd-a2d1-8d96172cfae6"
  }
}
abhi-zapr commented 4 years ago

Hey, thanks for your contribution.

Next steps :

tunix commented 4 years ago

@abhi-zapr thanks for your comments. I've updated the PR as you requested. Please check.