alex-plekhanov commented on code in PR #9923:
URL: https://github.com/apache/ignite/pull/9923#discussion_r842693997


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java:
##########
@@ -37,6 +46,8 @@
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.junit.Test;
 
+import static org.apache.ignite.internal.util.lang.GridFunc.compareArrays;

Review Comment:
   Usually `GridFunc` is not used directly, `F` alias is used instead.



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java:
##########
@@ -87,6 +98,62 @@
         stopAllGrids();
     }
 
+    /**
+     * @throws SQLException If failed.
+     */
+    @Test
+    public void testOtherType() throws Exception {
+        List<Object> values = new ArrayList<>();
+
+        values.add("str");
+        values.add(11);
+        values.add(101.1);
+        values.add(202.2f);

Review Comment:
   The same float type as for 101.1



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java:
##########
@@ -87,6 +98,62 @@
         stopAllGrids();
     }
 
+    /**
+     * @throws SQLException If failed.
+     */
+    @Test
+    public void testOtherType() throws Exception {
+        List<Object> values = new ArrayList<>();
+
+        values.add("str");
+        values.add(11);
+        values.add(101.1);
+        values.add(202.2f);
+        values.add(new byte[] {1, 2, 3});
+        values.add(UUID.randomUUID());
+        values.add(new ObjectToStore(1, "noname", 22.2));
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("a", "bb");
+        map.put("vvv", "zzz");
+        map.put("111", "222");
+        map.put("lst", Stream.of("abc", 1, null, 
20.f).collect(Collectors.toSet()));
+        values.add(map);
+
+        List<Object> lst = new ArrayList<>();
+        lst.add(1);
+        lst.add(2.2f);
+        lst.add(3.3d);
+        lst.add("str");
+        lst.add(map);
+        values.add(lst);
+
+        stmt.execute("CREATE TABLE tbl(id INT, oth OTHER, primary key(id))");
+
+        try (PreparedStatement ps = conn.prepareStatement("insert into tbl 
values(?, ?)")) {
+            int idx = 1;
+
+            for (Object obj : values) {
+                ps.setInt(1, idx++);
+
+                ps.setObject(2, obj);
+
+                assertEquals(1, ps.executeUpdate());
+            }
+        }
+
+        try (ResultSet rs = stmt.executeQuery("select * from tbl order by 
id")) {
+            for (Object obj : values) {
+                assertTrue(rs.next());
+
+                if (obj != null && obj.getClass().isArray())

Review Comment:
   ```
   if (F.isArray(obj))
       F.compareArrays(obj, rs.getObject(2));
   ```
   ?



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/type/IgniteTypeFactory.java:
##########
@@ -266,22 +266,36 @@ else if (type instanceof UuidType)
                 return 
createTypeWithNullability(createSqlIntervalType(INTERVAL_QUALIFIER_DAY_TIME), 
true);
             else if (clazz == Period.class)
                 return 
createTypeWithNullability(createSqlIntervalType(INTERVAL_QUALIFIER_YEAR_MONTH), 
true);
-            else if (clazz == UUID.class)
-                return createTypeWithNullability(createUuidType(), true);
+            else {
+                RelDataType relType = createCustomType(clazz.getName());
+
+                if (relType != null)
+                    return relType;
+            }
         }
 
         return super.toSql(type);
     }
 
-    /** @return UUID SQL type. */
-    public RelDataType createUuidType() {
-        return canonize(new UuidType(true));
+    /** @return Custom type by name or storeable class name. {@code Null} if 
type not found. */
+    public RelDataType createCustomType(String typeName) {
+        return createCustomType(typeName, true);
+    }
+
+    /** @return Nullable custom type by name or storeable class name. {@code 
Null} if type not found. */
+    public RelDataType createCustomType(String typeName, boolean nullable) {
+        if ("UUID".equals(typeName) || UUID.class.getName().equals(typeName))
+            return new UuidType(nullable);
+        else if ("OTHER".equals(typeName) || 
Object.class.getName().equals(typeName))
+            return new OtherType(nullable);
+
+        return null;
     }
 
     /** {@inheritDoc} */
     @Override public RelDataType createTypeWithNullability(RelDataType type, 
boolean nullable) {
-        if (type instanceof UuidType && type.isNullable() != nullable)
-            type = new UuidType(nullable);
+        if (type instanceof IgniteSqlCalciteType && type.isNullable() != 
nullable)

Review Comment:
   `createType` method also should be modified (`UUID` and `Object` classes 
should be added, and probably `byte[]` for BINARY type). To reproduce the 
problem without modifying `createType()` method you can add new column to the 
`JdbcQueryTest#testQueryColumnTypes` test, for example:
   ```
               "binary_col BINARY, " +
               "uuid_col UUID, " +
               "other_col OTHER, " +
   ```
   And check:
   ```
               assertEquals(Types.BINARY, md.getColumnType(18));
               assertEquals(byte[].class.getName(), md.getColumnClassName(18));
               assertEquals(Types.OTHER, md.getColumnType(19));
               assertEquals(UUID.class.getName(), md.getColumnClassName(19));
               assertEquals(Types.OTHER, md.getColumnType(20));
               assertEquals(Object.class.getName(), md.getColumnClassName(20));
   ```
   Currently, all these columns return `Types.OTHER`  as column type and 
`Object[]` as column class name



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/type/IgniteTypeFactory.java:
##########
@@ -266,22 +266,36 @@ else if (type instanceof UuidType)
                 return 
createTypeWithNullability(createSqlIntervalType(INTERVAL_QUALIFIER_DAY_TIME), 
true);
             else if (clazz == Period.class)
                 return 
createTypeWithNullability(createSqlIntervalType(INTERVAL_QUALIFIER_YEAR_MONTH), 
true);
-            else if (clazz == UUID.class)
-                return createTypeWithNullability(createUuidType(), true);
+            else {
+                RelDataType relType = createCustomType(clazz.getName());
+
+                if (relType != null)
+                    return relType;
+            }
         }
 
         return super.toSql(type);
     }
 
-    /** @return UUID SQL type. */
-    public RelDataType createUuidType() {
-        return canonize(new UuidType(true));
+    /** @return Custom type by name or storeable class name. {@code Null} if 
type not found. */
+    public RelDataType createCustomType(String typeName) {
+        return createCustomType(typeName, true);
+    }
+
+    /** @return Nullable custom type by name or storeable class name. {@code 
Null} if type not found. */
+    public RelDataType createCustomType(String typeName, boolean nullable) {

Review Comment:
   I don't like the idea of using the same parameter in different ways (as type 
name and as class name), perhaps it's better to use some enum. At least we 
should get rid of literal "UUID" and "OTHER" and use constants.



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/DataTypesTest.java:
##########
@@ -0,0 +1,414 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import com.google.common.collect.ImmutableSet;
+import org.apache.calcite.runtime.CalciteException;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.QueryEntity;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import 
org.apache.ignite.internal.processors.query.calcite.integration.AbstractBasicIntegrationTest;
+import org.apache.ignite.internal.util.typedef.F;
+import org.junit.Test;
+
+/**
+ * Test SQL data types.
+ */
+public class DataTypesTest extends AbstractBasicIntegrationTest {

Review Comment:
   Test was moved to `integration` package



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java:
##########
@@ -266,4 +333,38 @@ public void testQueryColumnTypes() throws Exception {
 
         stmt.close();
     }
+
+    /** Some object to store. */
+    private static class ObjectToStore implements Serializable {
+        /** */
+        private int id;
+
+        /** */
+        private String name;
+
+        /** */
+        private double val;
+
+        /** */
+        public ObjectToStore(int id, String name, double val) {
+            this.id = id;
+            this.name = name;
+            this.val = val;
+        }
+
+        /** */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+            if (o == null || getClass() != o.getClass())
+                return false;
+            ObjectToStore store = (ObjectToStore)o;
+            return id == store.id && Double.compare(store.val, val) == 0 && 
Objects.equals(name, store.name);

Review Comment:
   `Double.compare(store.val, val) == 0` -> `store.val == val`



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/type/IgniteSqlCalciteType.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.type;
+
+import java.lang.reflect.Type;
+import org.apache.calcite.rel.type.RelDataTypeFamily;
+import org.apache.calcite.rel.type.RelDataTypeImpl;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+/** Custom type base. */
+public abstract class IgniteSqlCalciteType extends RelDataTypeImpl {

Review Comment:
   `IgniteSql...` is a prefix for SQL AST nodes, but here it used as prefix for 
type. Let's rename this class to `IgniteCustomType` or `CustomType`



##########
modules/calcite/src/main/codegen/includes/parserImpls.ftl:
##########
@@ -99,6 +99,8 @@ SqlDataTypeSpec DataTypeEx() :
         dt = IntervalType()
     |
         dt = UuidType()
+    |
+        dt = OtherType()

Review Comment:
   Looks like `UuidType` and `OtherType` are redundant here. `DataType()` 
already accept `CompoundIdentifier` as user defined type.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to