godfreyhe commented on a change in pull request #11340: [Flink 14338] Upgrade Calcite version to 1.22 for Flink SQL URL: https://github.com/apache/flink/pull/11340#discussion_r391475263
########## File path: flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/plan/rules/common/LogicalWindowAggregateRule.scala ########## @@ -125,6 +142,105 @@ abstract class LogicalWindowAggregateRule(ruleName: String) call.transformTo(result) } + /** Trim out the HepRelVertex wrapper and get current relational expression. */ + private def trimHep(node: RelNode): RelNode = { + node match { + case hepRelVertex: HepRelVertex => + hepRelVertex.getCurrentRel + case _ => node + } + } + + /** + * Rewrite plan with function call as window call operand: rewrite the window call to + * reference the input instead of invoking the function directly, in order to simplify the + * subsequent rewrite logic. + * + * For example, plan + * <pre> + * LogicalAggregate(group=[{0}], a=[COUNT()]) + * LogicalProject($f0=[$TUMBLE(TUMBLE_ROWTIME($0), 4:INTERVAL SECOND)], a=[$1]) + * LogicalProject($f0=[1970-01-01 00:00:00:TIMESTAMP(3)], a=[$0]) + * </pre> + * + * would be rewritten to + * <pre> + * LogicalAggregate(group=[{0}], a=[COUNT()]) + * LogicalProject($f0=[TUMBLE($1, 4:INTERVAL SECOND)], a=[$0]) + * LogicalProject(a=[$1], zzzzz=[TUMBLE_ROWTIME($0)]) + * LogicalProject($f0=[1970-01-01 00:00:00:TIMESTAMP(3)], a=[$0]) + * </pre> + */ + private def rewriteWindowCallWithFuncOperands( + project: LogicalProject, + relBuilder: RelBuilder): LogicalProject = { + val projectInput = trimHep(project.getInput) + if (!projectInput.isInstanceOf[Project]) { + return project + } + val inputProjects = projectInput.asInstanceOf[Project].getChildExps + var hasWindowCallWithFuncOperands: Boolean = false + var lastIdx = projectInput.getRowType.getFieldCount - 1; + val pushDownCalls = new JArrayList[RexNode]() + 0 until projectInput.getRowType.getFieldCount foreach { + idx => pushDownCalls.add(RexInputRef.of(idx, projectInput.getRowType)) + } + val newProjectExprs = project.getChildExps.map { + case call: RexCall if isWindowCall(call) && + isTimeAttributeCall(call.getOperands.head, inputProjects) => + hasWindowCallWithFuncOperands = true + // Update the window call to reference a RexInputRef instead of a function call. + call.accept( + new RexShuttle { + override def visitCall(call: RexCall): RexNode = { + if (isTimeAttributeCall(call, inputProjects)) { + lastIdx += 1 + pushDownCalls.add(call) + relBuilder.getRexBuilder.makeInputRef( + call.getType, + // We would project plus an additional function call + // at the end of input projection. + lastIdx) + } else { + super.visitCall(call) + } + } + }) + case rex: RexNode => rex + } + + if (hasWindowCallWithFuncOperands) { + relBuilder + .push(projectInput) + // project plus the function call. + .project(pushDownCalls) + .project(newProjectExprs, project.getRowType.getFieldNames) + .build() + .asInstanceOf[LogicalProject] + } else { + project + } + } + + /** Decides if the [[RexNode]] is a call whose return type is + * a time indicator type. */ + def isTimeAttributeCall(rexNode: RexNode, projects: JList[RexNode]): Boolean = rexNode match { + case call: RexCall if FlinkTypeFactory.isTimeIndicatorType(call.getType) + && call.getOperands.forall { operand => + operand.isInstanceOf[RexInputRef] + } => + true Review comment: the if condition is too complex, change to the following ? case call: RexCall if FlinkTypeFactory.isTimeIndicatorType(call.getType) => call.getOperands.forall { operand => operand.isInstanceOf[RexInputRef] } ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services