twalthr 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_r269508344
########## 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; Review comment: side comment: it would be great if we can infer the unique attribute differently in the future. Most SQL vendors (including Calcite?) use information from the expression itself to compute unique attributes. ---------------------------------------------------------------- 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
