Java Code Examples for org.apache.calcite.sql.type.SqlOperandTypeChecker#Consistency

The following examples show how to use org.apache.calcite.sql.type.SqlOperandTypeChecker#Consistency . 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: StandardConvertletTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Converts a {@link SqlCall} to a {@link RexCall} with a perhaps different
 * operator. */
private RexNode convertCall(
    SqlRexContext cx,
    SqlCall call,
    SqlOperator op) {
  final List<SqlNode> operands = call.getOperandList();
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlOperandTypeChecker.Consistency consistency =
      op.getOperandTypeChecker() == null
          ? SqlOperandTypeChecker.Consistency.NONE
          : op.getOperandTypeChecker().getConsistency();
  final List<RexNode> exprs =
      convertExpressionList(cx, operands, consistency);
  RelDataType type = rexBuilder.deriveReturnType(op, exprs);
  return rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));
}
 
Example 2
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static List<RexNode> convertExpressionList(SqlRexContext cx,
    List<SqlNode> nodes, SqlOperandTypeChecker.Consistency consistency) {
  final List<RexNode> exprs = new ArrayList<>();
  for (SqlNode node : nodes) {
    exprs.add(cx.convertExpression(node));
  }
  if (exprs.size() > 1) {
    final RelDataType type =
        consistentType(cx, consistency, RexUtil.types(exprs));
    if (type != null) {
      final List<RexNode> oldExprs = Lists.newArrayList(exprs);
      exprs.clear();
      for (RexNode expr : oldExprs) {
        exprs.add(cx.getRexBuilder().ensureType(type, expr, true));
      }
    }
  }
  return exprs;
}
 
Example 3
Source File: ConvertletTable.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/** Converts a {@link SqlCall} to a {@link RexCall} with a perhaps different
 * operator. */
private RexNode convertCall(
    SqlRexContext cx,
    SqlCall call,
    SqlOperator op) {
  final List<SqlNode> operands = call.getOperandList();
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlOperandTypeChecker.Consistency consistency =
      op.getOperandTypeChecker() == null
          ? SqlOperandTypeChecker.Consistency.NONE
          : op.getOperandTypeChecker().getConsistency();
  final List<RexNode> exprs =
      convertExpressionList(cx, operands, consistency);
  RelDataType type = rexBuilder.deriveReturnType(op, exprs);
  return rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));
}
 
Example 4
Source File: ConvertletTable.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private static List<RexNode> convertExpressionList(SqlRexContext cx,
    List<SqlNode> nodes, SqlOperandTypeChecker.Consistency consistency) {
  final List<RexNode> exprs = Lists.newArrayList();
  for (SqlNode node : nodes) {
    exprs.add(cx.convertExpression(node));
  }
  if (exprs.size() > 1) {
    final RelDataType type =
        consistentType(cx, consistency, RexUtil.types(exprs));
    if (type != null) {
      final List<RexNode> oldExprs = Lists.newArrayList(exprs);
      exprs.clear();
      for (RexNode expr : oldExprs) {
        exprs.add(cx.getRexBuilder().ensureType(type, expr, true));
      }
    }
  }
  return exprs;
}
 
Example 5
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static List<RexNode> convertExpressionList(SqlRexContext cx,
    List<SqlNode> nodes, SqlOperandTypeChecker.Consistency consistency) {
  final List<RexNode> exprs = new ArrayList<>();
  for (SqlNode node : nodes) {
    exprs.add(cx.convertExpression(node));
  }
  if (exprs.size() > 1) {
    final RelDataType type =
        consistentType(cx, consistency, RexUtil.types(exprs));
    if (type != null) {
      final List<RexNode> oldExprs = Lists.newArrayList(exprs);
      exprs.clear();
      for (RexNode expr : oldExprs) {
        exprs.add(cx.getRexBuilder().ensureType(type, expr, true));
      }
    }
  }
  return exprs;
}
 
Example 6
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static RelDataType consistentType(SqlRexContext cx,
    SqlOperandTypeChecker.Consistency consistency, List<RelDataType> types) {
  switch (consistency) {
  case COMPARE:
    if (SqlTypeUtil.areSameFamily(types)) {
      // All arguments are of same family. No need for explicit casts.
      return null;
    }
    final List<RelDataType> nonCharacterTypes = new ArrayList<>();
    for (RelDataType type : types) {
      if (type.getFamily() != SqlTypeFamily.CHARACTER) {
        nonCharacterTypes.add(type);
      }
    }
    if (!nonCharacterTypes.isEmpty()) {
      final int typeCount = types.size();
      types = nonCharacterTypes;
      if (nonCharacterTypes.size() < typeCount) {
        final RelDataTypeFamily family =
            nonCharacterTypes.get(0).getFamily();
        if (family instanceof SqlTypeFamily) {
          // The character arguments might be larger than the numeric
          // argument. Give ourselves some headroom.
          switch ((SqlTypeFamily) family) {
          case INTEGER:
          case NUMERIC:
            nonCharacterTypes.add(
                cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT));
          }
        }
      }
    }
    // fall through
  case LEAST_RESTRICTIVE:
    return cx.getTypeFactory().leastRestrictive(types);
  default:
    return null;
  }
}
 
