You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by is...@apache.org on 2020/12/08 08:58:32 UTC

[ignite] branch master updated: IGNITE-13825: Fix precision and scale for columns in SQL result set

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

isapego 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 03c466b  IGNITE-13825: Fix precision and scale for columns in SQL result set
03c466b is described below

commit 03c466bc8fe6c90fc0a3c2cfac3fdf649a41b49e
Author: Igor Sapego <is...@apache.org>
AuthorDate: Tue Dec 8 11:57:48 2020 +0300

    IGNITE-13825: Fix precision and scale for columns in SQL result set
    
    This closes #8551
---
 .../internal/processors/odbc/odbc/OdbcUtils.java   |  2 +-
 .../internal/processors/query/h2/H2Utils.java      |  9 ++-
 .../IgniteCacheAbstractFieldsQuerySelfTest.java    | 49 ++++++++++++-
 .../processors/query/SqlResultSetMetaSelfTest.java | 84 ++++++++++++++++++++++
 .../IgniteBinaryCacheQueryTestSuite.java           |  3 +
 5 files changed, 143 insertions(+), 4 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/odbc/OdbcUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/odbc/OdbcUtils.java
index 110f71c..80fab2e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/odbc/OdbcUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/odbc/OdbcUtils.java
@@ -256,7 +256,7 @@ public class OdbcUtils {
      * @return Odbc query field metadata.
      */
     public static Collection<OdbcColumnMeta> convertMetadata(Collection<GridQueryFieldMetadata> meta,
-                                                              ClientListenerProtocolVersion ver) {
+        ClientListenerProtocolVersion ver) {
         List<OdbcColumnMeta> res = new ArrayList<>();
 
         if (meta != null) {
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java
index ecd8169..f766de9 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java
@@ -706,8 +706,13 @@ public class H2Utils {
     private static String dbTypeFromClass(Class<?> cls, int precision, int scale) {
         String dbType = H2DatabaseType.fromClass(cls).dBTypeAsString();
 
-        if (precision != -1 && dbType.equalsIgnoreCase(H2DatabaseType.VARCHAR.dBTypeAsString()))
-            return dbType + "(" + precision + ")";
+        if (precision != -1 && scale != -1 && dbType.equalsIgnoreCase(H2DatabaseType.DECIMAL.dBTypeAsString()))
+            return dbType + "(" + precision + ", " + scale + ')';
+
+        if (precision != -1 && (
+                dbType.equalsIgnoreCase(H2DatabaseType.VARCHAR.dBTypeAsString())
+                        || dbType.equalsIgnoreCase(H2DatabaseType.DECIMAL.dBTypeAsString())))
+            return dbType + '(' + precision + ')';
 
         return dbType;
     }
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java
index 19226a3..b4497c2 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.internal.processors.cache;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -336,7 +337,8 @@ public abstract class IgniteCacheAbstractFieldsQuerySelfTest extends GridCommonA
                 }
                 else if (DEFAULT_CACHE_NAME.equals(meta.cacheName()) || noOpCache.getName().equals(meta.cacheName()))
                     assertTrue("Invalid types size", types.isEmpty());
-                else if (!"cacheWithCustomKeyPrecision".equalsIgnoreCase(meta.cacheName()))
+                else if (!"cacheWithCustomKeyPrecision".equalsIgnoreCase(meta.cacheName()) &&
+                         !"cacheWithDecimalPrecisionAndScale".equalsIgnoreCase(meta.cacheName()))
                     fail("Unknown cache: " + meta.cacheName());
             }
         }
@@ -407,6 +409,51 @@ public abstract class IgniteCacheAbstractFieldsQuerySelfTest extends GridCommonA
         }
     }
 
