Description
I found Calcite can’t execute queries like select nullif(count(distinct colunm),0) from table, and dumped ‘Plan after trimming unused fields’ like this:
LogicalProject(NAME=[$0], GENDER=[CASE(=($1, 0), null, CAST($2):BIGINT)]) LogicalAggregate(group=[{0}], agg#0=[COUNT(DISTINCT $1)], agg#1=[COUNT($1)]) LogicalProject(NAME=[$1], GENDER=[$3]) CsvTableScan(table=[[SALES, EMPS]], fields=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
Then, I checked SqlNullifFunction rewriteCall method which used SqlNode.clone(SqlParserPos pos) method to create SqlCase call.
Finally, I guess the root cause may be that the clone(SqlParserPos pos) method discard the functionQuantifier which holds such as ‘distinct’ symbol when create new SqlCall instance.