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

The following examples show how to use org.apache.calcite.sql.type.SqlTypeName#TIME . 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: SqlTimestampAddFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RelDataType deduceType(RelDataTypeFactory typeFactory,
    TimeUnit timeUnit, RelDataType operandType1, RelDataType operandType2) {
  final RelDataType type;
  switch (timeUnit) {
  case HOUR:
  case MINUTE:
  case SECOND:
  case MILLISECOND:
  case MICROSECOND:
    switch (timeUnit) {
    case MILLISECOND:
      type = typeFactory.createSqlType(SqlTypeName.TIMESTAMP,
          MILLISECOND_PRECISION);
      break;
    case MICROSECOND:
      type = typeFactory.createSqlType(SqlTypeName.TIMESTAMP,
          MICROSECOND_PRECISION);
      break;
    default:
      if (operandType2.getSqlTypeName() == SqlTypeName.TIME) {
        type = typeFactory.createSqlType(SqlTypeName.TIME);
      } else {
        type = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
      }
    }
    break;
  default:
    type = operandType2;
  }
  return typeFactory.createTypeWithNullability(type,
      operandType1.isNullable()
          || operandType2.isNullable());
}
 
Example 2
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 3
Source File: SqlTimeType.java    From flink with Apache License 2.0 5 votes vote down vote up
public SqlTypeName getSqlTypeName() {
	if (withLocalTimeZone) {
		return SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE;
	} else {
		return SqlTypeName.TIME;
	}
}
 
Example 4
Source File: SqlTimestampAddFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelDataType deduceType(RelDataTypeFactory typeFactory,
    TimeUnit timeUnit, RelDataType operandType1, RelDataType operandType2) {
  final RelDataType type;
  switch (timeUnit) {
  case HOUR:
  case MINUTE:
  case SECOND:
  case MILLISECOND:
  case MICROSECOND:
    switch (timeUnit) {
    case MILLISECOND:
      type = typeFactory.createSqlType(SqlTypeName.TIMESTAMP,
          MILLISECOND_PRECISION);
      break;
    case MICROSECOND:
      type = typeFactory.createSqlType(SqlTypeName.TIMESTAMP,
          MICROSECOND_PRECISION);
      break;
    default:
      if (operandType2.getSqlTypeName() == SqlTypeName.TIME) {
        type = typeFactory.createSqlType(SqlTypeName.TIME);
      } else {
        type = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
      }
    }
    break;
  default:
    type = operandType2;
  }
  return typeFactory.createTypeWithNullability(type,
      operandType1.isNullable()
          || operandType2.isNullable());
}
 
Example 5
Source File: SqlBasicTypeNameSpec.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the local time zone definition of the {@code typeName}.
 *
 * @param typeName Type name
 * @return new type name without local time zone definition
 */
private SqlTypeName stripLocalTimeZoneDef(SqlTypeName typeName) {
  switch (typeName) {
  case TIME_WITH_LOCAL_TIME_ZONE:
    return SqlTypeName.TIME;
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return SqlTypeName.TIMESTAMP;
  default:
    throw new AssertionError(typeName);
  }
}
 
Example 6
Source File: SqlTimeLiteral.java    From Bats with Apache License 2.0 4 votes vote down vote up
SqlTimeLiteral(TimeString t, int precision, boolean hasTimeZone,
    SqlParserPos pos) {
  super(t, hasTimeZone, SqlTypeName.TIME, precision, pos);
  Preconditions.checkArgument(this.precision >= 0);
}
 
Example 7
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  RelDataTypeFactory factory = opBinding.getTypeFactory();
  // operands count ond order is checked at parsing stage
  RelDataType inputType = opBinding.getOperandType(2);
  boolean isNullable = inputType.isNullable() || opBinding.getOperandType(1).isNullable();

  SqlTypeName inputTypeName = inputType.getSqlTypeName();

  TimeUnit qualifier = ((SqlLiteral) ((SqlCallBinding) opBinding).operand(0)).getValueAs(TimeUnit.class);

  SqlTypeName sqlTypeName;

  // follow up with type inference of reduced expression
  switch (qualifier) {
    case DAY:
    case WEEK:
    case MONTH:
    case QUARTER:
    case YEAR:
    case NANOSECOND:  // NANOSECOND is not supported by Calcite SqlTimestampAddFunction.
                      // Once it is fixed, NANOSECOND should be moved to the group below.
      sqlTypeName = inputTypeName;
      break;
    case MICROSECOND:
    case MILLISECOND:
      // precision should be specified for MICROSECOND and MILLISECOND
      return factory.createTypeWithNullability(
          factory.createSqlType(SqlTypeName.TIMESTAMP, 3),
          isNullable);
    case SECOND:
    case MINUTE:
    case HOUR:
      if (inputTypeName == SqlTypeName.TIME) {
        sqlTypeName = SqlTypeName.TIME;
      } else {
        sqlTypeName = SqlTypeName.TIMESTAMP;
      }
      break;
    default:
      sqlTypeName = SqlTypeName.ANY;
  }

  // preserves precision of input type if it was specified
  if (inputType.getSqlTypeName().allowsPrecNoScale()) {
    RelDataType type = factory.createSqlType(sqlTypeName, inputType.getPrecision());
    return factory.createTypeWithNullability(type, isNullable);
  }
  return createCalciteTypeWithNullability(
      opBinding.getTypeFactory(),
      sqlTypeName,
      isNullable);
}
 
Example 8
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);
}
 
Example 9
Source File: SqlTimeLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
SqlTimeLiteral(TimeString t, int precision, boolean hasTimeZone,
    SqlParserPos pos) {
  super(t, hasTimeZone, SqlTypeName.TIME, precision, pos);
  Preconditions.checkArgument(this.precision >= 0);
}
 
Example 10
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;
}