use of org.apache.calcite.sql.SqlFunction in project calcite by apache.
the class ReflectiveSqlOperatorTable method lookupOperatorOverloads.
// implement SqlOperatorTable
public void lookupOperatorOverloads(SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax, List<SqlOperator> operatorList) {
// NOTE jvs 3-Mar-2005: ignore category until someone cares
String simpleName;
if (opName.names.size() > 1) {
if (opName.names.get(opName.names.size() - 2).equals(IS_NAME)) {
// per SQL99 Part 2 Section 10.4 Syntax Rule 7.b.ii.1
simpleName = Util.last(opName.names);
} else {
return;
}
} else {
simpleName = opName.getSimple();
}
// Always look up built-in operators case-insensitively. Even in sessions
// with unquotedCasing=UNCHANGED and caseSensitive=true.
final Collection<SqlOperator> list = operators.get(new Key(simpleName, syntax));
if (list.isEmpty()) {
return;
}
for (SqlOperator op : list) {
if (op.getSyntax() == syntax) {
operatorList.add(op);
} else if (syntax == SqlSyntax.FUNCTION && op instanceof SqlFunction) {
// this special case is needed for operators like CAST,
// which are treated as functions but have special syntax
operatorList.add(op);
}
}
// Shouldn't it be covered by search above?
switch(syntax) {
case BINARY:
case PREFIX:
case POSTFIX:
for (SqlOperator extra : operators.get(new Key(simpleName, syntax))) {
// REVIEW: should only search operators added during this method?
if (extra != null && !operatorList.contains(extra)) {
operatorList.add(extra);
}
}
break;
}
}
use of org.apache.calcite.sql.SqlFunction in project drill by axbaretto.
the class DrillOperatorTable method populateWrappedCalciteOperators.
private void populateWrappedCalciteOperators() {
for (SqlOperator calciteOperator : inner.getOperatorList()) {
final SqlOperator wrapper;
if (calciteOperator instanceof SqlAggFunction) {
wrapper = new DrillCalciteSqlAggFunctionWrapper((SqlAggFunction) calciteOperator, getFunctionListWithInference(calciteOperator.getName()));
} else if (calciteOperator instanceof SqlFunction) {
wrapper = new DrillCalciteSqlFunctionWrapper((SqlFunction) calciteOperator, getFunctionListWithInference(calciteOperator.getName()));
} else {
final String drillOpName = FunctionCallFactory.replaceOpWithFuncName(calciteOperator.getName());
final List<DrillFuncHolder> drillFuncHolders = getFunctionListWithInference(drillOpName);
if (drillFuncHolders.isEmpty() || calciteOperator == SqlStdOperatorTable.UNARY_MINUS || calciteOperator == SqlStdOperatorTable.UNARY_PLUS) {
continue;
}
wrapper = new DrillCalciteSqlOperatorWrapper(calciteOperator, drillOpName, drillFuncHolders);
}
calciteToWrapper.put(calciteOperator, wrapper);
}
}
use of org.apache.calcite.sql.SqlFunction in project calcite by apache.
the class MaterializedViewAggregateRule method generateMapping.
/**
* Mapping from node expressions to target expressions.
*
* <p>If any of the expressions cannot be mapped, we return null.
*/
@Nullable
protected Multimap<Integer, Integer> generateMapping(RexBuilder rexBuilder, RexSimplify simplify, RelMetadataQuery mq, RelNode node, RelNode target, ImmutableBitSet positions, BiMap<RelTableRef, RelTableRef> tableMapping, EquivalenceClasses sourceEC, List<RexNode> additionalExprs) {
Preconditions.checkArgument(additionalExprs.isEmpty());
Multimap<Integer, Integer> m = ArrayListMultimap.create();
Map<RexTableInputRef, Set<RexTableInputRef>> equivalenceClassesMap = sourceEC.getEquivalenceClassesMap();
Multimap<RexNode, Integer> exprsLineage = ArrayListMultimap.create();
final List<RexNode> timestampExprs = new ArrayList<>();
for (int i = 0; i < target.getRowType().getFieldCount(); i++) {
Set<RexNode> s = mq.getExpressionLineage(target, rexBuilder.makeInputRef(target, i));
if (s == null) {
// Bail out
continue;
}
// We only support project - filter - join, thus it should map to
// a single expression
final RexNode e = Iterables.getOnlyElement(s);
// Rewrite expr to be expressed on query tables
final RexNode simplified = simplify.simplifyUnknownAsFalse(e);
final RexNode expr = RexUtil.swapTableColumnReferences(rexBuilder, simplified, tableMapping.inverse(), equivalenceClassesMap);
exprsLineage.put(expr, i);
SqlTypeName sqlTypeName = expr.getType().getSqlTypeName();
if (sqlTypeName == SqlTypeName.TIMESTAMP || sqlTypeName == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
timestampExprs.add(expr);
}
}
// FLOOR(ts to DAY) via FLOOR(FLOOR(ts to HOUR) to DAY)
for (RexNode timestampExpr : timestampExprs) {
for (TimeUnitRange value : SUPPORTED_DATE_TIME_ROLLUP_UNITS) {
final SqlFunction[] functions = { getCeilSqlFunction(value), getFloorSqlFunction(value) };
for (SqlFunction function : functions) {
final RexNode call = rexBuilder.makeCall(function, timestampExpr, rexBuilder.makeFlag(value));
// References self-row
final RexNode rewrittenCall = shuttleReferences(rexBuilder, call, exprsLineage);
if (rewrittenCall == null) {
continue;
}
// We add the CEIL or FLOOR expression to the additional
// expressions, replacing the child expression by the position that
// it references
additionalExprs.add(rewrittenCall);
// Then we simplify the expression and we add it to the expressions
// lineage so we can try to find a match.
final RexNode simplified = simplify.simplifyUnknownAsFalse(call);
exprsLineage.put(simplified, target.getRowType().getFieldCount() + additionalExprs.size() - 1);
}
}
}
for (int i : positions) {
Set<RexNode> s = mq.getExpressionLineage(node, rexBuilder.makeInputRef(node, i));
if (s == null) {
// Bail out
return null;
}
// We only support project - filter - join, thus it should map to
// a single expression
final RexNode e = Iterables.getOnlyElement(s);
// Rewrite expr to be expressed on query tables
final RexNode simplified = simplify.simplifyUnknownAsFalse(e);
RexNode targetExpr = RexUtil.swapColumnReferences(rexBuilder, simplified, equivalenceClassesMap);
final Collection<Integer> c = exprsLineage.get(targetExpr);
if (!c.isEmpty()) {
for (Integer j : c) {
m.put(i, j);
}
} else {
// If we did not find the expression, try to navigate it
RexNode rewrittenTargetExpr = shuttleReferences(rexBuilder, targetExpr, exprsLineage);
if (rewrittenTargetExpr == null) {
// Some expressions were not present
return null;
}
m.put(i, target.getRowType().getFieldCount() + additionalExprs.size());
additionalExprs.add(rewrittenTargetExpr);
}
}
return m;
}
use of org.apache.calcite.sql.SqlFunction in project calcite by apache.
the class SqlValidatorImpl method validateCall.
@Override
public void validateCall(SqlCall call, SqlValidatorScope scope) {
final SqlOperator operator = call.getOperator();
if ((call.operandCount() == 0) && (operator.getSyntax() == SqlSyntax.FUNCTION_ID) && !call.isExpanded() && !this.config.conformance().allowNiladicParentheses()) {
// SqlIdentifier.)
throw handleUnresolvedFunction(call, operator, ImmutableList.of(), null);
}
SqlValidatorScope operandScope = scope.getOperandScope(call);
if (operator instanceof SqlFunction && ((SqlFunction) operator).getFunctionType() == SqlFunctionCategory.MATCH_RECOGNIZE && !(operandScope instanceof MatchRecognizeScope)) {
throw newValidationError(call, Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
}
// Delegate validation to the operator.
operator.validateCall(call, this, scope, operandScope);
}
use of org.apache.calcite.sql.SqlFunction in project calcite by apache.
the class SqlValidatorImpl method handleUnresolvedFunction.
@Override
public CalciteException handleUnresolvedFunction(SqlCall call, SqlOperator unresolvedFunction, List<RelDataType> argTypes, @Nullable List<String> argNames) {
// For builtins, we can give a better error message
final List<SqlOperator> overloads = new ArrayList<>();
opTab.lookupOperatorOverloads(unresolvedFunction.getNameAsId(), null, SqlSyntax.FUNCTION, overloads, catalogReader.nameMatcher());
if (overloads.size() == 1) {
SqlFunction fun = (SqlFunction) overloads.get(0);
if ((fun.getSqlIdentifier() == null) && (fun.getSyntax() != SqlSyntax.FUNCTION_ID)) {
final int expectedArgCount = fun.getOperandCountRange().getMin();
throw newValidationError(call, RESOURCE.invalidArgCount(call.getOperator().getName(), expectedArgCount));
}
}
final String signature;
if (unresolvedFunction instanceof SqlFunction) {
final SqlOperandTypeChecker typeChecking = new AssignableOperandTypeChecker(argTypes, argNames);
signature = typeChecking.getAllowedSignatures(unresolvedFunction, unresolvedFunction.getName());
} else {
signature = unresolvedFunction.getName();
}
throw newValidationError(call, RESOURCE.validatorUnknownFunction(signature));
}
Aggregations