Skip to content

Commit bc16742

Browse files
committed
IGNITE-13825: Fix precision and scale for columns in SQL result set
1 parent e8b9170 commit bc16742

File tree

5 files changed

+144
-4
lines changed

5 files changed

+144
-4
lines changed

modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/odbc/OdbcUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public static long rowsAffected(QueryCursor<List<?>> qryCur) {
256256
* @return Odbc query field metadata.
257257
*/
258258
public static Collection<OdbcColumnMeta> convertMetadata(Collection<GridQueryFieldMetadata> meta,
259-
ClientListenerProtocolVersion ver) {
259+
ClientListenerProtocolVersion ver) {
260260
List<OdbcColumnMeta> res = new ArrayList<>();
261261

262262
if (meta != null) {

modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,13 @@ public static void validateTypeDescriptor(GridQueryTypeDescriptor type)
706706
private static String dbTypeFromClass(Class<?> cls, int precision, int scale) {
707707
String dbType = H2DatabaseType.fromClass(cls).dBTypeAsString();
708708

709-
if (precision != -1 && dbType.equalsIgnoreCase(H2DatabaseType.VARCHAR.dBTypeAsString()))
710-
return dbType + "(" + precision + ")";
709+
if (precision != -1 && scale != -1 && dbType.equalsIgnoreCase(H2DatabaseType.DECIMAL.dBTypeAsString()))
710+
return dbType + "(" + precision + ", " + scale + ')';
711+
712+
if (precision != -1 && (
713+
dbType.equalsIgnoreCase(H2DatabaseType.VARCHAR.dBTypeAsString())
714+
|| dbType.equalsIgnoreCase(H2DatabaseType.DECIMAL.dBTypeAsString())))
715+
return dbType + '(' + precision + ')';
711716

712717
return dbType;
713718
}

modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheAbstractFieldsQuerySelfTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.ignite.internal.processors.cache;
1919

2020
import java.io.Serializable;
21+
import java.math.BigDecimal;
2122
import java.util.ArrayList;
2223
import java.util.Collection;
2324
import java.util.Collections;
@@ -336,7 +337,8 @@ else if (strCache.getName().equals(meta.cacheName())) {
336337
}
337338
else if (DEFAULT_CACHE_NAME.equals(meta.cacheName()) || noOpCache.getName().equals(meta.cacheName()))
338339
assertTrue("Invalid types size", types.isEmpty());
339-
else if (!"cacheWithCustomKeyPrecision".equalsIgnoreCase(meta.cacheName()))
340+
else if (!"cacheWithCustomKeyPrecision".equalsIgnoreCase(meta.cacheName()) &&
341+
!"cacheWithDecimalPrecisionAndScale".equalsIgnoreCase(meta.cacheName()))
340342
fail("Unknown cache: " + meta.cacheName());
341343
}
342344
}
@@ -407,6 +409,51 @@ public void testExecuteWithMetaDataAndPrecision() throws Exception {
407409
}
408410
}
409411

