AMashenkov commented on code in PR #1623:
URL: https://github.com/apache/ignite-3/pull/1623#discussion_r1102811016


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/type/IgniteTypeFactory.java:
##########
@@ -376,6 +498,49 @@ private boolean allEquals(List<RelDataType> types) {
         return true;
     }
 
+    private static final class CustomDataTypes {
+
+        /**
+         * Contains java types used registered custom data types.
+         * We need those to throw errors to reject attempts to create custom 
data types via
+         * {@link IgniteTypeFactory#createType(Type)}/{@link 
IgniteTypeFactory#createJavaType(Class)}
+         * methods of {@link IgniteTypeFactory}.
+         */
+        private final Set<Type> javaTypes;
+
+        /**
+         * Stores functions that are being used by {@link 
#createCustomType(String, int)} to create type instances.
+         */
+        private final Map<String, MakeCustomType> typeConstructors;
+
+        CustomDataTypes(Set<NewCustomType> customDataTypes) {
+            this.javaTypes = customDataTypes.stream()
+                    .map(t -> t.storageType)
+                    .collect(Collectors.toSet());
+
+            this.typeConstructors = 
customDataTypes.stream().collect(Collectors.toMap((v) -> v.typeName, (v) -> 
v.makeType));
+        }
+    }
+
+    private static final class NewCustomType {

Review Comment:
   ```suggestion
       private static final class IgniteCustomType {
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/type/IgniteTypeFactory.java:
##########
@@ -376,6 +498,49 @@ private boolean allEquals(List<RelDataType> types) {
         return true;
     }
 
+    private static final class CustomDataTypes {
+
+        /**
+         * Contains java types used registered custom data types.
+         * We need those to throw errors to reject attempts to create custom 
data types via
+         * {@link IgniteTypeFactory#createType(Type)}/{@link 
IgniteTypeFactory#createJavaType(Class)}
+         * methods of {@link IgniteTypeFactory}.
+         */
+        private final Set<Type> javaTypes;
+
+        /**
+         * Stores functions that are being used by {@link 
#createCustomType(String, int)} to create type instances.
+         */
+        private final Map<String, MakeCustomType> typeConstructors;
+
+        CustomDataTypes(Set<NewCustomType> customDataTypes) {
+            this.javaTypes = customDataTypes.stream()
+                    .map(t -> t.storageType)
+                    .collect(Collectors.toSet());
+
+            this.typeConstructors = 
customDataTypes.stream().collect(Collectors.toMap((v) -> v.typeName, (v) -> 
v.makeType));
+        }
+    }
+
+    private static final class NewCustomType {
+        final String typeName;
+
+        final Class<?> storageType;
+
+        final MakeCustomType makeType;
+
+        NewCustomType(String typeName, Class<?> storageType, MakeCustomType 
makeType) {
+            this.typeName = typeName;
+            this.storageType = storageType;
+            this.makeType = makeType;
+        }
+    }
+
+    @FunctionalInterface
+    interface MakeCustomType {

Review Comment:
   ```suggestion
       interface IgniteCustomTypeFactory {
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/type/IgniteTypeFactory.java:
##########
@@ -353,14 +419,70 @@ public Charset getDefaultCharset() {
         return super.toSql(type);
     }
 
+    /** {@inheritDoc} **/
+    @Override
+    public RelDataType createTypeWithNullability(RelDataType type, boolean 
nullable) {
+        if (type instanceof IgniteCustomType) {
+            return canonize(((IgniteCustomType) 
type).createWithNullability(nullable));
+        } else {
+            return super.createTypeWithNullability(type, nullable);
+        }
+    }
+
     /** {@inheritDoc} */
     @Override public RelDataType createType(Type type) {
         if (type == Duration.class || type == Period.class || type == 
LocalDate.class || type == LocalDateTime.class
                 || type == LocalTime.class) {
             return createJavaType((Class<?>) type);
+        } else if (customDataTypes.javaTypes.contains(type)) {
+            throw new IllegalArgumentException("Custom data type should not be 
created via createType call: " + type);
+        } else {
+            return super.createType(type);
+        }
+    }
+
+    /** {@inheritDoc} **/
+    @Override
+    public RelDataType createJavaType(Class clazz) {
+        if (customDataTypes.javaTypes.contains(clazz)) {
+            throw new IllegalArgumentException("Custom data type should not be 
created via createJavaType call: " + clazz);
+        } else {
+            return super.createJavaType(clazz);
+        }
+    }
+
+    /**
+     * Creates a custom data type with the given {@code typeName} and 
precision.
+     *
+     * @param typeName type name.
+     * @param precision precision if supported.
+     * @return a custom data type.
+     */
+    public RelDataType createCustomType(String typeName, int precision) {
+        MakeCustomType makeCustomType = 
customDataTypes.typeConstructors.get(typeName);
+        if (makeCustomType == null) {
+            throw new IllegalArgumentException("Unexpected custom data type: " 
+ typeName);
         }
 
-        return super.createType(type);
+        // By default a type must not be nullable.
+        // See SqlTypeFactory::createSqlType.
+        //
+        // Set nullable to false and uncomment the assertion when 
https://issues.apache.org/jira/browse/IGNITE-18753

Review Comment:
   Referred ticket looks like fixed
   https://issues.apache.org/jira/browse/IGNITE-18753



-- 
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