You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2022/08/09 11:37:15 UTC

[GitHub] [calcite] itiels opened a new pull request, #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

itiels opened a new pull request, #2868:
URL: https://github.com/apache/calcite/pull/2868

   Changed the return type of PERCENTILE_DISC to the type of the `order_by_expression`.
   
   Ticket: [CALCITE-5230](https://issues.apache.org/jira/browse/CALCITE-5230)


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] itiels commented on a diff in pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
itiels commented on code in PR #2868:
URL: https://github.com/apache/calcite/pull/2868#discussion_r1020077749


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java:
##########
@@ -2303,7 +2303,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
    */
   public static final SqlAggFunction PERCENTILE_DISC =
       SqlBasicAggFunction
-          .create(SqlKind.PERCENTILE_DISC, ReturnTypes.DOUBLE,
+          .create(SqlKind.PERCENTILE_DISC, ReturnTypes.PERCENTILE_DISC,

Review Comment:
   Thanks, fixed it



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] julianhyde commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
julianhyde commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1330281838

   @libenchao when you merge, please change the commit message to something that doesn’t start with “fix”


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] julianhyde commented on a diff in pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
julianhyde commented on code in PR #2868:
URL: https://github.com/apache/calcite/pull/2868#discussion_r985305240


##########
core/src/main/java/org/apache/calcite/sql/SqlWithinGroupOperator.java:
##########
@@ -79,7 +81,56 @@ public SqlWithinGroupOperator() {
       SqlValidator validator,
       SqlValidatorScope scope,
       SqlCall call) {
-    // Validate type of the inner aggregate call
-    return validateOperands(validator, scope, call);
+
+    SqlCall inner = call.operand(0);
+    throwIfNotAggregate(call, validator, inner.getOperator());
+    if (inner.getOperator().getKind() == SqlKind.PERCENTILE_DISC) {
+      // We first check the percentile call operands, and then derive the correct type using
+      // PercentileDiscCallBinding (See CALCITE-5230).
+      SqlCallBinding opBinding =
+          new PercentileDiscCallBinding(validator, scope, inner, getCollationColumn(call));
+      inner.getOperator().checkOperandTypes(opBinding, true);
+      RelDataType ret = inner.getOperator().inferReturnType(opBinding);
+      validator.setValidatedNodeType(inner, ret);
+      return ret;
+    } else {
+      return validateOperands(validator, scope, call);
+    }
+  }

Review Comment:
   There's a lot of duplicated code here. Maybe if you replace `new SqlCallBinding(validator, scope, call)` in the base method with a protected method, you can save all that duplication?



##########
core/src/main/java/org/apache/calcite/sql/SqlWithinGroupOperator.java:
##########
@@ -79,7 +81,56 @@ public SqlWithinGroupOperator() {
       SqlValidator validator,
       SqlValidatorScope scope,
       SqlCall call) {
-    // Validate type of the inner aggregate call
-    return validateOperands(validator, scope, call);
+
+    SqlCall inner = call.operand(0);
+    throwIfNotAggregate(call, validator, inner.getOperator());
+    if (inner.getOperator().getKind() == SqlKind.PERCENTILE_DISC) {
+      // We first check the percentile call operands, and then derive the correct type using
+      // PercentileDiscCallBinding (See CALCITE-5230).
+      SqlCallBinding opBinding =
+          new PercentileDiscCallBinding(validator, scope, inner, getCollationColumn(call));
+      inner.getOperator().checkOperandTypes(opBinding, true);
+      RelDataType ret = inner.getOperator().inferReturnType(opBinding);
+      validator.setValidatedNodeType(inner, ret);
+      return ret;
+    } else {
+      return validateOperands(validator, scope, call);
+    }
+  }
+
+  private SqlNode getCollationColumn(SqlCall call) {
+    return ((SqlNodeList) call.operand(1)).get(0);
+  }
+
+  private void throwIfNotAggregate(
+      SqlCall call,
+      SqlValidator validator,
+      SqlOperator operator
+  ) {
+    if (!operator.isAggregator()) {
+      throw validator.newValidationError(call,
+          RESOURCE.withinGroupNotAllowed(
+              operator.getName()));
+    }
+  }