Example 7
Source File: EqualityConvertlet.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static List<RexNode> convertExpressionList(SqlRexContext cx,
                                                   List<SqlNode> nodes) {
  final List<RexNode> exprs = Lists.newArrayList();
  for (SqlNode node : nodes) {
    exprs.add(cx.convertExpression(node));
  }
  if (exprs.size() > 1) {
    final SqlOperandTypeChecker.Consistency consistency = Consistency.LEAST_RESTRICTIVE;
    final List<RelDataType> types = RexUtil.types(exprs);
    final RelDataType type;
    if (allExactNumeric(types) && anyDecimal(types)) {
      // for mixed types, INT and BIGINT will be treated as DECIMAL(10,0) and DECIMAL(19,0) respectively
      type = consistentDecimalType(cx.getTypeFactory(), types);
    } else {
      // if there are no Decimal types or some of the types are non-exact, fall back to default Calcite
      // behavior which will convert to Double
      type = SqlTypeUtil.consistentType(cx.getTypeFactory(), consistency, types);
    }
    if (type != null) {
      final List<RexNode> oldExprs = Lists.newArrayList(exprs);
      exprs.clear();
      for (RexNode expr : oldExprs) {
        exprs.add(cx.getRexBuilder().ensureType(type, expr, true));
      }
    }
  }
  return exprs;
}
 
Example 8
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts a {@link SqlCall} to a {@link RexCall} with a perhaps different
 * operator. */
private RexNode convertCall(
    SqlRexContext cx, SqlOperator op, List<SqlNode> operands) {
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlOperandTypeChecker.Consistency consistency =
      op.getOperandTypeChecker() == null
          ? SqlOperandTypeChecker.Consistency.NONE
          : op.getOperandTypeChecker().getConsistency();
  final List<RexNode> exprs =
      convertExpressionList(cx, operands, consistency);
  RelDataType type = rexBuilder.deriveReturnType(op, exprs);
  return rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));
}
 
Example 9
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelDataType consistentType(SqlRexContext cx,
    SqlOperandTypeChecker.Consistency consistency, List<RelDataType> types) {
  switch (consistency) {
  case COMPARE:
    if (SqlTypeUtil.areSameFamily(types)) {
      // All arguments are of same family. No need for explicit casts.
      return null;
    }
    final List<RelDataType> nonCharacterTypes = new ArrayList<>();
    for (RelDataType type : types) {
      if (type.getFamily() != SqlTypeFamily.CHARACTER) {
        nonCharacterTypes.add(type);
      }
    }
    if (!nonCharacterTypes.isEmpty()) {
      final int typeCount = types.size();
      types = nonCharacterTypes;
      if (nonCharacterTypes.size() < typeCount) {
        final RelDataTypeFamily family =
            nonCharacterTypes.get(0).getFamily();
        if (family instanceof SqlTypeFamily) {
          // The character arguments might be larger than the numeric
          // argument. Give ourselves some headroom.
          switch ((SqlTypeFamily) family) {
          case INTEGER:
          case NUMERIC:
            nonCharacterTypes.add(
                cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT));
          }
        }
      }
    }
    // fall through
  case LEAST_RESTRICTIVE:
    return cx.getTypeFactory().leastRestrictive(types);
  default:
    return null;
  }
}
 
Example 10
Source File: ConvertletTable.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static RelDataType consistentType(SqlRexContext cx,
    SqlOperandTypeChecker.Consistency consistency, List<RelDataType> types) {
  switch (consistency) {
  case COMPARE:
    final Set<RelDataTypeFamily> families =
        Sets.newHashSet(RexUtil.families(types));
    if (families.size() < 2) {
      // All arguments are of same family. No need for explicit casts.
      return null;
    }
    final List<RelDataType> nonCharacterTypes = Lists.newArrayList();
    for (RelDataType type : types) {
      if (type.getFamily() != SqlTypeFamily.CHARACTER) {
        nonCharacterTypes.add(type);
      }
    }
    if (!nonCharacterTypes.isEmpty()) {
      final int typeCount = types.size();
      types = nonCharacterTypes;
      if (nonCharacterTypes.size() < typeCount) {
        final RelDataTypeFamily family =
            nonCharacterTypes.get(0).getFamily();
        if (family instanceof SqlTypeFamily) {
          // The character arguments might be larger than the numeric
          // argument. Give ourselves some headroom.
          switch ((SqlTypeFamily) family) {
          case INTEGER:
          case NUMERIC:
            nonCharacterTypes.add(
                cx.getTypeFactory().createSqlType(SqlTypeName.BIGINT));
          }
        }
      }
    }
    // fall through
  case LEAST_RESTRICTIVE:
    return cx.getTypeFactory().leastRestrictive(types);
  default:
    return null;
  }
}