Skip to content

Commit

Permalink
[SR-4368] Disable cast statements when creating materialized views (#78)
Browse files Browse the repository at this point in the history
The cast expr is not supported in materialized view. So should be prohibited.
```
create materialized view groupk1
as
select k1, sum(cast(v1 as double))
from demo_spark_load_tbl
group by k1 
order by k1;
```
  • Loading branch information
sevev authored Sep 15, 2021
1 parent 5b39560 commit 6062e8f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ public void gsonPostProcess() throws IOException {
connectContext.setCluster(SystemInfoService.DEFAULT_CLUSTER);
connectContext.setDatabase(Catalog.getCurrentCatalog().getDb(dbId).getFullName());
Analyzer analyzer = new Analyzer(Catalog.getCurrentCatalog(), connectContext);
analyzer.setIgnoreCast();
CreateMaterializedViewStmt stmt = null;
try {
stmt = (CreateMaterializedViewStmt) SqlParserUtils.getStmt(parser, origStmt.idx);
Expand Down
12 changes: 12 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ public class Analyzer {
// timezone specified for some operation, such as broker load
private String timezone = TimeUtils.DEFAULT_TIME_ZONE;

// Whether to ignore cast expressions
// Compatibility with older versions, maybe delete in near future
private boolean ignoreCast = false;

public void setIgnoreCast() {
ignoreCast = true;
}

public boolean ignoreCast() {
return ignoreCast;
}

public void setIsSubquery() {
isSubquery = true;
globalState.containsSubquery = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void analyze(Analyzer analyzer) throws UserException {
if (selectStmt.getAggInfo() != null) {
mvKeysType = KeysType.AGG_KEYS;
}
analyzeSelectClause();
analyzeSelectClause(analyzer.ignoreCast());
analyzeFromClause();
if (selectStmt.getWhereClause() != null) {
throw new AnalysisException("The where clause is not supported in add materialized view clause, expr:"
Expand All @@ -157,7 +157,7 @@ public void analyze(Analyzer analyzer) throws UserException {
}
}

public void analyzeSelectClause() throws AnalysisException {
public void analyzeSelectClause(boolean ignoreCast) throws AnalysisException {
SelectList selectList = selectStmt.getSelectList();
if (selectList.getItems().isEmpty()) {
throw new AnalysisException("The materialized view must contain at least one column");
Expand Down Expand Up @@ -206,6 +206,10 @@ public void analyzeSelectClause() throws AnalysisException {
throw new AnalysisException(
"The function " + functionName + " must match pattern:" + mvColumnPattern.toString());
}
if (!ignoreCast && functionCallExpr.getChild(0) instanceof CastExpr) {
throw new AnalysisException(
"The function " + functionName + " disable cast expression");
}
// check duplicate column
List<SlotRef> slots = new ArrayList<>();
functionCallExpr.collect(SlotRef.class, slots);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@ public void testCorrectSum() {
}

@Test
public void testCorrectMin(@Injectable CastExpr castExpr) {
public void testCorrectMin() {
TableName tableName = new TableName("db", "table");
SlotRef slotRef = new SlotRef(tableName, "c1");
List<Expr> child0Params = Lists.newArrayList();
child0Params.add(slotRef);
new Expectations() {
{
castExpr.unwrapSlotRef();
result = slotRef;
}
};
List<Expr> params = Lists.newArrayList();
params.add(castExpr);
params.add(slotRef);
FunctionCallExpr functionCallExpr = new FunctionCallExpr(AggregateType.MIN.name(), params);
MVColumnOneChildPattern mvColumnOneChildPattern = new MVColumnOneChildPattern(
AggregateType.MIN.name().toLowerCase());
Expand Down

0 comments on commit 6062e8f

Please sign in to comment.