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

The following examples show how to use org.apache.calcite.sql.SqlOperatorBinding#getTypeFactory() . 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: SqlDotOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType recordType = opBinding.getOperandType(0);
  switch (recordType.getSqlTypeName()) {
  case ROW:
    final String fieldName =
        opBinding.getOperandLiteralValue(1, String.class);
    final RelDataType type = opBinding.getOperandType(0)
        .getField(fieldName, false, false)
        .getType();
    if (recordType.isNullable()) {
      return typeFactory.createTypeWithNullability(type, true);
    } else {
      return type;
    }
  default:
    throw new AssertionError();
  }
}
 
Example 2
Source File: SqlItemOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType operandType = opBinding.getOperandType(0);
  switch (operandType.getSqlTypeName()) {
  case ARRAY:
    return typeFactory.createTypeWithNullability(
        operandType.getComponentType(), true);
  case MAP:
    return typeFactory.createTypeWithNullability(operandType.getValueType(),
        true);
  case ANY:
  case DYNAMIC_STAR:
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  default:
    throw new AssertionError();
  }
}
 
Example 3
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private static RelDataType getReturnType(final SqlOperatorBinding opBinding, final AbstractFunctionHolder func) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();

  // least restrictive type (nullable ANY type)
  final RelDataType nullableAnyType = factory.createTypeWithNullability(
          factory.createSqlType(SqlTypeName.ANY),
          true);

  if(!func.isReturnTypeIndependent()){
    return nullableAnyType;
  }

  CompleteType returnType = func.getReturnType(null);
  if(returnType.isList() || returnType.isComplex() || returnType.isUnion()  || returnType.isLate()){
    return nullableAnyType;
  }

  final TypeProtos.MinorType minorType = returnType.toMinorType();
  final SqlTypeName sqlTypeName = getCalciteTypeFromMinorType(minorType);
  if (sqlTypeName == null) {
    return nullableAnyType;
  }

  return createCalciteTypeWithNullability(factory, sqlTypeName, true, returnType.getPrecision());
}
 
Example 4
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();

  boolean isNullable = true;
  int precision = 0;
  for(RelDataType relDataType : opBinding.collectOperandTypes()) {
    if(!relDataType.isNullable()) {
      isNullable = false;
    }

    // If the underlying columns cannot offer information regarding the precision (i.e., the length) of the VarChar,
    // Dremio uses the largest to represent it
    if(relDataType.getPrecision() == TypeHelper.VARCHAR_DEFAULT_CAST_LEN
        || relDataType.getPrecision() == RelDataType.PRECISION_NOT_SPECIFIED) {
      precision = TypeHelper.VARCHAR_DEFAULT_CAST_LEN;
    } else {
      precision += relDataType.getPrecision();
    }
  }

  return factory.createTypeWithNullability(
      factory.createSqlType(SqlTypeName.VARCHAR, precision),
      isNullable);
}
 
Example 5
Source File: SqlDotOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType recordType = opBinding.getOperandType(0);
  switch (recordType.getSqlTypeName()) {
  case ROW:
    final String fieldName =
        opBinding.getOperandLiteralValue(1, String.class);
    final RelDataType type = opBinding.getOperandType(0)
        .getField(fieldName, false, false)
        .getType();
    if (recordType.isNullable()) {
      return typeFactory.createTypeWithNullability(type, true);
    } else {
      return type;
    }
  default:
    throw new AssertionError();
  }
}
 
Example 6
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding.getOperandType(1).isNullable();

  if (!(opBinding instanceof SqlCallBinding) || !(((SqlCallBinding) opBinding).operand(0) instanceof SqlCharStringLiteral)) {
    return createCalciteTypeWithNullability(factory,
        SqlTypeName.ANY,
        isNullable);
  }

  final String part = ((SqlCharStringLiteral) ((SqlCallBinding) opBinding).operand(0))
      .getNlsString()
      .getValue()
      .toUpperCase();

  final SqlTypeName sqlTypeName = getSqlTypeNameForTimeUnit(part);
  return createCalciteTypeWithNullability(
      factory,
      sqlTypeName,
      isNullable);
}
 
Example 7
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  return factory.createTypeWithNullability(
    factory.createSqlType(SqlTypeName.ANY),
    true);
}
 
