Java Code Examples for org.apache.calcite.rel.type.RelDataTypeFactory#leastRestrictive()

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory#leastRestrictive() . 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: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns the least restrictive type T, such that a value of type T can be
 * compared with values of type {@code type0} and {@code type1} using
 * {@code =}. */
public static RelDataType leastRestrictiveForComparison(
    RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2) {
  final RelDataType type =
      typeFactory.leastRestrictive(ImmutableList.of(type1, type2));
  if (type != null) {
    return type;
  }
  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);

  // If one of the arguments is of type 'ANY', we can compare.
  if (family1 == SqlTypeFamily.ANY) {
    return type2;
  }
  if (family2 == SqlTypeFamily.ANY) {
    return type1;
  }

  // If one of the arguments is of type 'NULL', we can compare.
  if (family1 == SqlTypeFamily.NULL) {
    return type2;
  }
  if (family2 == SqlTypeFamily.NULL) {
    return type1;
  }

  // We can implicitly convert from character to date, numeric, etc.
  if (family1 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family2)) {
    return type2;
  }
  if (family2 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family1)) {
    return type1;
  }

  return null;
}
 
Example 3
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns the least restrictive type T, such that a value of type T can be
 * compared with values of type {@code type0} and {@code type1} using
 * {@code =}. */
public static RelDataType leastRestrictiveForComparison(
    RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2) {
  final RelDataType type =
      typeFactory.leastRestrictive(ImmutableList.of(type1, type2));
  if (type != null) {
    return type;
  }
  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);

  // If one of the arguments is of type 'ANY', we can compare.
  if (family1 == SqlTypeFamily.ANY) {
    return type2;
  }
  if (family2 == SqlTypeFamily.ANY) {
    return type1;
  }

  // If one of the arguments is of type 'NULL', we can compare.
  if (family1 == SqlTypeFamily.NULL) {
    return type2;
  }
  if (family2 == SqlTypeFamily.NULL) {
    return type1;
  }

  // We can implicitly convert from character to date, numeric, etc.
  if (family1 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family2)) {
    return type2;
  }
  if (family2 == SqlTypeFamily.CHARACTER
      && canConvertStringInCompare(family1)) {
    return type1;
  }

  return null;
}
 
Example 4
Source File: SqlMultisetQueryConstructor.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RelDataType getComponentType(
    RelDataTypeFactory typeFactory,
    List<RelDataType> argTypes) {
  return typeFactory.leastRestrictive(argTypes);
}
 
Example 5
Source File: SqlMultisetValueConstructor.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected RelDataType getComponentType(
    RelDataTypeFactory typeFactory,
    List<RelDataType> argTypes) {
  return typeFactory.leastRestrictive(argTypes);
}
 
Example 6
Source File: SqlMultisetQueryConstructor.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RelDataType getComponentType(
    RelDataTypeFactory typeFactory,
    List<RelDataType> argTypes) {
  return typeFactory.leastRestrictive(argTypes);
}
 
Example 7
Source File: SqlMultisetValueConstructor.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected RelDataType getComponentType(
    RelDataTypeFactory typeFactory,
    List<RelDataType> argTypes) {
  return typeFactory.leastRestrictive(argTypes);
}