Search in sources :

Example 1 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project calcite by apache.

the class SqlBetweenOperator method inferReturnType.

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

Example 2 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project calcite 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;
}
Also used : SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project calcite by apache.

the class SqlOperatorBaseTest method testArgumentBounds.

/**
 * Test that calls all operators with all possible argument types, and for
 * each type, with a set of tricky values.
 */
@Test
public void testArgumentBounds() {
    if (!CalciteAssert.ENABLE_SLOW) {
        return;
    }
    final SqlValidatorImpl validator = (SqlValidatorImpl) tester.getValidator();
    final SqlValidatorScope scope = validator.getEmptyScope();
    final RelDataTypeFactory typeFactory = validator.getTypeFactory();
    final Builder builder = new Builder(typeFactory);
    builder.add0(SqlTypeName.BOOLEAN, true, false);
    builder.add0(SqlTypeName.TINYINT, 0, 1, -3, Byte.MAX_VALUE, Byte.MIN_VALUE);
    builder.add0(SqlTypeName.SMALLINT, 0, 1, -4, Short.MAX_VALUE, Short.MIN_VALUE);
    builder.add0(SqlTypeName.INTEGER, 0, 1, -2, Integer.MIN_VALUE, Integer.MAX_VALUE);
    builder.add0(SqlTypeName.BIGINT, 0, 1, -5, Integer.MAX_VALUE, Long.MAX_VALUE, Long.MIN_VALUE);
    builder.add1(SqlTypeName.VARCHAR, 11, "", " ", "hello world");
    builder.add1(SqlTypeName.CHAR, 5, "", "e", "hello");
    builder.add0(SqlTypeName.TIMESTAMP, 0L, DateTimeUtils.MILLIS_PER_DAY);
    for (SqlOperator op : SqlStdOperatorTable.instance().getOperatorList()) {
        switch(op.getKind()) {
            // can't handle the flag argument
            case TRIM:
            case EXISTS:
                continue;
        }
        switch(op.getSyntax()) {
            case SPECIAL:
                continue;
        }
        final SqlOperandTypeChecker typeChecker = op.getOperandTypeChecker();
        if (typeChecker == null) {
            continue;
        }
        final SqlOperandCountRange range = typeChecker.getOperandCountRange();
        for (int n = range.getMin(), max = range.getMax(); n <= max; n++) {
            final List<List<ValueType>> argValues = Collections.nCopies(n, builder.values);
            for (final List<ValueType> args : Linq4j.product(argValues)) {
                SqlNodeList nodeList = new SqlNodeList(SqlParserPos.ZERO);
                int nullCount = 0;
                for (ValueType arg : args) {
                    if (arg.value == null) {
                        ++nullCount;
                    }
                    nodeList.add(arg.node);
                }
                final SqlCall call = op.createCall(nodeList);
                final SqlCallBinding binding = new SqlCallBinding(validator, scope, call);
                if (!typeChecker.checkOperandTypes(binding, false)) {
                    continue;
                }
                final SqlPrettyWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
                op.unparse(writer, call, 0, 0);
                final String s = writer.toSqlString().toString();
                if (s.startsWith("OVERLAY(") || s.contains(" / 0") || s.matches("MOD\\(.*, 0\\)")) {
                    continue;
                }
                final Strong.Policy policy = Strong.policy(op.kind);
                try {
                    if (nullCount > 0 && policy == Strong.Policy.ANY) {
                        tester.checkNull(s);
                    } else {
                        final String query;
                        if (op instanceof SqlAggFunction) {
                            if (op.requiresOrder()) {
                                query = "SELECT " + s + " OVER () FROM (VALUES (1))";
                            } else {
                                query = "SELECT " + s + " FROM (VALUES (1))";
                            }
                        } else {
                            query = SqlTesterImpl.buildQuery(s);
                        }
                        tester.check(query, SqlTests.ANY_TYPE_CHECKER, SqlTests.ANY_PARAMETER_CHECKER, SqlTests.ANY_RESULT_CHECKER);
                    }
                } catch (Error e) {
                    System.out.println(s + ": " + e.getMessage());
                    throw e;
                } catch (Exception e) {
                    System.out.println("Failed: " + s + ": " + e.getMessage());
                }
            }
        }
    }
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCall(org.apache.calcite.sql.SqlCall) TimestampString(org.apache.calcite.util.TimestampString) SqlString(org.apache.calcite.sql.util.SqlString) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) Strong(org.apache.calcite.plan.Strong) SQLException(java.sql.SQLException) SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) SqlValidatorImpl(org.apache.calcite.sql.validate.SqlValidatorImpl) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlOperandTypeChecker(org.apache.calcite.sql.type.SqlOperandTypeChecker) List(java.util.List) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlLimitsTest(org.apache.calcite.test.SqlLimitsTest) Test(org.junit.Test)

Example 4 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project calcite by apache.

the class SqlValidatorImpl method inferUnknownTypes.