Example 8
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding
      .getOperandType(0)
      .isNullable();

  RelDataType ret = factory.createTypeWithNullability(
      opBinding.getOperandType(1),
      isNullable);
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    SqlNode operand0 = callBinding.operand(0);

    // dynamic parameters and null constants need their types assigned
    // to them using the type they are casted to.
    if(((operand0 instanceof SqlLiteral)
        && (((SqlLiteral) operand0).getValue() == null))
            || (operand0 instanceof SqlDynamicParam)) {
      callBinding.getValidator().setValidatedNodeType(
          operand0,
          ret);
    }
  }

  return ret;
}
 
Example 9
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding.getOperandType(0).isNullable();
  return createCalciteTypeWithNullability(
      factory,
      SqlTypeName.DOUBLE,
      isNullable);
}
 
Example 10
Source File: MockSqlOperatorTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory =
      opBinding.getTypeFactory();
  return typeFactory.builder()
      .add("NAME", SqlTypeName.VARCHAR, 1024)
      .build();
}
 
Example 11
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName type = SqlTypeName.VARBINARY;

  return createCalciteTypeWithNullability(
      factory, type, opBinding.getOperandType(0).isNullable());
}
 
Example 12
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.BIGINT;

  // We need to check only the first argument because
  // the second one is used to represent encoding type
  final boolean isNullable = opBinding.getOperandType(0).isNullable();
  return createCalciteTypeWithNullability(
      factory,
      sqlTypeName,
      isNullable);
}
 
Example 13
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.VARCHAR;

  for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    if(opBinding.getOperandType(i).isNullable()) {
      return createCalciteTypeWithNullability(factory, sqlTypeName, true, null);
    }
  }

  return createCalciteTypeWithNullability(factory, sqlTypeName, false, null);
}
 
Example 14
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.INTEGER;

  // We need to check only the first argument because
  // the second one is used to represent encoding type
  final boolean isNullable = opBinding.getOperandType(0).isNullable();
  return createCalciteTypeWithNullability(factory, sqlTypeName, isNullable, null);
}
 
Example 15
Source File: MockSqlOperatorTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory =
      opBinding.getTypeFactory();
  return typeFactory.builder()
      .add("I", SqlTypeName.INTEGER)
      .build();
}
 
Example 16
Source File: SqlDatetimePlusOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType leftType = opBinding.getOperandType(0);
  final IntervalSqlType unitType =
      (IntervalSqlType) opBinding.getOperandType(1);
  final TimeUnit timeUnit = unitType.getIntervalQualifier().getStartUnit();
  return SqlTimestampAddFunction.deduceType(typeFactory, timeUnit,
      unitType, leftType);
}
 
Example 17
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 18
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 19
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);
  }
}
 
Example 20
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();
  if (functions.isEmpty()) {
    return factory.createTypeWithNullability(
        factory.createSqlType(SqlTypeName.ANY),
        true);
  }

  // The following logic is just a safe play:
  // Even if any of the input arguments has ANY type,
  // it "might" still be possible to determine the return type based on other non-ANY types
  for (RelDataType type : opBinding.collectOperandTypes()) {
    if (getMinorTypeFromCalciteType(type) == TypeProtos.MinorType.LATE) {
      // This code for boolean output type is added for addressing DRILL-1729
      // In summary, if we have a boolean output function in the WHERE-CLAUSE,
      // this logic can validate and execute user queries seamlessly
      boolean allBooleanOutput = true;
      for (AbstractFunctionHolder function : functions) {
        if (!function.isReturnTypeIndependent() || function.getReturnType(null).toMinorType() != TypeProtos.MinorType.BIT) {
          allBooleanOutput = false;
          break;
        }
      }

      if(allBooleanOutput) {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.BOOLEAN), true);
      } else {
        return factory.createTypeWithNullability(
            factory.createSqlType(SqlTypeName.ANY),
            true);
      }
    }
  }

  final AbstractFunctionHolder func = resolveFunctionHolder(opBinding, functions, isDecimalV2Enabled);
  final RelDataType returnType = getReturnType(opBinding, func);
  return returnType.getSqlTypeName() == SqlTypeName.VARBINARY
      ? createCalciteTypeWithNullability(factory, SqlTypeName.ANY, returnType.isNullable(), null)
          : returnType;
}