use of org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlException in project beam by apache.
the class ExpressionConverter method convertIntervalToRexIntervalLiteral.
private RexNode convertIntervalToRexIntervalLiteral(ResolvedLiteral resolvedLiteral) {
if (resolvedLiteral.getType().getKind() != TYPE_STRING) {
throw new ZetaSqlException(INTERVAL_FORMAT_MSG);
}
String valStr = resolvedLiteral.getValue().getStringValue();
List<String> stringList = Arrays.stream(valStr.split(" ")).filter(s -> !s.isEmpty()).collect(Collectors.toList());
if (stringList.size() != 3) {
throw new ZetaSqlException(INTERVAL_FORMAT_MSG);
}
if (!Ascii.toUpperCase(stringList.get(0)).equals("INTERVAL")) {
throw new ZetaSqlException(INTERVAL_FORMAT_MSG);
}
long intervalValue;
try {
intervalValue = Long.parseLong(stringList.get(1));
} catch (NumberFormatException e) {
throw new ZetaSqlException(Status.UNIMPLEMENTED.withDescription(INTERVAL_FORMAT_MSG).withCause(e).asRuntimeException());
}
String intervalDatepart = Ascii.toUpperCase(stringList.get(2));
return createCalciteIntervalRexLiteral(intervalValue, intervalDatepart);
}
use of org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlException 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));
}
}
use of org.apache.beam.sdk.extensions.sql.zetasql.ZetaSqlException in project beam by apache.
the class ExpressionConverter method convertRexNodeFromComputedColumnWithFieldList.
private RexNode convertRexNodeFromComputedColumnWithFieldList(ResolvedComputedColumn column, List<ResolvedColumn> columnList, List<RelDataTypeField> fieldList, int windowFieldIndex) {
if (column.getExpr().nodeKind() != RESOLVED_FUNCTION_CALL) {
return convertRexNodeFromResolvedExpr(column.getExpr(), columnList, fieldList, ImmutableMap.of());
}
ResolvedFunctionCall functionCall = (ResolvedFunctionCall) column.getExpr();
// TODO: is there any other illegal case?
if (functionCall.getFunction().getName().equals(FIXED_WINDOW) || functionCall.getFunction().getName().equals(SLIDING_WINDOW) || functionCall.getFunction().getName().equals(SESSION_WINDOW)) {
throw new ZetaSqlException(functionCall.getFunction().getName() + " shouldn't appear in SELECT exprlist.");
}
if (!functionCall.getFunction().getGroup().equals(PRE_DEFINED_WINDOW_FUNCTIONS)) {
// non-window function should still go through normal FunctionCall conversion process.
return convertRexNodeFromResolvedExpr(column.getExpr(), columnList, fieldList, ImmutableMap.of());
}
// ONLY window_start and window_end should arrive here.
// TODO: Have extra verification here to make sure window start/end functions have the same
// parameter with window function.
List<RexNode> operands = new ArrayList<>();
switch(functionCall.getFunction().getName()) {
case FIXED_WINDOW_START:
case SLIDING_WINDOW_START:
case SESSION_WINDOW_START:
// in Calcite.
case SESSION_WINDOW_END:
return rexBuilder().makeInputRef(fieldList.get(windowFieldIndex).getType(), windowFieldIndex);
case FIXED_WINDOW_END:
operands.add(rexBuilder().makeInputRef(fieldList.get(windowFieldIndex).getType(), windowFieldIndex));
// TODO: check window_end 's duration is the same as it's aggregate window.
operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(0)));
return rexBuilder().makeCall(SqlOperators.ZETASQL_TIMESTAMP_ADD, operands);
case SLIDING_WINDOW_END:
operands.add(rexBuilder().makeInputRef(fieldList.get(windowFieldIndex).getType(), windowFieldIndex));
operands.add(convertIntervalToRexIntervalLiteral((ResolvedLiteral) functionCall.getArgumentList().get(1)));
return rexBuilder().makeCall(SqlOperators.ZETASQL_TIMESTAMP_ADD, operands);
default:
throw new UnsupportedOperationException("Does not support window start/end: " + functionCall.getFunction().getName());
}
}
Aggregations