use of com.google.zetasql.FunctionSignature in project beam by apache.
the class AggregateScanConverter method convertAggCall.
private AggregateCall convertAggCall(ResolvedComputedColumn computedColumn, int columnRefOff, int groupCount, RelNode input) {
ResolvedAggregateFunctionCall aggregateFunctionCall = (ResolvedAggregateFunctionCall) computedColumn.getExpr();
// Reject AVG(INT64)
if (aggregateFunctionCall.getFunction().getName().equals("avg")) {
FunctionSignature signature = aggregateFunctionCall.getSignature();
if (signature.getFunctionArgumentList().get(0).getType().getKind().equals(TypeKind.TYPE_INT64)) {
throw new UnsupportedOperationException(AVG_ILLEGAL_LONG_INPUT_TYPE);
}
}
// Reject aggregation DISTINCT
if (aggregateFunctionCall.getDistinct()) {
throw new UnsupportedOperationException("Does not support " + aggregateFunctionCall.getFunction().getSqlName() + " DISTINCT. 'SELECT DISTINCT' syntax could be used to deduplicate before" + " aggregation.");
}
final SqlAggFunction sqlAggFunction;
if (aggregateFunctionCall.getFunction().getGroup().equals(BeamZetaSqlCatalog.USER_DEFINED_JAVA_AGGREGATE_FUNCTIONS)) {
// Create a new operator for user-defined functions.
SqlReturnTypeInference typeInference = x -> ZetaSqlCalciteTranslationUtils.toCalciteType(aggregateFunctionCall.getFunction().getSignatureList().get(0).getResultType().getType(), // TODO(BEAM-9514) set nullable=true
false, getCluster().getRexBuilder());
UdafImpl<?, ?, ?> impl = new UdafImpl<>(getExpressionConverter().userFunctionDefinitions.javaAggregateFunctions().get(aggregateFunctionCall.getFunction().getNamePath()));
sqlAggFunction = SqlOperators.createUdafOperator(aggregateFunctionCall.getFunction().getName(), typeInference, impl);
} else {
// Look up builtin functions in SqlOperatorMappingTable.
sqlAggFunction = (SqlAggFunction) SqlOperatorMappingTable.create(aggregateFunctionCall);
if (sqlAggFunction == null) {
throw new UnsupportedOperationException("Does not support ZetaSQL aggregate function: " + aggregateFunctionCall.getFunction().getName());
}
}
List<Integer> argList = new ArrayList<>();
ResolvedAggregateFunctionCall expr = ((ResolvedAggregateFunctionCall) computedColumn.getExpr());
List<ZetaSQLResolvedNodeKind.ResolvedNodeKind> resolvedNodeKinds = Arrays.asList(RESOLVED_CAST, RESOLVED_COLUMN_REF, RESOLVED_GET_STRUCT_FIELD);
for (int i = 0; i < expr.getArgumentList().size(); i++) {
// Throw an error if aggregate function's input isn't either a ColumnRef or a cast(ColumnRef).
// TODO: is there a general way to handle aggregation calls conversion?
ZetaSQLResolvedNodeKind.ResolvedNodeKind resolvedNodeKind = expr.getArgumentList().get(i).nodeKind();
if (i == 0 && resolvedNodeKinds.contains(resolvedNodeKind)) {
argList.add(columnRefOff);
} else if (i > 0 && resolvedNodeKind == RESOLVED_LITERAL) {
continue;
} else {
throw new UnsupportedOperationException("Aggregate function only accepts Column Reference or CAST(Column Reference) as the first argument and " + "Literals as subsequent arguments as its inputs");
}
}
String aggName = getTrait().resolveAlias(computedColumn.getColumn());
return AggregateCall.create(sqlAggFunction, false, false, false, argList, -1, null, RelCollations.EMPTY, groupCount, input, // When we pass null as the return type, Calcite infers it for us.
null, aggName);
}
use of com.google.zetasql.FunctionSignature in project beam by apache.
the class BeamZetaSqlCatalog method addUdfsFromSchema.
private void addUdfsFromSchema() {
for (String functionName : calciteSchema.getFunctionNames()) {
Collection<org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function> functions = calciteSchema.getFunctions(functionName);
if (functions.size() != 1) {
throw new IllegalArgumentException(String.format("Expected exactly 1 definition for function '%s', but found %d." + " Beam ZetaSQL supports only a single function definition per function name (BEAM-12073).", functionName, functions.size()));
}
for (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.Function function : functions) {
List<String> path = Arrays.asList(functionName.split("\\."));
if (function instanceof ScalarFunctionImpl) {
ScalarFunctionImpl scalarFunction = (ScalarFunctionImpl) function;
// for unsupported types.
for (FunctionParameter parameter : scalarFunction.getParameters()) {
validateJavaUdfCalciteType(parameter.getType(typeFactory), functionName);
}
validateJavaUdfCalciteType(scalarFunction.getReturnType(typeFactory), functionName);
Method method = scalarFunction.method;
javaScalarUdfs.put(path, UserFunctionDefinitions.JavaScalarFunction.create(method, ""));
FunctionArgumentType resultType = new FunctionArgumentType(ZetaSqlCalciteTranslationUtils.toZetaSqlType(scalarFunction.getReturnType(typeFactory)));
FunctionSignature functionSignature = new FunctionSignature(resultType, getArgumentTypes(scalarFunction), 0L);
zetaSqlCatalog.addFunction(new Function(path, USER_DEFINED_JAVA_SCALAR_FUNCTIONS, ZetaSQLFunctions.FunctionEnums.Mode.SCALAR, ImmutableList.of(functionSignature)));
} else if (function instanceof UdafImpl) {
UdafImpl<?, ?, ?> udaf = (UdafImpl) function;
javaUdafs.put(path, udaf.getCombineFn());
FunctionArgumentType resultType = new FunctionArgumentType(ZetaSqlCalciteTranslationUtils.toZetaSqlType(udaf.getReturnType(typeFactory)));
FunctionSignature functionSignature = new FunctionSignature(resultType, getArgumentTypes(udaf), 0L);
zetaSqlCatalog.addFunction(new Function(path, USER_DEFINED_JAVA_AGGREGATE_FUNCTIONS, ZetaSQLFunctions.FunctionEnums.Mode.AGGREGATE, ImmutableList.of(functionSignature)));
} else {
throw new IllegalArgumentException(String.format("Function %s has unrecognized implementation type %s.", functionName, function.getClass().getName()));
}
}
}
}
use of com.google.zetasql.FunctionSignature in project beam by apache.
the class BeamZetaSqlCatalog method addWindowTvfs.
@SuppressWarnings({ // customContext and volatility are in fact nullable, but they are missing the
"nullness" // annotation upstream. TODO Unsuppress when this is fixed in ZetaSQL.
})
private void addWindowTvfs() {
FunctionArgumentType retType = new FunctionArgumentType(ZetaSQLFunctions.SignatureArgumentKind.ARG_TYPE_RELATION);
FunctionArgumentType inputTableType = new FunctionArgumentType(ZetaSQLFunctions.SignatureArgumentKind.ARG_TYPE_RELATION);
FunctionArgumentType descriptorType = new FunctionArgumentType(ZetaSQLFunctions.SignatureArgumentKind.ARG_TYPE_DESCRIPTOR, FunctionArgumentType.FunctionArgumentTypeOptions.builder().setDescriptorResolutionTableOffset(0).build(), 1);
FunctionArgumentType stringType = new FunctionArgumentType(TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_STRING));
// TUMBLE
zetaSqlCatalog.addTableValuedFunction(new TableValuedFunction.ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(ImmutableList.of(TVFStreamingUtils.FIXED_WINDOW_TVF), new FunctionSignature(retType, ImmutableList.of(inputTableType, descriptorType, stringType), -1), ImmutableList.of(TVFRelation.Column.create(TVFStreamingUtils.WINDOW_START, TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_TIMESTAMP)), TVFRelation.Column.create(TVFStreamingUtils.WINDOW_END, TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_TIMESTAMP))), null, null));
// HOP
zetaSqlCatalog.addTableValuedFunction(new TableValuedFunction.ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(ImmutableList.of(TVFStreamingUtils.SLIDING_WINDOW_TVF), new FunctionSignature(retType, ImmutableList.of(inputTableType, descriptorType, stringType, stringType), -1), ImmutableList.of(TVFRelation.Column.create(TVFStreamingUtils.WINDOW_START, TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_TIMESTAMP)), TVFRelation.Column.create(TVFStreamingUtils.WINDOW_END, TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_TIMESTAMP))), null, null));
// SESSION
zetaSqlCatalog.addTableValuedFunction(new TableValuedFunction.ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(ImmutableList.of(TVFStreamingUtils.SESSION_WINDOW_TVF), new FunctionSignature(retType, ImmutableList.of(inputTableType, descriptorType, descriptorType, stringType), -1), ImmutableList.of(TVFRelation.Column.create(TVFStreamingUtils.WINDOW_START, TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_TIMESTAMP)), TVFRelation.Column.create(TVFStreamingUtils.WINDOW_END, TypeFactory.createSimpleType(ZetaSQLType.TypeKind.TYPE_TIMESTAMP))), null, null));
}
Aggregations