use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project beam by apache.
the class ExpressionConverter method convertResolvedFunctionCall.
private RexNode convertResolvedFunctionCall(ResolvedFunctionCall functionCall, @Nullable List<ResolvedColumn> columnList, @Nullable List<RelDataTypeField> fieldList, Map<String, RexNode> outerFunctionArguments) {
final String funGroup = functionCall.getFunction().getGroup();
final String funName = functionCall.getFunction().getName();
SqlOperator op = SqlOperatorMappingTable.create(functionCall);
List<RexNode> operands = new ArrayList<>();
if (PRE_DEFINED_WINDOW_FUNCTIONS.equals(funGroup)) {
switch(funName) {
case FIXED_WINDOW:
case SESSION_WINDOW:
// TODO: check size and type of window function argument list.
// Add ts column reference to operands.
operands.add(convertRexNodeFromResolvedExpr(functionCall.getArgumentList().get(0), columnList, fieldList, outerFunctionArguments));
// Add fixed window size or session window gap to operands.
operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(1)));
break;
case SLIDING_WINDOW:
// Add ts column reference to operands.
operands.add(convertRexNodeFromResolvedExpr(functionCall.getArgumentList().get(0), columnList, fieldList, outerFunctionArguments));
// add sliding window emit frequency to operands.
operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(1)));
// add sliding window size to operands.
operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(2)));
break;
default:
throw new UnsupportedOperationException("Unsupported function: " + funName + ". Only support TUMBLE, HOP, and SESSION now.");
}
} else if (ZETASQL_FUNCTION_GROUP_NAME.equals(funGroup)) {
if (op == null) {
Type returnType = functionCall.getSignature().getResultType().getType();
if (returnType != null) {
op = SqlOperators.createZetaSqlFunction(funName, ZetaSqlCalciteTranslationUtils.toCalciteType(returnType, false, rexBuilder()).getSqlTypeName());
} else {
throw new UnsupportedOperationException("Does not support ZetaSQL function: " + funName);
}
}
for (ResolvedExpr expr : functionCall.getArgumentList()) {
operands.add(convertRexNodeFromResolvedExpr(expr, columnList, fieldList, outerFunctionArguments));
}
} else if (USER_DEFINED_JAVA_SCALAR_FUNCTIONS.equals(funGroup)) {
UserFunctionDefinitions.JavaScalarFunction javaScalarFunction = userFunctionDefinitions.javaScalarFunctions().get(functionCall.getFunction().getNamePath());
ArrayList<RexNode> innerFunctionArguments = new ArrayList<>();
for (int i = 0; i < functionCall.getArgumentList().size(); i++) {
ResolvedExpr argExpr = functionCall.getArgumentList().get(i);
RexNode argNode = convertRexNodeFromResolvedExpr(argExpr, columnList, fieldList, outerFunctionArguments);
innerFunctionArguments.add(argNode);
}
return rexBuilder().makeCall(SqlOperators.createUdfOperator(functionCall.getFunction().getName(), javaScalarFunction.method(), USER_DEFINED_JAVA_SCALAR_FUNCTIONS, javaScalarFunction.jarPath()), innerFunctionArguments);
} else if (USER_DEFINED_SQL_FUNCTIONS.equals(funGroup)) {
ResolvedCreateFunctionStmt createFunctionStmt = userFunctionDefinitions.sqlScalarFunctions().get(functionCall.getFunction().getNamePath());
ResolvedExpr functionExpression = createFunctionStmt.getFunctionExpression();
ImmutableMap.Builder<String, RexNode> innerFunctionArguments = ImmutableMap.builder();
for (int i = 0; i < functionCall.getArgumentList().size(); i++) {
String argName = createFunctionStmt.getArgumentNameList().get(i);
ResolvedExpr argExpr = functionCall.getArgumentList().get(i);
RexNode argNode = convertRexNodeFromResolvedExpr(argExpr, columnList, fieldList, outerFunctionArguments);
innerFunctionArguments.put(argName, argNode);
}
return this.convertRexNodeFromResolvedExpr(functionExpression, columnList, fieldList, innerFunctionArguments.build());
} else {
throw new UnsupportedOperationException("Does not support function group: " + funGroup);
}
SqlOperatorRewriter rewriter = SqlOperatorMappingTable.ZETASQL_FUNCTION_TO_CALCITE_SQL_OPERATOR_REWRITER.get(funName);
if (rewriter != null) {
return rewriter.apply(rexBuilder(), operands);
} else {
return rexBuilder().makeCall(op, operands);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project beam by apache.
the class SqlCaseWithValueOperatorRewriter method apply.
@Override
public RexNode apply(RexBuilder rexBuilder, List<RexNode> operands) {
Preconditions.checkArgument(operands.size() % 2 == 0 && !operands.isEmpty(), "$case_with_value should have an even number of arguments greater than 0 in function call" + " (The value operand, the else operand, and paired when/then operands).");
SqlOperator op = SqlStdOperatorTable.CASE;
List<RexNode> newOperands = new ArrayList<>();
RexNode value = operands.get(0);
for (int i = 1; i < operands.size() - 2; i += 2) {
RexNode when = operands.get(i);
RexNode then = operands.get(i + 1);
newOperands.add(rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, ImmutableList.of(value, when)));
newOperands.add(then);
}
RexNode elseOperand = Iterables.getLast(operands);
newOperands.add(elseOperand);
return rexBuilder.makeCall(op, newOperands);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project beam by apache.
the class CEPCall method of.
public static CEPCall of(RexCall operation) {
SqlOperator call = operation.getOperator();
CEPOperator myOp = CEPOperator.of(call);
ArrayList<CEPOperation> operandsList = new ArrayList<>();
for (RexNode i : operation.getOperands()) {
if (i.getClass() == RexCall.class) {
CEPCall callToAdd = CEPCall.of((RexCall) i);
operandsList.add(callToAdd);
} else if (i.getClass() == RexLiteral.class) {
RexLiteral lit = (RexLiteral) i;
CEPLiteral litToAdd = CEPLiteral.of(lit);
operandsList.add(litToAdd);
} else if (i.getClass() == RexPatternFieldRef.class) {
RexPatternFieldRef fieldRef = (RexPatternFieldRef) i;
CEPFieldRef fieldRefToAdd = CEPFieldRef.of(fieldRef);
operandsList.add(fieldRefToAdd);
} else {
throw new UnsupportedOperationException("RexNode not supported: " + i.getClass().getName());
}
}
return new CEPCall(myOp, operandsList);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project beam by apache.
the class BeamCalcRelType method canImplement.
@Override
protected boolean canImplement(RexCall call) {
final SqlOperator operator = call.getOperator();
RexImpTable.RexCallImplementor implementor = RexImpTable.INSTANCE.get(operator);
if (implementor == null) {
// Reject methods with no implementation
return false;
}
if (operator instanceof SqlUserDefinedFunction) {
SqlUserDefinedFunction udf = (SqlUserDefinedFunction) call.op;
if (udf.function instanceof ZetaSqlScalarFunctionImpl) {
ZetaSqlScalarFunctionImpl scalarFunction = (ZetaSqlScalarFunctionImpl) udf.function;
if (!scalarFunction.functionGroup.equals(BeamZetaSqlCatalog.USER_DEFINED_JAVA_SCALAR_FUNCTIONS)) {
// Reject ZetaSQL Builtin Scalar Functions
return false;
}
for (RexNode operand : call.getOperands()) {
if (operand instanceof RexLocalRef) {
if (!supportsType(operand.getType())) {
LOG.error("User-defined function {} received unsupported operand type {}.", call.op.getName(), ((RexLocalRef) operand).getType());
return false;
}
} else {
LOG.error("User-defined function {} received unrecognized operand kind {}.", call.op.getName(), operand.getKind());
return false;
}
}
} else {
// Reject other UDFs
return false;
}
} else {
// Reject Calcite implementations
return false;
}
return true;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator in project beam by apache.
the class CEPUtils method getCEPPatternFromPattern.
/**
* Construct a list of {@code CEPPattern}s from a {@code RexNode}.
*/
public static ArrayList<CEPPattern> getCEPPatternFromPattern(Schema upStreamSchema, RexNode call, Map<String, RexNode> patternDefs) {
ArrayList<CEPPattern> patternList = new ArrayList<>();
if (call.getClass() == RexLiteral.class) {
String p = ((RexLiteral) call).getValueAs(String.class);
RexNode pd = patternDefs.get(p);
patternList.add(CEPPattern.of(upStreamSchema, p, (RexCall) pd, Quantifier.NONE));
} else {
RexCall patCall = (RexCall) call;
SqlOperator operator = patCall.getOperator();
List<RexNode> operands = patCall.getOperands();
// check if the node has quantifier
if (operator.getKind() == SqlKind.PATTERN_QUANTIFIER) {
String p = ((RexLiteral) operands.get(0)).getValueAs(String.class);
RexNode pd = patternDefs.get(p);
int start = ((RexLiteral) operands.get(1)).getValueAs(Integer.class);
int end = ((RexLiteral) operands.get(2)).getValueAs(Integer.class);
boolean isReluctant = ((RexLiteral) operands.get(3)).getValueAs(Boolean.class);
patternList.add(CEPPattern.of(upStreamSchema, p, (RexCall) pd, getQuantifier(start, end, isReluctant)));
} else {
for (RexNode i : operands) {
patternList.addAll(getCEPPatternFromPattern(upStreamSchema, i, patternDefs));
}
}
}
return patternList;
}
Aggregations