Review Comment:
   I would remove the `throwIfNotAggregate` function. Methods that always throw are confusing to the reader, and leave regions of code that the compiler doesn't know are unreachable.



##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -988,4 +990,15 @@ public static SqlCall stripSeparator(SqlCall call) {
       return relDataType;
     }
   };
+
+  public static final SqlReturnTypeInference PERCENTILE_DISC = opBinding -> {
+    if (opBinding instanceof SqlWithinGroupOperator.PercentileDiscCallBinding) {

Review Comment:
   This 'if .. instanceof ... else if ... instanceof' pattern isn't good. How about adding a method `getCollationExpression()` to `SqlCallBinding`, with a default implementation that throws. Then you can remove both `getCollationType` methods.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] itiels commented on a diff in pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
itiels commented on code in PR #2868:
URL: https://github.com/apache/calcite/pull/2868#discussion_r985604527


##########
core/src/main/java/org/apache/calcite/sql/SqlWithinGroupOperator.java:
##########
@@ -79,7 +81,56 @@ public SqlWithinGroupOperator() {
       SqlValidator validator,
       SqlValidatorScope scope,
       SqlCall call) {
-    // Validate type of the inner aggregate call
-    return validateOperands(validator, scope, call);
+
+    SqlCall inner = call.operand(0);
+    throwIfNotAggregate(call, validator, inner.getOperator());
+    if (inner.getOperator().getKind() == SqlKind.PERCENTILE_DISC) {
+      // We first check the percentile call operands, and then derive the correct type using
+      // PercentileDiscCallBinding (See CALCITE-5230).
+      SqlCallBinding opBinding =
+          new PercentileDiscCallBinding(validator, scope, inner, getCollationColumn(call));
+      inner.getOperator().checkOperandTypes(opBinding, true);
+      RelDataType ret = inner.getOperator().inferReturnType(opBinding);
+      validator.setValidatedNodeType(inner, ret);
+      return ret;
+    } else {
+      return validateOperands(validator, scope, call);
+    }
+  }

Review Comment:
   I don't think we can, because the validation/derivation is needed to be done in terms of the `inner` operator. even if we supply the correct `SqlCallBinding` to `validateOperands`, it will still be done in the context of the outer operator(`SqlWithinGroupOperator`)



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] libenchao commented on a diff in pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
libenchao commented on code in PR #2868:
URL: https://github.com/apache/calcite/pull/2868#discussion_r1010047620


##########
core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java:
##########
@@ -2303,7 +2303,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable {
    */
   public static final SqlAggFunction PERCENTILE_DISC =
       SqlBasicAggFunction
-          .create(SqlKind.PERCENTILE_DISC, ReturnTypes.DOUBLE,
+          .create(SqlKind.PERCENTILE_DISC, ReturnTypes.PERCENTILE_DISC,

Review Comment:
   The java doc should also be adjusted.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] itiels commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
itiels commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1311502368

   Thanks @libenchao for the review. 
   
   I've fixed the comment.
   As for `PERCENTILE_CONT`, based on conversation with Julian in the Jira ticket, there is nothing to be done.
   
   Is there anything else that I need/should to do for this PR? 
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] libenchao commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
libenchao commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1329982053

   I'm ok with finishing it to another separate issue.
   This PR now looks good to me. The CI failure seems unrelated, I'll merge it.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] libenchao commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
libenchao commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1330293444

   @julianhyde Very thanks for the reminder 👍 , I did plan to do that while merging.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] itiels commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
itiels commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1329304370

   HI @libenchao, 
   Sorry for the late reply.
   
   I agree with your conclusion, however I prefer to do that in a separate PR if it's ok with you.


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] itiels commented on a diff in pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
itiels commented on code in PR #2868:
URL: https://github.com/apache/calcite/pull/2868#discussion_r985491360


##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -988,4 +990,15 @@ public static SqlCall stripSeparator(SqlCall call) {
       return relDataType;
     }
   };
