You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by al...@apache.org on 2022/04/04 14:03:56 UTC

[ignite] branch master updated: IGNITE-16711 SQL Calcite: Fix numeric dynamic parameters conversion - Fixes #9927.

This is an automated email from the ASF dual-hosted git repository.

alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 1e9108e62b1 IGNITE-16711 SQL Calcite: Fix numeric dynamic parameters conversion - Fixes #9927.
1e9108e62b1 is described below

commit 1e9108e62b1765774e39580a0adee714bcd06dcf
Author: Aleksey Plekhanov <pl...@gmail.com>
AuthorDate: Mon Apr 4 16:43:59 2022 +0300

    IGNITE-16711 SQL Calcite: Fix numeric dynamic parameters conversion - Fixes #9927.
    
    Signed-off-by: Aleksey Plekhanov <pl...@gmail.com>
---
 .../query/calcite/exec/ExecutionContext.java       |  8 +++
 .../query/calcite/exec/exp/IgniteRexBuilder.java   | 44 ++++++++++++++++
 .../query/calcite/exec/exp/RexToLixTranslator.java | 16 ++++--
 .../calcite/exec/exp/agg/AccumulatorsFactory.java  |  3 +-
 .../query/calcite/prepare/BaseQueryContext.java    |  3 +-
 .../calcite/prepare/ddl/ColumnDefinition.java      |  5 +-
 .../calcite/schema/CacheTableDescriptorImpl.java   | 11 ++--
 .../schema/SystemViewTableDescriptorImpl.java      |  4 +-
 .../query/calcite/util/IgniteMethod.java           |  4 ++
 .../processors/query/calcite/util/TypeUtils.java   | 48 ++++++++++++++++-
 .../calcite/{ => integration}/DataTypesTest.java   | 60 +++++++++++++++++++++-
 .../ignite/testsuites/IntegrationTestSuite.java    |  2 +-
 12 files changed, 191 insertions(+), 17 deletions(-)

diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionContext.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionContext.java
index fd0cf9ee992..87fe624ada3 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionContext.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionContext.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.processors.query.calcite.exec;
 
+import java.lang.reflect.Type;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -236,6 +237,13 @@ public class ExecutionContext<Row> extends AbstractQueryContext implements DataC
         return baseDataContext.get(name);
     }
 
+    /** */
+    public Object getParameter(String name, Type storageType) {
+        assert name.startsWith("?") : name;
+
+        return TypeUtils.toInternal(this, params.get(name), storageType);
+    }
+
     /**
      * Gets correlated value.
      *
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteRexBuilder.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteRexBuilder.java
new file mode 100644
index 00000000000..7d250c3fc7d
--- /dev/null
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteRexBuilder.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.exec.exp;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.ignite.internal.processors.query.calcite.util.TypeUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+/** */
+public class IgniteRexBuilder extends RexBuilder {
+    /** */
+    public IgniteRexBuilder(RelDataTypeFactory typeFactory) {
+        super(typeFactory);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected RexLiteral makeLiteral(@Nullable Comparable o, RelDataType type, SqlTypeName typeName) {
+        if (o != null && typeName == SqlTypeName.DECIMAL && TypeUtils.hasScale(type))
+            return super.makeLiteral(((BigDecimal)o).setScale(type.getScale(), RoundingMode.HALF_UP), type, typeName);
+
+        return super.makeLiteral(o, type, typeName);
+    }
+}
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/RexToLixTranslator.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/RexToLixTranslator.java
index b5440fca198..23ae2d2486b 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/RexToLixTranslator.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/RexToLixTranslator.java
@@ -70,6 +70,7 @@ import org.apache.calcite.sql.validate.SqlConformance;
 import org.apache.calcite.util.BuiltInMethod;
 import org.apache.calcite.util.ControlFlowException;
 import org.apache.calcite.util.Pair;
+import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
 import org.apache.ignite.internal.processors.query.calcite.util.IgniteMethod;
 
 import static org.apache.calcite.sql.fun.SqlStdOperatorTable.CASE;
@@ -176,7 +177,7 @@ public class RexToLixTranslator implements RexVisitor<RexToLixTranslator.Result>
                 storageTypes.add(outputPhysType.getJavaFieldType(i));
         }
         return new RexToLixTranslator(program, typeFactory, root, inputGetter,
-            list, new RexBuilder(typeFactory), conformance, null)
+            list, new IgniteRexBuilder(typeFactory), conformance, null)
             .setCorrelates(correlates)
             .translateList(program.getProjectList(), storageTypes);
     }
