beyond1920 commented on a change in pull request #14905: URL: https://github.com/apache/flink/pull/14905#discussion_r575740801
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/stream/PushWindowTableFunctionIntoWindowAggregateRule.scala ########## @@ -0,0 +1,152 @@ +/* + * 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.planner.plan.rules.physical.stream + +import org.apache.calcite.plan.RelOptRule.{any, operand} +import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall} +import org.apache.calcite.rel.`type`.RelDataType +import org.apache.calcite.rel.{RelCollations, RelNode} +import org.apache.calcite.rex._ +import org.apache.calcite.util.ImmutableBitSet +import org.apache.flink.table.planner.plan.`trait`.FlinkRelDistribution +import org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy +import org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery +import org.apache.flink.table.planner.plan.nodes.FlinkConventions +import org.apache.flink.table.planner.plan.nodes.physical.stream.{StreamPhysicalCalc, StreamPhysicalExchange, StreamPhysicalWindowAggregate, StreamPhysicalWindowTableFunction} +import org.apache.flink.table.planner.plan.utils.WindowUtil +import org.apache.flink.table.planner.plan.utils.WindowUtil.buildNewProgramWithoutWindowColumns + +import scala.collection.JavaConversions._ +import scala.collection.mutable.ArrayBuffer + + +/** + * Planner rule that tries to push [[StreamPhysicalWindowTableFunction]] into a + * [[StreamPhysicalWindowAggregate]]. + */ +class PushWindowTableFunctionIntoWindowAggregateRule + extends RelOptRule( Review comment: @wuchong yes, there is no WindowTableFunction after apply the rule. I only have a minor problem about `push`. I prefer to use `PullUp` instead of `Push`. ########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/stream/ExpandWindowTableFunctionTransposeRule.scala ########## @@ -0,0 +1,298 @@ +/* + * 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.planner.plan.rules.physical.stream + +import org.apache.flink.table.planner.calcite.FlinkTypeFactory +import org.apache.flink.table.planner.functions.sql.SqlWindowTableFunction +import org.apache.flink.table.planner.plan.logical.TimeAttributeWindowingStrategy +import org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery +import org.apache.flink.table.planner.plan.nodes.FlinkConventions +import org.apache.flink.table.planner.plan.nodes.physical.stream.{StreamPhysicalCalc, StreamPhysicalExpand, StreamPhysicalWindowAggregate, StreamPhysicalWindowTableFunction} +import org.apache.flink.table.planner.plan.utils.WindowUtil +import org.apache.flink.table.planner.plan.utils.WindowUtil.buildNewProgramWithoutWindowColumns + +import org.apache.calcite.plan.RelOptRule.{any, operand} +import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall} +import org.apache.calcite.rel.RelNode +import org.apache.calcite.rel.`type`.RelDataType +import org.apache.calcite.rex.{RexInputRef, RexLiteral, RexNode, RexProgram} + +import scala.collection.JavaConverters._ +import scala.collection.mutable +import scala.collection.mutable.ArrayBuffer + +/** + * This rule transposes [[StreamPhysicalExpand]] past [[StreamPhysicalWindowTableFunction]] to make + * [[PushWindowTableFunctionIntoWindowAggregateRule]] can match the rel tree pattern and optimize + * them into [[StreamPhysicalWindowAggregate]]. + * + * Example: + * + * MyTable: a INT, c STRING, rowtime TIMESTAMP(3) + * + * SQL: + * {{{ + * SELECT + * window_start, + * window_end, + * count(distinct a), + * count(distinct c) + * FROM TABLE(TUMBLE(TABLE MyTable, DESCRIPTOR(rowtime), INTERVAL '15' MINUTE)) + * GROUP BY window_start, window_end + * }}} + * + * We will get part of the initial physical plan like following: + * {{{ + * WindowAggregate(groupBy=[$f4, $f5], window=[TUMBLE(win_start=[window_start], + * win_end=[window_end], size=[15 min])], select=[$f4, $f5, COUNT(DISTINCT a) FILTER $g_1 AS $f2, + * COUNT(DISTINCT c) FILTER $g_2 AS $f3, start('w$) AS window_start, end('w$) AS window_end]) + * +- Exchange(distribution=[hash[$f4, $f5]]) + * +- Calc(select=[window_start, window_end, a, c, $f4, $f5, =($e, 1) AS $g_1, =($e, 2) AS $g_2]) + * +- Expand(projects=[{window_start, window_end, a, c, $f4, null AS $f5, 1 AS $e}, + * {window_start, window_end, a, c, null AS $f4, $f5, 2 AS $e}]) + * +- Calc(select=[window_start, window_end, a, c, + * MOD(HASH_CODE(a), 1024) AS $f4, MOD(HASH_CODE(c), 1024) AS $f5]) + * +- WindowTableFunction(window=[TUMBLE(time_col=[rowtime], size=[15 min])]) + * }}} + * + * However, it can't match [[PushWindowTableFunctionIntoWindowAggregateRule]], because + * [[StreamPhysicalWindowTableFunction]] is not near [[StreamPhysicalWindowAggregate]]. + * So we need to transpose [[StreamPhysicalExpand]] past [[StreamPhysicalWindowTableFunction]] + * to make the part of rel tree like this which can be matched by + * [[PushWindowTableFunctionIntoWindowAggregateRule]]. + * + * {{{ + * WindowAggregate(groupBy=[$f4, $f5], window=[TUMBLE(win_start=[window_start], + * win_end=[window_end], size=[15 min])], select=[$f4, $f5, COUNT(DISTINCT a) FILTER $g_1 AS $f2, + * COUNT(DISTINCT c) FILTER $g_2 AS $f3, start('w$) AS window_start, end('w$) AS window_end]) + * +- Exchange(distribution=[hash[$f4, $f5]]) + * +- Calc(select=[window_start, window_end, a, c, $f4, $f5, ($e = 1) AS $g_1, ($e = 2) AS $g_2]) + * +- WindowTableFunction(window=[TUMBLE(time_col=[rowtime], size=[15 min])]) + * +- Expand(...) + * }}} + * </pre> + */ +class ExpandWindowTableFunctionTransposeRule + extends RelOptRule( + operand(classOf[StreamPhysicalExpand], + operand(classOf[StreamPhysicalCalc], + operand(classOf[StreamPhysicalWindowTableFunction], any()))), + "ExpandWindowTableFunctionTransposeRule") { Review comment: @wuchong Could we update the rule to match `WindowAggregate` -> `Exchange` -> `Calc` (Optional) -> Expand -> Calc -> WindowTableFunction, and directly convert to equivalent tree which merge `WindowTableFunction` into `WindowAggregate`. ########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/functions/sql/SqlCumulateTableFunction.java ########## @@ -0,0 +1,72 @@ +/* + * 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.planner.functions.sql; + +import org.apache.flink.shaded.guava18.com.google.common.collect.ImmutableList; + +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlOperator; + +/** + * SqlCumulateTableFunction implements an operator for cumulative. + * + * <p>It allows four parameters: + * + * <ol> + * <li>a table + * <li>a descriptor to provide a time attribute column name from the input table + * <li>an interval parameter to specify the window size to increase. + * <li>an interval parameter to specify the max length of window size + * </ol> + */ +public class SqlCumulateTableFunction extends SqlWindowTableFunction { Review comment: OK ---------------------------------------------------------------- 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