protected void inferUnknownTypes(RelDataType inferredType, SqlValidatorScope scope, SqlNode node) {
    final SqlValidatorScope newScope = scopes.get(node);
    if (newScope != null) {
        scope = newScope;
    }
    boolean isNullLiteral = SqlUtil.isNullLiteral(node, false);
    if ((node instanceof SqlDynamicParam) || isNullLiteral) {
        if (inferredType.equals(unknownType)) {
            if (isNullLiteral) {
                throw newValidationError(node, RESOURCE.nullIllegal());
            } else {
                throw newValidationError(node, RESOURCE.dynamicParamIllegal());
            }
        }
        // REVIEW:  should dynamic parameter types always be nullable?
        RelDataType newInferredType = typeFactory.createTypeWithNullability(inferredType, true);
        if (SqlTypeUtil.inCharFamily(inferredType)) {
            newInferredType = typeFactory.createTypeWithCharsetAndCollation(newInferredType, inferredType.getCharset(), inferredType.getCollation());
        }
        setValidatedNodeType(node, newInferredType);
    } else if (node instanceof SqlNodeList) {
        SqlNodeList nodeList = (SqlNodeList) node;
        if (inferredType.isStruct()) {
            if (inferredType.getFieldCount() != nodeList.size()) {
                // bust out, and the error will be detected higher up
                return;
            }
        }
        int i = 0;
        for (SqlNode child : nodeList) {
            RelDataType type;
            if (inferredType.isStruct()) {
                type = inferredType.getFieldList().get(i).getType();
                ++i;
            } else {
                type = inferredType;
            }
            inferUnknownTypes(type, scope, child);
        }
    } else if (node instanceof SqlCase) {
        final SqlCase caseCall = (SqlCase) node;
        final RelDataType whenType = caseCall.getValueOperand() == null ? booleanType : unknownType;
        for (SqlNode sqlNode : caseCall.getWhenOperands().getList()) {
            inferUnknownTypes(whenType, scope, sqlNode);
        }
        RelDataType returnType = deriveType(scope, node);
        for (SqlNode sqlNode : caseCall.getThenOperands().getList()) {
            inferUnknownTypes(returnType, scope, sqlNode);
        }
        if (!SqlUtil.isNullLiteral(caseCall.getElseOperand(), false)) {
            inferUnknownTypes(returnType, scope, caseCall.getElseOperand());
        } else {
            setValidatedNodeType(caseCall.getElseOperand(), returnType);
        }
    } else if (node instanceof SqlCall) {
        final SqlCall call = (SqlCall) node;
        final SqlOperandTypeInference operandTypeInference = call.getOperator().getOperandTypeInference();
        final SqlCallBinding callBinding = new SqlCallBinding(this, scope, call);
        final List<SqlNode> operands = callBinding.operands();
        final RelDataType[] operandTypes = new RelDataType[operands.size()];
        if (operandTypeInference == null) {
            // TODO:  eventually should assert(operandTypeInference != null)
            // instead; for now just eat it
            Arrays.fill(operandTypes, unknownType);
        } else {
            operandTypeInference.inferOperandTypes(callBinding, inferredType, operandTypes);
        }
        for (int i = 0; i < operands.size(); ++i) {
            inferUnknownTypes(operandTypes[i], scope, operands.get(i));
        }
    }
}
Also used : SqlDynamicParam(org.apache.calcite.sql.SqlDynamicParam) SqlCase(org.apache.calcite.sql.fun.SqlCase) SqlCall(org.apache.calcite.sql.SqlCall) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) SqlNodeList(org.apache.calcite.sql.SqlNodeList) RelDataType(org.apache.calcite.rel.type.RelDataType) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ImmutableNullableList(org.apache.calcite.util.ImmutableNullableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlOperandTypeInference(org.apache.calcite.sql.type.SqlOperandTypeInference) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with SqlCallBinding

use of org.apache.calcite.sql.SqlCallBinding in project calcite by apache.

the class ProcedureNamespace method validateImpl.

// ~ Methods ----------------------------------------------------------------
public RelDataType validateImpl(RelDataType targetRowType) {
    validator.inferUnknownTypes(validator.unknownType, scope, call);
    final RelDataType type = validator.deriveTypeImpl(scope, call);
    final SqlOperator operator = call.getOperator();
    final SqlCallBinding callBinding = new SqlCallBinding(validator, scope, call);
    if (operator instanceof SqlUserDefinedTableFunction) {
        assert type.getSqlTypeName() == SqlTypeName.CURSOR : "User-defined table function should have CURSOR type, not " + type;
        final SqlUserDefinedTableFunction udf = (SqlUserDefinedTableFunction) operator;
        return udf.getRowType(validator.typeFactory, callBinding.operands());
    } else if (operator instanceof SqlUserDefinedTableMacro) {
        assert type.getSqlTypeName() == SqlTypeName.CURSOR : "User-defined table macro should have CURSOR type, not " + type;
        final SqlUserDefinedTableMacro udf = (SqlUserDefinedTableMacro) operator;
        return udf.getTable(validator.typeFactory, callBinding.operands()).getRowType(validator.typeFactory);
    }
    return type;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlCallBinding(org.apache.calcite.sql.SqlCallBinding) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)23 RelDataType (org.apache.calcite.rel.type.RelDataType)13 SqlNode (org.apache.calcite.sql.SqlNode)9 SqlCall (org.apache.calcite.sql.SqlCall)7 Method (java.lang.reflect.Method)6 ArrayList (java.util.ArrayList)6 SqlNodeList (org.apache.calcite.sql.SqlNodeList)6 SqlOperator (org.apache.calcite.sql.SqlOperator)6 UdfMetadata (org.apache.samza.sql.interfaces.UdfMetadata)6 SamzaSqlUdfMethod (org.apache.samza.sql.udfs.SamzaSqlUdfMethod)6 BasicSqlType (org.apache.calcite.sql.type.BasicSqlType)5 MapConfig (org.apache.samza.config.MapConfig)5 Test (org.junit.Test)5 List (java.util.List)4 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)4 SqlDynamicParam (org.apache.calcite.sql.SqlDynamicParam)4 SqlLiteral (org.apache.calcite.sql.SqlLiteral)4 ExplicitOperatorBinding (org.apache.calcite.sql.ExplicitOperatorBinding)3 SqlOperandCountRange (org.apache.calcite.sql.SqlOperandCountRange)3 SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)3