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

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#CHAR . 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: DrillProjectRelBase.java    From Bats 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() == SqlTypeName.CHAR) {
      return true;
    } else if (op0 instanceof RexCall &&
        op1 instanceof RexLiteral && ((RexLiteral) op1).getTypeName() == SqlTypeName.CHAR) {
      return op0.accept(this);
    }
  }

  return false;
}
 
Example 2
Source File: CassandraFilter.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Combines a field name, operator, and literal to produce a predicate string. */
private String translateOp2(String op, String name, RexLiteral right) {
  // In case this is a key, record that it is now restricted
  if (op.equals("=")) {
    partitionKeys.remove(name);
    if (clusteringKeys.contains(name)) {
      restrictedClusteringKeys++;
    }
  }

  Object value = literalValue(right);
  String valueString = value.toString();
  if (value instanceof String) {
    SqlTypeName typeName = rowType.getField(name, true, false).getType().getSqlTypeName();
    if (typeName != SqlTypeName.CHAR) {
      valueString = "'" + valueString + "'";
    }
  }
  return name + " " + op + " " + valueString;
}
 
Example 3
Source File: GeodeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public String visitCall(RexCall call) {
  final List<String> strings = new ArrayList<>();
  visitList(call.operands, strings);
  if (call.getOperator() == SqlStdOperatorTable.ITEM) {
    final RexNode op1 = call.getOperands().get(1);
    if (op1 instanceof RexLiteral) {
      if (op1.getType().getSqlTypeName() == SqlTypeName.INTEGER) {
        return stripQuotes(strings.get(0)) + "[" + ((RexLiteral) op1).getValue2() + "]";
      } else if (op1.getType().getSqlTypeName() == SqlTypeName.CHAR) {
        return stripQuotes(strings.get(0)) + "." + ((RexLiteral) op1).getValue2();
      }
    }
  }

  return super.visitCall(call);
}
 
