mihaibudiu commented on code in PR #5095:
URL: https://github.com/apache/calcite/pull/5095#discussion_r3573112080


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -4059,6 +4054,54 @@ public static BigDecimal mod(BigDecimal b0, BigDecimal 
b1) {
     return bigDecimals[1];
   }
 
+  // PERCENTILE_CONT / PERCENTILE_DISC
+
+  /** Support the PERCENTILE_CONT aggregate function.
+   *
+   * <p>The {@code values} list must already be sorted according to the
+   * {@code WITHIN GROUP (ORDER BY ...)} clause. The fraction must be in the
+   * range 0 to 1 inclusive. The result is a linear interpolation between the
+   * two values that surround the desired position. */
+  public static double percentileCont(List<? extends Number> values,
+      double fraction) {
+    final int n = values.size();
+    if (n == 0) {
+      throw new NoSuchElementException(
+          "PERCENTILE_CONT is not defined on an empty group");
+    }
+    final double rank = fraction * (n - 1);
+    final int lo = (int) Math.floor(rank);
+    final int hi = (int) Math.ceil(rank);
+    final double loValue = values.get(lo).doubleValue();

Review Comment:
   I think this won't work for DECIMAL values with large precisions, since 
double has only 53 bits of mantissa, which is insufficient to store large 
decimal values without a large precision loss. If the decimals are close in 
values, the error will be very large.
   
   I think using BigDecimal is probably a better approach.



##########
core/src/test/resources/sql/agg.iq:
##########
@@ -3844,6 +3844,52 @@ select distinct sum(deptno + '1') as deptsum from dept 
order by 1;
 
 !ok
 
+# [CALCITE-6767] PERCENTILE_CONT/PERCENTILE_DISC syntax not supported.
+# These sql was validated in PostgreSQL

Review Comment:
   sql -> sql programs
   was -> were



-- 
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]

Reply via email to