Search in sources :

Example 6 with ValueLiteralExpression

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

the class TrimConverter method convert.

@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
    checkArgumentNumber(call, 4);
    List<Expression> children = call.getChildren();
    ValueLiteralExpression removeLeadingExpr = (ValueLiteralExpression) children.get(0);
    Boolean removeLeading = extractValue(removeLeadingExpr, Boolean.class);
    ValueLiteralExpression removeTrailingExpr = (ValueLiteralExpression) children.get(1);
    Boolean removeTrailing = extractValue(removeTrailingExpr, Boolean.class);
    RexNode trimString = context.toRexNode(children.get(2));
    RexNode str = context.toRexNode(children.get(3));
    Enum trimMode;
    if (removeLeading && removeTrailing) {
        trimMode = SqlTrimFunction.Flag.BOTH;
    } else if (removeLeading) {
        trimMode = SqlTrimFunction.Flag.LEADING;
    } else if (removeTrailing) {
        trimMode = SqlTrimFunction.Flag.TRAILING;
    } else {
        throw new IllegalArgumentException("Unsupported trim mode.");
    }
    return context.getRelBuilder().call(FlinkSqlOperatorTable.TRIM, context.getRelBuilder().getRexBuilder().makeFlag(trimMode), trimString, str);
}
Also used : 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) RexNode(org.apache.calcite.rex.RexNode)

Example 7 with ValueLiteralExpression

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

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

Example 9 with ValueLiteralExpression

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

the class LogicalWindowJsonSerializer method serialize.

@Override
public void serialize(LogicalWindow logicalWindow, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
    gen.writeStartObject();
    if (logicalWindow instanceof TumblingGroupWindow) {
        TumblingGroupWindow window = (TumblingGroupWindow) logicalWindow;
        gen.writeStringField(FIELD_NAME_KIND, KIND_TUMBLING);
        serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
        FieldReferenceExpression timeField = logicalWindow.timeAttribute();
        serializeFieldReferenceExpression(timeField, gen, serializerProvider);
        ValueLiteralExpression size = window.size();
        if (hasTimeIntervalType(size)) {
            Duration duration = toDuration(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, true);
            gen.writeObjectField(FIELD_NAME_SIZE, duration);
        } else {
            Long duration = toLong(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, false);
            gen.writeNumberField(FIELD_NAME_SIZE, duration);
        }
    } else if (logicalWindow instanceof SlidingGroupWindow) {
        SlidingGroupWindow window = (SlidingGroupWindow) logicalWindow;
        gen.writeStringField(FIELD_NAME_KIND, KIND_SLIDING);
        serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
        serializeFieldReferenceExpression(window.timeAttribute(), gen, serializerProvider);
        ValueLiteralExpression size = window.size();
        if (hasTimeIntervalType(size)) {
            Duration duration = toDuration(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, true);
            gen.writeObjectField(FIELD_NAME_SIZE, duration);
            gen.writeObjectField(FIELD_NAME_SLIDE, toDuration(window.slide()));
        } else {
            Long duration = toLong(size);
            gen.writeBooleanField(FIELD_NAME_IS_TIME_WINDOW, false);
            gen.writeObjectField(FIELD_NAME_SIZE, duration);
            gen.writeObjectField(FIELD_NAME_SLIDE, toLong(window.slide()));
        }
    } else if (logicalWindow instanceof SessionGroupWindow) {
        gen.writeStringField(FIELD_NAME_KIND, KIND_SESSION);
        SessionGroupWindow window = (SessionGroupWindow) logicalWindow;
        serializerProvider.defaultSerializeField(FIELD_NAME_ALIAS, window.aliasAttribute(), gen);
        serializeFieldReferenceExpression(window.timeAttribute(), gen, serializerProvider);
        gen.writeObjectField(FIELD_NAME_GAP, toDuration(window.gap()));
    } else {
        throw new TableException("Unknown LogicalWindow: " + logicalWindow);
    }
    gen.writeEndObject();
}
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) AggregateUtil.toLong(org.apache.flink.table.planner.plan.utils.AggregateUtil.toLong) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) Duration(java.time.Duration) AggregateUtil.toDuration(org.apache.flink.table.planner.plan.utils.AggregateUtil.toDuration) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow)

Example 10 with ValueLiteralExpression

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

the class OrcFileSystemFilterTest method testApplyPredicate.

@Test
@SuppressWarnings("unchecked")
public void testApplyPredicate() {
    List<ResolvedExpression> args = new ArrayList<>();
    // equal
    FieldReferenceExpression fieldReferenceExpression = new FieldReferenceExpression("long1", DataTypes.BIGINT(), 0, 0);
    ValueLiteralExpression valueLiteralExpression = new ValueLiteralExpression(10);
    args.add(fieldReferenceExpression);
    args.add(valueLiteralExpression);
    CallExpression equalExpression = CallExpression.permanent(BuiltInFunctionDefinitions.EQUALS, args, DataTypes.BOOLEAN());
    OrcFilters.Predicate predicate1 = OrcFilters.toOrcPredicate(equalExpression);
    OrcFilters.Predicate predicate2 = new OrcFilters.Equals("long1", PredicateLeaf.Type.LONG, 10);
    assertTrue(predicate1.toString().equals(predicate2.toString()));
    // greater than
    CallExpression greaterExpression = CallExpression.permanent(BuiltInFunctionDefinitions.GREATER_THAN, args, DataTypes.BOOLEAN());
    OrcFilters.Predicate predicate3 = OrcFilters.toOrcPredicate(greaterExpression);
    OrcFilters.Predicate predicate4 = new OrcFilters.Not(new OrcFilters.LessThanEquals("long1", PredicateLeaf.Type.LONG, 10));
    assertTrue(predicate3.toString().equals(predicate4.toString()));
    // less than
    CallExpression lessExpression = CallExpression.permanent(BuiltInFunctionDefinitions.LESS_THAN, args, DataTypes.BOOLEAN());
    OrcFilters.Predicate predicate5 = OrcFilters.toOrcPredicate(lessExpression);
    OrcFilters.Predicate predicate6 = new OrcFilters.LessThan("long1", PredicateLeaf.Type.LONG, 10);
    assertTrue(predicate5.toString().equals(predicate6.toString()));
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ArrayList(java.util.ArrayList) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) CallExpression(org.apache.flink.table.expressions.CallExpression) Test(org.junit.Test)

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