kristoffSC commented on code in PR #21393:
URL: https://github.com/apache/flink/pull/21393#discussion_r1068628274


##########
flink-table/flink-table-code-splitter/src/main/java/org/apache/flink/table/codesplit/BlockStatementSplitter.java:
##########
@@ -0,0 +1,417 @@
+/*
+ * 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.codesplit;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.codesplit.JavaParser.BlockStatementContext;
+import org.apache.flink.table.codesplit.JavaParser.StatementContext;
+
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.ParserRuleContext;
+import org.antlr.v4.runtime.TokenStreamRewriter;
+import org.antlr.v4.runtime.atn.PredictionMode;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+/**
+ * Extract statements from IFs, ELSEs and WILEs blocks making them smaller.
+ *
+ * <p><i>Before</i>
+ *
+ * <pre><code>
+ * while (counter > 0) {
+ *     int localA = a + 1000;
+ *     System.out.println(localA);
+ *     if (a > 0) {
+ *         b = a * 2;
+ *         c = b * 2;
+ *         System.out.println(b);
+ *     } else {
+ *         b = a * 3;
+ *         System.out.println(b);
+ *     }
+ *     counter--;
+ * }
+ *
+ * </code></pre>
+ *
+ * <p><i>After</i>
+ *
+ * <pre><code>
+ * while (counter > 0) {
+ *     myFun_whileBody0_0(int a);
+ *     if (a > 0) {
+ *         myFun_whileBody0_0_ifBody0(int a);
+ *     } else {
+ *         myFun_whileBody0_0_ifBody1(int a);
+ *     }
+ *     counter--;
+ * }
+ * </code></pre>
+ *
+ * <p>Where bodies of extracted "methods" are:
+ *
+ * <pre><code>
+ * myFun_whileBody0_0(int a) ->
+ *     int localA = a + 1000;
+ *     System.out.println(localA);
+ * </code></pre>
+ *
+ * <pre><code>
+ * myFun_whileBody0_0_ifBody0(int a) ->
+ *     b = a * 2;
+ *     c = b * 2;
+ *     System.out.println(b);
+ * </code></pre>
+ *
+ * <pre><code>
+ * myFun_whileBody0_0_ifBody1(int a) ->
+ *     b = a * 3;
+ *     System.out.println(b);
+ * </code></pre>
+ */
+@Internal
+public class BlockStatementSplitter {
+
+    private final String code;
+
+    private final List<BlockStatementSplitter> children = new ArrayList<>();
+
+    private final String parameters;
+
+    private BlockStatementVisitor visitor;
+
+    /**
+     * Initialize new BlockStatementSplitter.
+     *
+     * @param code a code block that should be rewritten.
+     * @param parameters parameters definition that should be used for 
extracted methods.
+     */
+    public BlockStatementSplitter(String code, String parameters) {
+        this.code = code;
+        this.parameters = parameters;
+    }
+
+    /**
+     * This method extracts statements from IFs, ELSE's and WHILE blocks from 
block code used during
+     * initialization of this object. Every entry of returned map can be seen 
as new method nam (map
+     * key) and method's body. The block names will be prefixed with provided 
context.
+     *
+     * @param context prefix for extracted blocks.
+     * @return a map of block name to block statements mappings. The key can 
be interpreted as name
+     *     of extracted block/method and corresponding List represents 
individual statements (block'
+     *     lines) for this block.
+     */
+    public Map<String, List<String>> extractBlocks(String context) {
+
+        this.visitor = new BlockStatementVisitor(code, context, parameters);
+        JavaParser javaParser = new JavaParser(visitor.tokenStream);
+        javaParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
+        visitor.visitStatement(javaParser.statement());
+
+        Map<String, List<String>> extractedBlocks = new HashMap<>();
+        for (BlockStatementVisitor child : visitor.children) {
+            int counter = 0;
+            for (ContextTextPair extractedBlock : child.extractedSingleBlocks) 
{
+                ParserRuleContext parserRuleContext = 
extractedBlock.parserRuleContext;
+                if (parserRuleContext instanceof BlockStatementContext) {
+                    StatementContext statement =
+                            ((BlockStatementContext) 
parserRuleContext).statement();
+
+                    if (statement != null
+                            && (statement.IF() != null
+                                    || statement.ELSE() != null
+                                    || statement.WHILE() != null)) {
+
+                        BlockStatementSplitter splitter =
+                                new BlockStatementSplitter(
+                                        extractedBlock.ruleText, 
this.parameters);
+                        Map<String, List<String>> rewrite =
+                                splitter.extractBlocks(child.context + "_" + 
counter++);
+                        this.children.add(splitter);

Review Comment:
   Actually the `BlockStatementSplitter#extractBlocks` and 
`BlockStatementSplitter`'s `children` array was not needed. I have removed it 
and now it seems more straight forward.
   
   I will add additional javadoc explaining logic in more 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to