normanj-bitquill commented on code in PR #3942:
URL: https://github.com/apache/calcite/pull/3942#discussion_r1744280709
##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -613,6 +613,59 @@ public static SqlCall stripSeparator(SqlCall call) {
public static final SqlReturnTypeInference ARG0_EXCEPT_INTEGER_NULLABLE =
ARG0_EXCEPT_INTEGER.andThen(SqlTypeTransforms.TO_NULLABLE);
+ /**
+ * Returns the type of the first non NULL argument or INTEGER if all
arguments
+ * are NULL.
+ */
+ public static final SqlReturnTypeInference
LARGEST_INT_OR_FIRST_NON_NULL_DEFAULT_INTEGER =
+ opBinding -> {
+ final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
+ RelDataType integerReturnType = null;
+ boolean allArgsInteger = true;
+ for (RelDataType opType : opBinding.collectOperandTypes()) {
+ if (SqlTypeName.INT_TYPES.contains(opType.getSqlTypeName())) {
+ if (integerReturnType == null) {
+ integerReturnType = opType;
+ } else if (isIntegerTypeLarger(integerReturnType, opType)) {
+ integerReturnType = opType;
+ }
+ } else {
+ allArgsInteger = false;
+ break;
+ }
+ }
+ if (integerReturnType != null && allArgsInteger) {
+ return typeFactory.createTypeWithNullability(integerReturnType,
true);
+ }
+ for (RelDataType opType : opBinding.collectOperandTypes()) {
+ if (!opType.isNullable()) {
+ return typeFactory.createTypeWithNullability(opType, true);
+ }
+ }
+ return typeFactory.createTypeWithNullability(
+ typeFactory.createSqlType(SqlTypeName.INTEGER), true);
+ };
+
+ /**
+ * Returns true if type2 is of a larger integer type than type1.
+ *
+ * @param type1 first type
+ * @param type2 second type
+ * @return true if type2 is of a larger integer type than type1
+ */
+ private static boolean isIntegerTypeLarger(RelDataType type1, RelDataType
type2) {
+ if (SqlTypeName.TINYINT == type1.getSqlTypeName()) {
Review Comment:
Thanks, that helps a lot. I have cleaned up the code to make use of
precision.
--
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]