[fix](planner) Disable infer expr column name when query on old optimizer (#25317)

Disable infer expr column name when query on old optimizer.
This bug is be brought in #24990

if your query SQL is
select id, name, sum(target) FROM db_test.table_test2 group by id, name;
the result column name when query is as following:
|id|name |sum(cast(target as DOUBLE))|

when you create view as following:
CREATE VIEW v1 as select id, name, sum(target) FROM db_test.table_test2 group by id, name;
then query the view v1, the result is as following:
|id|name |__sum_2|
This commit is contained in:
JingDas
2023-10-16 15:08:52 +08:00
committed by GitHub
parent 1a27ac8d56
commit 8e9e1b1bfd
8 changed files with 113 additions and 56 deletions

View File

@ -21,6 +21,7 @@ import org.apache.doris.catalog.Env;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.UserException;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.qe.ConnectContext;
@ -42,7 +43,8 @@ public class AlterMaterializedViewStmt extends DdlStmt {
}
@Override
public void analyze(Analyzer analyzer) throws AnalysisException {
public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
super.analyze(analyzer);
mvName.analyze(analyzer);
if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(ConnectContext.get(), mvName.getDb(), mvName.getTbl(),
PrivPredicate.ALTER)) {

View File

@ -320,6 +320,10 @@ public class Analyzer {
// analysis.
public boolean isExplain;
// Record the statement clazz that the analyzer would to analyze,
// this give an opportunity to control analyzing behavior according to the statement type.
public Class<? extends StatementBase> rootStatementClazz;
// Indicates whether the query has plan hints.
public boolean hasPlanHints = false;
@ -568,6 +572,14 @@ public class Analyzer {
return globalState.isExplain;
}
public void setRootStatementClazz(Class<? extends StatementBase> statementClazz) {
globalState.rootStatementClazz = statementClazz;
}
public Class<? extends StatementBase> getRootStatementClazz() {
return globalState.rootStatementClazz;
}
public int incrementCallDepth() {
return ++callDepth;
}

View File

@ -72,6 +72,7 @@ public class CreateTableAsSelectStmt extends DdlStmt {
// To avoid duplicate registrations of table/colRefs,
// create a new root analyzer and clone the query statement for this initial pass.
Analyzer dummyRootAnalyzer = new Analyzer(analyzer.getEnv(), analyzer.getContext());
super.analyze(dummyRootAnalyzer);
QueryStmt tmpStmt = queryStmt.clone();
tmpStmt.analyze(dummyRootAnalyzer);
this.queryStmt = tmpStmt;

View File

@ -55,6 +55,7 @@ public class CreateViewStmt extends BaseViewStmt {
@Override
public void analyze(Analyzer analyzer) throws UserException {
super.analyze(analyzer);
tableName.analyze(analyzer);
FeNameFormat.checkTableName(tableName.getTbl());
viewDefStmt.setNeedToSql(true);

View File

@ -120,7 +120,6 @@ public class SelectListItem {
* Return a column label for the select list item. Without generate column name
* automatically.
*/
@Deprecated
public String toColumnLabel() {
Preconditions.checkState(!isStar());
if (alias != null) {

View File

@ -543,7 +543,17 @@ public class SelectStmt extends QueryStmt {
throw new AnalysisException("Subquery is not supported in the select list.");
}
resultExprs.add(rewriteQueryExprByMvColumnExpr(item.getExpr(), analyzer));
String columnLabel = item.toColumnLabel(i);
String columnLabel = null;
Class<? extends StatementBase> statementClazz = analyzer.getRootStatementClazz();
if (statementClazz != null && !QueryStmt.class.isAssignableFrom(statementClazz)) {
// Infer column name when item is expr
columnLabel = item.toColumnLabel(i);
}
if (columnLabel == null) {
// column label without position is applicative for query and do not infer
// column name when item is expr
columnLabel = item.toColumnLabel();
}
SlotRef aliasRef = new SlotRef(null, columnLabel);
Expr existingAliasExpr = aliasSMap.get(aliasRef);
if (existingAliasExpr != null && !existingAliasExpr.equals(item.getExpr())) {

View File

@ -78,12 +78,17 @@ public abstract class StatementBase implements ParseNode {
* were missing from the catalog.
* It is up to the analysis() implementation to ensure the maximum number of missing
* tables/views get collected in the Analyzer before failing analyze().
* Should call the method firstly when override the method, the analyzer param should be
* the one which statement would use.
*/
public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
if (isAnalyzed()) {
return;
}
this.analyzer = analyzer;
if (analyzer.getRootStatementClazz() == null) {
analyzer.setRootStatementClazz(this.getClass());
}
if (Strings.isNullOrEmpty(analyzer.getClusterName())) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_CLUSTER_NO_SELECT_CLUSTER);
}