dawidwys commented on a change in pull request #8018: [FLINK-11884][table] Separate creation and validation of LogicalNodes from TableImpl URL: https://github.com/apache/flink/pull/8018#discussion_r269561868
########## File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/ExpressionExtractionUtils.java ########## @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.expressions; + +import org.apache.flink.api.java.tuple.Tuple2; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +import static org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedFieldRef; +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.PROCTIME; +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.ROWTIME; +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.WINDOW_END; +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.WINDOW_START; +import static org.apache.flink.table.expressions.FunctionDefinition.Type.AGGREGATE_FUNCTION; + +/** + * Utilities for splitting {@link Expression} into subcategories, e.g. into projections, aggregations, window properties + * etc. + */ +public final class ExpressionExtractionUtils { + + /** + * Extracts and deduplicates all aggregation and window property expressions (zero, one, or more) + * from the given expressions. + * + * @param expressions a list of expressions to extract + * @param uniqueAttributeGenerator a supplier that every time returns a unique attribute + * @return a Tuple2, the first field contains the extracted and deduplicated aggregations, + * and the second field contains the extracted and deduplicated window properties. + */ + public static Tuple2<Map<Expression, String>, Map<Expression, String>> extractAggregationsAndProperties( + List<Expression> expressions, + Supplier<String> uniqueAttributeGenerator) { + AggregationAndPropertiesSplitter splitter = new AggregationAndPropertiesSplitter(uniqueAttributeGenerator); + expressions.forEach(expr -> expr.accept(splitter)); + + return Tuple2.of(splitter.aggregates, splitter.properties); + } + + private static final Set<FunctionDefinition> WINDOW_PROPERTIES = new HashSet<>(Arrays.asList( + WINDOW_START, WINDOW_END, PROCTIME, ROWTIME + )); + + private static class AggregationAndPropertiesSplitter extends DefaultExpressionVisitor<Void> { + + private final Map<Expression, String> aggregates = new LinkedHashMap<>(); + private final Map<Expression, String> properties = new LinkedHashMap<>(); + private final Supplier<String> uniqueAttributeGenerator; + + private AggregationAndPropertiesSplitter(Supplier<String> uniqueAttributeGenerator) { + this.uniqueAttributeGenerator = uniqueAttributeGenerator; + } + + @Override + public Void visitLookupCall(LookupCallExpression unresolvedCall) { + throw new IllegalStateException("All calls should be resolved by now. Got: " + unresolvedCall); + } + + @Override + public Void visitCall(CallExpression call) { + FunctionDefinition functionDefinition = call.getFunctionDefinition(); + if (functionDefinition.getType() == AGGREGATE_FUNCTION) { + aggregates.computeIfAbsent(call, expr -> uniqueAttributeGenerator.get()); + } else if (WINDOW_PROPERTIES.contains(functionDefinition)) { + properties.computeIfAbsent(call, expr -> uniqueAttributeGenerator.get()); + } else { + call.getChildren().forEach(c -> c.accept(this)); + } + return null; + } + + @Override + protected Void defaultMethod(Expression expression) { + return null; + } + } + + /** + * Replaces expressions with deduplicated aggregations and properties. + * + * @param expressions a list of expressions to replace + * @param aggNames the deduplicated aggregations + * @param propNames the deduplicated properties + * @return a list of replaced expressions + */ + public static List<Expression> replaceAggregationsAndProperties( + List<Expression> expressions, + Map<Expression, String> aggNames, + Map<Expression, String> propNames) { + AggregationAndPropertiesReplacer replacer = new AggregationAndPropertiesReplacer(aggNames, propNames); + return expressions.stream() + .map(expr -> expr.accept(replacer)) + .collect(Collectors.toList()); + } + + private static class AggregationAndPropertiesReplacer extends DefaultExpressionVisitor<Expression> { + + private final Map<Expression, String> aggregates; + private final Map<Expression, String> properties; + + private AggregationAndPropertiesReplacer( + Map<Expression, String> aggregates, + Map<Expression, String> properties) { + this.aggregates = aggregates; + this.properties = properties; + } + + @Override + public Expression visitLookupCall(LookupCallExpression unresolvedCall) { + throw new IllegalStateException("All calls should be resolved by now. Got: " + unresolvedCall); + } + + @Override + public Expression visitCall(CallExpression call) { + if (aggregates.get(call) != null) { + return unresolvedFieldRef(aggregates.get(call)); + } else if (properties.get(call) != null) { + return unresolvedFieldRef(properties.get(call)); + } + + List<Expression> args = call.getChildren() + .stream() + .map(c -> c.accept(this)) + .collect(Collectors.toList()); + return new CallExpression(call.getFunctionDefinition(), args); + } + + @Override + protected Expression defaultMethod(Expression expression) { + return expression; + } + } + + /** + * Extract all field references from the given expressions. + * + * @param expressions a list of expressions to extract + * @return a list of field references extracted from the given expressions + */ + public static List<Expression> extractFieldReferences(List<Expression> expressions) { + FieldReferenceExtractor referenceExtractor = new FieldReferenceExtractor(); + return expressions.stream() + .flatMap(expr -> expr.accept(referenceExtractor).stream()) + .distinct() + .collect(Collectors.toList()); + } + + private static class FieldReferenceExtractor extends DefaultExpressionVisitor<List<Expression>> { + + @Override + public List<Expression> visitCall(CallExpression call) { + FunctionDefinition functionDefinition = call.getFunctionDefinition(); + if (WINDOW_PROPERTIES.contains(functionDefinition)) { + return Collections.emptyList(); + } else { + return call.getChildren() + .stream() + .flatMap(c -> c.accept(this).stream()) + .distinct() + .collect(Collectors.toList()); + } + } + + @Override + public List<Expression> visitLookupCall(LookupCallExpression unresolvedCall) { + throw new IllegalStateException("All calls should be resolved by now. Got: " + unresolvedCall); + } + + @Override + public List<Expression> visitFieldReference(FieldReferenceExpression fieldReference) { + return Collections.singletonList(fieldReference); + } + + @Override + public List<Expression> visitUnresolvedField(UnresolvedFieldReferenceExpression fieldReference) { + return Collections.singletonList(fieldReference); + } + + @Override + protected List<Expression> defaultMethod(Expression expression) { + return Collections.emptyList(); + } + } + + private ExpressionExtractionUtils() { Review comment: Agree if there are variables, multiple ctors etc. This is a special case though. It will never have member variables, no non-static functions and always a single private empty ctor. For me Utils name is enough to say the class should not be instantiable. Whenever I see Utils in the name of the class. The first place where I look the private ctor is at the end. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
