morrySnow commented on code in PR #11035:
URL: https://github.com/apache/doris/pull/11035#discussion_r940883894
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/memo/GroupExpression.java:
##########
@@ -168,6 +170,12 @@ public boolean equals(Object o) {
return false;
}
GroupExpression that = (GroupExpression) o;
+ // if the plan is LogicalRelation, this == that should be true,
Review Comment:
need to update comment
```suggestion
// if the plan is LogicalRelation or PhysicalRelation, this == that
should be true,
```
##########
regression-test/conf/regression-conf.groovy:
##########
@@ -20,11 +20,11 @@
// **Note**: default db will be create if not exist
defaultDb = "regression_test"
-jdbcUrl = "jdbc:mysql://127.0.0.1:9030/?"
+jdbcUrl = "jdbc:mysql://127.0.0.1:6786/?"
Review Comment:
change back to original port
##########
regression-test/conf/regression-conf.groovy:
##########
@@ -20,11 +20,11 @@
// **Note**: default db will be create if not exist
defaultDb = "regression_test"
-jdbcUrl = "jdbc:mysql://127.0.0.1:9030/?"
+jdbcUrl = "jdbc:mysql://127.0.0.1:6786/?"
jdbcUser = "root"
jdbcPassword = ""
-feHttpAddress = "127.0.0.1:8030"
+feHttpAddress = "127.0.0.1:6780"
Review Comment:
ditto
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java:
##########
@@ -69,4 +68,33 @@ private Table getTable(String dbName, String tableName, Env
env) {
db.readUnlock();
}
}
+
+ private LogicalPlan useCurrentDb(ConnectContext ctx, List<String>
nameParts) {
+ String dbName = ctx.getDatabase();
+ Table table = getTable(dbName, nameParts.get(0), ctx.getEnv());
+ // TODO: should generate different Scan sub class according to table's
type
+ if (table.getType() == TableType.OLAP) {
+ return new LogicalOlapScan(table, ImmutableList.of(dbName));
+ } else if (table.getType() == TableType.VIEW) {
+ LogicalPlan viewPlan = new
NereidsAnalyzer(ctx).analyze(table.getDdlSql());
+ return new LogicalSubQueryAlias<>(table.getName(), viewPlan);
+ }
+ throw new RuntimeException("");
+ }
+
+ private LogicalPlan useDbNameFromNamePart(ConnectContext ctx, List<String>
nameParts) {
+ // if the relation is view, nameParts.get(0) is dbName.
+ String dbName = nameParts.get(0);
+ if (!dbName.equals(ctx.getDatabase())) {
+ dbName = ctx.getClusterName() + ":" + nameParts.get(0);
+ }
+ Table table = getTable(dbName, nameParts.get(1), ctx.getEnv());
+ if (table.getType() == TableType.OLAP) {
+ return new LogicalOlapScan(table, ImmutableList.of(dbName));
+ } else if (table.getType() == TableType.VIEW) {
+ LogicalPlan viewPlan = new
NereidsAnalyzer(ctx).analyze(table.getDdlSql());
+ return new LogicalSubQueryAlias<>(table.getName(), viewPlan);
+ }
+ throw new RuntimeException("");
Review Comment:
need to add some meaningful message in exception
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java:
##########
@@ -41,16 +45,11 @@ public Rule build() {
switch (nameParts.size()) {
case 1: {
// Use current database name from catalog.
- String dbName = connectContext.getDatabase();
- Table table = getTable(dbName, nameParts.get(0),
connectContext.getEnv());
- // TODO: should generate different Scan sub class
according to table's type
- return new LogicalOlapScan(table,
ImmutableList.of(dbName));
+ return useCurrentDb(connectContext, nameParts);
}
case 2: {
// Use database name from table name parts.
- String dbName = connectContext.getClusterName() + ":" +
nameParts.get(0);
- Table table = getTable(dbName, nameParts.get(1),
connectContext.getEnv());
- return new LogicalOlapScan(table,
ImmutableList.of(dbName));
+ return useDbNameFromNamePart(connectContext, nameParts);
Review Comment:
```suggestion
return bindWithDbNameFromNamePart(connectContext,
nameParts);
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -211,11 +217,33 @@ public LogicalPlan
visitRegularQuerySpecification(RegularQuerySpecificationConte
/**
* Create an aliased table reference. This is typically used in FROM
clauses.
*/
+ @Developing
+ private LogicalPlan withTableAlias(LogicalPlan plan, TableAliasContext
ctx) {
+ String alias = ctx.strictIdentifier().getText();
+ if (null != ctx.identifierList()) {
+ throw new ParseException("Do not implemented", ctx);
+ // TODO: multi-colName
+ }
+ return new LogicalSubQueryAlias<>(alias, plan);
+ }
+
@Override
public LogicalPlan visitTableName(TableNameContext ctx) {
List<String> tableId =
visitMultipartIdentifier(ctx.multipartIdentifier());
- // TODO: sample and time travel, alias, sub query
- return new UnboundRelation(tableId);
+ if (null == ctx.tableAlias().strictIdentifier()) {
+ return new UnboundRelation(tableId);
+ }
+ return withTableAlias(new UnboundRelation(tableId), ctx.tableAlias());
+ }
+
+ @Override
+ public LogicalPlan visitAliasedQuery(AliasedQueryContext ctx) {
+ return withTableAlias(visitQuery(ctx.query()), ctx.tableAlias());
+ }
+
+ @Override
+ public LogicalPlan visitAliasedRelation(AliasedRelationContext ctx) {
+ return withTableAlias((LogicalPlan) visitRelation(ctx.relation()),
ctx.tableAlias());
Review Comment:
```suggestion
return withTableAlias(visitRelation(ctx.relation()),
ctx.tableAlias());
```
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/NereidsPlanner.java:
##########
@@ -106,16 +107,20 @@ public PhysicalPlan plan(LogicalPlan plan,
PhysicalProperties outputProperties,
// cascades style optimize phase.
.setJobContext(outputProperties);
+ finalizeAnalyze();
rewrite();
// TODO: remove this condition, when stats collector is fully
developed.
if (ConnectContext.get().getSessionVariable().isEnableNereidsCBO()) {
deriveStats();
}
optimize();
- // Get plan directly. Just for SSB.
Review Comment:
why remove this comment?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java:
##########
@@ -567,23 +595,45 @@ public List<Expression>
visitNamedExpressionSeq(NamedExpressionSeqContext namedC
return visit(namedCtx.namedExpression(), Expression.class);
}
+ /**
+ * Create OrderKey list.
+ *
+ * @param ctx QueryOrganizationContext
+ * @return List of OrderKey
+ */
@Override
- public LogicalPlan visitFromClause(FromClauseContext ctx) {
+ public List<OrderKey> visitQueryOrganization(QueryOrganizationContext ctx)
{
Review Comment:
u need to rebase to newest master
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java:
##########
@@ -41,16 +45,11 @@ public Rule build() {
switch (nameParts.size()) {
case 1: {
// Use current database name from catalog.
- String dbName = connectContext.getDatabase();
- Table table = getTable(dbName, nameParts.get(0),
connectContext.getEnv());
- // TODO: should generate different Scan sub class
according to table's type
- return new LogicalOlapScan(table,
ImmutableList.of(dbName));
+ return useCurrentDb(connectContext, nameParts);
Review Comment:
```suggestion
return bindWithCurrentDb(connectContext, nameParts);
```
##########
regression-test/suites/nereids_syntax_p0/load.groovy:
##########
@@ -181,4 +181,6 @@ suite("load") {
(1309892, 1, 1303, 1432, 15, 19920517, "3-MEDIUM", 0, 24, 2959704,
5119906, 7, 2752524, 73992, 0, 19920619, "TRUCK"),
(1310179, 6, 1312, 1455, 29, 19921110, "3-MEDIUM", 0, 15, 1705830,
20506457, 10, 1535247, 68233, 8, 19930114, "FOB");
"""
+
+ sleep(6000)
Review Comment:
why need to sleep?
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java:
##########
@@ -69,4 +68,33 @@ private Table getTable(String dbName, String tableName, Env
env) {
db.readUnlock();
}
}
+
+ private LogicalPlan useCurrentDb(ConnectContext ctx, List<String>
nameParts) {
+ String dbName = ctx.getDatabase();
+ Table table = getTable(dbName, nameParts.get(0), ctx.getEnv());
+ // TODO: should generate different Scan sub class according to table's
type
+ if (table.getType() == TableType.OLAP) {
+ return new LogicalOlapScan(table, ImmutableList.of(dbName));
+ } else if (table.getType() == TableType.VIEW) {
+ LogicalPlan viewPlan = new
NereidsAnalyzer(ctx).analyze(table.getDdlSql());
+ return new LogicalSubQueryAlias<>(table.getName(), viewPlan);
+ }
+ throw new RuntimeException("");
Review Comment:
need to add some meaningful message in exception
##########
fe/fe-core/src/test/java/org/apache/doris/nereids/util/AnalyzeSubQueryTest.java:
##########
@@ -0,0 +1,196 @@
+// 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.analyzer.NereidsAnalyzer;
+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.parser.NereidsParser;
+import org.apache.doris.nereids.properties.PhysicalProperties;
+import org.apache.doris.nereids.rules.analysis.EliminateAliasNode;
+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.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 (SELECT * FROM T1 T) T2",
+ "SELECT * FROM T1 TT1 JOIN (SELECT * FROM T2 TT2) T ON TT1.ID =
T.ID",
+ "SELECT * FROM T1 TT1 JOIN (SELECT TT2.ID FROM T2 TT2) T ON TT1.ID
= T.ID",
+ "SELECT T.ID FROM T1 T",
+ "SELECT A.ID FROM T1 A, T2 B WHERE A.ID = B.ID",
+ "SELECT * FROM T1 JOIN T1 T2 ON T1.ID = T2.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(0));
+ }
+
+ @Test
+ public void testParseAllCase() {
+ for (String sql : testSql) {
+ System.out.println(parser.parseSingle(sql).treeString());
+ }
+ }
+
+ @Test
+ public void testParse() {
+ System.out.println(parser.parseSingle(testSql.get(0)).treeString());
+ }
+
+ @Test
+ public void testFinalizeAnalyze() {
+ finalizeAnalyze(testSql.get(0));
+ }
+
+ @Test
+ public void testFinalizeAnalyzeAllCase() {
+ for (String sql : testSql) {
+ System.out.println("*****\nStart test: " + sql + "\n*****\n");
+ finalizeAnalyze(sql);
+ }
+ }
+
+ @Test
+ public void testPlan() throws AnalysisException {
+ testPlanCase(testSql.get(0));
+ }
+
+ @Test
+ public void testPlanAllCase() throws AnalysisException {
+ for (String sql : testSql) {
+ System.out.println("*****\nStart test: " + sql + "\n*****\n");
+ testPlanCase(sql);
+ }
+ }
+
+ private void testPlanCase(String sql) throws AnalysisException {
+ PhysicalPlan plan = new NereidsPlanner(connectContext).plan(
+ parser.parseSingle(sql),
+ new PhysicalProperties(),
+ connectContext
+ );
+ System.out.println(plan.treeString());
+ PlanFragment root = new PhysicalPlanTranslator().translatePlan(plan,
new PlanTranslatorContext());
+ System.out.println(root.getPlanRoot().getPlanTreeExplainStr());
+ }
+
+ private void checkAnalyze(String sql) {
+ LogicalPlan analyzed = analyze(sql);
+ System.out.println(analyzed.treeString());
+ Assertions.assertTrue(checkBound(analyzed));
+ }
+
+ private void finalizeAnalyze(String sql) {
+ LogicalPlan plan = analyze(sql);
+ System.out.println(plan.treeString());
+ plan = (LogicalPlan) PlanRewriter.bottomUpRewrite(plan,
connectContext, new EliminateAliasNode());
+ System.out.println(plan.treeString());
Review Comment:
remove useless print
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]