Example 4
Source File: RexLiteralImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Computes if data type can be omitted from the digset.
 * <p>For instance, {@code 1:BIGINT} has to keep data type while {@code 1:INT}
 * should be represented as just {@code 1}.
 *
 * <p>Implementation assumption: this method should be fast. In fact might call
 * {@link NlsString#getValue()} which could decode the string, however we rely on the cache there.
 *
 * @see RexLiteral#computeDigest(RexDigestIncludeType)
 * @param value value of the literal
 * @param type type of the literal
 * @return NO_TYPE when type can be omitted, ALWAYS otherwise
 */
private static RexDigestIncludeType shouldIncludeType(Comparable value, RelDataType type) {
    if (type.isNullable()) {
        // This means "null literal", so we require a type for it
        // There might be exceptions like AND(null, true) which are handled by RexCall#computeDigest
        return RexDigestIncludeType.ALWAYS;
    }
    // The variable here simplifies debugging (one can set a breakpoint at return)
    // final ensures we set the value in all the branches, and it ensures the value is set just once
    final RexDigestIncludeType includeType;
    if (type.getSqlTypeName() == SqlTypeName.BOOLEAN || type.getSqlTypeName() == SqlTypeName.INTEGER
            || type.getSqlTypeName() == SqlTypeName.SYMBOL) {
        // We don't want false:BOOLEAN NOT NULL, so we don't print type information for
        // non-nullable BOOLEAN and INTEGER
        includeType = RexDigestIncludeType.NO_TYPE;
    } else if (type.getSqlTypeName() == SqlTypeName.CHAR && value instanceof NlsString) {
        NlsString nlsString = (NlsString) value;

        // Ignore type information for 'Bar':CHAR(3)
        if (((nlsString.getCharset() != null && type.getCharset().equals(nlsString.getCharset()))
                || (nlsString.getCharset() == null && SqlCollation.IMPLICIT.getCharset().equals(type.getCharset())))
                && nlsString.getCollation().equals(type.getCollation())
                && ((NlsString) value).getValue().length() == type.getPrecision()) {
            includeType = RexDigestIncludeType.NO_TYPE;
        } else {
            includeType = RexDigestIncludeType.ALWAYS;
        }
    } else if (type.getPrecision() == 0 && (type.getSqlTypeName() == SqlTypeName.TIME
            || type.getSqlTypeName() == SqlTypeName.TIMESTAMP || type.getSqlTypeName() == SqlTypeName.DATE)) {
        // Ignore type information for '12:23:20':TIME(0)
        // Note that '12:23:20':TIME WITH LOCAL TIME ZONE
        includeType = RexDigestIncludeType.NO_TYPE;
    } else {
        includeType = RexDigestIncludeType.ALWAYS;
    }
    return includeType;
}
 
Example 5
Source File: Hoist.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts a {@link Variable} to a string "?N",
 * where N is the {@link Variable#ordinal},
 * if the fragment is a character literal. Other fragments are unchanged. */
public static String ordinalStringIfChar(Variable v) {
  if (v.node instanceof SqlLiteral
      && ((SqlLiteral) v.node).getTypeName() == SqlTypeName.CHAR) {
    return "?" + v.ordinal;
  } else {
    return v.sql();
  }
}
 
Example 6
Source File: MysqlSqlDialect.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlNode getCastSpec(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case VARCHAR:
    // MySQL doesn't have a VARCHAR type, only CHAR.
    int vcMaxPrecision = this.getTypeSystem().getMaxPrecision(SqlTypeName.CHAR);
    int precision = type.getPrecision();
    if (vcMaxPrecision > 0 && precision > vcMaxPrecision) {
      precision = vcMaxPrecision;
    }
    return new SqlDataTypeSpec(
        new SqlBasicTypeNameSpec(SqlTypeName.CHAR, precision, SqlParserPos.ZERO),
        SqlParserPos.ZERO);
  case INTEGER:
  case BIGINT:
    return new SqlDataTypeSpec(
        new SqlAlienSystemTypeNameSpec(
            "SIGNED",
            type.getSqlTypeName(),
            SqlParserPos.ZERO),
        SqlParserPos.ZERO);
  case TIMESTAMP:
    return new SqlDataTypeSpec(
        new SqlAlienSystemTypeNameSpec(
            "DATETIME",
            type.getSqlTypeName(),
            SqlParserPos.ZERO),
        SqlParserPos.ZERO);
  }
  return super.getCastSpec(type);
}
 
Example 7
Source File: SplunkPushDownRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private String toString(boolean like, RexLiteral literal) {
  String value = null;
  SqlTypeName litSqlType = literal.getTypeName();
  if (SqlTypeName.NUMERIC_TYPES.contains(litSqlType)) {
    value = literal.getValue().toString();
  } else if (litSqlType == SqlTypeName.CHAR) {
    value = ((NlsString) literal.getValue()).getValue();
    if (like) {
      value = value.replace("%", "*");
    }
    value = searchEscape(value);
  }
  return value;
}
 
Example 8
Source File: Checker.java    From samza with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
  if (!udfMetadataOptional.isPresent() || udfMetadataOptional.get().isDisableArgCheck() || !throwOnFailure) {
    return true;
  } else {
    // 1. Generate a mapping from argument index to parsed calcite-type for the sql UDF.
    Map<Integer, RelDataType> argumentIndexToCalciteType = IntStream.range(0, callBinding.getOperandCount())
        .boxed()
        .collect(Collectors.toMap(operandIndex -> operandIndex, callBinding::getOperandType, (a, b) -> b));

    UdfMetadata udfMetadata = udfMetadataOptional.get();
    List<SamzaSqlFieldType> udfArguments = udfMetadata.getArguments();

    // 2. Compare the argument type in samza-sql UDF against the RelType generated by the
    // calcite parser engine.
    for (int udfArgumentIndex = 0; udfArgumentIndex < udfArguments.size(); ++udfArgumentIndex) {
      SamzaSqlFieldType udfArgumentType = udfArguments.get(udfArgumentIndex);
      SqlTypeName udfArgumentAsSqlType = toCalciteSqlType(udfArgumentType);
      RelDataType parsedSqlArgType = argumentIndexToCalciteType.get(udfArgumentIndex);

      // 3(a). Special-case, where static strings used as method-arguments in udf-methods during invocation are parsed as the Char type by calcite.
      if (parsedSqlArgType.getSqlTypeName() == SqlTypeName.CHAR && udfArgumentAsSqlType == SqlTypeName.VARCHAR) {
        return true;
      } else if (!Objects.equals(parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType)
              && !ANY_SQL_TYPE_NAMES.contains(parsedSqlArgType.getSqlTypeName()) && hasOneUdfMethod(udfMetadata)) {
        // 3(b). Throw up and fail on mismatch between the SamzaSqlType and CalciteType for any argument.
        String msg = String.format("Type mismatch in udf class: %s at argument index: %d." +
                        "Expected type: %s, actual type: %s.", udfMetadata.getName(),
                udfArgumentIndex, parsedSqlArgType.getSqlTypeName(), udfArgumentAsSqlType);
        LOG.error(msg);
        throw new SamzaSqlValidatorException(msg);
      }
    }
  }
  // 4. The SamzaSqlFieldType and CalciteType has matched for all the arguments in the UDF.
  return true;
}
 
