Java Code Examples for org.apache.calcite.sql.type.SqlTypeFamily#CHARACTER

The following examples show how to use org.apache.calcite.sql.type.SqlTypeFamily#CHARACTER . 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: ProjectRelBase.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visitCall(RexCall call) {
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (op0 instanceof RexInputRef &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return true;
    } else if (op0 instanceof RexCall &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return op0.accept(this);
    }
  }

  return false;
}
 
Example 2
Source File: SqlImplementor.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
    if (rex.getKind() == SqlKind.LITERAL) {
        final RexLiteral literal = (RexLiteral) rex;
        if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
            return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
        }
    }
    return super.toSql(program, rex);
}
 
Example 3
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 4
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the input is a 'loss-less' cast, that is, a cast from which
 * the original value of the field can be certainly recovered.
 *
 * <p>For instance, int &rarr; bigint is loss-less (as you can cast back to
 * int without loss of information), but bigint &rarr; int is not loss-less.
 *
 * <p>The implementation of this method does not return false positives.
 * However, it is not complete.
 */
public static boolean isLosslessCast(RexNode node) {
    if (!node.isA(SqlKind.CAST)) {
        return false;
    }
    final RelDataType source = ((RexCall) node).getOperands().get(0).getType();
    final SqlTypeName sourceSqlTypeName = source.getSqlTypeName();
    final RelDataType target = node.getType();
    final SqlTypeName targetSqlTypeName = target.getSqlTypeName();
    // 1) Both INT numeric types
    if (SqlTypeFamily.INTEGER.getTypeNames().contains(sourceSqlTypeName)
            && SqlTypeFamily.INTEGER.getTypeNames().contains(targetSqlTypeName)) {
        return targetSqlTypeName.compareTo(sourceSqlTypeName) >= 0;
    }
    // 2) Both CHARACTER types: it depends on the precision (length)
    if (SqlTypeFamily.CHARACTER.getTypeNames().contains(sourceSqlTypeName)
            && SqlTypeFamily.CHARACTER.getTypeNames().contains(targetSqlTypeName)) {
        return targetSqlTypeName.compareTo(sourceSqlTypeName) >= 0
                && source.getPrecision() <= target.getPrecision();
    }
    // 3) From NUMERIC family to CHARACTER family: it depends on the precision/scale
    if (sourceSqlTypeName.getFamily() == SqlTypeFamily.NUMERIC
            && targetSqlTypeName.getFamily() == SqlTypeFamily.CHARACTER) {
        int sourceLength = source.getPrecision() + 1; // include sign
        if (source.getScale() != -1 && source.getScale() != 0) {
            sourceLength += source.getScale() + 1; // include decimal mark
        }
        return target.getPrecision() >= sourceLength;
    }
    // Return FALSE by default
    return false;
}
 
Example 5
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example 6
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example 7
Source File: TypeUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static boolean isText(final RField field) {
  final Optional<SqlTypeFamily> familyOpt = getSqlTypeFamily(field);
  if (!familyOpt.isPresent()) {
    return false;
  }

  final SqlTypeFamily family = familyOpt.get();
  return family == SqlTypeFamily.CHARACTER;
}
 
Example 8
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
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: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the conversion from {@code source} to {@code target} type
 * is a 'loss-less' cast, that is, a cast from which
 * the original value of the field can be certainly recovered.
 *
 * <p>For instance, int &rarr; bigint is loss-less (as you can cast back to
 * int without loss of information), but bigint &rarr; int is not loss-less.
 *
 * <p>The implementation of this method does not return false positives.
 * However, it is not complete.
 * @param source source type
 * @param target target type
 * @return true iff the conversion is a loss-less cast
 */
@API(since = "1.22", status = API.Status.EXPERIMENTAL)
public static boolean isLosslessCast(RelDataType source, RelDataType target) {
  final SqlTypeName sourceSqlTypeName = source.getSqlTypeName();
  final SqlTypeName targetSqlTypeName = target.getSqlTypeName();
  // 1) Both INT numeric types
  if (SqlTypeFamily.INTEGER.getTypeNames().contains(sourceSqlTypeName)
      && SqlTypeFamily.INTEGER.getTypeNames().contains(targetSqlTypeName)) {
    return targetSqlTypeName.compareTo(sourceSqlTypeName) >= 0;
  }
  // 2) Both CHARACTER types: it depends on the precision (length)
  if (SqlTypeFamily.CHARACTER.getTypeNames().contains(sourceSqlTypeName)
      && SqlTypeFamily.CHARACTER.getTypeNames().contains(targetSqlTypeName)) {
    return targetSqlTypeName.compareTo(sourceSqlTypeName) >= 0
        && source.getPrecision() <= target.getPrecision();
  }
  // 3) From NUMERIC family to CHARACTER family: it depends on the precision/scale
  if (sourceSqlTypeName.getFamily() == SqlTypeFamily.NUMERIC
      && targetSqlTypeName.getFamily() == SqlTypeFamily.CHARACTER) {
    int sourceLength = source.getPrecision() + 1; // include sign
    if (source.getScale() != -1 && source.getScale() != 0) {
      sourceLength += source.getScale() + 1; // include decimal mark
    }
    return target.getPrecision() >= sourceLength;
  }
  // Return FALSE by default
  return false;
}
 
