Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
Signed-off-by: shuming.li <[email protected]>
  • Loading branch information
LiShuMing committed Oct 14, 2024
1 parent 4dd4a44 commit a71acc1
Show file tree
Hide file tree
Showing 12 changed files with 808 additions and 504 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@
import com.starrocks.sql.analyzer.RelationFields;
import com.starrocks.sql.analyzer.RelationId;
import com.starrocks.sql.analyzer.Scope;
import com.starrocks.sql.analyzer.SelectAnalyzer;
import com.starrocks.sql.ast.UserIdentity;
import com.starrocks.sql.common.PRangeCell;
import com.starrocks.sql.optimizer.CachingMvPlanContextBuilder;
import com.starrocks.sql.optimizer.MvRewritePreprocessor;
import com.starrocks.sql.optimizer.Utils;
import com.starrocks.sql.optimizer.rule.mv.MVUtils;
import com.starrocks.sql.optimizer.rule.transformation.materialization.MvUtils;
import com.starrocks.sql.parser.SqlParser;
import com.starrocks.sql.plan.ExecPlan;
Expand Down Expand Up @@ -973,6 +975,7 @@ public void onReload() {
if (desiredActive && reloadActive) {
setActive();
}
analyzePartitionExprs();
} catch (Throwable e) {
LOG.error("reload mv failed: {}", this, e);
setInactiveAndReason("reload failed: " + e.getMessage());
Expand Down Expand Up @@ -1784,6 +1787,53 @@ public void gsonPostProcess() throws IOException {
}
}

private void analyzePartitionExprs() {
try {
Map<Table, Expr> refBaseTablePartitionExprs = getRefBaseTablePartitionExprs();
ConnectContext connectContext = new ConnectContext();
if (refBaseTablePartitionExprs != null) {
for (BaseTableInfo baseTableInfo : baseTableInfos) {
Optional<Table> refBaseTableOpt = MvUtils.getTable(baseTableInfo);
if (refBaseTableOpt.isEmpty()) {
continue;
}
Table refBaseTable = refBaseTableOpt.get();
if (!refBaseTablePartitionExprs.containsKey(refBaseTable)) {
continue;
}
Expr partitionExpr = refBaseTablePartitionExprs.get(refBaseTable);
TableName tableName = new TableName(baseTableInfo.getCatalogName(),
baseTableInfo.getDbName(), baseTableInfo.getTableName());
analyzePartitionExpr(connectContext, refBaseTable, tableName, partitionExpr);
}
}
} catch (Exception e) {
LOG.warn("Analyze partition exprs failed", e);
}
}

private void analyzePartitionExpr(ConnectContext connectContext,
Table refBaseTable,
TableName tableName,
Expr partitionExpr) {
if (partitionExpr == null) {
return;
}
if (tableName == null) {
return;
}
SelectAnalyzer.SlotRefTableNameCleaner visitor = MVUtils.buildSlotRefTableNameCleaner(
connectContext, refBaseTable, tableName);
partitionExpr.accept(visitor, null);
ExpressionAnalyzer.analyzeExpression(partitionExpr, new AnalyzeState(),
new Scope(RelationId.anonymous(),
new RelationFields(refBaseTable.getBaseSchema().stream()
.map(col -> new Field(col.getName(), col.getType(),
tableName, null))
.collect(Collectors.toList()))), connectContext);

}

/**
* Parse partition expr from sql
* @param sql serialized partition expr sql
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ public static ColumnRangePredicate andRange(
for (Range<ConstantOperator> range : rangePredicate.columnRanges.asRanges()) {
if (otherRangePredicate.columnRanges.intersects(range)) {
for (Range<ConstantOperator> otherRange : otherRangePredicate.columnRanges.asRanges()) {
if (range.isConnected(otherRange)) {
Range<ConstantOperator> intersection = range.intersection(otherRange);
if (!intersection.isEmpty()) {
ranges.add(intersection);
}
// once there is a range that is not connected with the range, the result is null
if (!range.isConnected(otherRange)) {
return null;
}
Range<ConstantOperator> intersection = range.intersection(otherRange);
if (!intersection.isEmpty()) {
ranges.add(intersection);
}
}
} else {
return null;
}
}
return new ColumnRangePredicate(rangePredicate.getExpression(), TreeRangeSet.create(ranges));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.sql.optimizer.rule.transformation.materialization;

import com.starrocks.sql.optimizer.operator.scalar.ConstantOperator;
import com.starrocks.sql.optimizer.operator.scalar.ScalarOperator;

public class ConstRangePredicate extends RangePredicate {
private final boolean isAlwaysTrue;

public ConstRangePredicate(boolean isAlwaysTrue) {
this.isAlwaysTrue = isAlwaysTrue;
}

// convert RangePredicate to ScalarOperator
public ScalarOperator toScalarOperator() {
return isAlwaysTrue ? ConstantOperator.TRUE : ConstantOperator.FALSE;
}
}
Loading

0 comments on commit a71acc1

Please sign in to comment.