wangshuo128 commented on code in PR #11035:
URL: https://github.com/apache/doris/pull/11035#discussion_r931748268


##########
fe/fe-core/src/test/java/org/apache/doris/nereids/util/AnalyzeSubQueryTest.java:
##########
@@ -0,0 +1,249 @@
+// 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.doris.nereids.util;
+
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.nereids.NereidsPlanner;
+import org.apache.doris.nereids.PlannerContext;
+import org.apache.doris.nereids.analyzer.Unbound;
+import org.apache.doris.nereids.glue.translator.PhysicalPlanTranslator;
+import org.apache.doris.nereids.glue.translator.PlanTranslatorContext;
+import org.apache.doris.nereids.jobs.JobContext;
+import org.apache.doris.nereids.jobs.batch.FinalizeAnalyzeJob;
+import org.apache.doris.nereids.jobs.rewrite.RewriteBottomUpJob;
+import org.apache.doris.nereids.memo.Group;
+import org.apache.doris.nereids.memo.Memo;
+import org.apache.doris.nereids.parser.NereidsParser;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.rules.RuleFactory;
+import org.apache.doris.nereids.rules.analysis.BindFunction;
+import org.apache.doris.nereids.rules.analysis.BindRelation;
+import org.apache.doris.nereids.rules.analysis.BindSlotReference;
+import org.apache.doris.nereids.rules.analysis.BindSubQueryAlias;
+import org.apache.doris.nereids.rules.analysis.ProjectToGlobalAggregate;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.trees.plans.physical.PhysicalPlan;
+import org.apache.doris.planner.PlanFragment;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.utframe.TestWithFeService;
+
+import com.google.common.collect.Lists;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+public class AnalyzeSubQueryTest extends TestWithFeService {
+    private final NereidsParser parser = new NereidsParser();
+
+    private final List<String> testSql = Lists.newArrayList(
+            "SELECT * FROM T1",
+            "SELECT * FROM T1 ORDER BY ID",
+            "SELECT * FROM T1 JOIN T2 ON T1.ID = T2.ID",
+            "SELECT * FROM T1 T",
+            "SELECT T.ID FROM T1 T",
+            "SELECT * FROM (SELECT * FROM T1 T) T2",
+            "SELECT T1.ID ID FROM T1",
+            "SELECT T.ID FROM T1 T",
+            "SELECT A.ID, B.SCORE FROM T1 A, T2 B WHERE A.ID = B.ID GROUP BY 
A.ID ORDER BY A.ID",
+            "SELECT * FROM T1 JOIN T2 ON T1.ID = T2.ID JOIN T2 T ON T1.ID = 
T.ID"
+    );
+
+    @Override
+    protected void runBeforeAll() throws Exception {
+        createDatabase("test");
+        connectContext.setDatabase("default_cluster:test");
+
+        createTables(
+                "CREATE TABLE IF NOT EXISTS T1 (\n"
+                        + "    id bigint,\n"
+                        + "    score bigint\n"
+                        + ")\n"
+                        + "DUPLICATE KEY(id)\n"
+                        + "DISTRIBUTED BY HASH(id) BUCKETS 1\n"
+                        + "PROPERTIES (\n"
+                        + "  \"replication_num\" = \"1\"\n"
+                        + ")\n",
+                "CREATE TABLE IF NOT EXISTS T2 (\n"
+                        + "    id bigint,\n"
+                        + "    score bigint\n"
+                        + ")\n"
+                        + "DUPLICATE KEY(id)\n"
+                        + "DISTRIBUTED BY HASH(id) BUCKETS 1\n"
+                        + "PROPERTIES (\n"
+                        + "  \"replication_num\" = \"1\"\n"
+                        + ")\n"
+        );
+    }
+
+    /**
+     * TODO: check bound plan and expression details.
+     */
+    @Test
+    public void testAnalyzeAllCase() {
+        for (String sql : testSql) {
+            System.out.println("*****\nStart test: " + sql + "\n*****\n");
+            checkAnalyze(sql);
+        }
+    }
+
+    @Test
+    public void testAnalyze() {
+        checkAnalyze(testSql.get(10));
+    }
+
+    @Test
+    public void testParse() {
+        for (String sql : testSql) {
+            System.out.println(parser.parseSingle(sql).treeString());
+        }
+    }
+
+    @Test
+    public void testFinalizeAnalyze() {
+        finalizeAnalyze(testSql.get(5));
+    }
+
+    @Test
+    public void testFinalizeAnalyzeAllCase() {
+        for (String sql : testSql) {
+            System.out.println("*****\nStart test: " + sql + "\n*****\n");
+            finalizeAnalyze(sql);
+        }
+    }
+
+    @Test
+    public void testPlan() throws AnalysisException {
+        PhysicalPlan plan = testPlanCase(testSql.get(9));
+        PlanFragment root = new PhysicalPlanTranslator().translatePlan(plan, 
new PlanTranslatorContext());
+        System.out.println(root.getPlanRoot());
+    }
+
+    @Test
+    public void testPlanAllCase() throws AnalysisException {
+        for (String sql : testSql) {
+            System.out.println("*****\nStart test: " + sql + "\n*****\n");
+            testPlanCase(sql);
+        }
+    }
+
+    private PhysicalPlan testPlanCase(String sql) throws AnalysisException {
+        return new NereidsPlanner(connectContext).plan(
+                parser.parseSingle(sql),
+                new PhysicalProperties(),
+                connectContext
+        );
+    }
+
+    private void checkAnalyze(String sql) {
+        LogicalPlan analyzed = analyze(sql);
+        System.out.println(analyzed.treeString());
+        Assertions.assertTrue(checkBound(analyzed));
+    }
+
+    private void finalizeAnalyze(String sql) {
+        Memo memo = new Memo(parser.parseSingle(sql));
+        PlannerContext plannerContext = new PlannerContext(memo, 
connectContext);
+        JobContext jobContext = new JobContext(plannerContext, new 
PhysicalProperties(), Double.MAX_VALUE);
+        plannerContext.setCurrentJobContext(jobContext);
+
+        executeRewriteBottomUpJob(plannerContext,
+                new BindFunction(),
+                new BindRelation(),
+                new BindSubQueryAlias(),
+                new BindSlotReference(),
+                new ProjectToGlobalAggregate());
+        System.out.println(memo.copyOut().treeString());
+        new FinalizeAnalyzeJob(plannerContext).execute();
+        System.out.println(memo.copyOut().treeString());
+    }
+
+    private LogicalPlan analyze(String sql) {
+        try {
+            LogicalPlan parsed = parser.parseSingle(sql);
+            System.out.println(parsed.treeString());
+            return analyze(parsed, connectContext);
+        } catch (Throwable t) {
+            throw new IllegalStateException("Analyze failed", t);
+        }
+    }
+
+    private LogicalPlan analyze(LogicalPlan inputPlan, ConnectContext 
connectContext) {
+        Memo memo = new Memo(inputPlan);
+
+        PlannerContext plannerContext = new PlannerContext(memo, 
connectContext);
+        JobContext jobContext = new JobContext(plannerContext, new 
PhysicalProperties(), Double.MAX_VALUE);
+        plannerContext.setCurrentJobContext(jobContext);
+
+        executeRewriteBottomUpJob(plannerContext,
+                new BindFunction(),
+                new BindRelation(),
+                new BindSubQueryAlias(),
+                new BindSlotReference(),
+                new ProjectToGlobalAggregate());
+        return (LogicalPlan) memo.copyOut();
+    }
+
+    private void executeRewriteBottomUpJob(PlannerContext plannerContext, 
RuleFactory... ruleFactory) {
+        Group rootGroup = plannerContext.getMemo().getRoot();

Review Comment:
   ditto.



-- 
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: commits-unsubscr...@doris.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to