412+
/**
413+
* Test that scale and precision returned correctly for Decimal column in result set:
414+
*
415+
* 1. Start node;
416+
* 2. Create table with Decimal(3,0) column;
417+
* 3. Insert a new row into the table;
418+
* 4. Execute select with decimal row;
419+
* 5. Check that selected decimal column has precision 3 and scale 0.
420+
*
421+
* @throws Exception If failed.
422+
*/
423+
@Test
424+
public void testDecimalColumnScaleAndPrecision() throws Exception {
425+
QueryEntity qeWithPrecision = new QueryEntity()
426+
.setKeyType("java.lang.Long")
427+
.setValueType("TestType")
428+
.addQueryField("age", "java.math.BigDecimal", "age")
429+
.setFieldsPrecision(ImmutableMap.of("age", 3))
430+
.setFieldsScale(ImmutableMap.of("age", 0));
431+
432+
grid(0).getOrCreateCache(cacheConfiguration()
433+
.setName("cacheWithDecimalPrecisionAndScale")
434+
.setQueryEntities(Collections.singleton(qeWithPrecision)));
435+
436+
GridQueryProcessor qryProc = grid(0).context().query();
437+
438+
qryProc.querySqlFields(
439+
new SqlFieldsQuery("INSERT INTO TestType(_key, age) VALUES(?, ?)")
440+
.setSchema("cacheWithDecimalPrecisionAndScale")
441+
.setArgs(1, new BigDecimal(160)), true);
442+
443+
QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>)qryProc.querySqlFields(
444+
new SqlFieldsQuery("SELECT age FROM TestType")
445+
.setSchema("cacheWithDecimalPrecisionAndScale"), true);
446+
447+
List<GridQueryFieldMetadata> fieldsMeta = cursor.fieldsMeta();
448+
449+
assertEquals(1, fieldsMeta.size());
450+
451+
GridQueryFieldMetadata meta = fieldsMeta.get(0);
452+
453+
assertEquals(3, meta.precision());
454+
assertEquals(0, meta.scale());
455+
}
456+
410457
@Test
411458
public void testExecuteWithMetaDataAndCustomKeyPrecision() throws Exception {
412459
QueryEntity qeWithPrecision = new QueryEntity()
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.processors.query;
19+
20+
import org.apache.ignite.cache.query.SqlFieldsQuery;
21+
import org.apache.ignite.internal.IgniteEx;
22+
import org.apache.ignite.internal.processors.cache.QueryCursorImpl;
23+
import org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest;
24+
import org.junit.Test;
25+
26+
import java.math.BigDecimal;
27+
import java.util.List;
28+
29+
/**
30+
* Tests for result set metadata.
31+
*/
32+
public class SqlResultSetMetaSelfTest extends AbstractIndexingCommonTest {
33+
/** Node. */
34+
private IgniteEx node;
35+
36+
/** {@inheritDoc} */
37+
@Override protected void beforeTest() throws Exception {
38+
node = (IgniteEx)startGrid();
39+
}
40+
41+
/** {@inheritDoc} */
42+
@Override protected void afterTest() throws Exception {
43+
stopAllGrids();
44+
45+
node = null;
46+
}
47+
48+
/**
49+
* Test that scale and precision returned correctly for Decimal column in result set:
50+
*
51+
* 1. Start node;
52+
* 2. Create table with Decimal(3,0) column;
53+
* 3. Insert a new row into the table;
54+
* 4. Execute select with decimal row;
55+
* 5. Check that selected decimal column has precision 3 and scale 0.
56+
*
57+
* @throws Exception If failed.
58+
*/
59+
@Test
60+
public void testDecimalColumnScaleAndPrecision() throws Exception {
61+
GridQueryProcessor qryProc = node.context().query();
62+
63+
qryProc.querySqlFields(
64+
new SqlFieldsQuery("CREATE TABLE Person(id int, age decimal(3,0), primary key (id))")
65+
.setSchema("PUBLIC"), true);
66+
67+
qryProc.querySqlFields(
68+
new SqlFieldsQuery("INSERT INTO Person(id, age) VALUES(?, ?)")
69+
.setSchema("PUBLIC")
70+
.setArgs(1, new BigDecimal(160)), true);
71+
72+
QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>)qryProc.querySqlFields(
73+
new SqlFieldsQuery("SELECT age FROM Person")
74+
.setSchema("PUBLIC"), true);
75+
76+
List<GridQueryFieldMetadata> fieldsMeta = cursor.fieldsMeta();
77+
78+
assertEquals(1, fieldsMeta.size());
79+
80+
GridQueryFieldMetadata meta = fieldsMeta.get(0);
81+
82+
assertEquals(3, meta.precision());
83+
assertEquals(0, meta.scale());
84+
}
85+
}

modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@
233233
import org.apache.ignite.internal.processors.query.SqlPushDownFunctionTest;
234234
import org.apache.ignite.internal.processors.query.SqlQueryHistoryFromClientSelfTest;
235235
import org.apache.ignite.internal.processors.query.SqlQueryHistorySelfTest;
236+
import org.apache.ignite.internal.processors.query.SqlResultSetMetaSelfTest;
236237
import org.apache.ignite.internal.processors.query.SqlSchemaSelfTest;
237238
import org.apache.ignite.internal.processors.query.SqlSystemViewsSelfTest;
238239
import org.apache.ignite.internal.processors.query.h2.GridIndexRebuildSelfTest;
@@ -314,6 +315,8 @@
314315
SqlIllegalSchemaSelfTest.class,
315316
MultipleStatementsSqlQuerySelfTest.class,
316317

318+
SqlResultSetMetaSelfTest.class,
319+
317320
BasicIndexTest.class,
318321
ArrayIndexTest.class,
319322
BasicIndexMultinodeTest.class,

0 commit comments

Comments
 (0)