Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

anilbash
Copy link

…type factory

cascade(DECIMAL_TRUNCATE, SqlTypeTransforms.TO_NULLABLE);

/**
* Type-inference strategy whereby the result type of a call is
Copy link
Contributor

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test cases.

Copy link
Contributor

@chunweilei chunweilei left a 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?

@anilbash anilbash changed the title [CALCITE-3863] Truncate/Round return type inference through rel data … [CALCITE-3863] Make Truncate/Round return type inference overridable through rel data … Mar 18, 2020
* <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:
Copy link
Contributor

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.

Copy link
Author

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,
Copy link
Contributor

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.

Copy link
Contributor

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 ?

Copy link
Author

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);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return type1 ?

Copy link
Author

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.

@danny0405
Copy link
Contributor

I'm confused by your change, if you just want the Round or Truncate return type inference can be configurable, just replace the Return type inference, which is already pluggable.

@anilbash anilbash closed this Mar 24, 2020
@anilbash anilbash reopened this Mar 24, 2020
@anilbash
Copy link
Author

I'm confused by your change, if you just want the Round or Truncate return type inference can be configurable, just replace the Return type inference, which is already pluggable.

This change preserves the existing behaviour and enables to override the behaviour if needed, using RelDataTypeFactory for a custom type system.
The test cases testDecimalTruncateReturnTypeInference and testCustomDecimalTruncateReturnTypeInference demonstrates the same.

@praveenbingo
Copy link
Contributor

@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
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor

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

@danny0405
Copy link
Contributor

danny0405 commented Mar 25, 2020

@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.

In my opinion, #1311 is very different because + and - are builtin operators and we have no way for downstream projects to replace these operators, but Round and Truncate are different, they are sql functions, and if your project has maintained the sql operators by yourself, there is a chance you just override the type inference of these two instead of the whole type system, which seems too heavy and special to customize.

@danny0405 danny0405 added the returned-with-feedback There are review comments (in JIRA and/or in GitHub) to be implemented before merging the PR label Mar 25, 2020
@praveenbingo
Copy link
Contributor

@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.

In my opinion, #1311 is very different because + and - are builtin operators and we have no way for downstream projects to replace these operators, but Round and Truncate are different, they are sql functions, and if your project has maintained the sql operators by yourself, there is a chance you just override the type inference of these two instead of the whole type system, which seems too heavy and special to customize.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
returned-with-feedback There are review comments (in JIRA and/or in GitHub) to be implemented before merging the PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants