Skip to content

Commit dfc0e14

Browse files
committed
[fix](planner)should save original select list item before analyze
1 parent c41562a commit dfc0e14

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,14 @@ public void analyze(Analyzer analyzer) throws UserException {
560560
// remove excepted columns
561561
resultExprs.removeIf(expr -> exceptCols.contains(expr.toColumnLabel()));
562562
colLabels.removeIf(exceptCols::contains);
563+
if (needToSql) {
564+
originalExpr = Expr.cloneList(resultExprs);
565+
}
563566

564567
} else {
568+
if (needToSql) {
569+
originalExpr = new ArrayList<>();
570+
}
565571
List<SelectListItem> items = selectList.getItems();
566572
for (int i = 0; i < items.size(); i++) {
567573
SelectListItem item = items.get(i);
@@ -573,6 +579,11 @@ public void analyze(Analyzer analyzer) throws UserException {
573579
expandStar(analyzer, tblName);
574580
}
575581
} else {
582+
// save originalExpr before being analyzed
583+
// because analyze may change the expr by adding cast or some other stuff
584+
if (needToSql) {
585+
originalExpr.add(item.getExpr().clone());
586+
}
576587
// Analyze the resultExpr before generating a label to ensure enforcement
577588
// of expr child and depth limits (toColumn() label may call toSql()).
578589
item.getExpr().analyze(analyzer);
@@ -646,10 +657,6 @@ public void analyze(Analyzer analyzer) throws UserException {
646657
subColPath.add(expr.toSubColumnLabel());
647658
}
648659
}
649-
// analyze valueList if exists
650-
if (needToSql) {
651-
originalExpr = Expr.cloneList(resultExprs);
652-
}
653660

654661
// analyze selectListExprs
655662
Expr.analyze(resultExprs, analyzer);
@@ -1252,6 +1259,9 @@ private void expandStar(TableName tblName, TupleDescriptor desc) throws Analysis
12521259
slot.setTable(desc.getTable());
12531260
slot.setTupleId(desc.getId());
12541261
resultExprs.add(rewriteQueryExprByMvColumnExpr(slot, analyzer));
1262+
if (needToSql) {
1263+
originalExpr.add(slot);
1264+
}
12551265
colLabels.add(col.getName());
12561266
// empty sub lables
12571267
subColPath.add(Lists.newArrayList());
@@ -2504,6 +2514,9 @@ public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
25042514
tblRef.analyze(analyzer);
25052515
leftTblRef = tblRef;
25062516
}
2517+
if (needToSql) {
2518+
originalExpr = new ArrayList<>();
2519+
}
25072520
// populate selectListExprs, aliasSMap, and colNames
25082521
for (SelectListItem item : selectList.getItems()) {
25092522
if (item.isStar()) {
@@ -2514,6 +2527,11 @@ public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
25142527
expandStar(analyzer, tblName);
25152528
}
25162529
} else {
2530+
if (needToSql) {
2531+
// save originalExpr before being analyzed
2532+
// because analyze may change the expr by adding cast or some other stuff
2533+
originalExpr.add(item.getExpr().clone());
2534+
}
25172535
// to make sure the sortinfo's AnalyticExpr and resultExprs's AnalyticExpr analytic once
25182536
if (item.getExpr() instanceof AnalyticExpr) {
25192537
item.getExpr().analyze(analyzer);
@@ -2527,9 +2545,7 @@ public void substituteSelectList(Analyzer analyzer, List<String> newColLabels)
25272545
resultExprs.add(rewriteQueryExprByMvColumnExpr(item.getExpr(), analyzer));
25282546
}
25292547
}
2530-
if (needToSql) {
2531-
originalExpr = Expr.cloneList(resultExprs);
2532-
}
2548+
25332549
// substitute group by
25342550
if (groupByClause != null) {
25352551
boolean aliasFirst = false;

regression-test/suites/view_p0/view_p0.groovy

+28
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,32 @@ suite("view_p0") {
136136
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;"""
137137

138138
sql "drop view if exists test_view_abc;"
139+
140+
sql """DROP TABLE IF EXISTS test_view_table2"""
141+
142+
sql """
143+
CREATE TABLE test_view_table2 (
144+
c_date varchar(50)
145+
)
146+
ENGINE=OLAP
147+
UNIQUE KEY(`c_date`)
148+
distributed by hash(c_date) properties('replication_num'='1');
149+
"""
150+
151+
sql """ drop view if exists test_view_table2_view;"""
152+
sql """CREATE VIEW `test_view_table2_view`
153+
AS
154+
SELECT
155+
date_format(c_date,'%Y-%m-%d') AS `CREATE_DATE`
156+
FROM
157+
test_view_table2
158+
GROUP BY
159+
date_format(c_date, '%Y-%m-%d');
160+
"""
161+
sql """set enable_nereids_planner=false;"""
162+
sql """select * from test_view_table2_view;"""
163+
sql """set enable_nereids_planner=true;"""
164+
sql """select * from test_view_table2_view;"""
165+
sql """ drop view if exists test_view_table2_view;"""
166+
sql """DROP TABLE IF EXISTS test_view_table2"""
139167
}

0 commit comments

Comments
 (0)