hsyuan commented on a change in pull request #706: [CALCITE-2302] Implicit type 
cast support
URL: https://github.com/apache/calcite/pull/706#discussion_r314546071
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java
 ##########
 @@ -0,0 +1,647 @@
+/*
+ * 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.calcite.sql.validate.implicit;
+
+import org.apache.calcite.rel.type.DynamicRecordType;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCollation;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlTypeAssignmentRules;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.calcite.sql.validate.SqlValidator;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorScope;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+
+import com.google.common.collect.ImmutableList;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Base class for all the type coercion rules. If you want to have a custom 
type coercion rules,
+ * inheriting this class is not necessary, but would have some convenient tool 
methods.
+ *
+ * <p>We make tool methods: {@link #coerceIthOperandTo}, {@link 
#coerceIthColumnTo},
+ * {@link #needToCast}, {@link #updateInferredRowType},
+ * {@link #updateInferredTypeFor}, {@link #updateInferredRowType}
+ * all overridable by derived classes, you can define system specific type 
coercion logic.
+ *
+ * <p>Caution that these methods may modify the {@link SqlNode} tree, you 
should know what the
+ * effect is when using these methods to customize your type coercion 
rules.</p>
+ *
+ * <p>This class also defines the default implementation of the type widening 
strategies, see
+ * {@link TypeCoercion} doc and methods: {@link #getTightestCommonType}, 
{@link #getWiderTypeFor},
+ * {@link #getWiderTypeForTwo}, {@link #getWiderTypeForDecimal},
+ * {@link #commonTypeForBinaryComparison} for the detail strategies.</p>
+ */
+public abstract class AbstractTypeCoercion implements TypeCoercion {
+  protected SqlValidator validator;
+  protected RelDataTypeFactory factory;
+
+  //~ Constructors -----------------------------------------------------------
+
+  AbstractTypeCoercion(SqlValidator validator) {
+    Objects.requireNonNull(validator);
+    this.validator = validator;
+    this.factory = validator.getTypeFactory();
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  public RelDataTypeFactory getFactory() {
+    return this.factory;
+  }
+
+  public SqlValidator getValidator() {
+    return this.validator;
+  }
+
+  /**
+   * Cast ith operand to target type, we do this base on the fact that
+   * validate happens before type coercion.
+   */
+  protected boolean coerceIthOperandTo(
+      SqlValidatorScope scope,
+      SqlCall call,
+      int index,
+      RelDataType targetType) {
+    SqlNode operand = call.getOperandList().get(index);
+    // Check it early.
+    if (!needToCast(scope, operand, targetType)) {
+      return false;
+    }
+    // fix nullable attr.
+    RelDataType targetType1 = syncAttributes(validator.deriveType(scope, 
operand), targetType);
+    SqlNode desired = castTo(operand, targetType1);
+    call.setOperand(index, desired);
+    updateInferredTypeFor(desired, targetType1);
+    return true;
+  }
+
+
+  /**
+   * Cast ith column to target type.
+   *
+   * @param scope      validator scope for the node list
+   * @param nodeList   column node list
+   * @param index      index of column
+   * @param targetType target type to cast to
+   *
+   * @return true if type coercion actually happens.
+   */
+  protected boolean coerceIthColumnTo(
+      SqlValidatorScope scope,
+      SqlNodeList nodeList,
+      int index,
+      RelDataType targetType) {
+    // This will happen when there is a star/dynamic-star column in the select 
list, and the source
+    // is values, i.e. `select * from (values(1, 2, 3))`. There is no need to 
coerce the
+    // column type, only mark the inferred row type has changed, we will then 
add in type coercion
+    // when expanding star/dynamic-star.
+
+    // See SqlToRelConverter#convertSelectList for details.
+    if (index >= nodeList.getList().size()) {
+      // Can only happen when there is a star(*) in the column,
+      // just return true.
+      return true;
+    }
+
+    final SqlNode node = nodeList.get(index);
+    if (node instanceof SqlIdentifier) {
+      // Do not expand a star/dynamic table col.
+      SqlIdentifier node1 = (SqlIdentifier) node;
+      if (node1.isStar()) {
+        return true;
+      } else if 
(DynamicRecordType.isDynamicStarColName(Util.last(node1.names))) {
+        // should support implicit cast for dynamic table.
+        return false;
+      }
+    }
+
+    if (node instanceof SqlCall) {
+      SqlCall node2 = (SqlCall) node;
+      if (node2.getOperator().kind == SqlKind.AS) {
+        final SqlNode operand = node2.operand(0);
+        if (!needToCast(scope, operand, targetType)) {
+          return false;
+        }
+        RelDataType targetType2 = syncAttributes(validator.deriveType(scope, 
operand), targetType);
+        final SqlNode casted = castTo(operand, targetType2);
+        node2.setOperand(0, casted);
+        updateInferredTypeFor(casted, targetType2);
+        return true;
+      }
+    }
+    if (!needToCast(scope, node, targetType)) {
+      return false;
+    }
+    RelDataType targetType3 = syncAttributes(validator.deriveType(scope, 
node), targetType);
+    final SqlNode node3 = castTo(node, targetType3);
+    nodeList.set(index, node3);
+    updateInferredTypeFor(node3, targetType3);
+    return true;
+  }
+
+  /**
+   * Sync the data type additional attributes before casting, i.e. 
nullability, charset, collation.
+   */
+  RelDataType syncAttributes(
+      RelDataType type1,
+      RelDataType targetType) {
+    RelDataType targetType1 = targetType;
+    if (type1 != null) {
+      targetType1 = factory.createTypeWithNullability(targetType1, 
type1.isNullable());
+      if (SqlTypeUtil.inCharOrBinaryFamilies(type1)
+          && SqlTypeUtil.inCharOrBinaryFamilies(targetType)) {
+        Charset charset1 = type1.getCharset();
+        SqlCollation collation1 = type1.getCollation();
+        if (charset1 != null && SqlTypeUtil.inCharFamily(targetType1)) {
+          targetType1 = factory.createTypeWithCharsetAndCollation(targetType1, 
charset1,
+              collation1);
+        }
+      }
+    }
+    return targetType1;
+  }
+
+  /** Decide if a SqlNode should be cast to target type, derived class can 
override this strategy.*/
+  protected boolean needToCast(SqlValidatorScope scope, SqlNode node1, 
RelDataType targetType) {
+    RelDataType type1 = validator.deriveType(scope, node1);
+    // This depends on the fact that type validate happens before coercion.
+    // We do not have inferred type for some node, i.e. LOCALTIME.
+    if (type1 == null) {
+      return false;
+    }
+
+    // This prevents that we cast a JavaType to normal RelDataType.
+    if (targetType.getSqlTypeName() == type1.getSqlTypeName()) {
+      return false;
+    }
+
+    // do not make a cast when we don't know specific type (ANY) of the origin 
node.
+    if (targetType.getSqlTypeName() == SqlTypeName.ANY
+        || type1.getSqlTypeName() == SqlTypeName.ANY) {
+      return false;
+    }
+    // little promotion for character types,
 
 Review comment:
   Let's update the comment. It is more formal and understandable.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to