Example 9
Source File: SamzaSqlValidator.java    From samza with Apache License 2.0 5 votes vote down vote up
private boolean compareFieldTypes(RelDataType outputFieldType, SqlFieldSchema sqlFieldSchema,
    RelDataType selectQueryFieldType, RelSchemaProvider outputRelSchemaProvider) {

  SqlTypeName outputSqlType = outputFieldType.getSqlTypeName();
  SqlTypeName projectSqlType = selectQueryFieldType.getSqlTypeName();

  if (projectSqlType == SqlTypeName.ANY || outputSqlType == SqlTypeName.ANY) {
    return true;
  } else if (outputSqlType != SqlTypeName.ROW && outputSqlType == projectSqlType) {
    return true;
  }

  switch (outputSqlType) {
    case CHAR:
      return projectSqlType == SqlTypeName.VARCHAR;
    case VARCHAR:
      return projectSqlType == SqlTypeName.CHAR;
    case BIGINT:
      return projectSqlType == SqlTypeName.INTEGER;
    case INTEGER:
      return projectSqlType == SqlTypeName.BIGINT;
    case FLOAT:
      return projectSqlType == SqlTypeName.DOUBLE;
    case DOUBLE:
      return projectSqlType == SqlTypeName.FLOAT;
    case ROW:
      try {
        validateOutputRecords(sqlFieldSchema.getRowSchema(), (RelRecordType) outputFieldType,
            (RelRecordType) selectQueryFieldType, outputRelSchemaProvider);
      } catch (SamzaSqlValidatorException e) {
        LOG.error("A field in select query does not match with the output schema.", e);
        return false;
      }
      return true;
    default:
      return false;
  }
}
 
Example 10
Source File: JoinTranslator.java    From samza with Apache License 2.0 5 votes vote down vote up
private void validateJoinKeyType(RexInputRef ref) {
  SqlTypeName sqlTypeName = ref.getType().getSqlTypeName();

  // Primitive types and ANY (for the record key) are supported in the key
  if (sqlTypeName != SqlTypeName.BOOLEAN && sqlTypeName != SqlTypeName.TINYINT && sqlTypeName != SqlTypeName.SMALLINT
      && sqlTypeName != SqlTypeName.INTEGER && sqlTypeName != SqlTypeName.CHAR && sqlTypeName != SqlTypeName.BIGINT
      && sqlTypeName != SqlTypeName.VARCHAR && sqlTypeName != SqlTypeName.DOUBLE && sqlTypeName != SqlTypeName.FLOAT
      && sqlTypeName != SqlTypeName.ANY && sqlTypeName != SqlTypeName.OTHER) {
    log.error("Unsupported key type " + sqlTypeName + " used in join condition.");
    throw new SamzaException("Unsupported key type used in join condition.");
  }
}
 
