Skip to content

Commit

Permalink
[Fix](planner) fix create view star except and modify cast to sql (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
feiniaofeiafei committed May 24, 2024
1 parent 0b414ba commit 01c06f3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public String toSql(int depth) {
break;
case VARCHAR:
if (isWildcardVarchar()) {
stringBuilder.append("VARCHAR(*)");
return "VARCHAR(" + MAX_VARCHAR_LENGTH + ")";
} else if (Strings.isNullOrEmpty(lenStr)) {
stringBuilder.append("VARCHAR").append("(").append(len).append(")");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public String toSqlImpl() {
return getChild(0).toSql();
}
if (isAnalyzed) {
return "CAST(" + getChild(0).toSql() + " AS " + type.toString() + ")";
return "CAST(" + getChild(0).toSql() + " AS " + type.toSql() + ")";
} else {
return "CAST(" + getChild(0).toSql() + " AS "
+ (isImplicit ? type.toString() : targetTypeDef.toSql()) + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ public void analyze(Analyzer analyzer) throws UserException {
}
// populate selectListExprs, aliasSMap, groupingSmap and colNames
if (selectList.isExcept()) {
if (needToSql) {
originalExpr = new ArrayList<>();
}
List<SelectListItem> items = selectList.getItems();
TableName tblName = items.get(0).getTblName();
if (tblName == null) {
Expand All @@ -550,10 +553,6 @@ public void analyze(Analyzer analyzer) throws UserException {
// remove excepted columns
resultExprs.removeIf(expr -> exceptCols.contains(expr.toColumnLabel()));
colLabels.removeIf(exceptCols::contains);
if (needToSql) {
originalExpr = Expr.cloneList(resultExprs);
}

} else {
if (needToSql) {
originalExpr = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ public void testVarcharLength() throws Exception {
String showStr = showResultSet.getResultRows().get(0).get(1);
Assertions.assertEquals(
"CREATE TABLE `varchar_len1` (\n"
+ " `_col0` varchar(*) NULL,\n"
+ " `_col1` varchar(*) NULL,\n"
+ " `_col0` varchar(65533) NULL,\n"
+ " `_col1` varchar(65533) NULL,\n"
+ " `userId` varchar(255) NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(`_col0`)\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void tableFunctionInWhere() throws Exception {
String sql = "explain select /*+ SET_VAR(enable_nereids_planner=false) */ k1 from db1.tbl1 where explode_split(k2, \",\");";
String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql);
Assert.assertTrue(explainString,
explainString.contains("No matching function with signature: explode_split(varchar(1), varchar(*))."));
explainString.contains("No matching function with signature: explode_split(VARCHAR(1), VARCHAR(65533))."));
}

// test projection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !test_select_star_except --
1 1
2 1
3 5
4 5
6 \N

-- !test_sql --
v_mal_old_create_view2 CREATE VIEW `v_mal_old_create_view2` COMMENT 'VIEW' AS SELECT CAST(CAST(`a` AS TEXT) AS TIME(0)) AS `__cast_expr_0` FROM `regression_test_view_p0`.`mal_old_create_view`; utf8mb4 utf8mb4_0900_bin

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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("create_view_star_except_and_cast_to_sql") {
sql "SET enable_nereids_planner=false;"

sql """
DROP TABLE IF EXISTS mal_old_create_view
"""
sql """
create table mal_old_create_view(pk int, a int, b int) distributed by hash(pk) buckets 10
properties('replication_num' = '1');
"""

sql """
insert into mal_old_create_view values(2,1,3),(1,1,2),(3,5,6),(6,null,6),(4,5,6);
"""
sql "sync"
sql "drop view if EXISTS v_mal_old_create_view"

sql "create view v_mal_old_create_view as select * except(a) from mal_old_create_view"

qt_test_select_star_except "select * from v_mal_old_create_view order by pk,b"

sql "drop view if EXISTS v_mal_old_create_view2"

sql "create view v_mal_old_create_view2 as select cast(cast(a as string) as time) from mal_old_create_view"
qt_test_sql "show create view v_mal_old_create_view2"
sql "select * from v_mal_old_create_view2"

}

0 comments on commit 01c06f3

Please sign in to comment.