Julian Hyde, I have created a fix for this in https://github.com/julianhyde/calcite/pull/9/commits/66339365bd40119d7bba5fa3ba42e5bbac6835d0 . I created a PR against
CALCITE-1578 too. Could you review the new code and merge it into your branch?
The PR contains tests. However, one of the tests does not currently pass, as the Sort operation is not pushed into Druid. I double-checked and logic is correct, hence I think it has to do with the optimizer and the cost of the expression.
For those new tests in the PR, which are transformed into timeseries queries, I do not run the query against Druid: in my environment, Druid crashes (probably not enough memory to execute timeseries + sort).