Java Code Examples for org.apache.calcite.sql.SqlOperatorBinding

The following examples show how to use org.apache.calcite.sql.SqlOperatorBinding. These examples are extracted from open source projects. 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 Project: Bats   Source File: SqlLiteralChainOperator.java    License: Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // Here we know all the operands have the same type,
  // which has a size (precision), but not a scale.
  RelDataType ret = opBinding.getOperandType(0);
  SqlTypeName typeName = ret.getSqlTypeName();
  assert typeName.allowsPrecNoScale()
      : "LiteralChain has impossible operand type "
      + typeName;
  int size = 0;
  for (RelDataType type : opBinding.collectOperandTypes()) {
    size += type.getPrecision();
    assert type.getSqlTypeName() == typeName;
  }
  return opBinding.getTypeFactory().createSqlType(typeName, size);
}
 
Example 2
Source Project: Bats   Source File: SqlItemOperator.java    License: 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 Project: flink   Source File: OperatorBindingCallContext.java    License: Apache License 2.0 6 votes vote down vote up
public OperatorBindingCallContext(
		DataTypeFactory dataTypeFactory,
		FunctionDefinition definition,
		SqlOperatorBinding binding) {
	super(
		dataTypeFactory,
		definition,
		binding.getOperator().getNameAsId().toString());

	this.binding = binding;
	this.argumentDataTypes = new AbstractList<DataType>() {
		@Override
		public DataType get(int pos) {
			final LogicalType logicalType = FlinkTypeFactory.toLogicalType(binding.getOperandType(pos));
			return TypeConversions.fromLogicalToDataType(logicalType);
		}

		@Override
		public int size() {
			return binding.getOperandCount();
		}
	};
}
 
Example 4
Source Project: calcite   Source File: SqlAbstractTimeFunction.java    License: Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // REVIEW jvs 20-Feb-2005: Need to take care of time zones.
  int precision = 0;
  if (opBinding.getOperandCount() == 1) {
    RelDataType type = opBinding.getOperandType(0);
    if (SqlTypeUtil.isNumeric(type)) {
      precision = opBinding.getOperandLiteralValue(0, Integer.class);
    }
  }
  assert precision >= 0;
  if (precision > SqlTypeName.MAX_DATETIME_PRECISION) {
    throw opBinding.newError(
        RESOURCE.argumentMustBeValidPrecision(
            opBinding.getOperator().getName(), 0,
            SqlTypeName.MAX_DATETIME_PRECISION));
  }
  return opBinding.getTypeFactory().createSqlType(typeName, precision);
}
 
Example 5
Source Project: calcite   Source File: SqlRowOperator.java    License: Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    final SqlOperatorBinding opBinding) {
  // The type of a ROW(e1,e2) expression is a record with the types
  // {e1type,e2type}.  According to the standard, field names are
  // implementation-defined.
  return opBinding.getTypeFactory().createStructType(
      new AbstractList<Map.Entry<String, RelDataType>>() {
        public Map.Entry<String, RelDataType> get(int index) {
          return Pair.of(
              SqlUtil.deriveAliasFromOrdinal(index),
              opBinding.getOperandType(index));
        }

        public int size() {
          return opBinding.getOperandCount();
        }
      });
}
 
Example 6
Source Project: Bats   Source File: TypeInferenceUtils.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  boolean isNullable = isNullable(opBinding.collectOperandTypes());

  boolean isScalarString = isScalarStringType(opBinding.getOperandType(0).getSqlTypeName());
  int precision = opBinding.getOperandType(0).getPrecision();

  if (isScalarString && precision != RelDataType.PRECISION_NOT_SPECIFIED) {
    RelDataType sqlType = opBinding.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, precision);
    return opBinding.getTypeFactory().createTypeWithNullability(sqlType, isNullable);
  }

  return createCalciteTypeWithNullability(
      opBinding.getTypeFactory(),
      SqlTypeName.VARCHAR,
      isNullable);
}
 
Example 7
Source Project: flink   Source File: FlinkReturnTypes.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	final RelDataType numType = opBinding.getOperandType(0);
	if (numType.getSqlTypeName() != SqlTypeName.DECIMAL) {
		return numType;
	}
	final BigDecimal lenVal;
	if (opBinding.getOperandCount() == 1) {
		lenVal = BigDecimal.ZERO;
	} else if (opBinding.getOperandCount() == 2) {
		lenVal = getArg1Literal(opBinding); // may return null
	} else {
		throw new AssertionError();
	}
	if (lenVal == null) {
		return numType; //
	}
	// ROUND( decimal(p,s), r )
	final int p = numType.getPrecision();
	final int s = numType.getScale();
	final int r = lenVal.intValueExact();
	DecimalType dt = LogicalTypeMerging.findRoundDecimalType(p, s, r);
	return opBinding.getTypeFactory().createSqlType(
		SqlTypeName.DECIMAL, dt.getPrecision(), dt.getScale());
}
 
