[fix](planner) create view generate wrong sql when sql contains multi count distinct (#16092)

If sql in create view has more than one count distinct, and write column name explicitly.
We will generate sql contains function multi_count_distinct.
It cannot be analyzed and all query containing this view will fail.
This commit is contained in:
morrySnow
2023-01-31 23:42:53 +08:00
committed by GitHub
parent 6470ae58ea
commit c63a960df6
3 changed files with 80 additions and 1 deletions

View File

@ -117,6 +117,7 @@ public class BaseViewStmt extends DdlStmt {
Analyzer tmpAnalyzer = new Analyzer(analyzer);
List<String> colNames = cols.stream().map(c -> c.getColName()).collect(Collectors.toList());
cloneStmt.setNeedToSql(true);
cloneStmt.substituteSelectList(tmpAnalyzer, colNames);
try (ToSqlContext toSqlContext = ToSqlContext.getOrNewThreadLocalContext()) {

View File

@ -2030,7 +2030,7 @@ public class SelectStmt extends QueryStmt {
if (i != 0) {
strBuilder.append(", ");
}
if (needToSql) {
if (needToSql && CollectionUtils.isNotEmpty(originalExpr)) {
strBuilder.append(originalExpr.get(i).toSql());
} else {
strBuilder.append(resultExprs.get(i).toSql());
@ -2204,6 +2204,9 @@ public class SelectStmt extends QueryStmt {
// Resolve and replace non-InlineViewRef table refs with a BaseTableRef or ViewRef.
TableRef tblRef = fromClause.get(i);
tblRef = analyzer.resolveTableRef(tblRef);
if (tblRef instanceof InlineViewRef) {
((InlineViewRef) tblRef).setNeedToSql(needToSql);
}
Preconditions.checkNotNull(tblRef);
fromClause.set(i, tblRef);
tblRef.setLeftTblRef(leftTblRef);
@ -2233,6 +2236,9 @@ public class SelectStmt extends QueryStmt {
resultExprs.add(item.getExpr());
}
}
if (needToSql) {
originalExpr = Expr.cloneList(resultExprs);
}
// substitute group by
if (groupByClause != null) {
substituteOrdinalsAliases(groupByClause.getGroupingExprs(), "GROUP BY", analyzer, false);