Search in sources :

Example 1 with ValueLiteralExpression

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

the class StreamExecGroupWindowAggregate method createAggsHandler.

private GeneratedClass<?> createAggsHandler(AggregateInfoList aggInfoList, ExecNodeConfig config, RelBuilder relBuilder, List<LogicalType> fieldTypes, ZoneId shiftTimeZone) {
    final boolean needMerge;
    final Class<?> windowClass;
    if (window instanceof SlidingGroupWindow) {
        ValueLiteralExpression size = ((SlidingGroupWindow) window).size();
        needMerge = hasTimeIntervalType(size);
        windowClass = hasRowIntervalType(size) ? CountWindow.class : TimeWindow.class;
    } else if (window instanceof TumblingGroupWindow) {
        needMerge = false;
        ValueLiteralExpression size = ((TumblingGroupWindow) window).size();
        windowClass = hasRowIntervalType(size) ? CountWindow.class : TimeWindow.class;
    } else if (window instanceof SessionGroupWindow) {
        needMerge = true;
        windowClass = TimeWindow.class;
    } else {
        throw new TableException("Unsupported window: " + window.toString());
    }
    final AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), relBuilder, JavaScalaConversionUtil.toScala(fieldTypes), // copyInputField
    false).needAccumulate();
    if (needMerge) {
        generator.needMerge(0, false, null);
    }
    if (needRetraction) {
        generator.needRetract();
    }
    final List<WindowProperty> windowProperties = Arrays.asList(Arrays.stream(namedWindowProperties).map(NamedWindowProperty::getProperty).toArray(WindowProperty[]::new));
    final boolean isTableAggregate = isTableAggregate(Arrays.asList(aggInfoList.getActualAggregateCalls()));
    if (isTableAggregate) {
        return generator.generateNamespaceTableAggsHandler("GroupingWindowTableAggsHandler", aggInfoList, JavaScalaConversionUtil.toScala(windowProperties), windowClass, shiftTimeZone);
    } else {
        return generator.generateNamespaceAggsHandler("GroupingWindowAggsHandler", aggInfoList, JavaScalaConversionUtil.toScala(windowProperties), windowClass, shiftTimeZone);
    }
}
Also used : NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) CountWindow(org.apache.flink.table.runtime.operators.window.CountWindow) TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) TimeWindow(org.apache.flink.table.runtime.operators.window.TimeWindow) NamedWindowProperty(org.apache.flink.table.runtime.groupwindow.NamedWindowProperty) WindowProperty(org.apache.flink.table.runtime.groupwindow.WindowProperty) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 2 with ValueLiteralExpression

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

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

the class AggregateOperationFactory method validateAndCreateSessionWindow.

private ResolvedGroupWindow validateAndCreateSessionWindow(SessionWithGapOnTimeWithAlias window, String windowName, FieldReferenceExpression timeField) {
    ValueLiteralExpression windowGap = getAsValueLiteral(window.getGap(), "A session window expects a gap value literal.");
    final LogicalType windowGapType = windowGap.getOutputDataType().getLogicalType();
    if (!windowGapType.is(INTERVAL_DAY_TIME)) {
        throw new ValidationException("A session window expects a gap literal of a day-time interval type.");
    }
    return ResolvedGroupWindow.sessionWindow(windowName, timeField, windowGap);
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ValidationException(org.apache.flink.table.api.ValidationException) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 4 with ValueLiteralExpression

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

the class AliasOperationUtils method createAliasList.

/**
 * Creates a list of valid alias expressions. Resulting expression might still contain {@link
 * UnresolvedReferenceExpression}.
 *
 * @param aliases aliases to validate
 * @param child relational operation on top of which to apply the aliases
 * @return validated list of aliases
 */
static List<Expression> createAliasList(List<Expression> aliases, QueryOperation child) {
    ResolvedSchema childSchema = child.getResolvedSchema();
    if (aliases.size() > childSchema.getColumnCount()) {
        throw new ValidationException("Aliasing more fields than we actually have.");
    }
    List<ValueLiteralExpression> fieldAliases = aliases.stream().map(f -> f.accept(aliasLiteralValidator)).collect(Collectors.toList());
    List<String> childNames = childSchema.getColumnNames();
    return IntStream.range(0, childNames.size()).mapToObj(idx -> {
        UnresolvedReferenceExpression oldField = unresolvedRef(childNames.get(idx));
        if (idx < fieldAliases.size()) {
            ValueLiteralExpression alias = fieldAliases.get(idx);
            return unresolvedCall(BuiltInFunctionDefinitions.AS, oldField, alias);
        } else {
            return oldField;
        }
    }).collect(Collectors.toList());
}
Also used : IntStream(java.util.stream.IntStream) QueryOperation(org.apache.flink.table.operations.QueryOperation) ApiExpressionUtils.valueLiteral(org.apache.flink.table.expressions.ApiExpressionUtils.valueLiteral) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) ExpressionUtils(org.apache.flink.table.expressions.ExpressionUtils) ApiExpressionDefaultVisitor(org.apache.flink.table.expressions.utils.ApiExpressionDefaultVisitor) BuiltInFunctionDefinitions(org.apache.flink.table.functions.BuiltInFunctionDefinitions) ApiExpressionUtils.unresolvedCall(org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedCall) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) Collectors(java.util.stream.Collectors) ApiExpressionUtils.unresolvedRef(org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedRef) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) Internal(org.apache.flink.annotation.Internal) ValidationException(org.apache.flink.table.api.ValidationException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema)

Example 5 with ValueLiteralExpression

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

the class OverConvertRule method createBound.

private RexWindowBound createBound(ConvertContext context, Expression bound, SqlKind sqlKind) {
    if (bound instanceof CallExpression) {
        CallExpression callExpr = (CallExpression) bound;
        FunctionDefinition func = callExpr.getFunctionDefinition();
        if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE.equals(func)) {
            SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO) : SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
            return RexWindowBound.create(unbounded, null);
        } else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE.equals(func)) {
            SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
            return RexWindowBound.create(currentRow, null);
        } else {
            throw new IllegalArgumentException("Unexpected expression: " + bound);
        }
    } else if (bound instanceof ValueLiteralExpression) {
        RelDataType returnType = context.getTypeFactory().createFieldTypeFromLogicalType(new DecimalType(true, 19, 0));
        SqlOperator sqlOperator = new SqlPostfixOperator(sqlKind.name(), sqlKind, 2, new OrdinalReturnTypeInference(0), null, null);
        SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) };
        SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO);
        ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound;
        RexNode literalRexNode = literalExpr.getValueAs(BigDecimal.class).map(v -> context.getRelBuilder().literal(v)).orElse(context.getRelBuilder().literal(extractValue(literalExpr, Object.class)));
        List<RexNode> expressions = new ArrayList<>();
        expressions.add(literalRexNode);
        RexNode rexNode = context.getRelBuilder().getRexBuilder().makeCall(returnType, sqlOperator, expressions);
        return RexWindowBound.create(node, rexNode);
    } else {
        throw new TableException("Unexpected expression: " + bound);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlPostfixOperator(org.apache.calcite.sql.SqlPostfixOperator) OrdinalReturnTypeInference(org.apache.calcite.sql.type.OrdinalReturnTypeInference) DecimalType(org.apache.flink.table.types.logical.DecimalType) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) CallExpression(org.apache.flink.table.expressions.CallExpression) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

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