Example 8
Source Project: calcite   Source File: ComparableOperandTypeChecker.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Similar functionality to
 * {@link #checkOperandTypes(SqlCallBinding, boolean)}, but not part of the
 * interface, and cannot throw an error.
 */
public boolean checkOperandTypes(
    SqlOperatorBinding operatorBinding, SqlCallBinding callBinding) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (type.getComparability().ordinal() < requiredComparability.ordinal()) {
      b = false;
      break;
    }
  }
  if (b) {
    b = super.checkOperandTypes(operatorBinding, callBinding);
  }
  return b;
}
 
Example 9
Source Project: Bats   Source File: TypeInferenceUtils.java    License: Apache License 2.0 6 votes vote down vote up
private static DrillFuncHolder resolveDrillFuncHolder(final SqlOperatorBinding opBinding,
    final List<DrillFuncHolder> functions, FunctionCall functionCall) {
  final FunctionResolver functionResolver = FunctionResolverFactory.getResolver(functionCall);
  final DrillFuncHolder func = functionResolver.getBestMatch(functions, functionCall);

  // Throw an exception
  // if no DrillFuncHolder matched for the given list of operand types
  if (func == null) {
    StringBuilder operandTypes = new StringBuilder();
    for (int i = 0; i < opBinding.getOperandCount(); ++i) {
      operandTypes.append(opBinding.getOperandType(i).getSqlTypeName());
      if (i < opBinding.getOperandCount() - 1) {
        operandTypes.append(",");
      }
    }

    throw UserException
        .functionError()
        .message(String.format("%s does not support operand types (%s)",
            opBinding.getOperator().getName(),
            operandTypes.toString()))
        .build(logger);
  }
  return func;
}
 
Example 10
Source Project: flink   Source File: FlinkReturnTypes.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	RelDataType type = super.inferReturnType(opBinding);
	RelDataType newType;
	switch (type.getSqlTypeName()) {
		case CHAR:
			newType = opBinding.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, type.getPrecision());
			break;
		case VARCHAR:
			newType = type;
			break;
		default:
			throw new UnsupportedOperationException("Unsupported type: " + type);
	}
	return opBinding.getTypeFactory().createTypeWithNullability(newType, true);
}
 
Example 11
Source Project: Bats   Source File: SqlBetweenOperator.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  SqlCallBinding callBinding = (SqlCallBinding) opBinding;
  ExplicitOperatorBinding newOpBinding =
      new ExplicitOperatorBinding(
          opBinding,
          collectOperandTypes(
              callBinding.getValidator(),
              callBinding.getScope(),
              callBinding.getCall()));
  return ReturnTypes.BOOLEAN_NULLABLE.inferReturnType(
      newOpBinding);
}
 
Example 12
Source Project: flink   Source File: NumericOrDefaultReturnTypeInference.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	int nOperands = opBinding.getOperandCount();
	List<RelDataType> types = new ArrayList<>();
	for (int i = startTypeIdx; i < nOperands; i++) {
		RelDataType type = opBinding.getOperandType(i);
		if (SqlTypeUtil.isNumeric(type)) {
			types.add(type);
		} else {
			return opBinding.getOperandType(defaultTypeIdx);
		}
	}
	return opBinding.getTypeFactory().leastRestrictive(types);
}
 
Example 13
Source Project: Bats   Source File: SqlSubstringFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  // SUBSTRING(x FROM 0 FOR constant) has same monotonicity as x
  if (call.getOperandCount() == 3) {
    final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
    if ((mono0 != SqlMonotonicity.NOT_MONOTONIC)
        && call.getOperandMonotonicity(1) == SqlMonotonicity.CONSTANT
        && call.getOperandLiteralValue(1, BigDecimal.class)
            .equals(BigDecimal.ZERO)
        && call.getOperandMonotonicity(2) == SqlMonotonicity.CONSTANT) {
      return mono0.unstrict();
    }
  }
  return super.getMonotonicity(call);
}
 
