-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[CALCITE-5230] Fix PERCENTILE_DISC return type derivation #2868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cfaae07
to
fdf2b63
Compare
@@ -988,4 +990,15 @@ public static SqlCall stripSeparator(SqlCall call) { | |||
return relDataType; | |||
} | |||
}; | |||
|
|||
public static final SqlReturnTypeInference PERCENTILE_DISC = opBinding -> { | |||
if (opBinding instanceof SqlWithinGroupOperator.PercentileDiscCallBinding) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
core/src/main/java/org/apache/calcite/sql/SqlWithinGroupOperator.java
Outdated
Show resolved
Hide resolved
} else { | ||
return validateOperands(validator, scope, call); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
)
5c86873
to
b0e6502
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@itiels Thanks for your PR, generally it looks good now, I only left one minor comment.
Besides, PERCENTILE_COUT
also should be fixed, we can do it in this issue, or a follow-up issue.
@@ -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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The java doc should also be adjusted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed it
Thanks @libenchao for the review. I've fixed the comment. Is there anything else that I need/should to do for this PR? |
I'm not sure about this conclusion, I've checked the SQL standard (2011), 10.9:
We can see that the standard is using And also I've checked some databases, and found: oracle and postgrelsql are consistent with the standard. There are some databases, such as server sql, 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 |
@itiels Sorry to ping you. |
HI @libenchao, I agree with your conclusion, however I prefer to do that in a separate PR if it's ok with you. |
I'm ok with finishing it to another separate issue. |
@libenchao when you merge, please change the commit message to something that doesn’t start with “fix” |
@julianhyde Very thanks for the reminder 👍 , I did plan to do that while merging. |
Changed the return type of PERCENTILE_DISC to the type of the
order_by_expression
.Ticket: CALCITE-5230