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;
}
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()));
}
}
}
}
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;
}
});
}
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);
}
}
Aggregations