Java Code Examples for org.apache.calcite.sql.type.SqlTypeName#NULL

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#NULL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValuesRel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method that gets the type from tuples for given fieldIndex.
 * An IN list is represented by a single iteration through the tuples at fieldIndex.
 *
 * @param fieldIndex        Field index used to retrieve the relevant RexLiteral.
 * @param tuples            RexLiterals for the Values Rel
 *
 * @return the derived RelDataType from literal.
 */
private static RelDataType getFieldTypeFromInput(RelDataTypeFactory typeFactory, final int fieldIndex,
                                                 final ImmutableList<ImmutableList<RexLiteral>> tuples) {
  // Search for a non-NULL, non-ANY type.
  List<RelDataType> literalTypes = Lists.newArrayListWithExpectedSize(tuples.size());

  for(ImmutableList<RexLiteral> literals : tuples) {
    final RexLiteral literal = literals.get(fieldIndex);
    if (literal != null
      && literal.getType().getSqlTypeName() != SqlTypeName.NULL
      && literal.getType().getSqlTypeName() != SqlTypeName.ANY) {
      literalTypes.add(literal.getType());
    }
  }

  // Return the least restrictive type unless it is null, in which case return the first non-null, non-ANY type.
  RelDataType leastRestrictiveType = typeFactory.leastRestrictive(literalTypes);
  return (leastRestrictiveType != null) ? leastRestrictiveType : literalTypes.get(0);
}
 
Example 2
Source File: Prepare.java    From calcite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public boolean columnHasDefaultValue(RelDataType rowType, int ordinal,
    InitializerContext initializerContext) {
  // This method is no longer used
  final Table table = this.unwrap(Table.class);
  if (table != null && table instanceof Wrapper) {
    final InitializerExpressionFactory initializerExpressionFactory =
        ((Wrapper) table).unwrap(InitializerExpressionFactory.class);
    if (initializerExpressionFactory != null) {
      return initializerExpressionFactory
          .newColumnDefaultValue(this, ordinal, initializerContext)
          .getType().getSqlTypeName() != SqlTypeName.NULL;
    }
  }
  if (ordinal >= rowType.getFieldList().size()) {
    return true;
  }
  return !rowType.getFieldList().get(ordinal).getType().isNullable();
}
 
Example 3
Source File: RexUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether a {@link RexCall} requires decimal expansion. It
 * usually requires expansion if it has decimal operands.
 *
 * <p>Exceptions to this rule are:
 *
 * <ul>
 * <li>isNull doesn't require expansion
 * <li>It's okay to cast decimals to and from char types
 * <li>It's okay to cast nulls as decimals
 * <li>Casts require expansion if their return type is decimal
 * <li>Reinterpret casts can handle a decimal operand
 * </ul>
 *
 * @param expr    expression possibly in need of expansion
 * @param recurse whether to check nested calls
 * @return whether the expression requires expansion
 */
public static boolean requiresDecimalExpansion(RexNode expr, boolean recurse) {
    if (!(expr instanceof RexCall)) {
        return false;
    }
    RexCall call = (RexCall) expr;

    boolean localCheck = true;
    switch (call.getKind()) {
    case REINTERPRET:
    case IS_NULL:
        localCheck = false;
        break;
    case CAST:
        RelDataType lhsType = call.getType();
        RelDataType rhsType = call.getOperands().get(0).getType();
        if (rhsType.getSqlTypeName() == SqlTypeName.NULL) {
            return false;
        }
        if (SqlTypeUtil.inCharFamily(lhsType) || SqlTypeUtil.inCharFamily(rhsType)) {
            localCheck = false;
        } else if (SqlTypeUtil.isDecimal(lhsType) && (lhsType != rhsType)) {
            return true;
        }
        break;
    default:
        localCheck = call.getOperator().requiresDecimalExpansion();
    }

    if (localCheck) {
        if (SqlTypeUtil.isDecimal(call.getType())) {
            // NOTE jvs 27-Mar-2007: Depending on the type factory, the
            // result of a division may be decimal, even though both inputs
            // are integer.
            return true;
        }
        for (int i = 0; i < call.getOperands().size(); i++) {
            if (SqlTypeUtil.isDecimal(call.getOperands().get(i).getType())) {
                return true;
            }
        }
    }
    return recurse && requiresDecimalExpansion(call.getOperands(), true);
}
 
Example 4
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Determines common type for a comparison operator when one operand is String type and the
 * other is not. For date + timestamp operands, use timestamp as common type,
 * i.e. Timestamp(2017-01-01 00:00 ...) &gt; Date(2018) evaluates to be false.
 */
