amaliujia commented on a change in pull request #2447:
URL: https://github.com/apache/calcite/pull/2447#discussion_r671488569
##########
File path: core/src/test/resources/sql/agg.iq
##########
@@ -2990,4 +2990,146 @@ from (select 1 as a, 2 as b, 3 as c);
!ok
+# [CALCITE-3661] Add MODE aggregate function
+
+# mode function applied to input value and return the most frequent value.
+select MODE(gender)
+from emp;
++--------+
+| EXPR$0 |
++--------+
+| F |
++--------+
+(1 row)
+
+!ok
+
+# mode function applied to distinct value and return the first input value.
+select MODE(distinct gender)
+from emp;
++--------+
+| EXPR$0 |
++--------+
+| F |
++--------+
+(1 row)
+
+!ok
+
+# mode function applied to filter and input value is not NULL.
+select MODE(gender)
+from emp
+where deptno <= 20;
++--------+
+| EXPR$0 |
++--------+
+| M |
++--------+
+(1 row)
+
+!ok
+
+# mode function applied to filter and input value is NULL.
+select MODE(gender)
+from emp
+where deptno > 60;
++--------+
+| EXPR$0 |
++--------+
+| |
++--------+
Review comment:
Interesting. So if the input is only NULL so nothing will be returned?
##########
File path:
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
##########
@@ -1241,6 +1243,83 @@ public static Expression multiplyDivide(Expression e,
BigDecimal multiplier,
}
}
+ /** Implementor for the {@code MODE} aggregate function. */
+ static class ModeImplementor extends StrictAggImplementor {
+ @Override protected void implementNotNullReset(AggContext info,
+ AggResetContext reset) {
+ // acc[0] = null;
+ reset.currentBlock().add(
+ Expressions.statement(
+ Expressions.assign(reset.accumulator().get(0),
+ Expressions.constant(null))));
+ // acc[1] = new HashMap<>();
+ reset.currentBlock().add(
+ Expressions.statement(
+ Expressions.assign(reset.accumulator().get(1),
+ Expressions.new_(HashMap.class))));
+ // acc[2] = Long.valueOf(0);
+ reset.currentBlock().add(
+ Expressions.statement(
+ Expressions.assign(reset.accumulator().get(2),
+ Expressions.constant(0, Long.class))));
+ }
+
+ @Override protected void implementNotNullAdd(AggContext info,
+ AggAddContext add) {
+ Expression currentArg = add.arguments().get(0);
Review comment:
Nit: this will be very useful if you can also add equivalent code/pesudo
code into comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]