Search in sources :

Example 16 with ValueLiteralExpression

use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.

the class JsonObjectConverter method convert.

@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
    checkArgument(call, (call.getChildren().size() - 1) % 2 == 0);
    final List<RexNode> operands = new LinkedList<>();
    final SqlJsonConstructorNullClause onNull = JsonConverterUtil.getOnNullArgument(call, 0);
    operands.add(context.getRelBuilder().getRexBuilder().makeFlag(onNull));
    for (int i = 1; i < call.getChildren().size(); i++) {
        final Expression operand = call.getChildren().get(i);
        if (i % 2 == 1 && !(operand instanceof ValueLiteralExpression)) {
            throw new TableException(String.format("Argument at position %s must be a string literal.", i));
        }
        operands.add(context.toRexNode(operand));
    }
    return context.getRelBuilder().getRexBuilder().makeCall(FlinkSqlOperatorTable.JSON_OBJECT, operands);
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlJsonConstructorNullClause(org.apache.calcite.sql.SqlJsonConstructorNullClause) LinkedList(java.util.LinkedList) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with ValueLiteralExpression

use of org.apache.flink.table.expressions.ValueLiteralExpression 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 18 with ValueLiteralExpression

use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.

the class StreamExecPythonGroupWindowAggregate method getGeneralPythonStreamGroupWindowAggregateFunctionOperator.

@SuppressWarnings({ "unchecked", "rawtypes" })
private OneInputStreamOperator<RowData, RowData> getGeneralPythonStreamGroupWindowAggregateFunctionOperator(Configuration config, RowType inputType, RowType outputType, WindowAssigner<?> windowAssigner, PythonAggregateFunctionInfo[] aggregateFunctions, DataViewSpec[][] dataViewSpecs, int inputTimeFieldIndex, int indexOfCountStar, boolean generateUpdateBefore, boolean countStarInserted, long allowance, ZoneId shiftTimeZone) {
    Class clazz = CommonPythonUtil.loadClass(GENERAL_STREAM_PYTHON_GROUP_WINDOW_AGGREGATE_FUNCTION_OPERATOR_NAME);
    boolean isRowTime = AggregateUtil.isRowtimeAttribute(window.timeAttribute());
    try {
        if (window instanceof TumblingGroupWindow) {
            ValueLiteralExpression size = ((TumblingGroupWindow) window).size();
            Method create = clazz.getMethod(GENERAL_STREAM_PYTHON_CREATE_TUMBLING_GROUP_WINDOW_METHOD, Configuration.class, RowType.class, RowType.class, PythonAggregateFunctionInfo[].class, DataViewSpec[][].class, int[].class, int.class, boolean.class, boolean.class, int.class, WindowAssigner.class, boolean.class, boolean.class, long.class, long.class, NamedWindowProperty[].class, ZoneId.class);
            return (OneInputStreamOperator<RowData, RowData>) create.invoke(null, config, inputType, outputType, aggregateFunctions, dataViewSpecs, grouping, indexOfCountStar, generateUpdateBefore, countStarInserted, inputTimeFieldIndex, windowAssigner, isRowTime, AggregateUtil.hasTimeIntervalType(size), AggregateUtil.toDuration(size).toMillis(), allowance, namedWindowProperties, shiftTimeZone);
        } else if (window instanceof SlidingGroupWindow) {
            ValueLiteralExpression size = ((SlidingGroupWindow) window).size();
            ValueLiteralExpression slide = ((SlidingGroupWindow) window).slide();
            Method create = clazz.getMethod(GENERAL_STREAM_PYTHON_CREATE_SLIDING_GROUP_WINDOW_METHOD, Configuration.class, RowType.class, RowType.class, PythonAggregateFunctionInfo[].class, DataViewSpec[][].class, int[].class, int.class, boolean.class, boolean.class, int.class, WindowAssigner.class, boolean.class, boolean.class, long.class, long.class, long.class, NamedWindowProperty[].class, ZoneId.class);
            return (OneInputStreamOperator<RowData, RowData>) create.invoke(null, config, inputType, outputType, aggregateFunctions, dataViewSpecs, grouping, indexOfCountStar, generateUpdateBefore, countStarInserted, inputTimeFieldIndex, windowAssigner, isRowTime, AggregateUtil.hasTimeIntervalType(size), AggregateUtil.toDuration(size).toMillis(), AggregateUtil.toDuration(slide).toMillis(), allowance, namedWindowProperties, shiftTimeZone);
        } else if (window instanceof SessionGroupWindow) {
            ValueLiteralExpression gap = ((SessionGroupWindow) window).gap();
            Method create = clazz.getMethod(GENERAL_STREAM_PYTHON_CREATE_SESSION_GROUP_WINDOW_METHOD, Configuration.class, RowType.class, RowType.class, PythonAggregateFunctionInfo[].class, DataViewSpec[][].class, int[].class, int.class, boolean.class, boolean.class, int.class, WindowAssigner.class, boolean.class, long.class, long.class, NamedWindowProperty[].class, ZoneId.class);
            return (OneInputStreamOperator<RowData, RowData>) create.invoke(null, config, inputType, outputType, aggregateFunctions, dataViewSpecs, grouping, indexOfCountStar, generateUpdateBefore, countStarInserted, inputTimeFieldIndex, windowAssigner, isRowTime, AggregateUtil.toDuration(gap).toMillis(), allowance, namedWindowProperties, shiftTimeZone);
        }
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new TableException("Python PythonStreamGroupWindowAggregateOperator constructed failed.", e);
    }
    throw new RuntimeException(String.format("Unsupported LogicWindow Type %s", window));
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) Configuration(org.apache.flink.configuration.Configuration) DataViewSpec(org.apache.flink.table.runtime.dataview.DataViewSpec) ZoneId(java.time.ZoneId) RowType(org.apache.flink.table.types.logical.RowType) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SlidingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.SlidingWindowAssigner) CountTumblingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CountTumblingWindowAssigner) WindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.WindowAssigner) TumblingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.TumblingWindowAssigner) CountSlidingWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.CountSlidingWindowAssigner) SessionWindowAssigner(org.apache.flink.table.runtime.operators.window.assigners.SessionWindowAssigner) RowData(org.apache.flink.table.data.RowData) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) PythonAggregateFunctionInfo(org.apache.flink.table.functions.python.PythonAggregateFunctionInfo) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Aggregations

ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)18 TableException (org.apache.flink.table.api.TableException)9 CallExpression (org.apache.flink.table.expressions.CallExpression)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)6 SessionGroupWindow (org.apache.flink.table.planner.plan.logical.SessionGroupWindow)6 SlidingGroupWindow (org.apache.flink.table.planner.plan.logical.SlidingGroupWindow)6 TumblingGroupWindow (org.apache.flink.table.planner.plan.logical.TumblingGroupWindow)6 RexNode (org.apache.calcite.rex.RexNode)5 ValidationException (org.apache.flink.table.api.ValidationException)4 Expression (org.apache.flink.table.expressions.Expression)3 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)3 LogicalType (org.apache.flink.table.types.logical.LogicalType)3 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 NamedWindowProperty (org.apache.flink.table.runtime.groupwindow.NamedWindowProperty)2 ImmutableList (com.google.common.collect.ImmutableList)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1