@@ -1188,10 +1189,15 @@ public class RexToLixTranslator implements RexVisitor<RexToLixTranslator.Result>
 
         final Type storageType = currentStorageType != null
             ? currentStorageType : typeFactory.getJavaClass(dynamicParam.getType());
-        final Expression valueExpression = ConverterUtils.convert(
-            Expressions.call(root, BuiltInMethod.DATA_CONTEXT_GET.method,
-                Expressions.constant("?" + dynamicParam.getIndex())),
-            storageType);
+
+        final Type paramType = ((IgniteTypeFactory)typeFactory).getResultClass(dynamicParam.getType());
+
+        final Expression ctxGet = Expressions.call(root, IgniteMethod.CONTEXT_GET_PARAMETER_VALUE.method(),
+            Expressions.constant("?" + dynamicParam.getIndex()), Expressions.constant(paramType));
+
+        final Expression valueExpression = SqlTypeUtil.isDecimal(dynamicParam.getType()) ?
+            ConverterUtils.convertToDecimal(ctxGet, dynamicParam.getType()) : ConverterUtils.convert(ctxGet, storageType);
+
         final ParameterExpression valueVariable =
             Expressions.parameter(valueExpression.getType(), list.newName("value_dynamic_param"));
         list.add(Expressions.declare(Modifier.FINAL, valueVariable, valueExpression));
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/AccumulatorsFactory.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/AccumulatorsFactory.java
index b3e3ba94cdf..6be23a8047c 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/AccumulatorsFactory.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/agg/AccumulatorsFactory.java
@@ -49,6 +49,7 @@ import org.apache.calcite.util.Pair;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
 import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.IgniteRexBuilder;
 import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
 import org.apache.ignite.internal.processors.query.calcite.util.Commons;
 import org.apache.ignite.internal.util.typedef.F;
@@ -118,7 +119,7 @@ public class AccumulatorsFactory<Row> implements Supplier<List<AccumulatorWrappe
                         PhysTypeImpl.of(typeFactory, rowType,
                             JavaRowFormat.SCALAR, false))));
 
-        RexBuilder builder = new RexBuilder(typeFactory);
+        RexBuilder builder = new IgniteRexBuilder(typeFactory);
         RexProgramBuilder programBuilder = new RexProgramBuilder(rowType, builder);
         RexNode cast = builder.makeCast(to, builder.makeInputRef(from, 0));
         programBuilder.addProject(cast, null);
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/BaseQueryContext.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/BaseQueryContext.java
index f45e2a76bd9..b9ab3114435 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/BaseQueryContext.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/BaseQueryContext.java
@@ -46,6 +46,7 @@ import org.apache.calcite.tools.FrameworkConfig;
 import org.apache.calcite.tools.Frameworks;
 import org.apache.ignite.IgniteLogger;
 import org.apache.ignite.internal.processors.query.GridQueryCancel;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.IgniteRexBuilder;
 import org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
 import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
 import org.apache.ignite.logger.NullLogger;
