You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by iv...@apache.org on 2021/08/17 07:41:38 UTC
[ignite] branch master updated: IGNITE-15302 Add support of
precision parameter for varbinary type. - Fixes #9315.
This is an automated email from the ASF dual-hosted git repository.
ivandasch 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 3b094b1 IGNITE-15302 Add support of precision parameter for varbinary type. - Fixes #9315.
3b094b1 is described below
commit 3b094b1cefdb0a15cc8e2c6eae3d2591a804de6a
Author: Maksim Timonin <ti...@gmail.com>
AuthorDate: Tue Aug 17 10:37:15 2021 +0300
IGNITE-15302 Add support of precision parameter for varbinary type. - Fixes #9315.
Signed-off-by: Ivan Daschinsky <iv...@apache.org>
---
.../cache/query/annotations/QuerySqlField.java | 4 +-
.../processors/query/QueryTypeDescriptorImpl.java | 14 +-
.../processors/query/h2/CommandProcessor.java | 3 +-
.../internal/processors/query/h2/H2Utils.java | 3 +-
.../processors/cache/FieldsPrecisionTest.java | 200 +++++++++++++++++++++
.../IgniteCacheWithIndexingTestSuite.java | 5 +-
6 files changed, 219 insertions(+), 10 deletions(-)
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/annotations/QuerySqlField.java b/modules/core/src/main/java/org/apache/ignite/cache/query/annotations/QuerySqlField.java
index b05f6b6..1593018 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/query/annotations/QuerySqlField.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/query/annotations/QuerySqlField.java
@@ -64,9 +64,9 @@ public @interface QuerySqlField {
boolean notNull() default false;
/**
- * Specifies precision for a decimal field.
+ * Specifies field precision for variable length types - decimal, string and byte array.
*
- * @return precision for a decimal field.
+ * @return field precision for variable length types - decimal, string and byte array.
*/
int precision() default -1;
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
index 1bfff1a..4e686f3 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryTypeDescriptorImpl.java
@@ -646,11 +646,15 @@ public class QueryTypeDescriptorImpl implements GridQueryTypeDescriptor {
if (propVal == null || prop.precision() == -1)
continue;
- if (String.class == propVal.getClass() &&
- ((String)propVal).length() > prop.precision()) {
- throw new IgniteSQLException("Value for a column '" + prop.name() + "' is too long. " +
- "Maximum length: " + prop.precision() + ", actual length: " + ((CharSequence)propVal).length(),
- isKey ? TOO_LONG_KEY : TOO_LONG_VALUE);
+ if (String.class == propVal.getClass() || byte[].class == propVal.getClass()) {
+ int propValLen = String.class == propVal.getClass() ? ((String)propVal).length()
+ : ((byte[])propVal).length;
+
+ if (propValLen > prop.precision()) {
+ throw new IgniteSQLException("Value for a column '" + prop.name() + "' is too long. " +
+ "Maximum length: " + prop.precision() + ", actual length: " + propValLen,
+ isKey ? TOO_LONG_KEY : TOO_LONG_VALUE);
+ }
}
else if (BigDecimal.class == propVal.getClass()) {
BigDecimal dec = (BigDecimal)propVal;
diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/CommandProcessor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/CommandProcessor.java
index 8f5ef13..aaa19c1 100644
--- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/CommandProcessor.java
+++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/CommandProcessor.java
@@ -1173,7 +1173,8 @@ public class CommandProcessor {
if (col.getType() == Value.STRING ||
col.getType() == Value.STRING_FIXED ||
- col.getType() == Value.STRING_IGNORECASE)
+ col.getType() == Value.STRING_IGNORECASE ||
+ col.getType() == Value.BYTES)
if (col.getPrecision() < H2Utils.STRING_DEFAULT_PRECISION)
precision.put(e.getKey(), (int)col.getPrecision());
}
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 7df3838..356d46c 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
@@ -694,7 +694,8 @@ public class H2Utils {
if (precision != -1 && (
dbType.equalsIgnoreCase(H2DatabaseType.VARCHAR.dBTypeAsString())
- || dbType.equalsIgnoreCase(H2DatabaseType.DECIMAL.dBTypeAsString())))
+ || dbType.equalsIgnoreCase(H2DatabaseType.DECIMAL.dBTypeAsString())
+ || dbType.equalsIgnoreCase(H2DatabaseType.BINARY.dBTypeAsString())))
return dbType + '(' + precision + ')';
return dbType;
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/FieldsPrecisionTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/FieldsPrecisionTest.java
new file mode 100644
index 0000000..43c1f8e
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/FieldsPrecisionTest.java
@@ -0,0 +1,200 @@
+/*
+ * 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.cache;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Objects;
+import java.util.concurrent.Callable;
+import java.util.stream.Stream;
+import javax.cache.CacheException;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.QueryEntity;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+/** */
+public class FieldsPrecisionTest extends GridCommonAbstractTest {
+ /** */
+ private static final String PERSON_CACHE = "PERSON";
+
+ /** */
+ private static IgniteEx ignite;
+
+ /** */
+ private static final int KEY = 0;
+
+ /** */
+ private static final String VALID_STR = "01234";
+
+ /** */
+ private static final String INVALID_STR = "012345";
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTestsStarted() throws Exception {
+ super.beforeTestsStarted();
+
+ ignite = startGrids(2);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() throws Exception {
+ ignite.destroyCache(PERSON_CACHE);
+ }
+
+ /** */
+ @Test
+ public void testInsertTableVarColumns() {
+ checkCachePutInsert(startSqlPersonCache());
+ }
+
+ /** */
+ @Test
+ public void testInsertValueVarColumns() {
+ checkCachePutInsert(startPersonCache());
+ }
+
+ /** */
+ private void checkCachePutInsert(IgniteCache<Integer, Person> cache) {
+ Stream.of("str", "bin").forEach(fld -> {
+ Person validPerson = Person.newPerson(fld, true);
+
+ cache.put(KEY, validPerson);
+ assertEquals(validPerson, cache.get(KEY));
+
+ cache.clear();
+
+ cache.query(sqlInsertQuery(fld, VALID_STR));
+ assertEquals(validPerson, cache.get(KEY));
+
+ cache.clear();
+
+ assertPrecision(cache, () -> {
+ cache.put(KEY, Person.newPerson(fld, false));
+
+ return null;
+ }, fld.toUpperCase());
+
+ assertPrecision(cache, () -> {
+ cache.query(sqlInsertQuery(fld, INVALID_STR));
+
+ return null;
+ }, fld.toUpperCase());
+ });
+ }
+
+ /** */
+ private void assertPrecision(IgniteCache<Integer, Person> cache, Callable<Object> clo, String colName) {
+ GridTestUtils.assertThrows(null, clo, CacheException.class,
+ "Value for a column '" + colName + "' is too long. Maximum length: 5, actual length: 6");
+
+ assertNull(cache.get(KEY));
+ }
+
+ /** */
+ private IgniteCache<Integer, Person> startPersonCache() {
+ return ignite.createCache(new CacheConfiguration<Integer, Person>()
+ .setName(PERSON_CACHE)
+ .setQueryEntities(Collections.singletonList(personQueryEntity())));
+ }
+
+ /** */
+ private IgniteCache<Integer, Person> startSqlPersonCache() {
+ ignite.context().query().querySqlFields(new SqlFieldsQuery(
+ "create table " + PERSON_CACHE + "(" +
+ " id int PRIMARY KEY," +
+ " str varchar(5)," +
+ " bin binary(5)" +
+ ") with \"CACHE_NAME=" + PERSON_CACHE + ",VALUE_TYPE=" + Person.class.getName() + "\""), false);
+
+ return ignite.cache(PERSON_CACHE);
+ }
+
+ /** */
+ private SqlFieldsQuery sqlInsertQuery(String field, String s) {
+ Object arg = "bin".equalsIgnoreCase(field) ? s.getBytes(StandardCharsets.UTF_8) : s;
+
+ return new SqlFieldsQuery("insert into " + PERSON_CACHE + "(id, " + field + ") values (?, ?)")
+ .setArgs(KEY, arg);
+ }
+
+ /** */
+ private QueryEntity personQueryEntity() {
+ QueryEntity entity = new QueryEntity(Integer.class, Person.class);
+ entity.setKeyFieldName("id");
+ entity.addQueryField("id", Integer.class.getName(), "ID");
+
+ return entity;
+ }
+
+ /** */
+ static class Person {
+ /** */
+ @QuerySqlField(precision = 5)
+ private final String str;
+
+ /** */
+ @QuerySqlField(precision = 5)
+ private final byte[] bin;
+
+ /** */
+ Person(String str) {
+ this.str = str;
+ this.bin = null;
+ }
+
+ /** */
+ Person(byte[] arr) {
+ this.str = null;
+ this.bin = arr;
+ }
+
+ /** */
+ static Person newPerson(String fld, boolean valid) {
+ String s = valid ? VALID_STR : INVALID_STR;
+
+ return "bin".equals(fld) ? new Person(s.getBytes(StandardCharsets.UTF_8)) : new Person(s);
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ Person person = (Person)o;
+
+ return Objects.equals(str, person.str) && Arrays.equals(bin, person.bin);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ int result = Objects.hash(str);
+ result = 31 * result + Arrays.hashCode(bin);
+ return result;
+ }
+ }
+}
diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
index 813a612..57ab453 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
@@ -33,6 +33,7 @@ import org.apache.ignite.internal.processors.cache.CacheRegisterMetadataLocallyT
import org.apache.ignite.internal.processors.cache.ClientReconnectAfterClusterRestartTest;
import org.apache.ignite.internal.processors.cache.ClusterReadOnlyModeDoesNotBreakSqlSelectTest;
import org.apache.ignite.internal.processors.cache.ClusterReadOnlyModeSqlTest;
+import org.apache.ignite.internal.processors.cache.FieldsPrecisionTest;
import org.apache.ignite.internal.processors.cache.GridCacheOffHeapSelfTest;
import org.apache.ignite.internal.processors.cache.GridCacheOffheapIndexEntryEvictTest;
import org.apache.ignite.internal.processors.cache.GridCacheOffheapIndexGetSelfTest;
@@ -115,7 +116,9 @@ import org.junit.runners.Suite;
WrongIndexedTypesTest.class,
- IndexPagesMetricsInMemoryTest.class
+ IndexPagesMetricsInMemoryTest.class,
+
+ FieldsPrecisionTest.class
})
public class IgniteCacheWithIndexingTestSuite {
}