Search in sources :

Example 1 with SqlOperandCountRange

use of org.apache.calcite.sql.SqlOperandCountRange 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 2 with SqlOperandCountRange

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

the class CompositeOperandTypeChecker method getOperandCountRange.

public SqlOperandCountRange getOperandCountRange() {
    switch(composition) {
        case REPEAT:
            return range;
        case SEQUENCE:
            return SqlOperandCountRanges.of(allowedRules.size());
        case AND:
        case OR:
        default:
            final List<SqlOperandCountRange> ranges = new AbstractList<SqlOperandCountRange>() {

                public SqlOperandCountRange get(int index) {
                    return allowedRules.get(index).getOperandCountRange();
                }

                public int size() {
                    return allowedRules.size();
                }
            };
            final int min = minMin(ranges);
            final int max = maxMax(ranges);
            SqlOperandCountRange composite = new SqlOperandCountRange() {

                public boolean isValidCount(int count) {
                    switch(composition) {
                        case AND:
                            for (SqlOperandCountRange range : ranges) {
                                if (!range.isValidCount(count)) {
                                    return false;
                                }
                            }
                            return true;
                        case OR:
                        default:
                            for (SqlOperandCountRange range : ranges) {
                                if (range.isValidCount(count)) {
                                    return true;
                                }
                            }
                            return false;
                    }
                }

                public int getMin() {
                    return min;
                }

                public int getMax() {
                    return max;
                }
            };
            if (max >= 0) {
                for (int i = min; i <= max; i++) {
                    if (!composite.isValidCount(i)) {
                        // so return the composite.
                        return composite;
                    }
                }
            }
            return min == max ? SqlOperandCountRanges.of(min) : SqlOperandCountRanges.between(min, max);
    }
}
Also used : SqlOperandCountRange(org.apache.calcite.sql.SqlOperandCountRange) AbstractList(java.util.AbstractList)

Aggregations

SqlOperandCountRange (org.apache.calcite.sql.SqlOperandCountRange)2 SQLException (java.sql.SQLException)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Strong (org.apache.calcite.plan.Strong)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)1 SqlCall (org.apache.calcite.sql.SqlCall)1 SqlCallBinding (org.apache.calcite.sql.SqlCallBinding)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)1 SqlOperandTypeChecker (org.apache.calcite.sql.type.SqlOperandTypeChecker)1 SqlString (org.apache.calcite.sql.util.SqlString)1 SqlValidatorImpl (org.apache.calcite.sql.validate.SqlValidatorImpl)1 SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)1 SqlLimitsTest (org.apache.calcite.test.SqlLimitsTest)1 TimestampString (org.apache.calcite.util.TimestampString)1 Test (org.junit.Test)1