Example 11
Source File: SqlDatePartOperator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkSingleOperandType(SqlCallBinding callBinding, SqlNode node,
    int iFormalOperand, boolean throwOnFailure) {

  // check that the input is a literal.
  if(!super.checkSingleOperandType(callBinding, node, iFormalOperand, throwOnFailure)) {
    return false;
  }

  final RelDataType type = callBinding.getValidator().deriveType(callBinding.getScope(), node);
  final SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY) {
    return true;
  }

  if(!(typeName == SqlTypeName.CHAR || typeName == SqlTypeName.VARCHAR)) {
    if(throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }

  final SqlLiteral literal = (SqlLiteral) node;
  final String value = ((NlsString)literal.getValue()).getValue();
  if(validStrings.contains(value.toLowerCase())) {
    return true;
  }

  if(throwOnFailure) {
    throw callBinding.newValidationSignatureError();
    //throw new SqlValidatorException(String.format("DATE_PART function only accepts the following values for a date type: %s.", Joiner.on(", ").join(validStrings)), null);
  }

  return false;
}
 
Example 12
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static boolean areRowTypesCompatible(RelDataType rowType1, RelDataType rowType2, boolean compareNames,
        boolean allowSubstring) {
    if (rowType1 == rowType2) {
        return true;
    }
    if (compareNames) {
        // if types are not identity-equal, then either the names or
        // the types must be different
        return false;
    }
    if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
        return false;
    }
    final List<RelDataTypeField> f1 = rowType1.getFieldList();
    final List<RelDataTypeField> f2 = rowType2.getFieldList();
    for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
        final RelDataType type1 = pair.left.getType();
        final RelDataType type2 = pair.right.getType();
        // If one of the types is ANY comparison should succeed
        if (type1.getSqlTypeName() == SqlTypeName.ANY || type2.getSqlTypeName() == SqlTypeName.ANY) {
            continue;
        }
        if (type1.getSqlTypeName() != type2.getSqlTypeName()) {
            if (allowSubstring
                    && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR)
                    && (type1.getPrecision() <= type2.getPrecision())) {
                return true;
            }

            // Check if Drill implicit casting can resolve the incompatibility
            List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2);
            types.add(Types.getMinorTypeFromName(type1.getSqlTypeName().getName()));
            types.add(Types.getMinorTypeFromName(type2.getSqlTypeName().getName()));
            return TypeCastRules.getLeastRestrictiveType(types) != null;
        }
    }
    return true;
}
 
Example 13
Source File: MoreRelOptUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static boolean checkRowTypesCompatiblity(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames,
    boolean allowSubstring,
    boolean insertOp) {
  if (rowType1 == rowType2) {
    return true;
  }
  if (compareNames) {
    // if types are not identity-equal, then either the names or
    // the types must be different
    return false;
  }
  if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
    return false;
  }
  final List<RelDataTypeField> f1 = rowType1.getFieldList();
  final List<RelDataTypeField> f2 = rowType2.getFieldList();
  for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
    final RelDataType type1 = pair.left.getType();
    final RelDataType type2 = pair.right.getType();
    // If one of the types is ANY comparison should succeed
    if (type1.getSqlTypeName() == SqlTypeName.ANY
      || type2.getSqlTypeName() == SqlTypeName.ANY) {
      continue;
    }
    if (!(type1.toString().equals(type2.toString()))) {
      if (allowSubstring
          && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR)
          && (type1.getPrecision() <= type2.getPrecision())) {
        continue;
      }

      // Check if Dremio implicit casting can resolve the incompatibility
      List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2);
      TypeProtos.MinorType minorType1 = Types.getMinorTypeFromName(type1.getSqlTypeName().getName());
      TypeProtos.MinorType minorType2 = Types.getMinorTypeFromName(type2.getSqlTypeName().getName());
      types.add(minorType1);
      types.add(minorType2);
      if (insertOp) {
        // Insert is more strict than normal select in terms of implicit casts
        // Return false if TypeCastRules do not allow implicit cast
        if (TypeCastRules.isCastable(minorType1, minorType2, true) &&
          TypeCastRules.getLeastRestrictiveTypeForInsert(types) != null) {
          if (TypeCastRules.isCastSafeFromDataTruncation(type1, type2)) {
            continue;
          }
        }
      } else {
        if (TypeCastRules.getLeastRestrictiveType(types) != null) {
          continue;
        }
      }

      return false;
    }
  }
  return true;
}
 