Example 11
Source File: MaterializationExpander.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Compare row types ignoring field names, nullability, ANY and CHAR/VARCHAR types
 */
@VisibleForTesting
static boolean areRowTypesEqual(RelDataType rowType1, RelDataType rowType2) {
    if (rowType1 == rowType2) {
      return true;
    }

    if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
      return false;
    }

    final List<RelDataTypeField> f1 = rowType1.getFieldList(); // materialized field
    final List<RelDataTypeField> f2 = rowType2.getFieldList(); // original materialization query field
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
      // remove nullability
      final RelDataType type1 = JavaTypeFactoryImpl.INSTANCE.createTypeWithNullability(pair.left.getType(), false);
      final RelDataType type2 = JavaTypeFactoryImpl.INSTANCE.createTypeWithNullability(pair.right.getType(), false);

      // are types equal ?
      if (type1.equals(type2)) {
        continue;
      }

      // ignore ANY types
      if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) {
        continue;
      }

      // are both types from the CHARACTER family ?
      if (type1.getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER &&
          type2.getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
        continue;
      }

      // safely ignore when materialized field is DOUBLE instead of DECIMAL
      if (type1.getSqlTypeName() == SqlTypeName.DOUBLE && type2.getSqlTypeName() == SqlTypeName
        .DECIMAL || isSumAggOutput(type1, type2)) {
        continue;
      }

      return false;
    }

    return true;
}
 
Example 12
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;
  }
}
 
Example 13
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Type coercion based on the inferred type from passed in operand
 * and the {@link SqlTypeFamily} defined in the checkers,
 * e.g. the {@link org.apache.calcite.sql.type.FamilyOperandTypeChecker}.
 *
 * <p>Caution that we do not cast from NUMERIC to NUMERIC.
 * See <a href="https://docs.google.com/spreadsheets/d/1GhleX5h5W8-kJKh7NMJ4vtoE78pwfaZRJl88ULX_MgU/edit?usp=sharing">CalciteImplicitCasts</a>
 * for the details.
 *
 * @param in       Inferred operand type
 * @param expected Expected {@link SqlTypeFamily} of registered SqlFunction
 * @return common type of implicit cast, null if we do not find any
 */
public RelDataType implicitCast(RelDataType in, SqlTypeFamily expected) {
  List<SqlTypeFamily> numericFamilies = ImmutableList.of(
      SqlTypeFamily.NUMERIC,
      SqlTypeFamily.DECIMAL,
      SqlTypeFamily.APPROXIMATE_NUMERIC,
      SqlTypeFamily.EXACT_NUMERIC,
      SqlTypeFamily.INTEGER);
  List<SqlTypeFamily> dateTimeFamilies = ImmutableList.of(SqlTypeFamily.DATE,
      SqlTypeFamily.TIME, SqlTypeFamily.TIMESTAMP);
  // If the expected type is already a parent of the input type, no need to cast.
  if (expected.getTypeNames().contains(in.getSqlTypeName())) {
    return in;
  }
  // Cast null type (usually from null literals) into target type.
  if (SqlTypeUtil.isNull(in)) {
    return expected.getDefaultConcreteType(factory);
  }
  if (SqlTypeUtil.isNumeric(in) && expected == SqlTypeFamily.DECIMAL) {
    return factory.decimalOf(in);
  }
  // FLOAT/DOUBLE -> DECIMAL
  if (SqlTypeUtil.isApproximateNumeric(in) && expected == SqlTypeFamily.EXACT_NUMERIC) {
    return factory.decimalOf(in);
  }
  // DATE to TIMESTAMP
  if (SqlTypeUtil.isDate(in) && expected == SqlTypeFamily.TIMESTAMP) {
    return factory.createSqlType(SqlTypeName.TIMESTAMP);
  }
  // TIMESTAMP to DATE.
  if (SqlTypeUtil.isTimestamp(in) && expected == SqlTypeFamily.DATE) {
    return factory.createSqlType(SqlTypeName.DATE);
  }
  // If the function accepts any NUMERIC type and the input is a STRING,
  // returns the expected type family's default type.
  // REVIEW Danny 2018-05-22: same with MS-SQL and MYSQL.
  if (SqlTypeUtil.isCharacter(in) && numericFamilies.contains(expected)) {
    return expected.getDefaultConcreteType(factory);
  }
  // STRING + DATE -> DATE;
  // STRING + TIME -> TIME;
  // STRING + TIMESTAMP -> TIMESTAMP
  if (SqlTypeUtil.isCharacter(in) && dateTimeFamilies.contains(expected)) {
    return expected.getDefaultConcreteType(factory);
  }
  // STRING + BINARY -> VARBINARY
  if (SqlTypeUtil.isCharacter(in) && expected == SqlTypeFamily.BINARY) {
    return expected.getDefaultConcreteType(factory);
  }
  // If we get here, `in` will never be STRING type.
  if (SqlTypeUtil.isAtomic(in)
      && (expected == SqlTypeFamily.STRING
      || expected == SqlTypeFamily.CHARACTER)) {
    return expected.getDefaultConcreteType(factory);
  }
  return null;
}
 
Example 14
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Determine whether or not the given field can have a collation added to it.
 * @param field
 * @return
 */
protected boolean canAddCollation(RelDataTypeField field) {
  return field.getType().getSqlTypeName().getFamily() == SqlTypeFamily.CHARACTER;
}