Search in sources :

Example 1 with PredicateImpl

use of org.apache.calcite.runtime.PredicateImpl in project calcite by apache.

the class SqlParserUtil method toTreeEx.

/**
 * Converts a list of {expression, operator, expression, ...} into a tree,
 * taking operator precedence and associativity into account.
 *
 * @param list        List of operands and operators. This list is modified as
 *                    expressions are reduced.
 * @param start       Position of first operand in the list. Anything to the
 *                    left of this (besides the immediately preceding operand)
 *                    is ignored. Generally use value 1.
 * @param minPrec     Minimum precedence to consider. If the method encounters
 *                    an operator of lower precedence, it doesn't reduce any
 *                    further.
 * @param stopperKind If not {@link SqlKind#OTHER}, stop reading the list if
 *                    we encounter a token of this kind.
 * @return the root node of the tree which the list condenses into
 */
public static SqlNode toTreeEx(SqlSpecialOperator.TokenSequence list, int start, final int minPrec, final SqlKind stopperKind) {
    final Predicate<PrecedenceClimbingParser.Token> predicate = new PredicateImpl<PrecedenceClimbingParser.Token>() {

        public boolean test(PrecedenceClimbingParser.Token t) {
            if (t instanceof PrecedenceClimbingParser.Op) {
                final SqlOperator op = ((ToTreeListItem) t.o).op;
                return stopperKind != SqlKind.OTHER && op.kind == stopperKind || minPrec > 0 && op.getLeftPrec() < minPrec;
            } else {
                return false;
            }
        }
    };
    PrecedenceClimbingParser parser = list.parser(start, predicate);
    final int beforeSize = parser.all().size();
    parser.partialParse();
    final int afterSize = parser.all().size();
    final SqlNode node = convert(parser.all().get(0));
    list.replaceSublist(start, start + beforeSize - afterSize + 1, node);
    return node;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) PrecedenceClimbingParser(org.apache.calcite.util.PrecedenceClimbingParser) SqlNode(org.apache.calcite.sql.SqlNode)

Example 2 with PredicateImpl

use of org.apache.calcite.runtime.PredicateImpl in project calcite by apache.

the class TableNamespace method checkExtendedColumnTypes.

/**
 * Ensures that extended columns that have the same name as a base column also
 * have the same data-type.
 */
private void checkExtendedColumnTypes(SqlNodeList extendList) {
    final List<RelDataTypeField> extendedFields = SqlValidatorUtil.getExtendedColumns(validator.getTypeFactory(), table, extendList);
    final List<RelDataTypeField> baseFields = getBaseRowType().getFieldList();
    final Map<String, Integer> nameToIndex = SqlValidatorUtil.mapNameToIndex(baseFields);
    for (final RelDataTypeField extendedField : extendedFields) {
        final String extFieldName = extendedField.getName();
        if (nameToIndex.containsKey(extFieldName)) {
            final Integer baseIndex = nameToIndex.get(extFieldName);
            final RelDataType baseType = baseFields.get(baseIndex).getType();
            final RelDataType extType = extendedField.getType();
            if (!extType.equals(baseType)) {
                // Get the extended column node that failed validation.
                final Predicate<SqlNode> nameMatches = new PredicateImpl<SqlNode>() {

                    @Override
                    public boolean test(SqlNode sqlNode) {
                        if (sqlNode instanceof SqlIdentifier) {
                            final SqlIdentifier identifier = (SqlIdentifier) sqlNode;
                            return Util.last(identifier.names).equals(extendedField.getName());
                        }
                        return false;
                    }
                };
                final SqlNode extColNode = Iterables.find(extendList.getList(), nameMatches);
                throw validator.getValidationErrorFunction().apply(extColNode, RESOURCE.typeNotAssignable(baseFields.get(baseIndex).getName(), baseType.getFullTypeString(), extendedField.getName(), extType.getFullTypeString()));
            }
        }
    }
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with PredicateImpl

use of org.apache.calcite.runtime.PredicateImpl in project calcite by apache.

the class SqlUtil method filterRoutinesByParameterType.

/**
 * @see Glossary#SQL99 SQL:1999 Part 2 Section 10.4 Syntax Rule 6.b.iii.2.B
 */
private static Iterator<SqlOperator> filterRoutinesByParameterType(SqlSyntax syntax, final Iterator<SqlOperator> routines, final List<RelDataType> argTypes, final List<String> argNames) {
    if (syntax != SqlSyntax.FUNCTION) {
        return routines;
    }
    // noinspection unchecked
    return (Iterator) Iterators.filter(Iterators.filter(routines, SqlFunction.class), new PredicateImpl<SqlFunction>() {

        public boolean test(SqlFunction function) {
            List<RelDataType> paramTypes = function.getParamTypes();
            if (paramTypes == null) {
                // no parameter information for builtins; keep for now
                return true;
            }
            final List<RelDataType> permutedArgTypes;
            if (argNames != null) {
                // Arguments passed by name. Make sure that the function has
                // parameters of all of these names.
                final Map<Integer, Integer> map = new HashMap<>();
                for (Ord<String> argName : Ord.zip(argNames)) {
                    final int i = function.getParamNames().indexOf(argName.e);
                    if (i < 0) {
                        return false;
                    }
                    map.put(i, argName.i);
                }
                permutedArgTypes = Functions.generate(paramTypes.size(), new Function1<Integer, RelDataType>() {

                    public RelDataType apply(Integer a0) {
                        if (map.containsKey(a0)) {
                            return argTypes.get(map.get(a0));
                        } else {
                            return null;
                        }
                    }
                });
            } else {
                permutedArgTypes = Lists.newArrayList(argTypes);
                while (permutedArgTypes.size() < argTypes.size()) {
                    paramTypes.add(null);
                }
            }
            for (Pair<RelDataType, RelDataType> p : Pair.zip(paramTypes, permutedArgTypes)) {
                final RelDataType argType = p.right;
                final RelDataType paramType = p.left;
                if (argType != null && !SqlTypeUtil.canCastFrom(paramType, argType, false)) {
                    return false;
                }
            }
            return true;
        }
    });
}
Also used : HashMap(java.util.HashMap) Function1(org.apache.calcite.linq4j.function.Function1) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) Iterator(java.util.Iterator)

