I've added a pull request that supports the AS MEASURE construct so that you can define measures in a subquery or view (and then use them in an enclosing query).
Can people please review https://github.com/apache/calcite/pull/3837. Not everything works yet (I describe future changes in https://issues.apache.org/jira/browse/CALCITE-4496) but a lot of things do. I would be especially interested in a discussion of how to implement the rewrites for measures - by means of planner rules, metadata (RelMdMeasure), or some combination. You can find the theory in our paper, https://dl.acm.org/doi/pdf/10.1145/3626246.3653374. Julian
