Search in sources :

Example 6 with FieldReferenceExpression

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

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

the class HiveTableUtilTest method testMakePartitionFilter.

@Test
public void testMakePartitionFilter() {
    List<String> partColNames = Arrays.asList("p1", "p2", "p3");
    ResolvedExpression p1Ref = new FieldReferenceExpression("p1", DataTypes.INT(), 0, 2);
    ResolvedExpression p2Ref = new FieldReferenceExpression("p2", DataTypes.STRING(), 0, 3);
    ResolvedExpression p3Ref = new FieldReferenceExpression("p3", DataTypes.DOUBLE(), 0, 4);
    ResolvedExpression p1Exp = CallExpression.permanent(BuiltInFunctionDefinitions.EQUALS, Arrays.asList(p1Ref, valueLiteral(1)), DataTypes.BOOLEAN());
    ResolvedExpression p2Exp = CallExpression.permanent(BuiltInFunctionDefinitions.EQUALS, Arrays.asList(p2Ref, valueLiteral("a", DataTypes.STRING().notNull())), DataTypes.BOOLEAN());
    ResolvedExpression p3Exp = CallExpression.permanent(BuiltInFunctionDefinitions.EQUALS, Arrays.asList(p3Ref, valueLiteral(1.1)), DataTypes.BOOLEAN());
    Optional<String> filter = HiveTableUtil.makePartitionFilter(2, partColNames, Arrays.asList(p1Exp), hiveShim);
    assertEquals("(p1 = 1)", filter.orElse(null));
    filter = HiveTableUtil.makePartitionFilter(2, partColNames, Arrays.asList(p1Exp, p3Exp), hiveShim);
    assertEquals("(p1 = 1) and (p3 = 1.1)", filter.orElse(null));
    filter = HiveTableUtil.makePartitionFilter(2, partColNames, Arrays.asList(p2Exp, CallExpression.permanent(BuiltInFunctionDefinitions.OR, Arrays.asList(p1Exp, p3Exp), DataTypes.BOOLEAN())), hiveShim);
    assertEquals("(p2 = 'a') and ((p1 = 1) or (p3 = 1.1))", filter.orElse(null));
}
Also used : ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) Test(org.junit.Test)

Example 8 with FieldReferenceExpression

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

Example 9 with FieldReferenceExpression

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

the class QueryOperationTest method testSummaryString.

@Test
public void testSummaryString() {
    ResolvedSchema schema = ResolvedSchema.physical(Collections.singletonList("a"), Collections.singletonList(DataTypes.INT()));
    ProjectQueryOperation tableOperation = new ProjectQueryOperation(Collections.singletonList(new FieldReferenceExpression("a", DataTypes.INT(), 0, 0)), new SourceQueryOperation(ContextResolvedTable.temporary(ObjectIdentifier.of("cat1", "db1", "tab1"), new ResolvedCatalogTable(CatalogTable.of(Schema.newBuilder().build(), null, Collections.emptyList(), Collections.emptyMap()), schema))), schema);
    SetQueryOperation unionQueryOperation = new SetQueryOperation(tableOperation, tableOperation, SetQueryOperation.SetQueryOperationType.UNION, true, schema);
    assertEquals("Union: (all: [true])\n" + "    Project: (projections: [a])\n" + "        CatalogTable: (identifier: [cat1.db1.tab1], fields: [a])\n" + "    Project: (projections: [a])\n" + "        CatalogTable: (identifier: [cat1.db1.tab1], fields: [a])", unionQueryOperation.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 10 with FieldReferenceExpression

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

the class AggregateOperationFactory method getValidatedTimeAttribute.

private FieldReferenceExpression getValidatedTimeAttribute(GroupWindow window, ExpressionResolver resolver) {
    List<ResolvedExpression> timeFieldExprs = resolver.resolve(singletonList(window.getTimeField()));
    if (timeFieldExprs.size() != 1) {
        throw new ValidationException("A group window only supports a single time field column.");
    }
    Expression timeFieldExpr = timeFieldExprs.get(0);
    if (!(timeFieldExpr instanceof FieldReferenceExpression)) {
        throw new ValidationException("A group window expects a time attribute for grouping.");
    }
    FieldReferenceExpression timeField = (FieldReferenceExpression) timeFieldExpr;
    final LogicalType timeFieldType = timeField.getOutputDataType().getLogicalType();
    validateTimeAttributeType(timeFieldType);
    return timeField;
}
Also used : 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) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) LogicalType(org.apache.flink.table.types.logical.LogicalType)

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