dawidwys commented on a change in pull request #8087: [FLINK-12029][table] Add column operations for TableApi URL: https://github.com/apache/flink/pull/8087#discussion_r274313795
########## File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/expressions/ColumnsOperationExpander.java ########## @@ -0,0 +1,177 @@ +/* + * 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.annotation.Internal; +import org.apache.flink.api.common.typeinfo.IntegerTypeInfo; +import org.apache.flink.table.api.TableException; +import org.apache.flink.util.Preconditions; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.COLUMNS; +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.MINUS_PREFIX; +import static org.apache.flink.table.expressions.BuiltInFunctionDefinitions.RANGE_TO; + +/** + * Expands column expressions to it's real parent's input references. + */ +@Internal +public class ColumnsOperationExpander extends ApiExpressionDefaultVisitor<List<Expression>> { + + private final List<UnresolvedReferenceExpression> inputFieldReferences; + + public ColumnsOperationExpander(List<UnresolvedReferenceExpression> inputFieldReferences) { + this.inputFieldReferences = inputFieldReferences; + } + + @Override + public List<Expression> visitCall(CallExpression call) { + + List<Expression> result; + + String definitionName = call.getFunctionDefinition().getName(); + if (definitionName.equals(COLUMNS.getName())) { + result = resolveArgsOfColumns(call.getChildren(), false); + } else if (definitionName.equals(MINUS_PREFIX.getName()) + && call.getChildren().get(0) instanceof CallExpression + && ((CallExpression) call.getChildren().get(0)).getFunctionDefinition().getName() + .equals(COLUMNS.getName())) { + + result = resolveArgsOfColumns(call.getChildren().get(0).getChildren(), true); + } else { + List<Expression> args = call.getChildren() + .stream() + .flatMap(c -> c.accept(this).stream()) + .collect(Collectors.toList()); + result = Collections.singletonList(new CallExpression(call.getFunctionDefinition(), args)); + } + + return result; + } + + @Override + protected List<Expression> defaultMethod(Expression expression) { + return Collections.singletonList(expression); + } + + /** + * Expand the columns expression in the input Expression List. + */ + private List<Expression> resolveArgsOfColumns(List<Expression> args, boolean isReverseProjection) { + + List<Expression> finalResult = new LinkedList<>(); + List<UnresolvedReferenceExpression> result = args.stream() + .flatMap(e -> resolveSingleArg(e).stream()) + .collect(Collectors.toList()); + + if (isReverseProjection) { + for (UnresolvedReferenceExpression field: inputFieldReferences) { + if (indexOfName(result, field.getName()) == -1) { + finalResult.add(field); + } + } + } else { + finalResult.addAll(result); + } + + return finalResult; + } + + /** + * Expand the columns expression in the input Expression. + */ + private List<UnresolvedReferenceExpression> resolveSingleArg(Expression arg) { Review comment: Could we convert this method to another Visitor? ---------------------------------------------------------------- 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
