Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikyou1997 committed Dec 2, 2022
1 parent e5000c7 commit 11a9168
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public abstract class QueryStmt extends StatementBase implements Queriable {
*/
private Set<TupleId> disableTuplesMVRewriter = Sets.newHashSet();

protected boolean toSQLWithSelectList;

QueryStmt(ArrayList<OrderByElement> orderByElements, LimitElement limitElement) {
this.orderByElements = orderByElements;
this.limitElement = limitElement;
Expand Down Expand Up @@ -805,4 +807,10 @@ public OutFileClause getOutFileClause() {
public boolean hasOutFileClause() {
return outFileClause != null;
}

public String toSqlWithSelectList() {
toSQLWithSelectList = true;
return toSql();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) ? ", " : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
"""
}

0 comments on commit 11a9168

Please sign in to comment.