@@ -104,7 +105,7 @@ public final class BaseQueryContext extends AbstractQueryContext {
         RelDataTypeSystem typeSys = CALCITE_CONNECTION_CONFIG.typeSystem(RelDataTypeSystem.class, FRAMEWORK_CONFIG.getTypeSystem());
         TYPE_FACTORY = new IgniteTypeFactory(typeSys);
 
-        REX_BUILDER = new RexBuilder(TYPE_FACTORY);
+        REX_BUILDER = new IgniteRexBuilder(TYPE_FACTORY);
 
         CLUSTER = RelOptCluster.create(EMPTY_PLANNER, REX_BUILDER);
 
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/ddl/ColumnDefinition.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/ddl/ColumnDefinition.java
index b865078f365..540b73daca0 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/ddl/ColumnDefinition.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/ddl/ColumnDefinition.java
@@ -19,6 +19,7 @@ package org.apache.ignite.internal.processors.query.calcite.prepare.ddl;
 import java.util.Objects;
 
 import org.apache.calcite.rel.type.RelDataType;
+import org.apache.ignite.internal.processors.query.calcite.util.TypeUtils;
 import org.jetbrains.annotations.Nullable;
 
 /** Definies a particular column within table. */
@@ -71,13 +72,13 @@ public class ColumnDefinition {
      * @return Column's precision.
      */
     public Integer precision() {
-        return type.getPrecision() != RelDataType.PRECISION_NOT_SPECIFIED ? type.getPrecision() : null;
+        return TypeUtils.hasPrecision(type) ? type.getPrecision() : null;
     }
 
     /**
      * @return Column's scale.
      */
     public Integer scale() {
-        return type.getScale() != RelDataType.SCALE_NOT_SPECIFIED ? type.getScale() : null;
+        return TypeUtils.hasScale(type) ? type.getScale() : null;
     }
 }
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableDescriptorImpl.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableDescriptorImpl.java
index f84847efaf4..5eb394c2f41 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableDescriptorImpl.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/CacheTableDescriptorImpl.java
@@ -75,6 +75,8 @@ import org.jetbrains.annotations.Nullable;
 
 import static java.util.Collections.emptyList;
 import static java.util.Collections.singletonList;
+import static org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED;
+import static org.apache.calcite.rel.type.RelDataType.SCALE_NOT_SPECIFIED;
 
 /**
  *
@@ -685,7 +687,7 @@ public class CacheTableDescriptorImpl extends NullInitializerExpressionFactory
         /** {@inheritDoc} */
         @Override public RelDataType logicalType(IgniteTypeFactory f) {
             if (logicalType == null)
-                logicalType = TypeUtils.sqlType(f, f.createJavaType(storageType));
+                logicalType = TypeUtils.sqlType(f, storageType, PRECISION_NOT_SPECIFIED, SCALE_NOT_SPECIFIED);
 
             return logicalType;
         }
@@ -764,8 +766,11 @@ public class CacheTableDescriptorImpl extends NullInitializerExpressionFactory
 
         /** {@inheritDoc} */
         @Override public RelDataType logicalType(IgniteTypeFactory f) {
-            if (logicalType == null)
-                logicalType = TypeUtils.sqlType(f, f.createJavaType(storageType));
+            if (logicalType == null) {
+                logicalType = TypeUtils.sqlType(f, storageType,
+                    desc.precision() == -1 ? PRECISION_NOT_SPECIFIED : desc.precision(),
+                    desc.scale() == -1 ? SCALE_NOT_SPECIFIED : desc.scale());
+            }
 
             return logicalType;
         }
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/SystemViewTableDescriptorImpl.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/SystemViewTableDescriptorImpl.java
index 753dfce84a1..921b96a2910 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/SystemViewTableDescriptorImpl.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/SystemViewTableDescriptorImpl.java
@@ -42,6 +42,8 @@ import org.apache.ignite.spi.systemview.view.SystemView;
 import org.apache.ignite.spi.systemview.view.SystemViewRowAttributeWalker;
 import org.jetbrains.annotations.Nullable;
 
+import static org.apache.calcite.rel.type.RelDataType.PRECISION_NOT_SPECIFIED;
+import static org.apache.calcite.rel.type.RelDataType.SCALE_NOT_SPECIFIED;
 import static org.apache.ignite.internal.processors.metric.impl.MetricUtils.toSqlName;
 
 /**
@@ -259,7 +261,7 @@ public class SystemViewTableDescriptorImpl<ViewRow> extends NullInitializerExpre
         /** {@inheritDoc} */
         @Override public RelDataType logicalType(IgniteTypeFactory f) {
             if (logicalType == null)
-                logicalType = TypeUtils.sqlType(f, f.createJavaType(type));
+                logicalType = TypeUtils.sqlType(f, type, PRECISION_NOT_SPECIFIED, SCALE_NOT_SPECIFIED);
 
             return logicalType;
         }
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/IgniteMethod.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/IgniteMethod.java
index 8e065c79c55..16d9f0322be 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/IgniteMethod.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/IgniteMethod.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.processors.query.calcite.util;
 
 import java.lang.reflect.Method;
+import java.lang.reflect.Type;
 import org.apache.calcite.avatica.util.ByteString;
 import org.apache.calcite.linq4j.tree.Types;
 import org.apache.calcite.sql.SqlIntervalQualifier;
@@ -53,6 +54,9 @@ public enum IgniteMethod {
     /** See {@link ExecutionContext#getCorrelated(int)} */
     CONTEXT_GET_CORRELATED_VALUE(ExecutionContext.class, "getCorrelated", int.class),
 
+    /** See {@link ExecutionContext#getParameter(String, Type)} */
+    CONTEXT_GET_PARAMETER_VALUE(ExecutionContext.class, "getParameter", String.class, Type.class),
+
     /** See {@link SingleScalar#execute(ExecutionContext, Object, Object)} */
     SCALAR_EXECUTE(SingleScalar.class, "execute", ExecutionContext.class, Object.class, Object.class),
 
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
index 7c59c3814e6..a01b6d90d2f 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/util/TypeUtils.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.processors.query.calcite.util;
 
 import java.lang.reflect.Type;
+import java.math.BigDecimal;
 import java.sql.Date;
 import java.sql.Time;
 import java.sql.Timestamp;
@@ -64,6 +65,7 @@ import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
 import org.apache.ignite.internal.processors.query.calcite.schema.ColumnDescriptor;
 import org.apache.ignite.internal.processors.query.calcite.schema.TableDescriptor;
 import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
 import org.apache.ignite.internal.util.typedef.F;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -168,7 +170,18 @@ public class TypeUtils {
     }
 
     /** */
-    public static RelDataType sqlType(IgniteTypeFactory typeFactory, RelDataType rowType) {
+    public static RelDataType sqlType(IgniteTypeFactory typeFactory, Class<?> cls, int precision, int scale) {
+        RelDataType javaType = typeFactory.createJavaType(cls);
+
+        if (javaType.getSqlTypeName().allowsPrecScale(true, true) &&
+            (precision != RelDataType.PRECISION_NOT_SPECIFIED || scale != RelDataType.SCALE_NOT_SPECIFIED))
+            return typeFactory.createSqlType(javaType.getSqlTypeName(), precision, scale);
+
+        return sqlType(typeFactory, javaType);
+    }
+
+    /** */
+    private static RelDataType sqlType(IgniteTypeFactory typeFactory, RelDataType rowType) {
         if (!rowType.isStruct())
             return typeFactory.toSql(rowType);
 
@@ -273,6 +286,26 @@ public class TypeUtils {
             .anyMatch(TypeUtils::isConvertableType);
     }
 
+    /** */
+    public static boolean hasPrecision(RelDataType type) {
+        // Special case for DECIMAL type without precision and scale specified.
+        if (type.getSqlTypeName() == SqlTypeName.DECIMAL &&
+            type.getPrecision() == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL))
+            return false;
+
+        return type.getPrecision() != RelDataType.PRECISION_NOT_SPECIFIED;
+    }
+
+    /** */
+    public static boolean hasScale(RelDataType type) {
+        // Special case for DECIMAL type without precision and scale specified.
+        if (type.getSqlTypeName() == SqlTypeName.DECIMAL &&
+            type.getPrecision() == IgniteTypeSystem.INSTANCE.getDefaultPrecision(SqlTypeName.DECIMAL))
+            return false;
+
+        return type.getScale() != RelDataType.SCALE_NOT_SPECIFIED;
+    }
+
     /** */
     public static Object toInternal(DataContext ctx, Object val) {
         return val == null ? null : toInternal(ctx, val, val.getClass());
@@ -298,6 +331,19 @@ public class TypeUtils {
             return (int)((Period)val).toTotalMonths();
         else if (storageType == byte[].class)
             return new ByteString((byte[])val);
+        else if (val instanceof Number && storageType != val.getClass()) {
+            // For dynamic parameters we don't know exact parameter type in compile time. To avoid casting errors in
+            // runtime we should convert parameter value to expected type.
+            Number num = (Number)val;
+
+            return Byte.class.equals(storageType) || byte.class.equals(storageType) ? SqlFunctions.toByte(num) :
+                Short.class.equals(storageType) || short.class.equals(storageType) ? SqlFunctions.toShort(num) :
+                Integer.class.equals(storageType) || int.class.equals(storageType) ? SqlFunctions.toInt(num) :
+                Long.class.equals(storageType) || long.class.equals(storageType) ? SqlFunctions.toLong(num) :
+                Float.class.equals(storageType) || float.class.equals(storageType) ? SqlFunctions.toFloat(num) :
+                Double.class.equals(storageType) || double.class.equals(storageType) ? SqlFunctions.toDouble(num) :
+                BigDecimal.class.equals(storageType) ? SqlFunctions.toBigDecimal(num) : num;
+        }
         else
             return val;
     }
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/DataTypesTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DataTypesTest.java
similarity index 81%
rename from modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/DataTypesTest.java
rename to modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DataTypesTest.java
index 82cd4523379..b4992745a26 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/DataTypesTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DataTypesTest.java
@@ -15,8 +15,9 @@
  * limitations under the License.
  */
 
-package org.apache.ignite.internal.processors.query.calcite;
+package org.apache.ignite.internal.processors.query.calcite.integration;
 
+import java.math.BigDecimal;
 import java.util.List;
 import java.util.Objects;
 import java.util.UUID;
@@ -25,7 +26,6 @@ import com.google.common.collect.ImmutableSet;
 import org.apache.ignite.IgniteCache;
 import org.apache.ignite.cache.QueryEntity;
 import org.apache.ignite.configuration.CacheConfiguration;
-import org.apache.ignite.internal.processors.query.calcite.integration.AbstractBasicIntegrationTest;
 import org.apache.ignite.internal.util.typedef.F;
 import org.junit.Test;
 
@@ -345,4 +345,60 @@ public class DataTypesTest extends AbstractBasicIntegrationTest {
             executeSql("DROP TABLE IF EXISTS tbl");
         }
     }
+
+    /** */
+    @Test
+    public void testDecimalScale() {
+        sql("CREATE TABLE t (id INT PRIMARY KEY, val1 DECIMAL(5, 3), val2 DECIMAL(3), val3 DECIMAL)");
+
+        // Check literals scale.
+        sql("INSERT INTO t values (0, 0, 0, 0)");
+        sql("INSERT INTO t values (1.1, 1.1, 1.1, 1.1)");
+        sql("INSERT INTO t values (2.123, 2.123, 2.123, 2.123)");
+        sql("INSERT INTO t values (3.123456, 3.123456, 3.123456, 3.123456)");
+
+        // Check dynamic parameters scale.
+        List<Number> params = F.asList(4, 5L, 6f, 7.25f, 8d, 9.03125d, new BigDecimal("10"),
+            new BigDecimal("11.1"), new BigDecimal("12.123456"));
+
+        for (Object val : params)
+            sql("INSERT INTO t values (?, ?, ?, ?)", val, val, val, val);
+
+        assertQuery("SELECT * FROM t")
+            .returns(0, new BigDecimal("0.000"), new BigDecimal("0"), new BigDecimal("0"))
+            .returns(1, new BigDecimal("1.100"), new BigDecimal("1"), new BigDecimal("1.1"))
+            .returns(2, new BigDecimal("2.123"), new BigDecimal("2"), new BigDecimal("2.123"))
+            .returns(3, new BigDecimal("3.123"), new BigDecimal("3"), new BigDecimal("3.123456"))
+            .returns(4, new BigDecimal("4.000"), new BigDecimal("4"), new BigDecimal("4"))
+            .returns(5, new BigDecimal("5.000"), new BigDecimal("5"), new BigDecimal("5"))
+            .returns(6, new BigDecimal("6.000"), new BigDecimal("6"), new BigDecimal("6"))
+            .returns(7, new BigDecimal("7.250"), new BigDecimal("7"), new BigDecimal("7.25"))
+            .returns(8, new BigDecimal("8.000"), new BigDecimal("8"), new BigDecimal("8"))
+            .returns(9, new BigDecimal("9.031"), new BigDecimal("9"), new BigDecimal("9.03125"))
+            .returns(10, new BigDecimal("10.000"), new BigDecimal("10"), new BigDecimal("10"))
+            .returns(11, new BigDecimal("11.100"), new BigDecimal("11"), new BigDecimal("11.1"))
+            .returns(12, new BigDecimal("12.123"), new BigDecimal("12"), new BigDecimal("12.123456"))
+            .check();
+    }
+
+    /** */
+    @Test
+    public void testNumericConversion() {
+        sql("CREATE TABLE t (v1 TINYINT, v2 SMALLINT, v3 INT, v4 BIGINT, v5 DECIMAL, v6 FLOAT, v7 DOUBLE)");
+
+        List<Number> params = F.asList((byte)1, (short)2, 3, 4L, BigDecimal.valueOf(5), 6f, 7d);
+
+        for (Object val : params)
+            sql("INSERT INTO t values (?, ?, ?, ?, ?, ?, ?)", val, val, val, val, val, val, val);
+
+        assertQuery("SELECT * FROM t")
+            .returns((byte)1, (short)1, 1, 1L, BigDecimal.valueOf(1), 1f, 1d)
+            .returns((byte)2, (short)2, 2, 2L, BigDecimal.valueOf(2), 2f, 2d)
+            .returns((byte)3, (short)3, 3, 3L, BigDecimal.valueOf(3), 3f, 3d)
+            .returns((byte)4, (short)4, 4, 4L, BigDecimal.valueOf(4), 4f, 4d)
+            .returns((byte)5, (short)5, 5, 5L, BigDecimal.valueOf(5), 5f, 5d)
+            .returns((byte)6, (short)6, 6, 6L, BigDecimal.valueOf(6), 6f, 6d)
+            .returns((byte)7, (short)7, 7, 7L, BigDecimal.valueOf(7), 7f, 7d)
+            .check();
+    }
 }
diff --git a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
index 67fd87ccb01..ef4ece9de47 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/testsuites/IntegrationTestSuite.java
@@ -19,7 +19,6 @@ package org.apache.ignite.testsuites;
 
 import org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessorTest;
 import org.apache.ignite.internal.processors.query.calcite.CancelTest;
-import org.apache.ignite.internal.processors.query.calcite.DataTypesTest;
 import org.apache.ignite.internal.processors.query.calcite.DateTimeTest;
 import org.apache.ignite.internal.processors.query.calcite.FunctionsTest;
 import org.apache.ignite.internal.processors.query.calcite.LimitOffsetTest;
@@ -30,6 +29,7 @@ import org.apache.ignite.internal.processors.query.calcite.integration.Aggregate
 import org.apache.ignite.internal.processors.query.calcite.integration.CalciteBasicSecondaryIndexIntegrationTest;
 import org.apache.ignite.internal.processors.query.calcite.integration.CalciteErrorHandlilngIntegrationTest;
 import org.apache.ignite.internal.processors.query.calcite.integration.CorrelatesIntegrationTest;
+import org.apache.ignite.internal.processors.query.calcite.integration.DataTypesTest;
 import org.apache.ignite.internal.processors.query.calcite.integration.HashSpoolIntegrationTest;
 import org.apache.ignite.internal.processors.query.calcite.integration.IndexDdlIntegrationTest;
 import org.apache.ignite.internal.processors.query.calcite.integration.IndexRebuildIntegrationTest;