+    /**
+     * Test that scale and precision returned correctly for Decimal column in result set:
+     *
+     * 1. Start node;
+     * 2. Create table with Decimal(3,0) column;
+     * 3. Insert a new row into the table;
+     * 4. Execute select with decimal row;
+     * 5. Check that selected decimal column has precision 3 and scale 0.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testDecimalColumnScaleAndPrecision() throws Exception {
+        QueryEntity qeWithPrecision = new QueryEntity()
+                .setKeyType("java.lang.Long")
+                .setValueType("TestType")
+                .addQueryField("age", "java.math.BigDecimal", "age")
+                .setFieldsPrecision(ImmutableMap.of("age", 3))
+                .setFieldsScale(ImmutableMap.of("age", 0));
+
+        grid(0).getOrCreateCache(cacheConfiguration()
+                .setName("cacheWithDecimalPrecisionAndScale")
+                .setQueryEntities(Collections.singleton(qeWithPrecision)));
+
+        GridQueryProcessor qryProc = grid(0).context().query();
+
+        qryProc.querySqlFields(
+                new SqlFieldsQuery("INSERT INTO TestType(_key, age) VALUES(?, ?)")
+                        .setSchema("cacheWithDecimalPrecisionAndScale")
+                        .setArgs(1, new BigDecimal(160)), true);
+
+        QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>)qryProc.querySqlFields(
+                new SqlFieldsQuery("SELECT age FROM TestType")
+                        .setSchema("cacheWithDecimalPrecisionAndScale"), true);
+
+        List<GridQueryFieldMetadata> fieldsMeta = cursor.fieldsMeta();
+
+        assertEquals(1, fieldsMeta.size());
+
+        GridQueryFieldMetadata meta = fieldsMeta.get(0);
+
+        assertEquals(3, meta.precision());
+        assertEquals(0, meta.scale());
+    }
+
     @Test
     public void testExecuteWithMetaDataAndCustomKeyPrecision() throws Exception {
         QueryEntity qeWithPrecision = new QueryEntity()
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlResultSetMetaSelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlResultSetMetaSelfTest.java
new file mode 100644
index 0000000..5f6178c
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/SqlResultSetMetaSelfTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.QueryCursorImpl;
+import org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest;
+import org.junit.Test;
+
+/**
+ * Tests for result set metadata.
+ */
+public class SqlResultSetMetaSelfTest extends AbstractIndexingCommonTest {
+    /** Node. */
+    private IgniteEx node;
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        node = (IgniteEx)startGrid();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+
+        node = null;
+    }
+
+    /**
+     * Test that scale and precision returned correctly for Decimal column in result set:
+     *
+     * 1. Start node;
+     * 2. Create table with Decimal(3,0) column;
+     * 3. Insert a new row into the table;
+     * 4. Execute select with decimal row;
+     * 5. Check that selected decimal column has precision 3 and scale 0.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testDecimalColumnScaleAndPrecision() throws Exception {
+        GridQueryProcessor qryProc = node.context().query();
+
+        qryProc.querySqlFields(
+                new SqlFieldsQuery("CREATE TABLE Person(id int, age decimal(3,0), primary key (id))")
+                        .setSchema("PUBLIC"), true);
+
+        qryProc.querySqlFields(
+                new SqlFieldsQuery("INSERT INTO Person(id, age) VALUES(?, ?)")
+                        .setSchema("PUBLIC")
+                        .setArgs(1, new BigDecimal(160)), true);
+
+        QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>)qryProc.querySqlFields(
+                new SqlFieldsQuery("SELECT age FROM Person")
+                        .setSchema("PUBLIC"), true);
+
+        List<GridQueryFieldMetadata> fieldsMeta = cursor.fieldsMeta();
+
+        assertEquals(1, fieldsMeta.size());
+
+        GridQueryFieldMetadata meta = fieldsMeta.get(0);
+
+        assertEquals(3, meta.precision());
+        assertEquals(0, meta.scale());
+    }
+}
diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java
index ab6d67c..cd5ffe5 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java
@@ -233,6 +233,7 @@ import org.apache.ignite.internal.processors.query.SqlNotNullKeyValueFieldTest;
 import org.apache.ignite.internal.processors.query.SqlPushDownFunctionTest;
 import org.apache.ignite.internal.processors.query.SqlQueryHistoryFromClientSelfTest;
 import org.apache.ignite.internal.processors.query.SqlQueryHistorySelfTest;
+import org.apache.ignite.internal.processors.query.SqlResultSetMetaSelfTest;
 import org.apache.ignite.internal.processors.query.SqlSchemaSelfTest;
 import org.apache.ignite.internal.processors.query.SqlSystemViewsSelfTest;
 import org.apache.ignite.internal.processors.query.h2.GridIndexRebuildSelfTest;
@@ -314,6 +315,8 @@ import org.junit.runners.Suite;
     SqlIllegalSchemaSelfTest.class,
     MultipleStatementsSqlQuerySelfTest.class,
 
+    SqlResultSetMetaSelfTest.class,
+
     BasicIndexTest.class,
     ArrayIndexTest.class,
     BasicIndexMultinodeTest.class,