Search in sources :

Example 11 with SqlValidatorScope

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;
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlValidator(org.apache.calcite.sql.validate.SqlValidator)

Example 12 with SqlValidatorScope

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);
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) OverScope(org.apache.calcite.sql.validate.OverScope) CalciteException(org.apache.calcite.runtime.CalciteException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 13 with SqlValidatorScope

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();
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) ImmutableList(com.google.common.collect.ImmutableList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 14 with SqlValidatorScope

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();
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 15 with SqlValidatorScope

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);
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlSelect(org.apache.calcite.sql.SqlSelect) RelNode(org.apache.calcite.rel.RelNode)

Aggregations

SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)20 RelDataType (org.apache.calcite.rel.type.RelDataType)12 SqlNode (org.apache.calcite.sql.SqlNode)10 ArrayList (java.util.ArrayList)7 SqlNodeList (org.apache.calcite.sql.SqlNodeList)6 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)6 RelNode (org.apache.calcite.rel.RelNode)5 SqlCall (org.apache.calcite.sql.SqlCall)5 List (java.util.List)4 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)4 NlsString (org.apache.calcite.util.NlsString)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)3 RexNode (org.apache.calcite.rex.RexNode)3 SqlOperator (org.apache.calcite.sql.SqlOperator)3 SqlSelect (org.apache.calcite.sql.SqlSelect)3 ImmutableList (com.google.common.collect.ImmutableList)2 HazelcastSqlValidator (com.hazelcast.jet.sql.impl.validate.HazelcastSqlValidator)2 QueryDataTypeFamily (com.hazelcast.sql.impl.type.QueryDataTypeFamily)2 Consumer (java.util.function.Consumer)2