use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class ExistingField method getExpression.
/**
* Returns an {@link Expression} that casts a {@link Long}, {@link Timestamp}, or timestamp
* formatted {@link String} field (e.g., "2018-05-28 12:34:56.000") into a rowtime attribute.
*/
@Override
public Expression getExpression(ResolvedFieldReference[] fieldAccesses) {
ResolvedFieldReference fieldAccess = fieldAccesses[0];
DataType type = fromLegacyInfoToDataType(fieldAccess.resultType());
FieldReferenceExpression fieldReferenceExpr = new FieldReferenceExpression(fieldAccess.name(), type, 0, fieldAccess.fieldIndex());
switch(type.getLogicalType().getTypeRoot()) {
case BIGINT:
case TIMESTAMP_WITHOUT_TIME_ZONE:
return fieldReferenceExpr;
case VARCHAR:
DataType outputType = TIMESTAMP(3).bridgedTo(Timestamp.class);
return CallExpression.permanent(CAST, Arrays.asList(fieldReferenceExpr, typeLiteral(outputType)), outputType);
default:
throw new RuntimeException("Unsupport type: " + type);
}
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class StreamExecGroupWindowAggregate method createWindowOperator.
private WindowOperator<?, ?> createWindowOperator(ReadableConfig config, GeneratedClass<?> aggsHandler, GeneratedRecordEqualiser recordEqualiser, LogicalType[] accTypes, LogicalType[] windowPropertyTypes, LogicalType[] aggValueTypes, LogicalType[] inputFields, int timeFieldIndex, ZoneId shiftTimeZone, int inputCountIndex) {
WindowOperatorBuilder builder = WindowOperatorBuilder.builder().withInputFields(inputFields).withShiftTimezone(shiftTimeZone).withInputCountIndex(inputCountIndex);
if (window instanceof TumblingGroupWindow) {
TumblingGroupWindow tumblingWindow = (TumblingGroupWindow) window;
FieldReferenceExpression timeField = tumblingWindow.timeField();
ValueLiteralExpression size = tumblingWindow.size();
if (isProctimeAttribute(timeField) && hasTimeIntervalType(size)) {
builder = builder.tumble(toDuration(size)).withProcessingTime();
} else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
builder = builder.tumble(toDuration(size)).withEventTime(timeFieldIndex);
} else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
builder = builder.countWindow(toLong(size));
} else {
// ProcessingTimeTumblingGroupWindow
throw new UnsupportedOperationException("Event-time grouping windows on row intervals are currently not supported.");
}
} else if (window instanceof SlidingGroupWindow) {
SlidingGroupWindow slidingWindow = (SlidingGroupWindow) window;
FieldReferenceExpression timeField = slidingWindow.timeField();
ValueLiteralExpression size = slidingWindow.size();
ValueLiteralExpression slide = slidingWindow.slide();
if (isProctimeAttribute(timeField) && hasTimeIntervalType(size)) {
builder = builder.sliding(toDuration(size), toDuration(slide)).withProcessingTime();
} else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
builder = builder.sliding(toDuration(size), toDuration(slide)).withEventTime(timeFieldIndex);
} else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
builder = builder.countWindow(toLong(size), toLong(slide));
} else {
// ProcessingTimeTumblingGroupWindow
throw new UnsupportedOperationException("Event-time grouping windows on row intervals are currently not supported.");
}
} else if (window instanceof SessionGroupWindow) {
SessionGroupWindow sessionWindow = (SessionGroupWindow) window;
FieldReferenceExpression timeField = sessionWindow.timeField();
ValueLiteralExpression gap = sessionWindow.gap();
if (isProctimeAttribute(timeField)) {
builder = builder.session(toDuration(gap)).withProcessingTime();
} else if (isRowtimeAttribute(timeField)) {
builder = builder.session(toDuration(gap)).withEventTime(timeFieldIndex);
} else {
throw new UnsupportedOperationException("This should not happen.");
}
} else {
throw new TableException("Unsupported window: " + window.toString());
}
WindowEmitStrategy emitStrategy = WindowEmitStrategy.apply(config, window);
if (emitStrategy.produceUpdates()) {
// mark this operator will send retraction and set new trigger
builder.produceUpdates().triggering(emitStrategy.getTrigger()).withAllowedLateness(Duration.ofMillis(emitStrategy.getAllowLateness()));
}
if (aggsHandler instanceof GeneratedNamespaceAggsHandleFunction) {
return builder.aggregate((GeneratedNamespaceAggsHandleFunction<?>) aggsHandler, recordEqualiser, accTypes, aggValueTypes, windowPropertyTypes).build();
} else if (aggsHandler instanceof GeneratedNamespaceTableAggsHandleFunction) {
return builder.aggregate((GeneratedNamespaceTableAggsHandleFunction<?>) aggsHandler, accTypes, aggValueTypes, windowPropertyTypes).build();
} else {
throw new TableException("Unsupported agg handler class: " + aggsHandler.getClass().getSimpleName());
}
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class LogicalWindowJsonDeserializer method deserializeFieldReferenceExpression.
private FieldReferenceExpression deserializeFieldReferenceExpression(JsonNode input, JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
String name = input.get(FIELD_NAME_FIELD_NAME).asText();
int fieldIndex = input.get(FIELD_NAME_FIELD_INDEX).asInt();
int inputIndex = input.get(FIELD_NAME_INPUT_INDEX).asInt();
LogicalType type = deserializationContext.readValue(input.get(FIELD_NAME_FIELD_TYPE).traverse(jsonParser.getCodec()), LogicalType.class);
return new FieldReferenceExpression(name, new AtomicDataType(type), inputIndex, fieldIndex);
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class AggregatePushDownSpec method buildAggregateExpressions.
private static List<AggregateExpression> buildAggregateExpressions(RowType inputType, List<AggregateCall> aggregateCalls) {
AggregateInfoList aggInfoList = AggregateUtil.transformToBatchAggregateInfoList(inputType, JavaScalaConversionUtil.toScala(aggregateCalls), null, null);
if (aggInfoList.aggInfos().length == 0) {
// no agg function need to be pushed down
return Collections.emptyList();
}
List<AggregateExpression> aggExpressions = new ArrayList<>();
for (AggregateInfo aggInfo : aggInfoList.aggInfos()) {
List<FieldReferenceExpression> arguments = new ArrayList<>(1);
for (int argIndex : aggInfo.argIndexes()) {
DataType argType = TypeConversions.fromLogicalToDataType(inputType.getFields().get(argIndex).getType());
FieldReferenceExpression field = new FieldReferenceExpression(inputType.getFieldNames().get(argIndex), argType, 0, argIndex);
arguments.add(field);
}
if (aggInfo.function() instanceof AvgAggFunction) {
Tuple2<Sum0AggFunction, CountAggFunction> sum0AndCountFunction = AggregateUtil.deriveSumAndCountFromAvg((AvgAggFunction) aggInfo.function());
AggregateExpression sum0Expression = new AggregateExpression(sum0AndCountFunction._1(), arguments, null, aggInfo.externalResultType(), aggInfo.agg().isDistinct(), aggInfo.agg().isApproximate(), aggInfo.agg().ignoreNulls());
aggExpressions.add(sum0Expression);
AggregateExpression countExpression = new AggregateExpression(sum0AndCountFunction._2(), arguments, null, aggInfo.externalResultType(), aggInfo.agg().isDistinct(), aggInfo.agg().isApproximate(), aggInfo.agg().ignoreNulls());
aggExpressions.add(countExpression);
} else {
AggregateExpression aggregateExpression = new AggregateExpression(aggInfo.function(), arguments, null, aggInfo.externalResultType(), aggInfo.agg().isDistinct(), aggInfo.agg().isApproximate(), aggInfo.agg().ignoreNulls());
aggExpressions.add(aggregateExpression);
}
}
return aggExpressions;
}
Aggregations