You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "mihaibudiu (via GitHub)" <gi...@apache.org> on 2024/12/16 22:18:37 UTC

[PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

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

   The current implementation is based on the rules for inferring the result for addition (but does not require an extra digit).
   


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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "NobiGo (via GitHub)" <gi...@apache.org>.
NobiGo commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887767056


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
     }
 
     if (SqlTypeUtil.isExactNumeric(type1) && SqlTypeUtil.isExactNumeric(type2)) {
-      if (SqlTypeUtil.isDecimal(type1)) {
-        // Use max precision
+      if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+        // Precision used must be large enough to fit either of the types
+        int maxScale = Math.max(type1.getScale(), type2.getScale());
         RelDataType result =
-            factory.createSqlType(type1.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type1.getScale());
-        return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
-      } else if (SqlTypeUtil.isDecimal(type2)) {
-        // Use max precision
-        RelDataType result =
-            factory.createSqlType(type2.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type2.getScale());
+            factory.createSqlType(SqlTypeName.DECIMAL,
+                Math.max(type1.getPrecision() - type1.getScale(),
+                         type2.getPrecision() - type2.getScale()) + maxScale,
+                maxScale);
         return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
       }
       if (type1.getPrecision() > type2.getPrecision()) {

Review Comment:
   But the `RelDataTypeSystemImpl#getDefaultPrecision` has a default precision for this. It is right. I misunderstood.



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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887766756


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
     }
 
     if (SqlTypeUtil.isExactNumeric(type1) && SqlTypeUtil.isExactNumeric(type2)) {
-      if (SqlTypeUtil.isDecimal(type1)) {
-        // Use max precision
+      if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+        // Precision used must be large enough to fit either of the types
+        int maxScale = Math.max(type1.getScale(), type2.getScale());
         RelDataType result =
-            factory.createSqlType(type1.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type1.getScale());
-        return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
-      } else if (SqlTypeUtil.isDecimal(type2)) {
-        // Use max precision
-        RelDataType result =
-            factory.createSqlType(type2.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type2.getScale());
+            factory.createSqlType(SqlTypeName.DECIMAL,
+                Math.max(type1.getPrecision() - type1.getScale(),
+                         type2.getPrecision() - type2.getScale()) + maxScale,
+                maxScale);
         return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
       }
       if (type1.getPrecision() > type2.getPrecision()) {

Review Comment:
   See RelDataTypeSystemImpl.getDefaultPrecision



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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "NobiGo (via GitHub)" <gi...@apache.org>.
NobiGo commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887760820


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
     }
 
     if (SqlTypeUtil.isExactNumeric(type1) && SqlTypeUtil.isExactNumeric(type2)) {
-      if (SqlTypeUtil.isDecimal(type1)) {
-        // Use max precision
+      if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+        // Precision used must be large enough to fit either of the types
+        int maxScale = Math.max(type1.getScale(), type2.getScale());
         RelDataType result =
-            factory.createSqlType(type1.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type1.getScale());
-        return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
-      } else if (SqlTypeUtil.isDecimal(type2)) {
-        // Use max precision
-        RelDataType result =
-            factory.createSqlType(type2.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type2.getScale());
+            factory.createSqlType(SqlTypeName.DECIMAL,
+                Math.max(type1.getPrecision() - type1.getScale(),
+                         type2.getPrecision() - type2.getScale()) + maxScale,
+                maxScale);
         return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
       }
       if (type1.getPrecision() > type2.getPrecision()) {

Review Comment:
   Can this handle types without precision and proportion? For example:
   TINYINT、SMALLINT、INTEGER and BIGINT? Or the default behavior is right.
   If type1 is TINYINT type2 is SMALLINT, then the common type becomes SMALLINT. This is right.
   But when type1 is SMALLINT and  type2 is TINYINT. then the common type becomes TINYINT. This should be unreasonable. 



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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887762953


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
     }
 
     if (SqlTypeUtil.isExactNumeric(type1) && SqlTypeUtil.isExactNumeric(type2)) {
-      if (SqlTypeUtil.isDecimal(type1)) {
-        // Use max precision
+      if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+        // Precision used must be large enough to fit either of the types
+        int maxScale = Math.max(type1.getScale(), type2.getScale());
         RelDataType result =
-            factory.createSqlType(type1.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type1.getScale());
-        return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
-      } else if (SqlTypeUtil.isDecimal(type2)) {
-        // Use max precision
-        RelDataType result =
-            factory.createSqlType(type2.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type2.getScale());
+            factory.createSqlType(SqlTypeName.DECIMAL,
+                Math.max(type1.getPrecision() - type1.getScale(),
+                         type2.getPrecision() - type2.getScale()) + maxScale,
+                maxScale);
         return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
       }
       if (type1.getPrecision() > type2.getPrecision()) {

Review Comment:
   BTW: all integer types have a positive precision and a scale or zero.



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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "sonarcloud[bot] (via GitHub)" <gi...@apache.org>.
sonarcloud[bot] commented on PR #4093:
URL: https://github.com/apache/calcite/pull/4093#issuecomment-2547025465

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4093) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [1 New issue](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4093&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=4093&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=4093&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4093&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=4093&metric=new_duplicated_lines_density&view=list)  
     
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=4093)
   
   


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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "NobiGo (via GitHub)" <gi...@apache.org>.
NobiGo commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887765545


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
     }
 
     if (SqlTypeUtil.isExactNumeric(type1) && SqlTypeUtil.isExactNumeric(type2)) {
-      if (SqlTypeUtil.isDecimal(type1)) {
-        // Use max precision
+      if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+        // Precision used must be large enough to fit either of the types
+        int maxScale = Math.max(type1.getScale(), type2.getScale());
         RelDataType result =
-            factory.createSqlType(type1.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type1.getScale());
-        return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
-      } else if (SqlTypeUtil.isDecimal(type2)) {
-        // Use max precision
-        RelDataType result =
-            factory.createSqlType(type2.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type2.getScale());
+            factory.createSqlType(SqlTypeName.DECIMAL,
+                Math.max(type1.getPrecision() - type1.getScale(),
+                         type2.getPrecision() - type2.getScale()) + maxScale,
+                maxScale);
         return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
       }
       if (type1.getPrecision() > type2.getPrecision()) {

Review Comment:
   According to SqlTypeName, TINYINT、SMALLINT、INTEGER, and BIGINT don't have Precision and Scale.



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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887762456


##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
     }
 
     if (SqlTypeUtil.isExactNumeric(type1) && SqlTypeUtil.isExactNumeric(type2)) {
-      if (SqlTypeUtil.isDecimal(type1)) {
-        // Use max precision
+      if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+        // Precision used must be large enough to fit either of the types
+        int maxScale = Math.max(type1.getScale(), type2.getScale());
         RelDataType result =
-            factory.createSqlType(type1.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type1.getScale());
-        return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
-      } else if (SqlTypeUtil.isDecimal(type2)) {
-        // Use max precision
-        RelDataType result =
-            factory.createSqlType(type2.getSqlTypeName(),
-                Math.max(type1.getPrecision(), type2.getPrecision()), type2.getScale());
+            factory.createSqlType(SqlTypeName.DECIMAL,
+                Math.max(type1.getPrecision() - type1.getScale(),
+                         type2.getPrecision() - type2.getScale()) + maxScale,
+                maxScale);
         return factory.createTypeWithNullability(result, type1.isNullable() || type2.isNullable());
       }
       if (type1.getPrecision() > type2.getPrecision()) {

Review Comment:
   this should be completely symmetric in type1 and type2.



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


Re: [PR] [CALCITE-6733] Type inferred by coercion for comparisons with decimal is too narrow [calcite]

Posted by "mihaibudiu (via GitHub)" <gi...@apache.org>.
mihaibudiu merged PR #4093:
URL: https://github.com/apache/calcite/pull/4093


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