public RelDataType commonTypeForBinaryComparison(RelDataType type1, RelDataType type2) {
  SqlTypeName typeName1 = type1.getSqlTypeName();
  SqlTypeName typeName2 = type2.getSqlTypeName();

  if (typeName1 == null || typeName2 == null) {
    return null;
  }

  // DATETIME + CHARACTER -> DATETIME
  // REVIEW Danny 2019-09-23: There is some legacy redundant code in SqlToRelConverter
  // that coerce Datetime and CHARACTER comparison.
  if (SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isDatetime(type2)) {
    return type2;
  }

  if (SqlTypeUtil.isDatetime(type1) && SqlTypeUtil.isCharacter(type2)) {
    return type1;
  }

  // DATE + TIMESTAMP -> TIMESTAMP
  if (SqlTypeUtil.isDate(type1) && SqlTypeUtil.isTimestamp(type2)) {
    return type2;
  }

  if (SqlTypeUtil.isDate(type2) && SqlTypeUtil.isTimestamp(type1)) {
    return type1;
  }

  if (SqlTypeUtil.isString(type1) && typeName2 == SqlTypeName.NULL) {
    return type1;
  }

  if (typeName1 == SqlTypeName.NULL && SqlTypeUtil.isString(type2)) {
    return type2;
  }

  if (SqlTypeUtil.isDecimal(type1) && SqlTypeUtil.isCharacter(type2)
      || SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isDecimal(type2)) {
    // There is no proper DECIMAL type for VARCHAR, using max precision/scale DECIMAL
    // as the best we can do.
    return SqlTypeUtil.getMaxPrecisionScaleDecimal(factory);
  }

  // Keep sync with MS-SQL:
  // 1. BINARY/VARBINARY can not cast to FLOAT/REAL/DOUBLE
  // because of precision loss,
  // 2. CHARACTER to TIMESTAMP need explicit cast because of TimeZone.
  // Hive:
  // 1. BINARY can not cast to any other types,
  // 2. CHARACTER can only be coerced to DOUBLE/DECIMAL.
  if (SqlTypeUtil.isBinary(type2) && SqlTypeUtil.isApproximateNumeric(type1)
      || SqlTypeUtil.isBinary(type1) && SqlTypeUtil.isApproximateNumeric(type2)) {
    return null;
  }

  // 1 > '1' will be coerced to 1 > 1.
  if (SqlTypeUtil.isAtomic(type1) && SqlTypeUtil.isCharacter(type2)) {
    if (SqlTypeUtil.isTimestamp(type1)) {
      return null;
    }
    return type1;
  }

  if (SqlTypeUtil.isCharacter(type1) && SqlTypeUtil.isAtomic(type2)) {
    if (SqlTypeUtil.isTimestamp(type2)) {
      return null;
    }
    return type2;
  }

  return null;
}
 
Example 5
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether a {@link RexCall} requires decimal expansion. It
 * usually requires expansion if it has decimal operands.
 *
 * <p>Exceptions to this rule are:
 *
 * <ul>
 * <li>isNull doesn't require expansion
 * <li>It's okay to cast decimals to and from char types
 * <li>It's okay to cast nulls as decimals
 * <li>Casts require expansion if their return type is decimal
 * <li>Reinterpret casts can handle a decimal operand
 * </ul>
 *
 * @param expr    expression possibly in need of expansion
 * @param recurse whether to check nested calls
 * @return whether the expression requires expansion
 */
public static boolean requiresDecimalExpansion(
    RexNode expr,
    boolean recurse) {
  if (!(expr instanceof RexCall)) {
    return false;
  }
  RexCall call = (RexCall) expr;

  boolean localCheck = true;
  switch (call.getKind()) {
  case REINTERPRET:
  case IS_NULL:
    localCheck = false;
    break;
  case CAST:
    RelDataType lhsType = call.getType();
    RelDataType rhsType = call.operands.get(0).getType();
    if (rhsType.getSqlTypeName() == SqlTypeName.NULL) {
      return false;
    }
    if (SqlTypeUtil.inCharFamily(lhsType)
        || SqlTypeUtil.inCharFamily(rhsType)) {
      localCheck = false;
    } else if (SqlTypeUtil.isDecimal(lhsType)
        && (lhsType != rhsType)) {
      return true;
    }
    break;
  default:
    localCheck = call.getOperator().requiresDecimalExpansion();
  }

  if (localCheck) {
    if (SqlTypeUtil.isDecimal(call.getType())) {
      // NOTE jvs 27-Mar-2007: Depending on the type factory, the
      // result of a division may be decimal, even though both inputs
      // are integer.
      return true;
    }
    for (int i = 0; i < call.operands.size(); i++) {
      if (SqlTypeUtil.isDecimal(call.operands.get(i).getType())) {
        return true;
      }
    }
  }
  return recurse && requiresDecimalExpansion(call.operands, true);
}
 
Example 6
Source File: SqlLiteral.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a NULL literal.
 *
 * <p>There's no singleton constant for a NULL literal. Instead, nulls must
 * be instantiated via createNull(), because different instances have
 * different context-dependent types.
 */
public static SqlLiteral createNull(SqlParserPos pos) {
  return new SqlLiteral(null, SqlTypeName.NULL, pos);
}
 
Example 7
Source File: SqlLiteral.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a NULL literal.
 *
 * <p>There's no singleton constant for a NULL literal. Instead, nulls must
 * be instantiated via createNull(), because different instances have
 * different context-dependent types.
 */
public static SqlLiteral createNull(SqlParserPos pos) {
  return new SqlLiteral(null, SqlTypeName.NULL, pos);
}