+
+  public static final SqlReturnTypeInference PERCENTILE_DISC = opBinding -> {
+    if (opBinding instanceof SqlWithinGroupOperator.PercentileDiscCallBinding) {

Review Comment:
   I can't implement `getCollationExpression` in `PercentileDiscAggCallBinding`, as I don't have the collation expression there. 
   (maybe i'm missing something). I can however implement `getCollationType` which will remove the need to use the `instance of` pattern here.



##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -988,4 +990,15 @@ public static SqlCall stripSeparator(SqlCall call) {
       return relDataType;
     }
   };
+
+  public static final SqlReturnTypeInference PERCENTILE_DISC = opBinding -> {
+    if (opBinding instanceof SqlWithinGroupOperator.PercentileDiscCallBinding) {

Review Comment:
   I can't implement `getCollationExpression` in `PercentileDiscAggCallBinding`, as I don't have the collation expression there. (maybe i'm missing something). 
   I can however implement `getCollationType` which will remove the need to use the `instance of` pattern here.



##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -988,4 +990,15 @@ public static SqlCall stripSeparator(SqlCall call) {
       return relDataType;
     }
   };
+
+  public static final SqlReturnTypeInference PERCENTILE_DISC = opBinding -> {
+    if (opBinding instanceof SqlWithinGroupOperator.PercentileDiscCallBinding) {

Review Comment:
   I can't implement `getCollationExpression` in `PercentileDiscAggCallBinding`, as I don't have the collation expression there. (maybe i'm missing something). 
   I can however add `getCollationType` to the base class which will remove the need to use the `instance of` pattern here.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] libenchao commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
libenchao commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1312638552

   > As for PERCENTILE_CONT, based on conversation with Julian in the Jira ticket, there is nothing to be done.
   
   I'm not sure about this conclusion, I've checked the SQL standard (2011), 10.9:
   
   > If PERCENTILE_CONT is specified, then:
   > 1) Let ROW0 be the greatest exact numeric value with scale 0 (zero) that is less than or equal
   > to NVE*(N–1). Let ROWLIT0 be a <literal> representing ROW0.
   > 2) Let ROW1 be the least exact numeric value with scale 0 (zero) that is greater than or equal to NVE*(N–1). Let ROWLIT1 be a <literal> representing ROW1.
   > 3) Let FACTOR be an <approximate numeric literal> representing NVE*(N–1)–ROW0.
   > 4) The result is the result of the <scalar subquery>
   >      ( WITH TEMPTABLE(X, Y) AS
   >            ( SELECT ROW_NUMBER()
   > OVER (ORDER BY WSP) - 1, TXCOLNAME
   > FROM TXANAME )
   > SELECT CAST ( T0.Y + FACTOR * (T1.Y - T0.Y) AS DT ) FROM TEMPTABLE T0, TEMPTABLE T1
   > WHERE T0.ROWNUMBER = ROWLIT0
   > AND T1.ROWNUMBER = ROWLIT1 )
   
   We can see that the standard is using `CAST ( T0.Y + FACTOR * (T1.Y - T0.Y) AS DT )` as the result for `PERCENTILE_COUT`, whose type is the same as the sort key.
   
   And also I've checked some databases, and found: [oracle](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions110.htm) and [postgrelsql](https://www.postgresql.org/docs/9.4/functions-aggregate.html) are consistent with the standard.
   
   There are some databases, such as [server sql](https://learn.microsoft.com/en-us/sql/t-sql/functions/percentile-cont-transact-sql?view=sql-server-ver16), are declaring the return type which is not consistent with the standard.
   
   Hence, IMO, we should make it consistent with the standard by default. Of course if could be extensible for some dialects, if we want to achieve that, maybe the proper place to add the extension is in `RelDataTypeSystem`


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] libenchao commented on pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
libenchao commented on PR #2868:
URL: https://github.com/apache/calcite/pull/2868#issuecomment-1329213773

   @itiels Sorry to ping you.
   We plan to release 1.33 recently, it would be great if we can make it in this release. Do you have any comments about my last 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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] libenchao closed pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation

Posted by GitBox <gi...@apache.org>.
libenchao closed pull request #2868: [CALCITE-5230] Fix PERCENTILE_DISC return type derivation
URL: https://github.com/apache/calcite/pull/2868


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org