You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "liuyongvs (via GitHub)" <gi...@apache.org> on 2024/02/23 09:30:13 UTC

[PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

liuyongvs opened a new pull request, #24375:
URL: https://github.com/apache/flink/pull/24375

   ## What is the purpose of the change
   
   The PR migrates WindowGroupReorderRule to java
   it doesn't touch WindowGroupReorderRuleTest to be sure that java version continues passing it
   
   ## Verifying this change
   
   This change is already covered by existing tests
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (no)
     - If yes, how is the feature documented? (not applicable)
   


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #24375:
URL: https://github.com/apache/flink/pull/24375#discussion_r1535578497


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/WindowGroupReorderRule.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.logical;
+
+import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Window.Group;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.logical.LogicalWindow;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexInputRef;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * Planner rule that makes the over window groups which have the same shuffle keys and order keys
+ * together.
+ */
+@Value.Enclosing
+public class WindowGroupReorderRule
+        extends RelRule<WindowGroupReorderRule.WindowGroupReorderRuleConfig> {
+
+    public static final WindowGroupReorderRule INSTANCE =
+            WindowGroupReorderRule.WindowGroupReorderRuleConfig.DEFAULT.toRule();
+
+    private WindowGroupReorderRule(WindowGroupReorderRuleConfig config) {
+        super(config);
+    }
+
+    @Override
+    public boolean matches(RelOptRuleCall call) {
+        LogicalWindow window = call.rel(0);
+        return window.groups.size() > 1;
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        LogicalWindow window = call.rel(0);
+        RelNode input = call.rel(1);
+        List<Group> oldGroups = new ArrayList<>(window.groups);
+        List<Group> sequenceGroups = new ArrayList<>(window.groups);
+
+        sequenceGroups.sort(
+                (o1, o2) -> {
+                    int keyComp = o1.keys.compareTo(o2.keys);
+                    if (keyComp == 0) {
+                        return compareRelCollation(o1.orderKeys, o2.orderKeys);
+                    } else {
+                        return keyComp;
+                    }
+                });
+
+        if (!sequenceGroups.equals(oldGroups) && !Lists.reverse(sequenceGroups).equals(oldGroups)) {
+            int offset = input.getRowType().getFieldCount();
+            List<int[]> aggTypeIndexes = new ArrayList<>();
+            for (Group group : oldGroups) {
+                int aggCount = group.aggCalls.size();
+                int[] typeIndexes = new int[aggCount];
+                for (int i = 0; i < aggCount; i++) {
+                    typeIndexes[i] = offset + i;
+                }
+                offset += aggCount;
+                aggTypeIndexes.add(typeIndexes);
+            }
+
+            offset = input.getRowType().getFieldCount();
+            List<Integer> mapToOldTypeIndexes =
+                    IntStream.range(0, offset).boxed().collect(Collectors.toList());
+            for (Group newGroup : sequenceGroups) {
+                int aggCount = newGroup.aggCalls.size();
+                int oldIndex = oldGroups.indexOf(newGroup);
+                offset += aggCount;
+                for (int aggIndex = 0; aggIndex < aggCount; aggIndex++) {
+                    mapToOldTypeIndexes.add(aggTypeIndexes.get(oldIndex)[aggIndex]);
+                }
+            }
+
+            List<Map.Entry<String, RelDataType>> newFieldList =
+                    mapToOldTypeIndexes.stream()
+                            .map(index -> window.getRowType().getFieldList().get(index))
+                            .collect(Collectors.toList());
+            RelDataType intermediateRowType =
+                    window.getCluster().getTypeFactory().createStructType(newFieldList);
+            LogicalWindow newLogicalWindow =
+                    LogicalWindow.create(
+                            window.getCluster().getPlanner().emptyTraitSet(),
+                            input,
+                            window.constants,
+                            intermediateRowType,
+                            sequenceGroups);
+
+            List<Integer> sortedIndices =
+                    IntStream.range(0, mapToOldTypeIndexes.size())
+                            .boxed()
+                            .sorted(Comparator.comparingInt(i -> mapToOldTypeIndexes.get(i)))
+                            .collect(Collectors.toList());
+
+            List<RexInputRef> projects =
+                    sortedIndices.stream()
+                            .map(
+                                    index ->
+                                            new RexInputRef(
+                                                    index, newFieldList.get(index).getValue()))
+                            .collect(Collectors.toList());
+
+            LogicalProject project =
+                    LogicalProject.create(
+                            newLogicalWindow,
+                            java.util.Collections.emptyList(),

Review Comment:
   why can't we have `Collections.emptyList()` here?



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "liuyongvs (via GitHub)" <gi...@apache.org>.
liuyongvs commented on PR #24375:
URL: https://github.com/apache/flink/pull/24375#issuecomment-2556853407

   hi @snuyanzin ci passed, will you look again?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "liuyongvs (via GitHub)" <gi...@apache.org>.
liuyongvs commented on PR #24375:
URL: https://github.com/apache/flink/pull/24375#issuecomment-2559105631

   hi @snuyanzin ci passed, will you look again?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #24375:
URL: https://github.com/apache/flink/pull/24375#issuecomment-2308961960

   still failing on ci


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "liuyongvs (via GitHub)" <gi...@apache.org>.
liuyongvs commented on code in PR #24375:
URL: https://github.com/apache/flink/pull/24375#discussion_r1537071891


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/WindowGroupReorderRule.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.logical;
+
+import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Window.Group;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.logical.LogicalWindow;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexInputRef;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * Planner rule that makes the over window groups which have the same shuffle keys and order keys
+ * together.
+ */
+@Value.Enclosing
+public class WindowGroupReorderRule
+        extends RelRule<WindowGroupReorderRule.WindowGroupReorderRuleConfig> {
+
+    public static final WindowGroupReorderRule INSTANCE =
+            WindowGroupReorderRule.WindowGroupReorderRuleConfig.DEFAULT.toRule();
+
+    private WindowGroupReorderRule(WindowGroupReorderRuleConfig config) {
+        super(config);
+    }
+
+    @Override
+    public boolean matches(RelOptRuleCall call) {
+        LogicalWindow window = call.rel(0);
+        return window.groups.size() > 1;
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        LogicalWindow window = call.rel(0);
+        RelNode input = call.rel(1);
+        List<Group> oldGroups = new ArrayList<>(window.groups);
+        List<Group> sequenceGroups = new ArrayList<>(window.groups);
+
+        sequenceGroups.sort(
+                (o1, o2) -> {
+                    int keyComp = o1.keys.compareTo(o2.keys);
+                    if (keyComp == 0) {
+                        return compareRelCollation(o1.orderKeys, o2.orderKeys);
+                    } else {
+                        return keyComp;
+                    }
+                });
+
+        if (!sequenceGroups.equals(oldGroups) && !Lists.reverse(sequenceGroups).equals(oldGroups)) {
+            int offset = input.getRowType().getFieldCount();
+            List<int[]> aggTypeIndexes = new ArrayList<>();
+            for (Group group : oldGroups) {
+                int aggCount = group.aggCalls.size();
+                int[] typeIndexes = new int[aggCount];
+                for (int i = 0; i < aggCount; i++) {
+                    typeIndexes[i] = offset + i;
+                }
+                offset += aggCount;
+                aggTypeIndexes.add(typeIndexes);
+            }
+
+            offset = input.getRowType().getFieldCount();
+            List<Integer> mapToOldTypeIndexes =
+                    IntStream.range(0, offset).boxed().collect(Collectors.toList());
+            for (Group newGroup : sequenceGroups) {
+                int aggCount = newGroup.aggCalls.size();
+                int oldIndex = oldGroups.indexOf(newGroup);
+                offset += aggCount;
+                for (int aggIndex = 0; aggIndex < aggCount; aggIndex++) {
+                    mapToOldTypeIndexes.add(aggTypeIndexes.get(oldIndex)[aggIndex]);
+                }
+            }
+
+            List<Map.Entry<String, RelDataType>> newFieldList =
+                    mapToOldTypeIndexes.stream()
+                            .map(index -> window.getRowType().getFieldList().get(index))
+                            .collect(Collectors.toList());
+            RelDataType intermediateRowType =
+                    window.getCluster().getTypeFactory().createStructType(newFieldList);
+            LogicalWindow newLogicalWindow =
+                    LogicalWindow.create(
+                            window.getCluster().getPlanner().emptyTraitSet(),
+                            input,
+                            window.constants,
+                            intermediateRowType,
+                            sequenceGroups);
+
+            List<Integer> sortedIndices =
+                    IntStream.range(0, mapToOldTypeIndexes.size())
+                            .boxed()
+                            .sorted(Comparator.comparingInt(i -> mapToOldTypeIndexes.get(i)))
+                            .collect(Collectors.toList());
+
+            List<RexInputRef> projects =
+                    sortedIndices.stream()
+                            .map(
+                                    index ->
+                                            new RexInputRef(
+                                                    index, newFieldList.get(index).getValue()))
+                            .collect(Collectors.toList());
+
+            LogicalProject project =
+                    LogicalProject.create(
+                            newLogicalWindow,
+                            java.util.Collections.emptyList(),

Review Comment:
   done @snuyanzin 



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin merged PR #24375:
URL: https://github.com/apache/flink/pull/24375


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #24375:
URL: https://github.com/apache/flink/pull/24375#issuecomment-1960997302

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "197c1a1d82d3a7f09195b9d5594ae707b182a592",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "197c1a1d82d3a7f09195b9d5594ae707b182a592",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 197c1a1d82d3a7f09195b9d5594ae707b182a592 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on code in PR #24375:
URL: https://github.com/apache/flink/pull/24375#discussion_r1535579353


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/WindowGroupReorderRule.java:
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.logical;
+
+import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Window.Group;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.logical.LogicalWindow;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexInputRef;
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/**
+ * Planner rule that makes the over window groups which have the same shuffle keys and order keys
+ * together.
+ */
+@Value.Enclosing
+public class WindowGroupReorderRule
+        extends RelRule<WindowGroupReorderRule.WindowGroupReorderRuleConfig> {
+
+    public static final WindowGroupReorderRule INSTANCE =
+            WindowGroupReorderRule.WindowGroupReorderRuleConfig.DEFAULT.toRule();
+
+    private WindowGroupReorderRule(WindowGroupReorderRuleConfig config) {
+        super(config);
+    }
+
+    @Override
+    public boolean matches(RelOptRuleCall call) {
+        LogicalWindow window = call.rel(0);
+        return window.groups.size() > 1;
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        LogicalWindow window = call.rel(0);
+        RelNode input = call.rel(1);
+        List<Group> oldGroups = new ArrayList<>(window.groups);
+        List<Group> sequenceGroups = new ArrayList<>(window.groups);
+
+        sequenceGroups.sort(
+                (o1, o2) -> {
+                    int keyComp = o1.keys.compareTo(o2.keys);
+                    if (keyComp == 0) {
+                        return compareRelCollation(o1.orderKeys, o2.orderKeys);
+                    } else {
+                        return keyComp;
+                    }
+                });
+
+        if (!sequenceGroups.equals(oldGroups) && !Lists.reverse(sequenceGroups).equals(oldGroups)) {
+            int offset = input.getRowType().getFieldCount();
+            List<int[]> aggTypeIndexes = new ArrayList<>();
+            for (Group group : oldGroups) {
+                int aggCount = group.aggCalls.size();
+                int[] typeIndexes = new int[aggCount];
+                for (int i = 0; i < aggCount; i++) {
+                    typeIndexes[i] = offset + i;
+                }
+                offset += aggCount;
+                aggTypeIndexes.add(typeIndexes);
+            }
+
+            offset = input.getRowType().getFieldCount();
+            List<Integer> mapToOldTypeIndexes =
+                    IntStream.range(0, offset).boxed().collect(Collectors.toList());
+            for (Group newGroup : sequenceGroups) {
+                int aggCount = newGroup.aggCalls.size();
+                int oldIndex = oldGroups.indexOf(newGroup);
+                offset += aggCount;
+                for (int aggIndex = 0; aggIndex < aggCount; aggIndex++) {
+                    mapToOldTypeIndexes.add(aggTypeIndexes.get(oldIndex)[aggIndex]);
+                }
+            }
+
+            List<Map.Entry<String, RelDataType>> newFieldList =
+                    mapToOldTypeIndexes.stream()
+                            .map(index -> window.getRowType().getFieldList().get(index))
+                            .collect(Collectors.toList());
+            RelDataType intermediateRowType =
+                    window.getCluster().getTypeFactory().createStructType(newFieldList);
+            LogicalWindow newLogicalWindow =
+                    LogicalWindow.create(
+                            window.getCluster().getPlanner().emptyTraitSet(),
+                            input,
+                            window.constants,
+                            intermediateRowType,
+                            sequenceGroups);
+
+            List<Integer> sortedIndices =
+                    IntStream.range(0, mapToOldTypeIndexes.size())
+                            .boxed()
+                            .sorted(Comparator.comparingInt(i -> mapToOldTypeIndexes.get(i)))

Review Comment:
   ```suggestion
                               .sorted(Comparator.comparingInt(mapToOldTypeIndexes::get))
   ```



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-34505][table] Migrate WindowGroupReorderRule to java. [flink]

Posted by "snuyanzin (via GitHub)" <gi...@apache.org>.
snuyanzin commented on PR #24375:
URL: https://github.com/apache/flink/pull/24375#issuecomment-2042280323

   @liuyongvs the ci still fails could you please have a look here?


-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org