-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[CALCITE-3863] Make Truncate/Round return type inference overridable through rel data … #1859
base: main
Are you sure you want to change the base?
Conversation
cascade(DECIMAL_TRUNCATE, SqlTypeTransforms.TO_NULLABLE); | ||
|
||
/** | ||
* Type-inference strategy whereby the result type of a call is |
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.
Could you please add some test cases?
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.
Added test cases.
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 title is a little confusing. Could you improve it?
* <li>Let s2 be the scale value to truncate</li> | ||
* <li>Let p, s be the precision and scale of the result</li> | ||
* <li>Let d be the number of whole digits in the result</li> | ||
* <li>Then the result type is a decimal with: |
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.
Only p
s
p1
s2
are used, remove the useless comments.
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.
removed done.
RelDataType type1, Integer scale2) { | ||
if (SqlTypeUtil.isExactNumeric(type1)) { | ||
if (SqlTypeUtil.isDecimal(type1)) { | ||
// Java numeric will always have invalid precision/scale, |
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 scale2
is never used.
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.
you just return a decimal with same precision and scale of type1
, so why we need these methods in RelDataTypeSystem
, just return the type1
seems okey ?
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 enables to override the behaviour, where scale2 can also used. Since existing behaviour is to have return type as type1, preserving the same.
// passing random value for scale2 | ||
// since the default behaviour is to have the return type of result as the first operand | ||
return typeFactory.getTypeSystem().deriveDecimalTruncateType(typeFactory, type1, 0); | ||
}; |
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.
just return type1
?
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 enables to override the behaviour if needed.
I'm confused by your change, if you just want the |
This change preserves the existing behaviour and enables to override the behaviour if needed, using RelDataTypeFactory for a custom type system. |
@danny0405 This is similar to #1311 which made some of the other decimal related rules configurable using a custom type system. Our understanding back then was this was the recommended way. Could you please elaborate on your approach if you think this is not the right way to achieve the same. |
* <p>The default implementation is SQL:2003 compliant: the declared type of | ||
* the result is the declared type of the first operand (decimal to be truncated). | ||
* | ||
* @see Glossary#SQL2003 SQL:2003 Part 2 Section 6.27 |
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 know SQL standard document the general operators (+
, -
, *
, /
, %
) for decimal types. But can we find the guideline in SQL standard for truncate
and round
?
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 is not a consistent one from what we found SQL Server seems to go with what calcite does where as Oracle does not.
Also i think that is outside the scope of this PR..
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.
My point is the document added in this PR. If it is not a standard manner, we'd better not add this confusing paragraph.
The default implementation is SQL:2003 compliant: the declared type of
the result is the declared type of the first operand (decimal to be truncated).
@see Glossary#SQL2003 SQL:2003 Part 2 Section 6.27
In my opinion, #1311 is very different because |
Hi Danny, Thanks for the comments. I think we discussed something similar in this comment in 1311(https://issues.apache.org/jira/browse/CALCITE-3187?focusedCommentId=16882084&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-16882084). I think the same holds true even now, I think we want to consistently override return type inference for all decimal functions so that even Calcite internals use the same rules as us. Having two sets of rules applied inconsistently between Calcite internals and Application internals will be hard to debug. Please let us know if you have an alternative that keeps the rules consistent across the board. Thx. |
8a5cf83
to
cf7f71b
Compare
…type factory