From 11a9168f75b1840a9188e1b8b81baa643469239a Mon Sep 17 00:00:00 2001 From: kikyo Date: Fri, 2 Dec 2022 02:09:30 +0800 Subject: [PATCH] fix --- .../org/apache/doris/analysis/QueryStmt.java | 8 +++ .../org/apache/doris/analysis/SelectStmt.java | 2 +- .../org/apache/doris/analysis/WithClause.java | 2 +- .../apache/doris/analysis/SelectStmtTest.java | 9 ++-- .../apache/doris/catalog/CreateViewTest.java | 6 +-- .../test_view_with_with_clause.out | 5 ++ .../test_view_with_with_clause.groovy | 51 +++++++++++++++++++ 7 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 regression-test/data/correctness/test_view_with_with_clause.out create mode 100644 regression-test/suites/correctness/test_view_with_with_clause.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java index ea0d3f8881a20c..9616c1cb4760f5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java @@ -164,6 +164,8 @@ public abstract class QueryStmt extends StatementBase implements Queriable { */ private Set disableTuplesMVRewriter = Sets.newHashSet(); + protected boolean toSQLWithSelectList; + QueryStmt(ArrayList orderByElements, LimitElement limitElement) { this.orderByElements = orderByElements; this.limitElement = limitElement; @@ -805,4 +807,10 @@ public OutFileClause getOutFileClause() { public boolean hasOutFileClause() { return outFileClause != null; } + + public String toSqlWithSelectList() { + toSQLWithSelectList = true; + return toSql(); + } + } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java index b03ec0a691b6db..3e9743beacc22d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java @@ -1834,7 +1834,7 @@ public String toSql() { strBuilder.append("DISTINCT "); } ConnectContext ctx = ConnectContext.get(); - if (ctx == null || ctx.getSessionVariable().internalSession) { + if (ctx == null || ctx.getSessionVariable().internalSession || toSQLWithSelectList) { for (int i = 0; i < selectList.getItems().size(); i++) { strBuilder.append(selectList.getItems().get(i).toSql()); strBuilder.append((i + 1 != selectList.getItems().size()) ? ", " : ""); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/WithClause.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/WithClause.java index fb4fdbec6a2392..2842685a87bdef 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/WithClause.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/WithClause.java @@ -145,7 +145,7 @@ public String toSql() { aliasSql += "(" + Joiner.on(", ").join( ToSqlUtils.getIdentSqlList(view.getOriginalColLabels())) + ")"; } - viewStrings.add(aliasSql + " AS (" + view.getQueryStmt().toSql() + ")"); + viewStrings.add(aliasSql + " AS (" + view.getQueryStmt().toSqlWithSelectList() + ")"); } return "WITH " + Joiner.on(",").join(viewStrings); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java index dc66b2ea6a059d..7e0137e6bd0c64 100755 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/SelectStmtTest.java @@ -789,9 +789,8 @@ public void testWithUnionToSql() throws Exception { + ") t"; SelectStmt stmt1 = (SelectStmt) UtFrameUtils.parseAndAnalyzeStmt(sql1, ctx); stmt1.rewriteExprs(new Analyzer(ctx.getEnv(), ctx).getExprRewriter()); - Assert.assertEquals("SELECT `t`.`k1` AS `k1` " - + "FROM (WITH v1 AS (SELECT `t1`.`k1` AS `k1` FROM `default_cluster:db1`.`tbl1` t1)," - + "v2 AS (SELECT `t2`.`k1` AS `k1` FROM `default_cluster:db1`.`tbl1` t2) " + Assert.assertEquals("SELECT `t`.`k1` AS `k1` FROM (WITH v1 AS (SELECT `t1`.`k1` " + + "FROM `default_cluster:db1`.`tbl1` t1),v2 AS (SELECT `t2`.`k1` FROM `default_cluster:db1`.`tbl1` t2) " + "SELECT `v1`.`k1` AS `k1` FROM `v1` UNION SELECT `v2`.`k1` AS `k1` FROM `v2`) t", stmt1.toSql()); String sql2 = @@ -807,8 +806,8 @@ public void testWithUnionToSql() throws Exception { + ") t"; SelectStmt stmt2 = (SelectStmt) UtFrameUtils.parseAndAnalyzeStmt(sql2, ctx); stmt2.rewriteExprs(new Analyzer(ctx.getEnv(), ctx).getExprRewriter()); - Assert.assertTrue(stmt2.toSql().contains("WITH v1 AS (SELECT `t1`.`k1` AS `k1` FROM " - + "`default_cluster:db1`.`tbl1` t1),v2 AS (SELECT `t2`.`k1` AS `k1` FROM `default_cluster:db1`.`tbl1` t2)")); + Assert.assertTrue(stmt2.toSql().contains("WITH v1 AS (SELECT `t1`.`k1` FROM `default_cluster:db1`.`tbl1` t1)," + + "v2 AS (SELECT `t2`.`k1` FROM `default_cluster:db1`.`tbl1` t2)")); } @Test 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 fa95e3bb09c23c..1f2e98ea7cedc3 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 @@ -189,9 +189,9 @@ public void testAlterView() throws Exception { alter1 = (View) db.getTableOrDdlException("alter1"); 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", + "WITH test1_cte(w1, w2) AS (SELECT `k1`, `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()); } } diff --git a/regression-test/data/correctness/test_view_with_with_clause.out b/regression-test/data/correctness/test_view_with_with_clause.out new file mode 100644 index 00000000000000..354fa4a2f99046 --- /dev/null +++ b/regression-test/data/correctness/test_view_with_with_clause.out @@ -0,0 +1,5 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +001 3 +002 1 + diff --git a/regression-test/suites/correctness/test_view_with_with_clause.groovy b/regression-test/suites/correctness/test_view_with_with_clause.groovy new file mode 100644 index 00000000000000..2bb072aaeee10e --- /dev/null +++ b/regression-test/suites/correctness/test_view_with_with_clause.groovy @@ -0,0 +1,51 @@ +/* + * 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. + */ + +suite("test_view_with_with_clause") { + + sql """ + CREATE TABLE IF NOT EXISTS test_view_with_with_clause (`a` date, `b` varchar(30) ,`c` varchar(30) ) + DUPLICATE KEY(`a`) + DISTRIBUTED BY HASH(`a`) + BUCKETS 10 + PROPERTIES("replication_allocation" = "tag.location.default:1"); + """ + + sql """insert into test_view_with_with_clause values ('2022-12-01','001','001001');""" + sql """insert into test_view_with_with_clause values ('2022-12-02','001','001002');""" + sql """insert into test_view_with_with_clause values ('2022-12-01','001','001003');""" + sql """insert into test_view_with_with_clause values ('2022-12-01','002','002001');""" + sql """insert into test_view_with_with_clause values ('2022-12-02','002','002001');""" + + sql """ + create view viewtest_test_view_with_with_clause (b,cnt) as + with aaa as ( + select b,count(distinct c) cnt + from test_view_with_with_clause + group by b + order by cnt desc + limit 10 + ) + select * from aaa; + """ + + qt_sql """ + select * from viewtest_test_view_with_with_clause; + """ +} \ No newline at end of file