Example 14
Source File: SqlCharStringLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected SqlCharStringLiteral(NlsString val, SqlParserPos pos) {
  super(val, SqlTypeName.CHAR, pos);
}
 
Example 15
Source File: SqlCharStringLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected SqlCharStringLiteral(NlsString val, SqlParserPos pos) {
  super(val, SqlTypeName.CHAR, pos);
}
 
Example 16
Source File: RexSimplify.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RexNode simplifyCast(RexCall e) {
  RexNode operand = e.getOperands().get(0);
  operand = simplify(operand, UNKNOWN);
  if (sameTypeOrNarrowsNullability(e.getType(), operand.getType())) {
    return operand;
  }
  if (RexUtil.isLosslessCast(operand)) {
    // x :: y below means cast(x as y) (which is PostgreSQL-specifiic cast by the way)
    // A) Remove lossless casts:
    // A.1) intExpr :: bigint :: int => intExpr
    // A.2) char2Expr :: char(5) :: char(2) => char2Expr
    // B) There are cases when we can't remove two casts, but we could probably remove inner one
    // B.1) char2expression :: char(4) :: char(5) -> char2expression :: char(5)
    // B.2) char2expression :: char(10) :: char(5) -> char2expression :: char(5)
    // B.3) char2expression :: varchar(10) :: char(5) -> char2expression :: char(5)
    // B.4) char6expression :: varchar(10) :: char(5) -> char6expression :: char(5)
    // C) Simplification is not possible:
    // C.1) char6expression :: char(3) :: char(5) -> must not be changed
    //      the input is truncated to 3 chars, so we can't use char6expression :: char(5)
    // C.2) varchar2Expr :: char(5) :: varchar(2) -> must not be changed
    //      the input have to be padded with spaces (up to 2 chars)
    // C.3) char2expression :: char(4) :: varchar(5) -> must not be changed
    //      would not have the padding

    // The approach seems to be:
    // 1) Ensure inner cast is lossless (see if above)
    // 2) If operand of the inner cast has the same type as the outer cast,
    //    remove two casts except C.2 or C.3-like pattern (== inner cast is CHAR)
    // 3) If outer cast is lossless, remove inner cast (B-like cases)

    // Here we try to remove two casts in one go (A-like cases)
    RexNode intExpr = ((RexCall) operand).operands.get(0);
    // intExpr == CHAR detects A.1
    // operand != CHAR detects C.2
    if ((intExpr.getType().getSqlTypeName() == SqlTypeName.CHAR
        || operand.getType().getSqlTypeName() != SqlTypeName.CHAR)
        && sameTypeOrNarrowsNullability(e.getType(), intExpr.getType())) {
      return intExpr;
    }
    // Here we try to remove inner cast (B-like cases)
    if (RexUtil.isLosslessCast(intExpr.getType(), operand.getType())
        && (e.getType().getSqlTypeName() == operand.getType().getSqlTypeName()
        || e.getType().getSqlTypeName() == SqlTypeName.CHAR
        || operand.getType().getSqlTypeName() != SqlTypeName.CHAR)) {
      return rexBuilder.makeCast(e.getType(), intExpr);
    }
  }
  switch (operand.getKind()) {
  case LITERAL:
    final RexLiteral literal = (RexLiteral) operand;
    final Comparable value = literal.getValueAs(Comparable.class);
    final SqlTypeName typeName = literal.getTypeName();

    // First, try to remove the cast without changing the value.
    // makeCast and canRemoveCastFromLiteral have the same logic, so we are
    // sure to be able to remove the cast.
    if (rexBuilder.canRemoveCastFromLiteral(e.getType(), value, typeName)) {
      return rexBuilder.makeCast(e.getType(), operand);
    }

    // Next, try to convert the value to a different type,
    // e.g. CAST('123' as integer)
    switch (literal.getTypeName()) {
    case TIME:
      switch (e.getType().getSqlTypeName()) {
      case TIMESTAMP:
        return e;
      }
      break;
    }
    final List<RexNode> reducedValues = new ArrayList<>();
    final RexNode simplifiedExpr = rexBuilder.makeCast(e.getType(), operand);
    executor.reduce(rexBuilder, ImmutableList.of(simplifiedExpr), reducedValues);
    return Objects.requireNonNull(
        Iterables.getOnlyElement(reducedValues));
  default:
    if (operand == e.getOperands().get(0)) {
      return e;
    } else {
      return rexBuilder.makeCast(e.getType(), operand);
    }
  }
}
 
