danny0405 commented on a change in pull request #1862: [CALCITE-3864] Implement 
Concat function
URL: https://github.com/apache/calcite/pull/1862#discussion_r398602055
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java
 ##########
 @@ -655,6 +655,59 @@ public int size() {
         return ret;
       };
 
+
+  /**
+   * Type-inference strategy for String concatenation.
+   * Result is varying if either input is; otherwise fixed.
+   * For example,
+   *
+   * concat(cast('a' as varchar(2)), cast('b' as varchar(3)),cast('c' as 
varchar(2)))
+   * returns varchar(7),
+   *
+   * concat(cast('a' as varchar), cast('b' as varchar(2), cast('c' as 
varchar(2))))
+   * returns varchar,
+   *
+   * concat(cast('a' as varchar(65535)), cast('b' as varchar(2)), cast('c' as 
varchar(2)))
+   * returns varchar
+   */
+  public static final SqlReturnTypeInference MULTIVALENT_STRING_SUM_PRECISION =
+      opBinding -> {
+        boolean hasPrecisionNotSpecifiedOperand = false;
+        boolean precisionOverflow = false;
+        int typePrecision;
+        long amount = 0;
+        List<RelDataType> operandTypes = opBinding.collectOperandTypes();
+        final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
+        final RelDataTypeSystem typeSystem = typeFactory.getTypeSystem();
+        for (RelDataType operandType: operandTypes) {
+          int operandPrecision = operandType.getPrecision();
+          amount = (long) operandPrecision + amount;
+          if (operandPrecision == RelDataType.PRECISION_NOT_SPECIFIED) {
+            hasPrecisionNotSpecifiedOperand = true;
+            break;
+          }
+          if (amount > typeSystem.getMaxPrecision(SqlTypeName.VARCHAR)) {
+            precisionOverflow = true;
+            break;
+          }
+        }
+        if (hasPrecisionNotSpecifiedOperand || precisionOverflow) {
+          typePrecision = RelDataType.PRECISION_NOT_SPECIFIED;
+        } else {
+          typePrecision = (int) amount;
+        }
+
+        return opBinding.getTypeFactory()
+            .createSqlType(SqlTypeName.VARCHAR, typePrecision);
+      };
+
+  /**
+   * Same as {@link #MULTIVALENT_STRING_SUM_PRECISION} and using
+   * {@link org.apache.calcite.sql.type.SqlTypeTransforms#TO_NULLABLE}
+   */
 
 Review comment:
   End up the comment with dot.

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