You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "lowka (via GitHub)" <gi...@apache.org> on 2023/10/16 08:51:45 UTC

[PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

lowka opened a new pull request, #2690:
URL: https://github.com/apache/ignite-3/pull/2690

   Thank you for submitting the pull request.
   
   To streamline the review process of the patch and ensure better code quality
   we ask both an author and a reviewer to verify the following:
   
   ### The Review Checklist
   - [ ] **Formal criteria:** TC status, codestyle, mandatory documentation. Also make sure to complete the following:  
   \- There is a single JIRA ticket related to the pull request.  
   \- The web-link to the pull request is attached to the JIRA ticket.  
   \- The JIRA ticket has the Patch Available state.  
   \- The description of the JIRA ticket explains WHAT was made, WHY and HOW.  
   \- The pull request title is treated as the final commit message. The following pattern must be used: IGNITE-XXXX Change summary where XXXX - number of JIRA issue.
   - [ ] **Design:** new code conforms with the design principles of the components it is added to.
   - [ ] **Patch quality:** patch cannot be split into smaller pieces, its size must be reasonable.
   - [ ] **Code quality:** code is clean and readable, necessary developer documentation is added if needed.
   - [ ] **Tests code quality:** test set covers positive/negative scenarios, happy/edge cases. Tests are effective in terms of execution time and resources.
   
   ### Notes
   - [Apache Ignite Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Java+Code+Style+Guide)


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365060711


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -128,9 +128,54 @@ public static int length(Object b) {
         return b instanceof ByteString ? octetLength((ByteString) b) : charLength((String) b);
     }
 
-    private static BigDecimal setScale(int precision, int scale, BigDecimal decimal) {
-        return precision == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL)
-            ? decimal : decimal.setScale(scale, RoundingMode.HALF_UP);
+    // SQL ROUND function
+
+    /** SQL {@code ROUND} operator applied to int values. */
+    public static int sround(int b0) {
+        return sround(BigDecimal.valueOf(b0)).intValue();

Review Comment:
   can we avoid creating BigDecimal for primitive types? 



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365042396


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   Because `ROUND(1)` has type `ROUND(<int>)`.
   if there is no `ROUND(<int>)` overload,  then `SELECT ROUND(1)` is going to rejected by the validator.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1363759097


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDataTypesTest.java:
##########
@@ -322,7 +322,7 @@ public void testDecimalLiteral() {
                 "SELECT DECIMAL '0.09'  BETWEEN DECIMAL '0.06' AND DECIMAL '0.07'")
                 .returns(false).check();
 
-        assertQuery("SELECT ROUND(DECIMAL '10.000', 2)").returns(new BigDecimal("10.00")).check();
+        assertQuery("SELECT ROUND(DECIMAL '10.000', 2)").returns(new BigDecimal("10.000")).check();

Review Comment:
   probably we need to add more accurate test:
   assertQuery("SELECT TYPEOF(ROUND(DECIMAL '10.000', 2))").returns("DECIMAL(5, 3)").check();
   wdyt ?



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365179681


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sqllogic/ItSqlLogicTest.java:
##########
@@ -140,7 +140,7 @@
 @Tag(value = "sqllogic")
 @ExtendWith(SystemPropertiesExtension.class)
 @WithSystemProperty(key = "IMPLICIT_PK_ENABLED", value = "true")
-@SqlLogicTestEnvironment(scriptsRoot = "src/integrationTest/sql")
+@SqlLogicTestEnvironment(scriptsRoot = "src/integrationTest/sql", regex = "test_round")

Review Comment:
   Thanks. That was committed by mistake.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364304882


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   There only 4 variants of this functions - int, long, double and BigDecimal. shorts and byte are promoted to ints by calcite. 
   I can add tests for all numeric types at the SQL level.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365240524


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   Fixed.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365242159


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -434,4 +503,31 @@ public boolean rolledUpColumnValidInsideAgg(String column, SqlCall call,
             return true;
         }
     }
+
+    private static long divide(long p, long q, RoundingMode mode) {

Review Comment:
   We need HALF_UP for ROUND function and HALF_DOWN for TRUNCATE.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364304882


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   There only 4 variants of this function - int, long, double and BigDecimal. shorts and byte are promoted to ints by calcite. 
   I can add tests for all numeric types at the SQL level.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364949406


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   i did my best ))) to align RexImpTable with calcite and one more change (



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1367086651


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -1145,6 +1146,11 @@ private void defineMethod(SqlOperator operator, Method method,
       map.put(operator, new MethodImplementor(method, nullPolicy, false));
     }
 
+    private void defineIgniteMethod(SqlOperator operator, String method,

Review Comment:
   Done.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1368523867


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -128,9 +128,78 @@ public static int length(Object b) {
         return b instanceof ByteString ? octetLength((ByteString) b) : charLength((String) b);
     }
 
-    private static BigDecimal setScale(int precision, int scale, BigDecimal decimal) {
-        return precision == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL)
-            ? decimal : decimal.setScale(scale, RoundingMode.HALF_UP);
+    // SQL ROUND function
+
+    /** SQL {@code ROUND} operator applied to int values. */
+    public static int sround(int b0) {

Review Comment:
   although such set of overrides are working correctly for every numeric type, ExpressionFactory can't generate optimal code. Try to change tests in order to use dynamic params rather constants, and print out generated code of expressions to see difference



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365015533


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   >  int, long, double and BigDecimal. shorts and byte are promoted to ints by calcite.
   
   why do we need `int` version then? 🤔 



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   >  int, long, double and BigDecimal. shorts and byte are promoted to ints by calcite.
   
   why do we need `int` version then? 🤔 



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365042396


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   Because `ROUND(1)` has type `ROUND(<int>)`.
   if there is no ROUND(<int>) overload,  than `SELECT ROUND(1)` is going to rejected by the validator.



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   Because `ROUND(1)` has type `ROUND(<int>)`.
   if there is no `ROUND(<int>)` overload,  than `SELECT ROUND(1)` is going to rejected by the validator.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364924163


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDataTypesTest.java:
##########
@@ -322,7 +322,7 @@ public void testDecimalLiteral() {
                 "SELECT DECIMAL '0.09'  BETWEEN DECIMAL '0.06' AND DECIMAL '0.07'")
                 .returns(false).check();
 
-        assertQuery("SELECT ROUND(DECIMAL '10.000', 2)").returns(new BigDecimal("10.00")).check();
+        assertQuery("SELECT ROUND(DECIMAL '10.000', 2)").returns(new BigDecimal("10.000")).check();

Review Comment:
   This PR adds test cases for ROUND function to `ItFunctionsTest`. Test cases with return type assertions can be found there.



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -632,7 +632,7 @@ Builder populate() {
       defineMethod(DEGREES, "degrees", NullPolicy.STRICT);
       defineMethod(POW, "power", NullPolicy.STRICT);
       defineMethod(RADIANS, "radians", NullPolicy.STRICT);
-      defineMethod(ROUND, "sround", NullPolicy.STRICT);
+      defineIgniteMethod(ROUND, "sround", NullPolicy.STRICT);

Review Comment:
   Fixed.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "xtern (via GitHub)" <gi...@apache.org>.
xtern commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365124794


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sqllogic/ItSqlLogicTest.java:
##########
@@ -140,7 +140,7 @@
 @Tag(value = "sqllogic")
 @ExtendWith(SystemPropertiesExtension.class)
 @WithSystemProperty(key = "IMPLICIT_PK_ENABLED", value = "true")
-@SqlLogicTestEnvironment(scriptsRoot = "src/integrationTest/sql")
+@SqlLogicTestEnvironment(scriptsRoot = "src/integrationTest/sql", regex = "test_round")

Review Comment:
   Don't forget to revert this.
   Also unmute/rewrite TODOs related to ticket in ItSqlOperatorsTest#testMathFunctions



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364296401


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   Because `defineMethod` one expects that all methods are defined on calcite's `SqlFunctions`. (1)
   And it is not possible to define a method with multiple overloads in `IgniteSqlFunctions`.  (2) 
   
   The second one can be fixed if we to allow defining functions by name w/o specifying their signatures e.g. ROUND function would look like:
   
   > ROUND("round");
   
   This would also required adding methodName to `IgniteSqlFunctions`



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   Because `defineMethod` expects that all methods are defined on calcite's `SqlFunctions`. (1)
   And it is not possible to define a method with multiple overloads in `IgniteSqlFunctions`.  (2) 
   
   The second one can be fixed if we to allow defining functions by name w/o specifying their signatures e.g. ROUND function would look like:
   
   > ROUND("round");
   
   This would also required adding methodName to `IgniteSqlFunctions`



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1366962152


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItFunctionsTest.java:
##########
@@ -344,6 +351,193 @@ public void testSubstr() {
         assertThrowsWithCause(() -> sql("SELECT SUBSTR('1234567', 1, -3)"), IgniteException.class, "negative substring length");
     }
 
+    /** Test cases for ROUND function that accepts integral numeric types. */
+    @TestFactory
+    public Stream<DynamicTest> testRoundIntegral() {

Review Comment:
   please rewrite these tests without TestFactory. You don't need `TestFactory` until you are working on a test framework.



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/IgniteMethod.java:
##########
@@ -122,6 +128,25 @@ public enum IgniteMethod {
         method = Types.lookupMethod(clazz, methodName, argumentTypes);
     }
 
+    /**
+     * Constructor that allows to specify overloaded methods as SQL function implementations.
+     *
+     * @param clazz Class where to lookup method.
+     * @param methodName Method name.
+     * @param overloadedMethod If {@code true} to looks up overloaded methods, otherwise looks up a method w/o parameters.
+     */
+    IgniteMethod(Class<?> clazz, String methodName, boolean overloadedMethod) {
+        if (overloadedMethod) {
+            // Allow calcite to select appropriate method at a call site.
+            this.method = Arrays.stream(IgniteSqlFunctions.class.getMethods())

Review Comment:
   that's kinda strange decision: you have particular class as input argument, yet you are using `IgniteSqlFunctions.class`



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -1145,6 +1146,11 @@ private void defineMethod(SqlOperator operator, Method method,
       map.put(operator, new MethodImplementor(method, nullPolicy, false));
     }
 
+    private void defineIgniteMethod(SqlOperator operator, String method,

Review Comment:
   I think, this can be deleted



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364296401


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   Because `defineMethod` expects that all methods to be defined on calcite's `SqlFunctions`. (1)
   And it is not possible to define a method with multiple overloads in `IgniteSqlFunctions`.  (2) 
   
   The second one can be fixed if we to allow defining functions by name w/o specifying their signatures e.g. ROUND function would look like:
   
   > ROUND("round");
   
   This would also required adding methodName to `IgniteSqlFunctions`



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "zstan (via GitHub)" <gi...@apache.org>.
zstan commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1363760575


##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDataTypesTest.java:
##########
@@ -322,7 +322,7 @@ public void testDecimalLiteral() {
                 "SELECT DECIMAL '0.09'  BETWEEN DECIMAL '0.06' AND DECIMAL '0.07'")
                 .returns(false).check();
 
-        assertQuery("SELECT ROUND(DECIMAL '10.000', 2)").returns(new BigDecimal("10.00")).check();
+        assertQuery("SELECT ROUND(DECIMAL '10.000', 2)").returns(new BigDecimal("10.000")).check();

Review Comment:
   honestly i forgot about our discussion for such a case, believe that type need to be the same in such a case too.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365255965


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctions.java:
##########
@@ -128,9 +128,54 @@ public static int length(Object b) {
         return b instanceof ByteString ? octetLength((ByteString) b) : charLength((String) b);
     }
 
-    private static BigDecimal setScale(int precision, int scale, BigDecimal decimal) {
-        return precision == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL)
-            ? decimal : decimal.setScale(scale, RoundingMode.HALF_UP);
+    // SQL ROUND function
+
+    /** SQL {@code ROUND} operator applied to int values. */
+    public static int sround(int b0) {
+        return sround(BigDecimal.valueOf(b0)).intValue();

Review Comment:
   Fixed.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365058978


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   > Because defineMethod expects that all methods to be defined on calcite's SqlFunctions
   
   that's not true, you could supply reference to particular `java.lang.reflect.Method`. In that case you can use static method from any class
   
   > And it is not possible to define a method with multiple overloads in IgniteSqlFunctions
   
   why do you can define multiple overloads in `SqlFunctions`, but not in `IgniteSqlFunctions`? 



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365062432


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   Have you tried to delete this overload and run test?



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1363959297


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -632,7 +632,7 @@ Builder populate() {
       defineMethod(DEGREES, "degrees", NullPolicy.STRICT);
       defineMethod(POW, "power", NullPolicy.STRICT);
       defineMethod(RADIANS, "radians", NullPolicy.STRICT);
-      defineMethod(ROUND, "sround", NullPolicy.STRICT);
+      defineIgniteMethod(ROUND, "sround", NullPolicy.STRICT);

Review Comment:
   all ignite's overrides better to place to `populateIgnite()`



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {

Review Comment:
   let's add all possible numeric types to the test cases. The same for another variant of this function



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   why do we need new implementor?



##########
modules/runner/src/integrationTest/sql/types/decimal/test_decimal_ops.test:
##########


Review Comment:
   BTW, we have `sql/function/numeric/test_round.test`. Let's add test cases to this file to cover all possible types



##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {
+        assertEquals(new BigDecimal("1"), IgniteSqlFunctions.sround(new BigDecimal("1.000")));
+        assertEquals(1, IgniteSqlFunctions.sround(1), "int");
+        assertEquals(1L, IgniteSqlFunctions.sround(1L), "long");
+        assertEquals(1.0d, IgniteSqlFunctions.sround(1.123d), "double");
+    }
+
+    /** Tests for ROUND(x, s) function, where x is a BigDecimal value. */
+    @ParameterizedTest
+    @CsvSource({
+            "1.123, 0, 1.000",

Review Comment:
   can we have similar set of tests for every type? LIke, tests with negative `s`



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1365061011


##########
modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java:
##########
@@ -242,4 +245,57 @@ public void testConvertDecimal(String input, int precision, int scale, String re
             assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get);
         }
     }
+
+    /** Tests for ROUND(x) function. */
+    @Test
+    public void testRound() {
+        assertEquals(new BigDecimal("1"), IgniteSqlFunctions.sround(new BigDecimal("1.000")));
+        assertEquals(1, IgniteSqlFunctions.sround(1), "int");
+        assertEquals(1L, IgniteSqlFunctions.sround(1L), "long");
+        assertEquals(1.0d, IgniteSqlFunctions.sround(1.123d), "double");
+    }
+
+    /** Tests for ROUND(x, s) function, where x is a BigDecimal value. */
+    @ParameterizedTest
+    @CsvSource({
+            "1.123, 0, 1.000",

Review Comment:
   Sure. Done.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1364296401


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/exp/RexImpTable.java:
##########
@@ -4359,6 +4364,26 @@ private static class DefaultImplementor extends AbstractRexCallImplementor {
     }
   }
 
+  private static class IgniteMethodNameImplementor extends AbstractRexCallImplementor {

Review Comment:
   Because `defineMethod` expects that all methods to be defined on calcite's `SqlFunctions`. (1)
   And it is not possible to define a method with multiple overloads in `IgniteSqlFunctions`.  (2) 
   
   The second one can be fixed if we to allow defining functions by name w/o specifying their signatures e.g. ROUND function would look like:
   
   > ROUND("round");
   
   This would also require adding methodName to `IgniteSqlFunctions`



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "lowka (via GitHub)" <gi...@apache.org>.
lowka commented on code in PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690#discussion_r1367087044


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/util/IgniteMethod.java:
##########
@@ -122,6 +128,25 @@ public enum IgniteMethod {
         method = Types.lookupMethod(clazz, methodName, argumentTypes);
     }
 
+    /**
+     * Constructor that allows to specify overloaded methods as SQL function implementations.
+     *
+     * @param clazz Class where to lookup method.
+     * @param methodName Method name.
+     * @param overloadedMethod If {@code true} to looks up overloaded methods, otherwise looks up a method w/o parameters.
+     */
+    IgniteMethod(Class<?> clazz, String methodName, boolean overloadedMethod) {
+        if (overloadedMethod) {
+            // Allow calcite to select appropriate method at a call site.
+            this.method = Arrays.stream(IgniteSqlFunctions.class.getMethods())

Review Comment:
   That was a typo. Thanks, fixed it.



##########
modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItFunctionsTest.java:
##########
@@ -344,6 +351,193 @@ public void testSubstr() {
         assertThrowsWithCause(() -> sql("SELECT SUBSTR('1234567', 1, -3)"), IgniteException.class, "negative substring length");
     }
 
+    /** Test cases for ROUND function that accepts integral numeric types. */
+    @TestFactory
+    public Stream<DynamicTest> testRoundIntegral() {

Review Comment:
   Done. 



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-20311 Sql. Fix behaviour of ROUND function. [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 merged PR #2690:
URL: https://github.com/apache/ignite-3/pull/2690


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org