Example 17
Source File: RexLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Computes if data type can be omitted from the digset.
 * <p>For instance, {@code 1:BIGINT} has to keep data type while {@code 1:INT}
 * should be represented as just {@code 1}.
 *
 * <p>Implementation assumption: this method should be fast. In fact might call
 * {@link NlsString#getValue()} which could decode the string, however we rely on the cache there.
 *
 * @see RexLiteral#computeDigest(RexDigestIncludeType)
 * @param value value of the literal
 * @param type type of the literal
 * @return NO_TYPE when type can be omitted, ALWAYS otherwise
 */
private static RexDigestIncludeType shouldIncludeType(Comparable value, RelDataType type) {
  if (type.isNullable()) {
    // This means "null literal", so we require a type for it
    // There might be exceptions like AND(null, true) which are handled by RexCall#computeDigest
    return RexDigestIncludeType.ALWAYS;
  }
  // The variable here simplifies debugging (one can set a breakpoint at return)
  // final ensures we set the value in all the branches, and it ensures the value is set just once
  final RexDigestIncludeType includeType;
  if (type.getSqlTypeName() == SqlTypeName.BOOLEAN
      || type.getSqlTypeName() == SqlTypeName.INTEGER
      || type.getSqlTypeName() == SqlTypeName.SYMBOL) {
    // We don't want false:BOOLEAN NOT NULL, so we don't print type information for
    // non-nullable BOOLEAN and INTEGER
    includeType = RexDigestIncludeType.NO_TYPE;
  } else if (type.getSqlTypeName() == SqlTypeName.CHAR
          && value instanceof NlsString) {
    NlsString nlsString = (NlsString) value;

    // Ignore type information for 'Bar':CHAR(3)
    if ((
        (nlsString.getCharset() != null && type.getCharset().equals(nlsString.getCharset()))
        || (nlsString.getCharset() == null
        && SqlCollation.IMPLICIT.getCharset().equals(type.getCharset())))
        && nlsString.getCollation().equals(type.getCollation())
        && ((NlsString) value).getValue().length() == type.getPrecision()) {
      includeType = RexDigestIncludeType.NO_TYPE;
    } else {
      includeType = RexDigestIncludeType.ALWAYS;
    }
  } else if (type.getPrecision() == 0 && (
             type.getSqlTypeName() == SqlTypeName.TIME
          || type.getSqlTypeName() == SqlTypeName.TIMESTAMP
          || type.getSqlTypeName() == SqlTypeName.DATE)) {
    // Ignore type information for '12:23:20':TIME(0)
    // Note that '12:23:20':TIME WITH LOCAL TIME ZONE
    includeType = RexDigestIncludeType.NO_TYPE;
  } else {
    includeType = RexDigestIncludeType.ALWAYS;
  }
  return includeType;
}
 
Example 18
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if given type is string scalar type.
 *
 * @param sqlTypeName Calcite's sql type name
 * @return true if given type is string scalar type
 */
public static boolean isScalarStringType(final SqlTypeName sqlTypeName) {
  return sqlTypeName == SqlTypeName.VARCHAR || sqlTypeName == SqlTypeName.CHAR;
}