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_r269492459
########## File path: flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/OperationTreeBuilder.scala ########## @@ -0,0 +1,317 @@ +/* + * 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.plan + +import java.util.{Optional, List => JList, Map => JMap} + +import org.apache.flink.api.java.operators.join.JoinType +import org.apache.flink.table.api._ +import org.apache.flink.table.expressions.{Alias, Asc, Expression, Ordering, + PlannerExpressionConverter, ResolvedFieldReference, UnresolvedAlias, WindowProperty} +import org.apache.flink.table.functions.utils.UserDefinedFunctionUtils +import org.apache.flink.table.functions.{TemporalTableFunction, TemporalTableFunctionImpl} +import org.apache.flink.table.plan.ProjectionTranslator.{expandProjectList, flattenExpression, + resolveOverWindows} +import org.apache.flink.table.plan.logical._ +import org.apache.flink.table.util.JavaScalaConversionUtil.toScala + +import _root_.scala.collection.JavaConverters._ + +/** + * Builder for [[[Operation]] tree. + */ +class OperationTreeBuilder(private val tableEnv: TableEnvironment) { + + private val expressionBridge: PlannerExpressionConverter = PlannerExpressionConverter.INSTANCE + + def project( + projectList: JList[Expression], + child: Operation, + explicitAlias: Boolean = false) + : Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + + val convertedProjectList = projectList.asScala + .map(expr => expr.accept(expressionBridge)) + .flatMap(expr => flattenExpression(expr, childNode, tableEnv)) + .map(UnresolvedAlias).toList + + Project(convertedProjectList, childNode, explicitAlias).validate(tableEnv) + } + + def aggregate( + groupingExpressions: JList[Expression], + namedAggregates: JMap[Expression, String], + child: Operation) + : Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + + val convertedGroupings = groupingExpressions.asScala + .map(expr => expr.accept(expressionBridge)) + + val convertedAggregates = namedAggregates.asScala + .map(a => Alias(a._1.accept(expressionBridge), a._2)).toSeq + + Aggregate(convertedGroupings, convertedAggregates, childNode).validate(tableEnv) + } + + def windowAggregate( + groupingExpressions: JList[Expression], + window: GroupWindow, + namedProperties: JMap[Expression, String], + namedAggregates: JMap[Expression, String], + child: Operation) + : Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + + val convertedGroupings = groupingExpressions.asScala + .map(expr => expr.accept(expressionBridge)) + + val convertedAggregates = namedAggregates.asScala + .map(a => Alias(a._1.accept(expressionBridge), a._2)).toSeq + + val convertedProperties = namedProperties.asScala + .map(a => Alias(a._1.accept(expressionBridge), a._2)).toSeq + + WindowAggregate( + convertedGroupings, + createLogicalWindow(window), + convertedProperties, + convertedAggregates, + childNode) + .validate(tableEnv) + } + + def project( + projectList: JList[Expression], + child: Operation, + overWindows: JList[OverWindow]) + : Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + + val expandedFields = expandProjectList( + projectList.asScala.map(expr => expr.accept(expressionBridge)), + childNode, + tableEnv) + + if (expandedFields.exists(_.isInstanceOf[WindowProperty])){ + throw new ValidationException( + "Window start and end properties are not available for Over windows.") + } + + val expandedOverFields = resolveOverWindows( + expandedFields, + overWindows.asScala.map(createLogicalWindow)) + .map(UnresolvedAlias) + + Project(expandedOverFields, childNode, explicitAlias = true).validate(tableEnv) + } + + def join( + left: Operation, + right: Operation, + joinType: JoinType, + condition: Optional[Expression], + correlated: Boolean) + : Operation = { + Join( + left.asInstanceOf[LogicalNode], + right.asInstanceOf[LogicalNode], + joinType, + toScala(condition).map(_.accept(expressionBridge)), + correlated).validate(tableEnv) + } + + def joinLateral( + left: Operation, + tableFunction: Expression, + joinType: JoinType, + condition: Optional[Expression]) + : Operation = { + + val leftNode = left.asInstanceOf[LogicalNode] + + val temporalTable = UserDefinedFunctionUtils.createLogicalFunctionCall( + tableFunction.accept(expressionBridge), + leftNode).validate(tableEnv) + + join(left, temporalTable, joinType, condition, correlated = true) + } + + def createTemporalTableFunction( + timeAttribute: Expression, + primaryKey: Expression, + underlyingOperation: Operation) + : TemporalTableFunction = { + val underlyingOperationNode = underlyingOperation.asInstanceOf[LogicalNode] + val temporalTable = TemporalTable(timeAttribute.accept(expressionBridge), + primaryKey.accept(expressionBridge), + underlyingOperationNode) + .validate(tableEnv) + .asInstanceOf[TemporalTable] + + TemporalTableFunctionImpl.create( + underlyingOperationNode, + temporalTable.timeAttribute, + validatePrimaryKeyExpression(temporalTable.primaryKey)) + } + + private def validatePrimaryKeyExpression(expression: Expression): String = { + expression match { + case fieldReference: ResolvedFieldReference => + fieldReference.name + case _ => throw new ValidationException( + s"Unsupported expression [$expression] as primary key. " + + s"Only top-level (not nested) field references are supported.") + } + } + + def sort( + fields: JList[Expression], + child: Operation) + : Operation = { + val childNode = child.asInstanceOf[LogicalNode] + + val order: Seq[Ordering] = fields.asScala.map(_.accept(expressionBridge)).map { + case o: Ordering => o + case e => Asc(e) + } + + Sort(order, childNode).validate(tableEnv) + } + + def limit(offset: Optional[Int], fetch: Optional[Int], child: Operation): Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + val limit = if (!offset.isPresent && !fetch.isPresent) { + throw new TableException("Either offset or fetch must be provided") + } else if (offset.isPresent) { + Limit(offset.get(), fetch.orElse(-1), childNode) + } else { + applyFetch(fetch.get(), childNode) + } + + limit.validate(tableEnv) + } + + private def applyFetch(fetch: Int, child: LogicalNode): LogicalNode = { + child match { + case Limit(o, -1, c) => + // replace LIMIT without FETCH by LIMIT with FETCH + Limit(o, fetch, c) + case Limit(_, _, _) => + throw new ValidationException("FETCH is already defined.") + case _ => + Limit(0, fetch, child) + } + } + + def alias( + fields: JList[Expression], + child: Operation) + : Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + val convertedFields = fields.asScala.map(_.accept(expressionBridge)) + + AliasNode(convertedFields, childNode).validate(tableEnv) + } + + def filter( + condition: Expression, + child: Operation) + : Operation = { + + val childNode = child.asInstanceOf[LogicalNode] + val convertedFields = condition.accept(expressionBridge) + + Filter(convertedFields, childNode).validate(tableEnv) + } + + def distinct( + child: Operation) + : Operation = { + Distinct(child.asInstanceOf[LogicalNode]).validate(tableEnv) + } + + def minus( + left: Operation, + right: Operation, + all: Boolean) + : Operation = { + Minus(left.asInstanceOf[LogicalNode], right.asInstanceOf[LogicalNode], all).validate(tableEnv) + } + + def intersect( + left: Operation, + right: Operation, + all: Boolean) + : Operation = { + Intersect(left.asInstanceOf[LogicalNode], right.asInstanceOf[LogicalNode], all) + .validate(tableEnv) + } + + def union( + left: Operation, + right: Operation, + all: Boolean) + : Operation = { + Union(left.asInstanceOf[LogicalNode], right.asInstanceOf[LogicalNode], all).validate(tableEnv) + } + + /** + * Converts an API class to a logical window for planning. + */ + private def createLogicalWindow(overWindow: OverWindow): LogicalOverWindow = { + LogicalOverWindow( + overWindow.getAlias.accept(expressionBridge), + overWindow.getPartitioning.asScala.map(_.accept(expressionBridge)), + overWindow.getOrder.accept(expressionBridge), + overWindow.getPreceding.accept(expressionBridge), + toScala(overWindow.getFollowing).map(_.accept(expressionBridge)) + ) + } + + /** + * Converts an API class to a logical window for planning. + */ + private def createLogicalWindow(window: GroupWindow): LogicalWindow = window match { + case tw: TumbleWithSizeOnTimeWithAlias => + TumblingGroupWindow( + tw.getAlias.accept(expressionBridge), + tw.getTimeField.accept(expressionBridge), + tw.getSize.accept(expressionBridge)) + case sw: SlideWithSizeAndSlideOnTimeWithAlias => + SlidingGroupWindow( + sw.getAlias.accept(expressionBridge), + sw.getTimeField.accept(expressionBridge), + sw.getSize.accept(expressionBridge), + sw.getSlide.accept(expressionBridge)) + case sw: SessionWithGapOnTimeWithAlias => + SessionGroupWindow( + sw.getAlias.accept(expressionBridge), + sw.getTimeField.accept(expressionBridge), + sw.getGap.accept(expressionBridge)) + } + Review comment: nit: remove empty line ---------------------------------------------------------------- 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
