Java Code Examples for org.apache.calcite.sql.SqlOperatorBinding#getGroupCount()

The following examples show how to use org.apache.calcite.sql.SqlOperatorBinding#getGroupCount() . 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: TypeInferenceUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  // If there is group-by and the imput type is Non-nullable,
  // the output is Non-nullable;
  // Otherwise, the output is nullable.
  final boolean isNullable = opBinding.getGroupCount() == 0
      || opBinding.getOperandType(0).isNullable();

  if(getDrillTypeFromCalciteType(opBinding.getOperandType(0)) == TypeProtos.MinorType.LATE) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.ANY,
        isNullable);
  }

  // Determines SqlTypeName of the result.
  // For the case when input may be implicitly casted to BIGINT, the type of result is BIGINT.
  // Else for the case when input may be implicitly casted to FLOAT4, the type of result is DOUBLE.
  // Else for the case when input may be implicitly casted to VARDECIMAL, the type of result is DECIMAL
  // with the same scale as input and max allowed numeric precision.
  // Else for the case when input may be implicitly casted to FLOAT8, the type of result is DOUBLE.
  // When none of these conditions is satisfied, error is thrown.
  // This order of checks is caused by the order of types in ResolverTypePrecedence.precedenceMap
  final RelDataType operandType = opBinding.getOperandType(0);
  final TypeProtos.MinorType inputMinorType = getDrillTypeFromCalciteType(operandType);
  if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.BIGINT))
      == TypeProtos.MinorType.BIGINT) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.BIGINT,
        isNullable);
  } else if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.FLOAT4))
      == TypeProtos.MinorType.FLOAT4) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.DOUBLE,
        isNullable);
  } else if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.VARDECIMAL))
      == TypeProtos.MinorType.VARDECIMAL) {
    RelDataType sqlType = factory.createSqlType(SqlTypeName.DECIMAL,
      DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(),
      Math.min(operandType.getScale(),
        DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericScale()));
    return factory.createTypeWithNullability(sqlType, isNullable);
  } else if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.FLOAT8))
      == TypeProtos.MinorType.FLOAT8) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.DOUBLE,
        isNullable);
  } else {
    throw UserException
        .functionError()
        .message(String.format("%s does not support operand types (%s)",
            opBinding.getOperator().getName(),
            opBinding.getOperandType(0).getSqlTypeName()))
        .build(logger);
  }
}
 
Example 2
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  // If there is group-by and the imput type is Non-nullable,
  // the output is Non-nullable;
  // Otherwise, the output is nullable.
  final boolean isNullable = opBinding.getGroupCount() == 0
      || opBinding.getOperandType(0).isNullable();

  if (getDrillTypeFromCalciteType(opBinding.getOperandType(0)) == TypeProtos.MinorType.LATE) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.ANY,
        isNullable);
  }

  // Determines SqlTypeName of the result.
  // For the case when input may be implicitly casted to FLOAT4, the type of result is DOUBLE.
  // Else for the case when input may be implicitly casted to VARDECIMAL, the type of result is DECIMAL
  // with scale max(6, input) and max allowed numeric precision.
  // Else for the case when input may be implicitly casted to FLOAT8, the type of result is DOUBLE.
  // When none of these conditions is satisfied, error is thrown.
  // This order of checks is caused by the order of types in ResolverTypePrecedence.precedenceMap
  final RelDataType operandType = opBinding.getOperandType(0);
  final TypeProtos.MinorType inputMinorType = getDrillTypeFromCalciteType(operandType);
  if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.FLOAT4))
      == TypeProtos.MinorType.FLOAT4) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.DOUBLE,
        isNullable);
  } else if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.VARDECIMAL))
      == TypeProtos.MinorType.VARDECIMAL) {
    RelDataType sqlType = factory.createSqlType(SqlTypeName.DECIMAL,
        DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision(),
        Math.min(Math.max(6, operandType.getScale()),
            DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericScale()));
    return factory.createTypeWithNullability(sqlType, isNullable);
  } else if (TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, TypeProtos.MinorType.FLOAT8))
      == TypeProtos.MinorType.FLOAT8) {
    return createCalciteTypeWithNullability(
        factory,
        SqlTypeName.DOUBLE,
        isNullable);
  } else {
    throw UserException
        .functionError()
        .message(String.format("%s does not support operand types (%s)",
            opBinding.getOperator().getName(),
            opBinding.getOperandType(0).getSqlTypeName()))
        .build(logger);
  }
}