use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class StreamExecPythonGroupWindowAggregate method generateWindowAssignerAndTrigger.
private Tuple2<WindowAssigner<?>, Trigger<?>> generateWindowAssignerAndTrigger() {
WindowAssigner<?> windowAssiger;
Trigger<?> trigger;
if (window instanceof TumblingGroupWindow) {
TumblingGroupWindow tumblingWindow = (TumblingGroupWindow) window;
FieldReferenceExpression timeField = tumblingWindow.timeField();
ValueLiteralExpression size = tumblingWindow.size();
if (isProctimeAttribute(timeField) && hasTimeIntervalType(size)) {
windowAssiger = TumblingWindowAssigner.of(toDuration(size)).withProcessingTime();
trigger = ProcessingTimeTriggers.afterEndOfWindow();
} else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
windowAssiger = TumblingWindowAssigner.of(toDuration(size)).withEventTime();
trigger = EventTimeTriggers.afterEndOfWindow();
} else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
windowAssiger = CountTumblingWindowAssigner.of(toLong(size));
trigger = ElementTriggers.count(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)) {
windowAssiger = SlidingWindowAssigner.of(toDuration(size), toDuration(slide)).withProcessingTime();
trigger = ProcessingTimeTriggers.afterEndOfWindow();
} else if (isRowtimeAttribute(timeField) && hasTimeIntervalType(size)) {
windowAssiger = SlidingWindowAssigner.of(toDuration(size), toDuration(slide));
trigger = EventTimeTriggers.afterEndOfWindow();
} else if (isProctimeAttribute(timeField) && hasRowIntervalType(size)) {
windowAssiger = CountSlidingWindowAssigner.of(toLong(size), toLong(slide));
trigger = ElementTriggers.count(toLong(size));
} 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)) {
windowAssiger = SessionWindowAssigner.withGap(toDuration(gap));
trigger = ProcessingTimeTriggers.afterEndOfWindow();
} else if (isRowtimeAttribute(timeField)) {
windowAssiger = SessionWindowAssigner.withGap(toDuration(gap));
trigger = EventTimeTriggers.afterEndOfWindow();
} else {
throw new UnsupportedOperationException("This should not happen.");
}
} else {
throw new TableException("Unsupported window: " + window.toString());
}
return Tuple2.of(windowAssiger, trigger);
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class AggregateOperationFactory method createResolvedWindow.
/**
* Converts an API class to a resolved window for planning with expressions already resolved. It
* performs following validations:
*
* <ul>
* <li>The alias is represented with an unresolved reference
* <li>The time attribute is a single field reference of a {@link
* TimeIndicatorTypeInfo}(stream), {@link SqlTimeTypeInfo}(batch), or {@link
* BasicTypeInfo#LONG_TYPE_INFO}(batch) type
* <li>The size & slide are value literals of either {@link BasicTypeInfo#LONG_TYPE_INFO}, or
* {@link TimeIntervalTypeInfo} type
* <li>The size & slide are of the same type
* <li>The gap is a value literal of a {@link TimeIntervalTypeInfo} type
* </ul>
*
* @param window window to resolve
* @param resolver resolver to resolve potential unresolved field references
* @return window with expressions resolved
*/
ResolvedGroupWindow createResolvedWindow(GroupWindow window, ExpressionResolver resolver) {
Expression alias = window.getAlias();
if (!(alias instanceof UnresolvedReferenceExpression)) {
throw new ValidationException("Only unresolved reference supported for alias of a group window.");
}
final String windowName = ((UnresolvedReferenceExpression) alias).getName();
FieldReferenceExpression timeField = getValidatedTimeAttribute(window, resolver);
if (window instanceof TumbleWithSizeOnTimeWithAlias) {
return validateAndCreateTumbleWindow((TumbleWithSizeOnTimeWithAlias) window, windowName, timeField);
} else if (window instanceof SlideWithSizeAndSlideOnTimeWithAlias) {
return validateAndCreateSlideWindow((SlideWithSizeAndSlideOnTimeWithAlias) window, windowName, timeField);
} else if (window instanceof SessionWithGapOnTimeWithAlias) {
return validateAndCreateSessionWindow((SessionWithGapOnTimeWithAlias) window, windowName, timeField);
} else {
throw new TableException("Unknown window type: " + window);
}
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class QueryOperationTest method testWindowAggregationSummaryString.
@Test
public void testWindowAggregationSummaryString() {
ResolvedSchema schema = ResolvedSchema.physical(Collections.singletonList("a"), Collections.singletonList(DataTypes.INT()));
FieldReferenceExpression field = new FieldReferenceExpression("a", DataTypes.INT(), 0, 0);
WindowAggregateQueryOperation tableOperation = new WindowAggregateQueryOperation(Collections.singletonList(field), Collections.singletonList(CallExpression.permanent(BuiltInFunctionDefinitions.SUM, Collections.singletonList(field), DataTypes.INT())), Collections.emptyList(), WindowAggregateQueryOperation.ResolvedGroupWindow.sessionWindow("w", field, intervalOfMillis(10)), new SourceQueryOperation(ContextResolvedTable.temporary(ObjectIdentifier.of("cat1", "db1", "tab1"), new ResolvedCatalogTable(CatalogTable.of(Schema.newBuilder().build(), null, Collections.emptyList(), Collections.emptyMap()), schema))), schema);
DistinctQueryOperation distinctQueryOperation = new DistinctQueryOperation(tableOperation);
assertEquals("Distinct:\n" + " WindowAggregate: (group: [a], agg: [sum(a)], windowProperties: []," + " window: [SessionWindow(field: [a], gap: [10])])\n" + " CatalogTable: (identifier: [cat1.db1.tab1], fields: [a])", distinctQueryOperation.asSummaryString());
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class FilterUtils method getValue.
private static Comparable<?> getValue(Expression expr, Function<String, Comparable<?>> getter) {
if (expr instanceof ValueLiteralExpression) {
Optional<?> value = ((ValueLiteralExpression) expr).getValueAs(((ValueLiteralExpression) expr).getOutputDataType().getConversionClass());
return (Comparable<?>) value.orElse(null);
}
if (expr instanceof FieldReferenceExpression) {
return getter.apply(((FieldReferenceExpression) expr).getName());
}
if (expr instanceof CallExpression && expr.getChildren().size() == 1) {
Object child = getValue(expr.getChildren().get(0), getter);
FunctionDefinition functionDefinition = ((CallExpression) expr).getFunctionDefinition();
if (functionDefinition.equals(UPPER)) {
return child.toString().toUpperCase();
} else if (functionDefinition.equals(LOWER)) {
return child.toString().toLowerCase();
} else {
throw new UnsupportedOperationException(String.format("Unrecognized function definition: %s.", functionDefinition));
}
}
throw new UnsupportedOperationException(expr + " not supported!");
}
use of org.apache.flink.table.expressions.FieldReferenceExpression in project flink by apache.
the class LogicalWindowJsonDeserializer method deserialize.
@Override
public LogicalWindow deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode jsonNode = jsonParser.readValueAsTree();
String kind = jsonNode.get(FIELD_NAME_KIND).asText().toUpperCase();
WindowReference alias = deserializationContext.readValue(jsonNode.get(FIELD_NAME_ALIAS).traverse(jsonParser.getCodec()), WindowReference.class);
FieldReferenceExpression timeField = deserializeFieldReferenceExpression(jsonNode.get(FIELD_NAME_TIME_FIELD), jsonParser, deserializationContext);
switch(kind) {
case KIND_TUMBLING:
boolean isTimeTumblingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
if (isTimeTumblingWindow) {
Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
} else {
long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
}
case KIND_SLIDING:
boolean isTimeSlidingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
if (isTimeSlidingWindow) {
Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
Duration slide = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SLIDE), jsonParser.getCodec()), Duration.class);
return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
} else {
long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
long slide = jsonNode.get(FIELD_NAME_SLIDE).asLong();
return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
}
case KIND_SESSION:
Duration gap = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_GAP), jsonParser.getCodec()), Duration.class);
return new SessionGroupWindow(alias, timeField, new ValueLiteralExpression(gap));
default:
throw new TableException("Unknown Logical Window:" + jsonNode);
}
}
Aggregations