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-4838] Add RoundingMode in SqlConformance to document the rou… #3917

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

Conversation

NobiGo
Copy link
Contributor

@NobiGo NobiGo commented Aug 16, 2024

…nding mode when cast an approximate numeric to int

Copy link

sonarcloud bot commented Aug 16, 2024

!ok


select cast(5.5 as smallint),
Copy link
Contributor

Choose a reason for hiding this comment

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

for each statement, add comments explaining what rounding mode is being used

@@ -384,9 384,15 @@ static void checkRoundedRange(Number value, double min, double max) {
}
}

/** Called from BuiltInMethod.INTEGER_CAST */
Copy link
Contributor

Choose a reason for hiding this comment

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

javadoc is same as next method

if methods have the same description, are both required?

*
* <p> {@link org.apache.calcite.sql.SqlDialect.DatabaseProduct#CALCITE}'s
* default is {@link RoundingMode#DOWN} and {@link org.apache.calcite.sql.SqlDialect.DatabaseProduct#POSTGRESQL},
* {@link org.apache.calcite.sql.SqlDialect.DatabaseProduct#MYSQL} and {@link org.apache.calcite.sql.SqlDialect.DatabaseProduct#ORACLE} is {@link RoundingMode#HALF_UP}.
Copy link
Contributor

Choose a reason for hiding this comment

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

line is too long and can be wrapped

}
return Expressions.constant(null, type);
}

static Expression getDefaultValue(Type type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should the method without roundingMode be deprecated?


public RexExecutorImpl(DataContext dataContext) {
this.dataContext = dataContext;
this.sqlConformance = SqlConformanceEnum.DEFAULT;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think conformance belongs here. Conformance governs validation behavior, not runtime behavior.

@@ -133,7 152,7 @@ public static RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> e
try {
code = compile(rexBuilder, constExps, (list, index, storageType) -> {
throw new UnsupportedOperationException();
});
}, sqlConformance);
Copy link
Contributor

Choose a reason for hiding this comment

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

code is more readable if the lambda is the last parameter

Copy link
Contributor

@mihaibudiu mihaibudiu left a comment

Choose a reason for hiding this comment

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

This is sorely needed work, thank you for doing it.

@@ -505,9 505,9 @@ private Expression getConvertExpression(
case SMALLINT: {
if (SqlTypeName.NUMERIC_TYPES.contains(sourceType.getSqlTypeName())) {
return Expressions.call(
BuiltInMethod.INTEGER_CAST.method,
BuiltInMethod.INTEGER_CAST_ROUNDING_MODE.method,
Copy link
Contributor

Choose a reason for hiding this comment

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

this should apply to casts from decimal to decimal above

@@ -144,4 146,8 @@ public abstract class SqlAbstractConformance implements SqlConformance {
@Override public boolean allowLenientCoercion() {
return SqlConformanceEnum.DEFAULT.allowLenientCoercion();
}

@Override public RoundingMode roundingMode() {
return SqlConformanceEnum.DEFAULT.roundingMode();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think rounding should be part of the type system

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree.

I commented elsewhere that including SqlConformance in RexExecutor made me feel uneasy. SqlConformance is about what is allowed in valid SQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julianhyde @mihaibudiu I will move the roundingMode to RelDataTypeSystem. Thanks for the advice.

@@ -315,6 316,8 @@ public enum BuiltInMethod {
FP_DECIMAL_CAST(Primitive.class, "fpDecimalCast",
Number.class, int.class, int.class),
INTEGER_CAST(Primitive.class, "integerCast", Primitive.class, Object.class),
INTEGER_CAST_ROUNDING_MODE(Primitive.class, "integerCast",
Copy link
Contributor

Choose a reason for hiding this comment

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

The other cast method should probably be removed.
but many other cast methods have to be considered, including DECIMAL to DECIMAL, FP to integer, FP to decimal, DECIMAL to FP and perhaps even string to decimal

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. According to your advice, I found some places that use the default RoudingMode.DOWN. I'll see what I can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants