Vladsz83 commented on code in PR #11581:
URL: https://github.com/apache/ignite/pull/11581#discussion_r1806130403


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DataTypesTest.java:
##########
@@ -596,6 +600,214 @@ public void testNumericConversion() {
             .check();
     }
 
+    /** */
+    @Test
+    public void testCoercionOfVarcharLiterals() {
+        doTestCoercionOfVarchars(false);
+    }
+
+    /** */
+    @Test
+    public void testCoercionOfVarcharDynamicParameters() {
+        doTestCoercionOfVarchars(true);
+    }
+
+    /** */
+    private void doTestCoercionOfVarchars(boolean dynamics) {
+        for (List<Object> params : varcharsToCoerce()) {
+            String val = params.get(0).toString();
+            RelDataType type = (RelDataType)params.get(1);
+            String result = params.get(2).toString();
+
+            if (dynamics)
+                assertQuery(String.format("SELECT CAST(? AS %s)", 
type)).withParams(val).returns(result).check();
+            else
+                assertQuery(String.format("SELECT CAST('%s' AS %s)", val, 
type)).returns(result).check();
+        }
+    }
+
+    /** */
+    private static List<List<Object>> varcharsToCoerce() {
+        IgniteTypeFactory tf = Commons.typeFactory();
+
+        return F.asList(
+            F.asList("abcde", tf.createSqlType(SqlTypeName.VARCHAR, 3), "abc"),
+            F.asList("abcde", tf.createSqlType(SqlTypeName.VARCHAR, 5), 
"abcde"),
+            F.asList("abcde", tf.createSqlType(SqlTypeName.VARCHAR, 6), 
"abcde"),
+            F.asList("abcde", tf.createSqlType(SqlTypeName.VARCHAR), "abcde"),
+            F.asList("abcde", tf.createSqlType(SqlTypeName.CHAR), "a"),
+            F.asList("abcde", tf.createSqlType(SqlTypeName.CHAR, 3), "abc")
+        );
+    }
+
+    /** */
+    @Test
+    public void testCoercionOfNumericLiterals() {
+        doTestCoercionOfNumerics(false, false);
+    }
+
+    /** */
+    @Test
+    public void testCoercionOfNumericLiteralsPrecasted() {
+        doTestCoercionOfNumerics(false, true);
+    }
+
+    /** */
+    @Test
+    public void testCoercionOfNumericDynamicParameters() {
+        doTestCoercionOfNumerics(true, false);
+    }
+
+    /** */
+    @Test
+    public void testCoercionOfNumericDynamicParametersPrecasted() {
+        doTestCoercionOfNumerics(true, true);
+    }
+
+    /** */
+    private void doTestCoercionOfNumerics(boolean dynamic, boolean precasted) {
+        for (List<Object> params : numericsToCast()) {
+            assert params.size() == 4 : "Wrong params lenght: " + 
params.size();
+
+            RelDataType inputType = (RelDataType)params.get(0);
+            Object inputVal = params.get(1);
+            RelDataType targetType = (RelDataType)params.get(2);
+            Object expectedRes = params.get(3);
+
+            log.info("Params: inputType=" + inputType + ", inputValue=" + 
inputVal + ", targetType=" + targetType
+                + ", expectedResult=" + expectedRes);
+
+            if (dynamic) {
+                String qry = precasted
+                    ? String.format("SELECT CAST(?::%s AS %s)", inputType, 
targetType)
+                    : String.format("SELECT CAST(? AS %s)", targetType);
+
+                if (expectedRes instanceof Exception)
+                    assertThrows(qry, (Class<? extends 
Exception>)expectedRes.getClass(), ((Throwable)expectedRes).getMessage(), 
inputVal);
+                else
+                    
assertQuery(qry).withParams(inputType).returns(expectedRes);
+            }
+            else {
+                String qry = precasted
+                    ? String.format("SELECT CAST(%s::%s AS %s)", 
asLiteral(inputVal, inputType), inputType, targetType)
+                    : String.format("SELECT CAST(%s AS %s)", 
asLiteral(inputVal, inputType), targetType);
+
+                if (expectedRes instanceof Exception)
+                    assertThrows(qry, (Class<? extends 
Exception>)expectedRes.getClass(), ((Throwable)expectedRes).getMessage());
+                else
+                    assertQuery(qry).returns(expectedRes);
+            }
+        }
+    }
+
+    /** */
+    private static String asLiteral(Object val, RelDataType type) {
+        return SqlTypeUtil.isCharacter(type) ? String.format("'%s'", val) : 
String.valueOf(val);
+    }
+
+    /** */
+    private static List<List<Object>> numericsToCast() {
+        IgniteTypeFactory tf = Commons.typeFactory();
+
+        RelDataType varcharType = tf.createSqlType(SqlTypeName.VARCHAR);
+        RelDataType tinyIntType = tf.createSqlType(SqlTypeName.TINYINT);
+        RelDataType smallIntType = tf.createSqlType(SqlTypeName.SMALLINT);
+        RelDataType integerType = tf.createSqlType(SqlTypeName.INTEGER);
+        RelDataType bigintType = tf.createSqlType(SqlTypeName.BIGINT);
+        RelDataType realType = tf.createSqlType(SqlTypeName.REAL);
+        RelDataType doubleType = tf.createSqlType(SqlTypeName.DOUBLE);
+
+        Exception overflowErr = new 
IllegalArgumentException(IgniteSqlFunctions.NUMERIC_OVERFLOW_ERROR);
+        Exception numFormatErr = new NumberFormatException("is neither a 
decimal digit number");
+
+        //noinspection RedundantTypeArguments (explicit type arguments speedup 
compilation and analysis time)
+        return F.<List<Object>>asList(
+            // String
+            F.asList(varcharType, "100", decimalType(3), new 
BigDecimal("100")),

Review Comment:
   Used in `asLiteral()`



-- 
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-unsubscr...@ignite.apache.org

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

Reply via email to