Example 4 with PredicateImpl

use of org.apache.calcite.runtime.PredicateImpl in project calcite by apache.

the class CalciteCatalogReader method toOp.

/**
 * Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor.
 */
private static SqlOperator toOp(RelDataTypeFactory typeFactory, SqlIdentifier name, final Function function) {
    List<RelDataType> argTypes = new ArrayList<>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<>();
    for (FunctionParameter o : function.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    final Predicate<Integer> optional = new PredicateImpl<Integer>() {

        public boolean test(Integer input) {
            return function.getParameters().get(input).isOptional();
        }
    };
    final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, optional);
    final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
    if (function instanceof ScalarFunction) {
        return new SqlUserDefinedFunction(name, infer((ScalarFunction) function), InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
    } else if (function instanceof AggregateFunction) {
        return new SqlUserDefinedAggFunction(name, infer((AggregateFunction) function), InferTypes.explicit(argTypes), typeChecker, (AggregateFunction) function, false, false, typeFactory);
    } else if (function instanceof TableMacro) {
        return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableMacro) function);
    } else if (function instanceof TableFunction) {
        return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR, InferTypes.explicit(argTypes), typeChecker, paramTypes, (TableFunction) function);
    } else {
        throw new AssertionError("unknown function type " + function);
    }
}
Also used : SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) TableMacro(org.apache.calcite.schema.TableMacro) SqlTypeFamily(org.apache.calcite.sql.type.SqlTypeFamily) ArrayList(java.util.ArrayList) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlUserDefinedTableMacro(org.apache.calcite.sql.validate.SqlUserDefinedTableMacro) FamilyOperandTypeChecker(org.apache.calcite.sql.type.FamilyOperandTypeChecker) SqlUserDefinedFunction(org.apache.calcite.sql.validate.SqlUserDefinedFunction) ScalarFunction(org.apache.calcite.schema.ScalarFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) AggregateFunction(org.apache.calcite.schema.AggregateFunction) SqlUserDefinedAggFunction(org.apache.calcite.sql.validate.SqlUserDefinedAggFunction) TableFunction(org.apache.calcite.schema.TableFunction) SqlUserDefinedTableFunction(org.apache.calcite.sql.validate.SqlUserDefinedTableFunction) FunctionParameter(org.apache.calcite.schema.FunctionParameter)

Aggregations

PredicateImpl (org.apache.calcite.runtime.PredicateImpl)4 RelDataType (org.apache.calcite.rel.type.RelDataType)3 SqlNode (org.apache.calcite.sql.SqlNode)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Function1 (org.apache.calcite.linq4j.function.Function1)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 AggregateFunction (org.apache.calcite.schema.AggregateFunction)1 FunctionParameter (org.apache.calcite.schema.FunctionParameter)1 ScalarFunction (org.apache.calcite.schema.ScalarFunction)1 TableFunction (org.apache.calcite.schema.TableFunction)1 TableMacro (org.apache.calcite.schema.TableMacro)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 FamilyOperandTypeChecker (org.apache.calcite.sql.type.FamilyOperandTypeChecker)1 SqlTypeFamily (org.apache.calcite.sql.type.SqlTypeFamily)1 SqlUserDefinedAggFunction (org.apache.calcite.sql.validate.SqlUserDefinedAggFunction)1 SqlUserDefinedFunction (org.apache.calcite.sql.validate.SqlUserDefinedFunction)1 SqlUserDefinedTableFunction (org.apache.calcite.sql.validate.SqlUserDefinedTableFunction)1