Skip to content

Commit

Permalink
[fix](planner)should save original select list item before analyze (#…
Browse files Browse the repository at this point in the history
…28187)

* [fix](planner)should save original select list item before analyze

* fix test case

* fix failed case
  • Loading branch information
starocean999 authored Dec 25, 2023
1 parent 2411dbe commit 17f3ca7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public String toSql(int depth) {
StringBuilder stringBuilder = new StringBuilder();
switch (type) {
case CHAR:
if (isWildcardVarchar()) {
if (isWildcardChar()) {
stringBuilder.append("CHARACTER");
} else if (Strings.isNullOrEmpty(lenStr)) {
stringBuilder.append("CHAR").append("(").append(len).append(")");
Expand Down
30 changes: 23 additions & 7 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,14 @@ 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<>();
}
List<SelectListItem> items = selectList.getItems();
for (int i = 0; i < items.size(); i++) {
SelectListItem item = items.get(i);
Expand All @@ -574,6 +580,11 @@ public void analyze(Analyzer analyzer) throws UserException {
expandStar(analyzer, tblName);
}
} else {
// save originalExpr before being analyzed
// because analyze may change the expr by adding cast or some other stuff
if (needToSql) {
originalExpr.add(item.getExpr().clone());
}
// Analyze the resultExpr before generating a label to ensure enforcement
// of expr child and depth limits (toColumn() label may call toSql()).
item.getExpr().analyze(analyzer);
Expand Down Expand Up @@ -647,10 +658,6 @@ public void analyze(Analyzer analyzer) throws UserException {
subColPath.add(expr.toSubColumnLabel());
}
}
// analyze valueList if exists
if (needToSql) {
originalExpr = Expr.cloneList(resultExprs);
}

// analyze selectListExprs
Expr.analyze(resultExprs, analyzer);
Expand Down Expand Up @@ -1252,6 +1259,9 @@ private void expandStar(TableName tblName, TupleDescriptor desc) throws Analysis
slot.setTable(desc.getTable());
slot.setTupleId(desc.getId());
resultExprs.add(rewriteQueryExprByMvColumnExpr(slot, analyzer));
if (needToSql) {
originalExpr.add(slot);
}
colLabels.add(col.getName());
// empty sub lables
subColPath.add(Lists.newArrayList());
Expand Down Expand Up @@ -2601,6 +2611,9 @@ public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
tblRef.analyze(analyzer);
leftTblRef = tblRef;
}
if (needToSql) {
originalExpr = new ArrayList<>();
}
// populate selectListExprs, aliasSMap, and colNames
for (SelectListItem item : selectList.getItems()) {
if (item.isStar()) {
Expand All @@ -2611,6 +2624,11 @@ public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
expandStar(analyzer, tblName);
}
} else {
if (needToSql) {
// save originalExpr before being analyzed
// because analyze may change the expr by adding cast or some other stuff
originalExpr.add(item.getExpr().clone());
}
// to make sure the sortinfo's AnalyticExpr and resultExprs's AnalyticExpr analytic once
if (item.getExpr() instanceof AnalyticExpr) {
item.getExpr().analyze(analyzer);
Expand All @@ -2624,9 +2642,7 @@ public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
resultExprs.add(rewriteQueryExprByMvColumnExpr(item.getExpr(), analyzer));
}
}
if (needToSql) {
originalExpr = Expr.cloneList(resultExprs);
}

// substitute group by
if (groupByClause != null) {
boolean aliasFirst = false;
Expand Down
2 changes: 1 addition & 1 deletion regression-test/data/ddl_p0/test_create_view.out
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
3 [-1, 20, 0] [0, 1, 0]

-- !test_view_6 --
v1 CREATE VIEW `v1` COMMENT 'VIEW' AS SELECT `error_code` AS `error_code`, 1 AS `__literal_1`, 'string' AS `__literal_2`, now() AS `__now_3`, dayofyear(`op_time`) AS `__dayofyear_4`, CAST(`source` AS BIGINT) AS `__cast_expr_5`, min(`timestamp`) OVER (ORDER BY `op_time` DESC NULLS LAST ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) AS `__min_6`, 1 > 2 AS `__binary_predicate_7`, (2 + 3) AS `__arithmetic_expr_8`, 1 IN (1, 2, 3, 4) AS `__in_predicate_9`, `remark` LIKE '%like' AS `__like_predicate_10`, CASE WHEN `remark` = 's' THEN 1 ELSE 2 END AS `__case_expr_11`, (CAST(TRUE AS BIGINT) | CAST(FALSE AS BIGINT)) AS `__arithmetic_expr_12` FROM `regression_test_ddl_p0`.`view_column_name_test`;
v1 CREATE VIEW `v1` COMMENT 'VIEW' AS SELECT `error_code` AS `error_code`, 1 AS `__literal_1`, 'string' AS `__literal_2`, now() AS `__now_3`, dayofyear(`op_time`) AS `__dayofyear_4`, CAST(`source` AS BIGINT) AS `__cast_expr_5`, min(`timestamp`) OVER (ORDER BY `op_time` DESC NULLS LAST ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) AS `__min_6`, 1 > 2 AS `__binary_predicate_7`, (2 + 3) AS `__arithmetic_expr_8`, 1 IN (1, 2, 3, 4) AS `__in_predicate_9`, `remark` LIKE '%like' AS `__like_predicate_10`, CASE WHEN `remark` = 's' THEN 1 ELSE 2 END AS `__case_expr_11`, (TRUE | FALSE) AS `__arithmetic_expr_12` FROM `regression_test_ddl_p0`.`view_column_name_test`;

28 changes: 28 additions & 0 deletions regression-test/suites/view_p0/view_p0.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,32 @@ suite("view_p0") {
sql """CREATE VIEW IF NOT EXISTS `test_view_abc`(`a`) AS WITH T1 AS (SELECT 1 AS 'a'), T2 AS (SELECT 2 AS 'a') SELECT T1.a FROM T1 UNION ALL SELECT T2.a FROM T2;"""

sql "drop view if exists test_view_abc;"

sql """DROP TABLE IF EXISTS test_view_table2"""

sql """
CREATE TABLE test_view_table2 (
c_date varchar(50)
)
ENGINE=OLAP
UNIQUE KEY(`c_date`)
distributed by hash(c_date) properties('replication_num'='1');
"""

sql """ drop view if exists test_view_table2_view;"""
sql """CREATE VIEW `test_view_table2_view`
AS
SELECT
date_format(c_date,'%Y-%m-%d') AS `CREATE_DATE`
FROM
test_view_table2
GROUP BY
date_format(c_date, '%Y-%m-%d');
"""
sql """set enable_nereids_planner=false;"""
sql """select * from test_view_table2_view;"""
sql """set enable_nereids_planner=true;"""
sql """select * from test_view_table2_view;"""
sql """ drop view if exists test_view_table2_view;"""
sql """DROP TABLE IF EXISTS test_view_table2"""
}

0 comments on commit 17f3ca7

Please sign in to comment.