Example 14
Source Project: calcite   Source File: SameOperandTypeChecker.java    License: Apache License 2.0 5 votes vote down vote up
protected boolean checkOperandTypesImpl(
    SqlOperatorBinding operatorBinding,
    boolean throwOnFailure,
    SqlCallBinding callBinding) {
  int nOperandsActual = nOperands;
  if (nOperandsActual == -1) {
    nOperandsActual = operatorBinding.getOperandCount();
  }
  assert !(throwOnFailure && (callBinding == null));
  RelDataType[] types = new RelDataType[nOperandsActual];
  final List<Integer> operandList =
      getOperandList(operatorBinding.getOperandCount());
  for (int i : operandList) {
    types[i] = operatorBinding.getOperandType(i);
  }
  int prev = -1;
  for (int i : operandList) {
    if (prev >= 0) {
      if (!SqlTypeUtil.isComparable(types[i], types[prev])) {
        if (!throwOnFailure) {
          return false;
        }

        // REVIEW jvs 5-June-2005: Why don't we use
        // newValidationSignatureError() here?  It gives more
        // specific diagnostics.
        throw callBinding.newValidationError(
            RESOURCE.needSameTypeParameter());
      }
    }
    prev = i;
  }
  return true;
}
 
Example 15
Source Project: dremio-oss   Source File: TypeInferenceUtils.java    License: 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(), null);
}
 
Example 16
Source Project: Bats   Source File: SqlCaseOperator.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // REVIEW jvs 4-June-2005:  can't these be unified?
  if (!(opBinding instanceof SqlCallBinding)) {
    return inferTypeFromOperands(
        opBinding.getTypeFactory(),
        opBinding.collectOperandTypes());
  }
  return inferTypeFromValidator((SqlCallBinding) opBinding);
}
 
Example 17
Source Project: calcite   Source File: MockSqlOperatorTable.java    License: 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 18
Source Project: calcite   Source File: SqlReturnTypeInferenceChain.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  for (SqlReturnTypeInference rule : rules) {
    RelDataType ret = rule.inferReturnType(opBinding);
    if (ret != null) {
      return ret;
    }
  }
  return null;
}
 
Example 19
Source Project: calcite   Source File: SqlSubstringFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  // SUBSTRING(x FROM 0 FOR constant) has same monotonicity as x
  if (call.getOperandCount() == 3) {
    final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
    if ((mono0 != SqlMonotonicity.NOT_MONOTONIC)
        && call.getOperandMonotonicity(1) == SqlMonotonicity.CONSTANT
        && call.getOperandLiteralValue(1, BigDecimal.class)
            .equals(BigDecimal.ZERO)
        && call.getOperandMonotonicity(2) == SqlMonotonicity.CONSTANT) {
      return mono0.unstrict();
    }
  }
  return super.getMonotonicity(call);
}
 
Example 20
Source Project: Bats   Source File: SqlMapValueConstructor.java    License: Apache License 2.0 5 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  Pair<RelDataType, RelDataType> type =
      getComponentTypes(
          opBinding.getTypeFactory(), opBinding.collectOperandTypes());
  if (null == type) {
    return null;
  }
  return SqlTypeUtil.createMapType(
      opBinding.getTypeFactory(),
      type.left,
      type.right,
      false);
}
 
Example 21
Source Project: Bats   Source File: SameOperandTypeChecker.java    License: Apache License 2.0 5 votes vote down vote up
protected boolean checkOperandTypesImpl(
    SqlOperatorBinding operatorBinding,
    boolean throwOnFailure,
    SqlCallBinding callBinding) {
  int nOperandsActual = nOperands;
  if (nOperandsActual == -1) {
    nOperandsActual = operatorBinding.getOperandCount();
  }
  assert !(throwOnFailure && (callBinding == null));
  RelDataType[] types = new RelDataType[nOperandsActual];
  final List<Integer> operandList =
      getOperandList(operatorBinding.getOperandCount());
  for (int i : operandList) {
    types[i] = operatorBinding.getOperandType(i);
  }
  int prev = -1;
  for (int i : operandList) {
    if (prev >= 0) {
      if (!SqlTypeUtil.isComparable(types[i], types[prev])) {
        if (!throwOnFailure) {
          return false;
        }

        // REVIEW jvs 5-June-2005: Why don't we use
        // newValidationSignatureError() here?  It gives more
        // specific diagnostics.
        throw callBinding.newValidationError(
            RESOURCE.needSameTypeParameter());
      }
    }
    prev = i;
  }
  return true;
}
 
