Search in sources :

Example 1 with FieldReferenceExpression

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);
}
Also used : TableException(org.apache.flink.table.api.TableException) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 2 with FieldReferenceExpression

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);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) ValidationException(org.apache.flink.table.api.ValidationException) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) TumbleWithSizeOnTimeWithAlias(org.apache.flink.table.api.TumbleWithSizeOnTimeWithAlias) SlideWithSizeAndSlideOnTimeWithAlias(org.apache.flink.table.api.SlideWithSizeAndSlideOnTimeWithAlias) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) SessionWithGapOnTimeWithAlias(org.apache.flink.table.api.SessionWithGapOnTimeWithAlias)

Example 3 with FieldReferenceExpression

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());
}
Also used : ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Test(org.junit.Test)

Example 4 with FieldReferenceExpression

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!");
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) CallExpression(org.apache.flink.table.expressions.CallExpression)

Example 5 with FieldReferenceExpression

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);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) Duration(java.time.Duration) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow) WindowReference(org.apache.flink.table.runtime.groupwindow.WindowReference)

Aggregations

FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)14 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)8 TableException (org.apache.flink.table.api.TableException)5 CallExpression (org.apache.flink.table.expressions.CallExpression)4 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)4 SessionGroupWindow (org.apache.flink.table.planner.plan.logical.SessionGroupWindow)4 SlidingGroupWindow (org.apache.flink.table.planner.plan.logical.SlidingGroupWindow)4 TumblingGroupWindow (org.apache.flink.table.planner.plan.logical.TumblingGroupWindow)4 Test (org.junit.Test)4 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 ValidationException (org.apache.flink.table.api.ValidationException)2 ResolvedCatalogTable (org.apache.flink.table.catalog.ResolvedCatalogTable)2 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)2 Expression (org.apache.flink.table.expressions.Expression)2 UnresolvedReferenceExpression (org.apache.flink.table.expressions.UnresolvedReferenceExpression)2 DataType (org.apache.flink.table.types.DataType)2 LogicalType (org.apache.flink.table.types.logical.LogicalType)2 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)1 SessionWithGapOnTimeWithAlias (org.apache.flink.table.api.SessionWithGapOnTimeWithAlias)1