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

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#DOUBLE . 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: JethroDataSqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
private SqlTypeName parse(String strType) {
  switch (strType.toLowerCase(Locale.ROOT)) {
  case "bigint":
  case "long":
    return SqlTypeName.BIGINT;
  case "integer":
  case "int":
    return SqlTypeName.INTEGER;
  case "double":
    return SqlTypeName.DOUBLE;
  case "float":
    return SqlTypeName.FLOAT;
  case "string":
    return SqlTypeName.VARCHAR;
  case "timestamp":
    return SqlTypeName.TIMESTAMP;
  default:
    return SqlTypeName.ANY;
  }
}
 
Example 2
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * For Extract and date_part functions, infer the return types based on timeUnit
 */
public static SqlTypeName getSqlTypeNameForTimeUnit(String timeUnitStr) {
  TimeUnit timeUnit = TimeUnit.valueOf(timeUnitStr);
  switch (timeUnit) {
    case YEAR:
    case MONTH:
    case DAY:
    case HOUR:
    case MINUTE:
      return SqlTypeName.BIGINT;
    case SECOND:
      return SqlTypeName.DOUBLE;
    default:
      throw UserException
          .functionError()
          .message("extract function supports the following time units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND")
          .build(logger);
  }
}
 
Example 3
Source File: JethroDataSqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
private SqlTypeName parse(String strType) {
  switch (strType.toLowerCase(Locale.ROOT)) {
  case "bigint":
  case "long":
    return SqlTypeName.BIGINT;
  case "integer":
  case "int":
    return SqlTypeName.INTEGER;
  case "double":
    return SqlTypeName.DOUBLE;
  case "float":
    return SqlTypeName.FLOAT;
  case "string":
    return SqlTypeName.VARCHAR;
  case "timestamp":
    return SqlTypeName.TIMESTAMP;
  default:
    return SqlTypeName.ANY;
  }
}
 
Example 4
Source File: SqlNumericLiteral.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected SqlNumericLiteral(
    BigDecimal value,
    Integer prec,
    Integer scale,
    boolean isExact,
    SqlParserPos pos) {
  super(
      value,
      isExact ? SqlTypeName.DECIMAL : SqlTypeName.DOUBLE,
      pos);
  this.prec = prec;
  this.scale = scale;
  this.isExact = isExact;
}
 
Example 5
Source File: DremioArgChecker.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Checker ofDouble(String argName) {
  return new Checker() {

    @Override
    public boolean check(RelDataType type) {
      return type.getSqlTypeName() == SqlTypeName.DOUBLE;
    }

    @Override
    public String signature() {
      return argName + " <DOUBLE>";
    }

  };
}
 
Example 6
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 7
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 8
Source File: Checker.java    From samza with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the {@link SamzaSqlFieldType} to the calcite {@link SqlTypeName}.
 * @param samzaSqlFieldType the samza sql field type.
 * @return the converted calcite SqlTypeName.
 */
@VisibleForTesting
static SqlTypeName toCalciteSqlType(SamzaSqlFieldType samzaSqlFieldType) {
  switch (samzaSqlFieldType) {
    case ANY:
    case ROW:
      return SqlTypeName.ANY;
    case MAP:
      return SqlTypeName.MAP;
    case ARRAY:
      return SqlTypeName.ARRAY;
    case REAL:
      return SqlTypeName.REAL;
    case DOUBLE:
      return SqlTypeName.DOUBLE;
    case STRING:
      return SqlTypeName.VARCHAR;
    case INT16:
    case INT32:
      return SqlTypeName.INTEGER;
    case FLOAT:
      return SqlTypeName.FLOAT;
    case INT64:
      return SqlTypeName.BIGINT;
    case BOOLEAN:
      return SqlTypeName.BOOLEAN;
    case BYTES:
      return SqlTypeName.VARBINARY;
    default:
      String msg = String.format("Field Type %s is not supported", samzaSqlFieldType);
      LOG.error(msg);
      throw new SamzaException(msg);
  }
}
 
Example 9
Source File: SqlNumericLiteral.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected SqlNumericLiteral(
    BigDecimal value,
    Integer prec,
    Integer scale,
    boolean isExact,
    SqlParserPos pos) {
  super(
      value,
      isExact ? SqlTypeName.DECIMAL : SqlTypeName.DOUBLE,
      pos);
  this.prec = prec;
  this.scale = scale;
  this.isExact = isExact;
}
 
Example 10
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 11
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  SqlTypeName typeToCastTo = null;
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding sqlCallBinding = (SqlCallBinding) opBinding;
    if (sqlCallBinding.operand(1).getKind() == SqlKind.LITERAL) {
      String type = null;
      try {
        SqlLiteral sqlLiteral = (SqlLiteral) sqlCallBinding.operand(1);
        type = ((NlsString) sqlLiteral.getValue()).getValue();
        switch(type) {
          case "JSON":
            typeToCastTo = SqlTypeName.ANY;
            break;
          case "UTF8":
          case "UTF16":
            typeToCastTo = SqlTypeName.VARCHAR;
            break;
          case "BOOLEAN_BYTE":
            typeToCastTo = SqlTypeName.BOOLEAN;
            break;
          case "TINYINT_BE":
          case "TINYINT":
            typeToCastTo = SqlTypeName.TINYINT;
            break;
          case "SMALLINT_BE":
          case "SMALLINT":
            typeToCastTo = SqlTypeName.SMALLINT;
            break;
          case "INT_BE":
          case "INT":
          case "INT_HADOOPV":
            typeToCastTo = SqlTypeName.INTEGER;
            break;
          case "BIGINT_BE":
          case "BIGINT":
          case "BIGINT_HADOOPV":
            typeToCastTo = SqlTypeName.BIGINT;
            break;
          case "FLOAT":
            typeToCastTo = SqlTypeName.FLOAT;
            break;
          case "DOUBLE":
            typeToCastTo = SqlTypeName.DOUBLE;
            break;
          case "DATE_EPOCH_BE":
          case "DATE_EPOCH":
            typeToCastTo = SqlTypeName.DATE;
            break;
          case "TIME_EPOCH_BE":
          case "TIME_EPOCH":
            typeToCastTo = SqlTypeName.TIME;
            break;
          case "TIMESTAMP_EPOCH":
          case "TIMESTAMP_IMPALA":
            typeToCastTo = SqlTypeName.TIMESTAMP;
            break;
          default:
            typeToCastTo = SqlTypeName.ANY;
            break;
        }
      } catch (final ClassCastException e) {
        logger.debug("Failed to parse string for convert_from()");
      }
    }
  }

  if (typeToCastTo == null) {
    typeToCastTo = SqlTypeName.ANY;
  }
  return factory.createTypeWithNullability(
      factory.createSqlType(typeToCastTo),
      true);
}