Search in sources :

Example 11 with FieldReferenceExpression

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);
    }
}
Also used : ResolvedFieldReference(org.apache.flink.table.expressions.ResolvedFieldReference) DataType(org.apache.flink.table.types.DataType) TypeConversions.fromLegacyInfoToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression)

Example 12 with FieldReferenceExpression

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());
    }
}
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) WindowEmitStrategy(org.apache.flink.table.planner.plan.utils.WindowEmitStrategy) GeneratedNamespaceAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedNamespaceAggsHandleFunction) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) WindowOperatorBuilder(org.apache.flink.table.runtime.operators.window.WindowOperatorBuilder) GeneratedNamespaceTableAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedNamespaceTableAggsHandleFunction) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 13 with FieldReferenceExpression

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);
}
Also used : AtomicDataType(org.apache.flink.table.types.AtomicDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression)

Example 14 with FieldReferenceExpression

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;
}
Also used : AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) CountAggFunction(org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction) ArrayList(java.util.ArrayList) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) AggregateExpression(org.apache.flink.table.expressions.AggregateExpression) AvgAggFunction(org.apache.flink.table.planner.functions.aggfunctions.AvgAggFunction) DataType(org.apache.flink.table.types.DataType) AggregateInfo(org.apache.flink.table.planner.plan.utils.AggregateInfo) Sum0AggFunction(org.apache.flink.table.planner.functions.aggfunctions.Sum0AggFunction)

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