Example 22
Source Project: calcite   Source File: SqlCaseOperator.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // REVIEW jvs 4-June-2005:  can't these be unified?
  if (!(opBinding instanceof SqlCallBinding)) {
    return inferTypeFromOperands(opBinding);
  }
  return inferTypeFromValidator((SqlCallBinding) opBinding);
}
 
Example 23
Source Project: Bats   Source File: SqlReturnTypeInferenceChain.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  for (SqlReturnTypeInference rule : rules) {
    RelDataType ret = rule.inferReturnType(opBinding);
    if (ret != null) {
      return ret;
    }
  }
  return null;
}
 
Example 24
Source Project: Bats   Source File: MatchReturnTypeInference.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  for (int i = start; i < opBinding.getOperandCount(); i++) {
    RelDataType argType = opBinding.getOperandType(i);
    if (SqlTypeUtil.isOfSameTypeName(typeNames, argType)) {
      return argType;
    }
  }
  return null;
}
 
Example 25
Source Project: Bats   Source File: ComparableOperandTypeChecker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Similar functionality to
 * {@link #checkOperandTypes(SqlCallBinding, boolean)}, but not part of the
 * interface, and cannot throw an error.
 */
public boolean checkOperandTypes(
    SqlOperatorBinding callBinding) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (type.getComparability().ordinal() < requiredComparability.ordinal()) {
      b = false;
    }
  }
  if (b) {
    b = super.checkOperandTypes(callBinding);
  }
  return b;
}
 
Example 26
Source Project: Bats   Source File: DrillReduceAggregatesRule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    return TypeInferenceUtils.createCalciteTypeWithNullability(
        opBinding.getTypeFactory(),
        SqlTypeName.ANY,
        opBinding.getOperandType(0).isNullable());
}
 
Example 27
Source Project: Bats   Source File: DrillCalciteSqlAggFunctionWrapper.java    License: Apache License 2.0 5 votes vote down vote up
public DrillCalciteSqlAggFunctionWrapper(
    final SqlAggFunction sqlAggFunction,
    final RelDataType relDataType) {
  this(sqlAggFunction, new SqlReturnTypeInference() {
    @Override
    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
      return relDataType;
    }
  });
}
 
Example 28
Source Project: dremio-oss   Source File: TypeInferenceUtils.java    License: 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 29
Source Project: Bats   Source File: TypeInferenceUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  if (opBinding instanceof SqlCallBinding && (((SqlCallBinding) opBinding).operand(1) instanceof  SqlNumericLiteral)) {
    int precision = ((SqlNumericLiteral) ((SqlCallBinding) opBinding).operand(1)).intValue(true);
    RelDataType sqlType = opBinding.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, Math.max(precision, 0));
    return opBinding.getTypeFactory().createTypeWithNullability(sqlType, isNullable(opBinding.collectOperandTypes()));
  }

  return createCalciteTypeWithNullability(
      opBinding.getTypeFactory(),
      SqlTypeName.VARCHAR,
      isNullable(opBinding.collectOperandTypes()));

}
 
Example 30
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  for (RelDataType type : opBinding.collectOperandTypes()) {
    final TypeProtos.MinorType minorType = TypeInferenceUtils.getMinorTypeFromCalciteType(type);
    if(minorType == TypeProtos.MinorType.LATE) {
      return opBinding.getTypeFactory()
        .createTypeWithNullability(
          opBinding.getTypeFactory().createSqlType(SqlTypeName.ANY),
          true);
    }
  }

  final FunctionCall functionCall = TypeInferenceUtils.convertSqlOperatorBindingToFunctionCall(opBinding);
  final AbstractFunctionHolder funcHolder = registry.getFunction(functionCall);
  if(funcHolder == null) {
    final StringBuilder operandTypes = new StringBuilder();
    for(int j = 0; j < opBinding.getOperandCount(); ++j) {
      operandTypes.append(opBinding.getOperandType(j).getSqlTypeName());
      if(j < opBinding.getOperandCount() - 1) {
        operandTypes.append(",");
      }
    }

    throw UserException
      .functionError()
      .message(String.format("%s does not support operand types (%s)",
        opBinding.getOperator().getName(),
        operandTypes))
      .build(logger);
  }

  return TypeInferenceUtils.createCalciteTypeWithNullability(
    opBinding.getTypeFactory(),
    TypeInferenceUtils.getCalciteTypeFromMinorType(funcHolder.getReturnType(null).toMinorType()),
    true,
    null);
}