You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2022/07/17 12:54:45 UTC

[doris] branch master updated: [fix](planner) fix create view when using union (#10849)

This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b1408ce41 [fix](planner) fix create view when using union (#10849)
6b1408ce41 is described below

commit 6b1408ce41d42bc985463d845ec9a934ac527b32
Author: jakevin <ja...@gmail.com>
AuthorDate: Sun Jul 17 20:54:40 2022 +0800

    [fix](planner) fix create view when using union (#10849)
---
 .../apache/doris/analysis/SetOperationStmt.java    | 22 ++++++----
 .../org/apache/doris/catalog/CreateViewTest.java   | 48 ++++++++++++++++++----
 2 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java
index 9816c30bb2..5d81c38b59 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java
@@ -40,7 +40,7 @@ import java.util.Set;
  * During analysis, the operands are normalized (separated into a single sequence of
  * DISTINCT followed by a single sequence of ALL operands) and unnested to the extent
  * possible. This also creates the AggregationInfo for DISTINCT operands.
- *
+ * <p>
  * Use of resultExprs vs. baseTblResultExprs:
  * We consistently use/cast the resultExprs of set operands because the final expr
  * substitution happens during planning. The only place where baseTblResultExprs are
@@ -326,8 +326,8 @@ public class SetOperationStmt extends QueryStmt {
             List<Expr> exprs = query.getResultExprs();
             if (firstExprs.size() != exprs.size()) {
                 throw new AnalysisException("Operands have unequal number of columns:\n"
-                        +    "'" + queryStmtToSql(firstQuery) + "' has "
-                        +    firstExprs.size() + " column(s)\n"
+                        + "'" + queryStmtToSql(firstQuery) + "' has "
+                        + firstExprs.size() + " column(s)\n"
                         + "'" + queryStmtToSql(query) + "' has " + exprs.size() + " column(s)");
             }
         }
@@ -791,13 +791,17 @@ public class SetOperationStmt extends QueryStmt {
 
     @Override
     public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
-            throws AnalysisException, UserException {
-        QueryStmt firstQuery = operands.get(0).getQueryStmt();
-        firstQuery.substituteSelectList(analyzer, newColLabels);
-        // substitute order by
-        if (orderByElements != null) {
-            orderByElements = OrderByElement.substitute(orderByElements, firstQuery.aliasSMap, analyzer);
+            throws UserException {
+        for (int i = 0; i < operands.size(); i++) {
+            Analyzer childAnalyzer = new Analyzer(analyzer);
+            QueryStmt query = operands.get(i).getQueryStmt();
+            query.substituteSelectList(childAnalyzer, newColLabels);
+            // substitute order by
+            if (orderByElements != null && i == 0) {
+                orderByElements = OrderByElement.substitute(orderByElements, query.aliasSMap, childAnalyzer);
+            }
         }
+
     }
 
     /**
diff --git a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java
index 6b71365c09..36edf9734d 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateViewTest.java
@@ -53,7 +53,8 @@ public class CreateViewTest {
         // create table
         String createTableStmtStr = "create table test.tbl1(k1 int, k2 int, v1 int, v2 int) duplicate key(k1)"
                 + " distributed by hash(k2) buckets 1 properties('replication_num' = '1');";
-        CreateTableStmt createTableStmt = (CreateTableStmt) UtFrameUtils.parseAndAnalyzeStmt(createTableStmtStr, connectContext);
+        CreateTableStmt createTableStmt = (CreateTableStmt) UtFrameUtils.parseAndAnalyzeStmt(createTableStmtStr,
+                connectContext);
         Catalog.getCurrentCatalog().createTable(createTableStmt);
     }
 
@@ -87,6 +88,18 @@ public class CreateViewTest {
                 () -> createView("create view test.view5 as select * from test.tbl1 where hour(now()) > 3"
                         + " and curdate() > '2021-06-26';"));
 
+        // test union all
+        ExceptionChecker.expectThrowsNoException(
+                () -> createView("create view test.view6 as "
+                        + "select * from test.tbl1 where curdate() > '2021-06-26' order by k1 limit 10 "
+                        + "union all "
+                        + "select * from test.tbl1 where curdate() > '2021-06-26' order by k2 limit 10, 50;"));
+        ExceptionChecker.expectThrowsNoException(
+                () -> createView("create view test.view7 (k1, k2) as "
+                        + "select k1, k2 from test.tbl1 where curdate() > '2021-06-26' order by k1 limit 10 "
+                        + "union all "
+                        + "select k1, k2 from test.tbl1 where curdate() > '2021-06-26' order by k2 limit 10, 50;"));
+
         Database db = Catalog.getCurrentInternalCatalog().getDbOrDdlException("default_cluster:test");
 
         View view1 = (View) db.getTableOrDdlException("view1");
@@ -112,9 +125,21 @@ public class CreateViewTest {
         Assert.assertNotNull(view4.getColumn("s1"));
 
         View view5 = (View) db.getTableOrDdlException("view5");
-        System.out.println(view5.getDdlSql());
-        Assert.assertTrue(view5.getDdlSql().contains("hour") && view5.getDdlSql().contains("now")
-                && view5.getDdlSql().contains("curdate"));
+        Assert.assertTrue(view5.getDdlSql().contains("hour"));
+        Assert.assertTrue(view5.getDdlSql().contains("now"));
+        Assert.assertTrue(view5.getDdlSql().contains("curdate"));
+
+        View view6 = (View) db.getTableOrDdlException("view6");
+        Assert.assertEquals(4, view6.getFullSchema().size());
+        Assert.assertNotNull(view6.getColumn("k1"));
+        Assert.assertNotNull(view6.getColumn("k2"));
+        Assert.assertNotNull(view6.getColumn("v1"));
+        Assert.assertNotNull(view6.getColumn("v2"));
+
+        View view7 = (View) db.getTableOrDdlException("view7");
+        Assert.assertEquals(2, view7.getFullSchema().size());
+        Assert.assertNotNull(view7.getColumn("k1"));
+        Assert.assertNotNull(view7.getColumn("k2"));
     }
 
     @Test
@@ -138,14 +163,21 @@ public class CreateViewTest {
                 () -> createView("create view test.alter1 as " + originStmt));
         Database db = Catalog.getCurrentInternalCatalog().getDbOrDdlException("default_cluster:test");
         View alter1 = (View) db.getTableOrDdlException("alter1");
-        Assert.assertEquals("SELECT `k1` AS `kc1`, sum(`k2`) AS `kc2` FROM `default_cluster:test`.`tbl1` GROUP BY `kc1`", alter1.getInlineViewDef());
+        Assert.assertEquals(
+                "SELECT `k1` AS `kc1`, sum(`k2`) AS `kc2` FROM `default_cluster:test`.`tbl1` GROUP BY `kc1`",
+                alter1.getInlineViewDef());
 
-        String alterStmt = "alter view test.alter1 as with test1_cte (w1, w2) as (select k1, k2 from test.tbl1) select w1 as c1, sum(w2) as c2 from test1_cte where w1 > 10 group by w1 order by w1";
+        String alterStmt
+                = "alter view test.alter1 as with test1_cte (w1, w2) as (select k1, k2 from test.tbl1) "
+                + "select w1 as c1, sum(w2) as c2 from test1_cte where w1 > 10 group by w1 order by w1";
         AlterViewStmt alterViewStmt = (AlterViewStmt) UtFrameUtils.parseAndAnalyzeStmt(alterStmt, connectContext);
         Catalog.getCurrentCatalog().alterView(alterViewStmt);
 
         alter1 = (View) db.getTableOrDdlException("alter1");
-        System.out.println(alter1.getInlineViewDef());
-        Assert.assertEquals("WITH test1_cte(w1, w2) AS (SELECT `k1` AS `k1`, `k2` AS `k2` FROM `default_cluster:test`.`tbl1`) SELECT `w1` AS `c1`, sum(`w2`) AS `c2` FROM `test1_cte` WHERE `w1` > 10 GROUP BY `w1` ORDER BY `w1` ASC", alter1.getInlineViewDef());
+        Assert.assertEquals(
+                "WITH test1_cte(w1, w2) "
+                        + "AS (SELECT `k1` AS `k1`, `k2` AS `k2` FROM `default_cluster:test`.`tbl1`) "
+                        + "SELECT `w1` AS `c1`, sum(`w2`) AS `c2` FROM `test1_cte` WHERE `w1` > 10 GROUP BY `w1` ORDER BY `w1` ASC",
+                alter1.getInlineViewDef());
     }
 }


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