use of com.google.zetasql.io.grpc.StatusRuntimeException in project beam by apache.
the class SqlOperators method createStringAggOperator.
public static SqlOperator createStringAggOperator(ResolvedNodes.ResolvedFunctionCallBase aggregateFunctionCall) {
List<ResolvedNodes.ResolvedExpr> args = aggregateFunctionCall.getArgumentList();
String inputType = args.get(0).getType().typeName();
Value delimiter = null;
if (args.size() == 2) {
ResolvedNodes.ResolvedExpr resolvedExpr = args.get(1);
if (resolvedExpr instanceof ResolvedNodes.ResolvedLiteral) {
delimiter = ((ResolvedNodes.ResolvedLiteral) resolvedExpr).getValue();
} else {
// TODO(BEAM-13673) Add support for params
throw new ZetaSqlException(new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription(String.format("STRING_AGG only supports ResolvedLiteral as delimiter, provided %s", resolvedExpr.getClass().getName()))));
}
}
switch(inputType) {
case "BYTES":
return SqlOperators.createUdafOperator("string_agg", x -> SqlOperators.createTypeFactory().createSqlType(SqlTypeName.VARBINARY), new UdafImpl<>(new StringAgg.StringAggByte(delimiter == null ? ",".getBytes(StandardCharsets.UTF_8) : delimiter.getBytesValue().toByteArray())));
case "STRING":
return SqlOperators.createUdafOperator("string_agg", x -> SqlOperators.createTypeFactory().createSqlType(SqlTypeName.VARCHAR), new UdafImpl<>(new StringAgg.StringAggString(delimiter == null ? "," : delimiter.getStringValue())));
default:
throw new UnsupportedOperationException(String.format("[%s] is not supported in STRING_AGG", inputType));
}
}
Aggregations