use of org.apache.calcite.sql.validate.SqlValidatorScope in project calcite by apache.
the class SqlTypeUtil method isCharTypeComparable.
/**
* Returns whether the operands to a call are char type-comparable.
*
* @param binding Binding of call to operands
* @param operands Operands to check for compatibility; usually the
* operands of the bound call, but not always
* @param throwOnFailure Whether to throw an exception on failure
* @return whether operands are valid
*/
public static boolean isCharTypeComparable(SqlCallBinding binding, List<SqlNode> operands, boolean throwOnFailure) {
final SqlValidator validator = binding.getValidator();
final SqlValidatorScope scope = binding.getScope();
assert operands != null;
assert operands.size() >= 2;
if (!isCharTypeComparable(deriveAndCollectTypes(validator, scope, operands))) {
if (throwOnFailure) {
String msg = "";
for (int i = 0; i < operands.size(); i++) {
if (i > 0) {
msg += ", ";
}
msg += operands.get(i).toString();
}
throw binding.newError(RESOURCE.operandNotComparable(msg));
}
return false;
}
return true;
}
use of org.apache.calcite.sql.validate.SqlValidatorScope in project calcite by apache.
the class SqlAdvisorValidator method validateOver.
protected void validateOver(SqlCall call, SqlValidatorScope scope) {
try {
final OverScope overScope = (OverScope) getOverScope(call);
final SqlNode relation = call.operand(0);
validateFrom(relation, unknownType, scope);
final SqlNode window = call.operand(1);
SqlValidatorScope opScope = scopes.get(relation);
if (opScope == null) {
opScope = overScope;
}
validateWindow(window, opScope, null);
} catch (CalciteException e) {
Util.swallow(e, TRACER);
}
}
use of org.apache.calcite.sql.validate.SqlValidatorScope 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.SqlValidatorScope in project calcite by apache.
the class SqlFunction method deriveType.
private RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call, boolean convertRowArgToColumnList) {
// Scope for operands. Usually the same as 'scope'.
final SqlValidatorScope operandScope = scope.getOperandScope(call);
// Indicate to the validator that we're validating a new function call
validator.pushFunctionCall();
final List<String> argNames = constructArgNameList(call);
final List<SqlNode> args = constructOperandList(validator, call, argNames);
final List<RelDataType> argTypes = constructArgTypeList(validator, scope, call, args, convertRowArgToColumnList);
final SqlFunction function = (SqlFunction) SqlUtil.lookupRoutine(validator.getOperatorTable(), getNameAsId(), argTypes, argNames, getFunctionType(), SqlSyntax.FUNCTION, getKind());
try {
// type
if (convertRowArgToColumnList && containsRowArg(args)) {
if (function == null && SqlUtil.matchRoutinesByParameterCount(validator.getOperatorTable(), getNameAsId(), argTypes, getFunctionType())) {
// row arguments before re-validating
for (SqlNode operand : args) {
if (operand.getKind() == SqlKind.ROW) {
validator.removeValidatedNodeType(operand);
}
}
return deriveType(validator, scope, call, false);
} else if (function != null) {
validator.validateColumnListParams(function, argTypes, args);
}
}
if (getFunctionType() == SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR) {
return validator.deriveConstructorType(scope, call, this, function, argTypes);
}
if (function == null) {
throw validator.handleUnresolvedFunction(call, this, argTypes, argNames);
}
// REVIEW jvs 25-Mar-2005: This is, in a sense, expanding
// identifiers, but we ignore shouldExpandIdentifiers()
// because otherwise later validation code will
// choke on the unresolved function.
((SqlBasicCall) call).setOperator(function);
return function.validateOperands(validator, operandScope, call);
} finally {
validator.popFunctionCall();
}
}
use of org.apache.calcite.sql.validate.SqlValidatorScope in project calcite by apache.
the class SqlToRelConverter method convertExists.
/**
* Converts an EXISTS or IN predicate into a join. For EXISTS, the sub-query
* produces an indicator variable, and the result is a relational expression
* which outer joins that indicator to the original query. After performing
* the outer join, the condition will be TRUE if the EXISTS condition holds,
* NULL otherwise.
*
* @param seek A query, for example 'select * from emp' or
* 'values (1,2,3)' or '('Foo', 34)'.
* @param subQueryType Whether sub-query is IN, EXISTS or scalar
* @param logic Whether the answer needs to be in full 3-valued logic (TRUE,
* FALSE, UNKNOWN) will be required, or whether we can accept an
* approximation (say representing UNKNOWN as FALSE)
* @param notIn Whether the operation is NOT IN
* @return join expression
*/
private RelOptUtil.Exists convertExists(SqlNode seek, RelOptUtil.SubQueryType subQueryType, RelOptUtil.Logic logic, boolean notIn, RelDataType targetDataType) {
final SqlValidatorScope seekScope = (seek instanceof SqlSelect) ? validator.getSelectScope((SqlSelect) seek) : null;
final Blackboard seekBb = createBlackboard(seekScope, null, false);
RelNode seekRel = convertQueryOrInList(seekBb, seek, targetDataType);
return RelOptUtil.createExistsPlan(seekRel, subQueryType, logic, notIn, relBuilder);
}
Aggregations