[ https://issues.apache.org/jira/browse/CALCITE-1740?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15963471#comment-15963471 ]
Julian Hyde edited comment on CALCITE-1740 at 4/10/17 8:50 PM: --------------------------------------------------------------- I do not agree that the "distinct" flag should be part of the function. It is more like an argument to the function call, or an instruction to the "GROUP BY" operator for how to pass values to the aggregate function. If you have an aggregate function f (even a user-defined one) then the result of "f(distinct x)" is always the same as calling "f\(x)" having eliminated the duplicate values of "x" first. If we went with your proposal we'd have to have two variants of each aggregate function, and an aggregate function would have to know how to find the distinct or non-distinct version of itself. The solution is to fix the rule that converts {{AggregateCall}}s to {{AggregateCall}}s, and make sure that it preserves the {{distinct}} field. Run {{RelOptRulesTest}} and put a break point in the {{AggregateCall}} constructor and it should become clearer. was (Author: julianhyde): I do not agree that the "distinct" flag should be part of the function. It is more like an argument to the function call, or an instruction to the "GROUP BY" operator for how to pass values to the aggregate function. If you have an aggregate function f (even a user-defined one) then the result of "f(distinct x)" is always the same as calling "f(x)" having eliminated the duplicate values of "x" first. If we went with your proposal we'd have to have two variants of each aggregate function, and an aggregate function would have to know how to find the distinct or non-distinct version of itself. The solution is to fix the rule that converts {{AggregateCall}}s to {{AggregateCall}}s, and make sure that it preserves the {{distinct}} field. Run {{RelOptRulesTest}} and put a break point in the {{AggregateCall}} constructor and it should become clearer. > Distinct aggregate flag in window function > ------------------------------------------ > > Key: CALCITE-1740 > URL: https://issues.apache.org/jira/browse/CALCITE-1740 > Project: Calcite > Issue Type: Bug > Components: core > Reporter: radu > Assignee: Julian Hyde > Labels: build, windows > > When parsing queries such as > SELECT B1, SUM(DISTINCT B2) OVER (ORDER BY B4 RANGE BETWEEN INTERVAL '10' > SECOND PRECEDING AND CURRENT ROW) FROM T > The aggregates in the LogicalWindow do not have any marker of being distinct. > isDistinct() flag is not set. Probably some rule(s) silently swallow the > DISTINCT keyword > The LogicalWindow object that result is > LogicalWindow(window#0=[window(partition {} order by [2] range between $3 > PRECEDING and CURRENT ROW aggs [COUNT($1), $SUM0($1)])]) -- This message was sent by Atlassian JIRA (v6.3.15#6346)