[fix] fix invalid SQL rewrite for field in materialized view (#9877)

This commit is contained in:
Kikyou1997
2022-06-02 23:43:13 +08:00
committed by GitHub
parent 3241cc2bfd
commit 67fa1fcf2a
3 changed files with 34 additions and 7 deletions

View File

@ -22,7 +22,6 @@ import org.apache.doris.analysis.CreateMaterializedViewStmt;
import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.FunctionCallExpr;
import org.apache.doris.analysis.SlotRef;
import org.apache.doris.analysis.TableName;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.FunctionSet;
import org.apache.doris.catalog.OlapTable;
@ -81,15 +80,18 @@ public class CountFieldToSum implements ExprRewriteRule {
}
// rewrite expr
return rewriteExpr(fnChild0, mvColumn, analyzer);
return rewriteExpr(mvColumn, analyzer);
}
private Expr rewriteExpr(SlotRef queryColumnSlotRef, Column mvColumn, Analyzer analyzer) {
private Expr rewriteExpr(Column mvColumn, Analyzer analyzer) {
Preconditions.checkNotNull(mvColumn);
Preconditions.checkNotNull(queryColumnSlotRef);
TableName tableName = queryColumnSlotRef.getTableName();
Preconditions.checkNotNull(tableName);
SlotRef mvSlotRef = new SlotRef(tableName, mvColumn.getName());
// Notice that we shouldn't set table name field of mvSlotRef here, for we will analyze the new mvSlotRef
// later, if the table name was set here, the Analyzer::registerColumnRef would invoke
// Analyzer::resolveColumnRef(TableName, String) which only try to find the column from the tupleByAlias,
// as at the most time the alias is not equal with the origin table name, so it would cause the unexpected
// exception to Unknown column, because we can't find an alias which named as origin table name that has
// required column.
SlotRef mvSlotRef = new SlotRef(null, mvColumn.getName());
List<Expr> newFnParams = Lists.newArrayList();
newFnParams.add(mvSlotRef);
FunctionCallExpr result = new FunctionCallExpr("sum", newFnParams);

View File

@ -850,4 +850,14 @@ public class MaterializedViewFunctionTest {
dorisAssert.query(query).explainContains("mv");
dorisAssert.dropTable("agg_table", true);
}
@Test
public void testSelectMVWithTableAlias() throws Exception {
String createUserTagMVSql = "create materialized view " + USER_TAG_MV_NAME + " as select user_id, "
+ "count(tag_id) from " + USER_TAG_TABLE_NAME + " group by user_id;";
dorisAssert.withMaterializedView(createUserTagMVSql);
String query = "select count(tag_id) from " + USER_TAG_TABLE_NAME + " t ;";
String mvColumnName = CreateMaterializedViewStmt.mvColumnBuilder(FunctionSet.COUNT, "tag_id");
dorisAssert.query(query).explainContains(USER_TAG_MV_NAME, mvColumnName);
}
}

View File

@ -76,8 +76,23 @@ suite("test_materialized_view", "rollup") {
qt_sql "SELECT store_id, sum(sale_amt) FROM ${tbName1} GROUP BY store_id order by store_id;"
qt_sql "SELECT * FROM ${tbName2} order by record_id;"
qt_sql "SELECT store_id, sum(sale_amt) FROM ${tbName2} GROUP BY store_id order by store_id;"
sql "CREATE materialized VIEW amt_count AS SELECT store_id, count(sale_amt) FROM ${tbName1} GROUP BY store_id;"
res = "null"
while (!res.contains("FINISHED") || !res.contains("amt_count")){
res = sql "SHOW ALTER TABLE MATERIALIZED VIEW WHERE TableName='${tbName1}' ORDER BY CreateTime DESC LIMIT 1;"
if(res.contains("CANCELLED")){
print("job is cancelled")
break
}
Thread.sleep(1000)
}
sql "SELECT store_id, count(sale_amt) FROM ${tbName1} t GROUP BY store_id;"
sql "DROP TABLE ${tbName1}"
sql "DROP TABLE ${tbName2}"
}