use of org.apache.calcite.sql.validate.SqlValidatorImpl in project flink by apache.
the class SqlCastFunction method inferReturnType.
// ~ Methods ----------------------------------------------------------------
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
assert opBinding.getOperandCount() == 2;
RelDataType ret = opBinding.getOperandType(1);
RelDataType firstType = opBinding.getOperandType(0);
ret = opBinding.getTypeFactory().createTypeWithNullability(ret, firstType.isNullable());
if (opBinding instanceof SqlCallBinding) {
SqlCallBinding callBinding = (SqlCallBinding) opBinding;
SqlNode operand0 = callBinding.operand(0);
// to them using the type they are casted to.
if (((operand0 instanceof SqlLiteral) && (((SqlLiteral) operand0).getValue() == null)) || (operand0 instanceof SqlDynamicParam)) {
final SqlValidatorImpl validator = (SqlValidatorImpl) callBinding.getValidator();
validator.setValidatedNodeType(operand0, ret);
}
}
return ret;
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project flink by apache.
the class FlinkConvertletTable method convertTryCast.
// Slightly modified version of StandardConvertletTable::convertCast
private RexNode convertTryCast(SqlRexContext cx, final SqlCall call) {
RelDataTypeFactory typeFactory = cx.getTypeFactory();
final SqlNode leftNode = call.operand(0);
final SqlNode rightNode = call.operand(1);
final RexNode valueRex = cx.convertExpression(leftNode);
RelDataType type;
if (rightNode instanceof SqlIntervalQualifier) {
type = typeFactory.createSqlIntervalType((SqlIntervalQualifier) rightNode);
} else if (rightNode instanceof SqlDataTypeSpec) {
SqlDataTypeSpec dataType = ((SqlDataTypeSpec) rightNode);
type = dataType.deriveType(cx.getValidator());
if (type == null) {
type = cx.getValidator().getValidatedNodeType(dataType.getTypeName());
}
} else {
throw new IllegalStateException("Invalid right argument type for TRY_CAST: " + rightNode);
}
type = typeFactory.createTypeWithNullability(type, true);
if (SqlUtil.isNullLiteral(leftNode, false)) {
final SqlValidatorImpl validator = (SqlValidatorImpl) cx.getValidator();
validator.setValidatedNodeType(leftNode, type);
return cx.convertExpression(leftNode);
}
return cx.getRexBuilder().makeCall(type, FlinkSqlOperatorTable.TRY_CAST, Collections.singletonList(valueRex));
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlOperator method constructArgTypeList.
protected List<RelDataType> constructArgTypeList(SqlValidator validator, SqlValidatorScope scope, SqlCall call, List<SqlNode> args, boolean convertRowArgToColumnList) {
// Scope for operands. Usually the same as 'scope'.
final SqlValidatorScope operandScope = scope.getOperandScope(call);
final ImmutableList.Builder<RelDataType> argTypeBuilder = ImmutableList.builder();
for (SqlNode operand : args) {
RelDataType nodeType;
// for sure that the row argument maps to a ColumnList type
if (operand.getKind() == SqlKind.ROW && convertRowArgToColumnList) {
RelDataTypeFactory typeFactory = validator.getTypeFactory();
nodeType = typeFactory.createSqlType(SqlTypeName.COLUMN_LIST);
((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
} else {
nodeType = validator.deriveType(operandScope, operand);
}
argTypeBuilder.add(nodeType);
}
return argTypeBuilder.build();
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlOperator method validateOperands.
/**
* Validates the operands of a call, inferring the return type in the
* process.
*
* @param validator active validator
* @param scope validation scope
* @param call call to be validated
* @return inferred type
*/
public final RelDataType validateOperands(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
// Let subclasses know what's up.
preValidateCall(validator, scope, call);
// Check the number of operands
checkOperandCount(validator, operandTypeChecker, call);
SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);
checkOperandTypes(opBinding, true);
// Now infer the result type.
RelDataType ret = inferReturnType(opBinding);
((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
return ret;
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlOverOperator method deriveType.
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
// Validate type of the inner aggregate call
validateOperands(validator, scope, call);
// Assume the first operand is an aggregate call and derive its type.
// When we are sure the window is not empty, pass that information to the
// aggregate's operator return type inference as groupCount=1
// Otherwise pass groupCount=0 so the agg operator understands the window
// can be empty
SqlNode agg = call.operand(0);
if (!(agg instanceof SqlCall)) {
throw new IllegalStateException("Argument to SqlOverOperator" + " should be SqlCall, got " + agg.getClass() + ": " + agg);
}
SqlNode window = call.operand(1);
SqlWindow w = validator.resolveWindow(window, scope, false);
final int groupCount = w.isAlwaysNonEmpty() ? 1 : 0;
final SqlCall aggCall = (SqlCall) agg;
SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {
@Override
public int getGroupCount() {
return groupCount;
}
};
RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);
// Copied from validateOperands
((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
((SqlValidatorImpl) validator).setValidatedNodeType(agg, ret);
return ret;
}
Aggregations