Beispiel #1
0
  /** Checks for type compatibility of column and expr. Returns compatible (possibly cast) expr. */
  private Expr checkTypeCompatibility(Column column, Expr expr) throws AnalysisException {
    // Check for compatible type, and add casts to the selectListExprs if necessary.
    // We don't allow casting to a lower precision type.
    Type colType = column.getType();
    Type exprType = expr.getType();
    // Trivially compatible.
    if (colType.equals(exprType)) return expr;

    Type compatibleType = Type.getAssignmentCompatibleType(colType, exprType);
    // Incompatible types.
    if (!compatibleType.isValid()) {
      throw new AnalysisException(
          String.format(
              "Target table '%s' is incompatible with SELECT / PARTITION expressions.\n"
                  + "Expression '%s' (type: %s) is not compatible with column '%s' (type: %s)",
              targetTableName_, expr.toSql(), exprType, column.getName(), colType));
    }
    // Loss of precision when inserting into the table.
    if (!compatibleType.equals(colType) && !compatibleType.isNull()) {
      throw new AnalysisException(
          String.format(
              "Possible loss of precision for target table '%s'.\n"
                  + "Expression '%s' (type: %s) would need to be cast to %s"
                  + " for column '%s'",
              targetTableName_, expr.toSql(), exprType, colType, column.getName()));
    }
    // Add a cast to the selectListExpr to the higher type.
    return expr.castTo(compatibleType);
  }