Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected boolean shouldSelectIndexWithoutAgg(LogicalOlapScan scan) {
}
}

protected boolean containAllRequiredColumns(MaterializedIndex index, LogicalOlapScan scan,
protected static boolean containAllRequiredColumns(MaterializedIndex index, LogicalOlapScan scan,
Set<Slot> requiredScanOutput, Set<? extends Expression> requiredExpr, Set<Expression> predicateExpr) {
OlapTable table = scan.getTable();
MaterializedIndexMeta meta = table.getIndexMetaByIndexId(index.getId());
Expand Down Expand Up @@ -177,7 +177,7 @@ protected static boolean containsAllColumn(Expression expression, Set<String> mv
* 1. find matching key prefix most.
* 2. sort by row count, column count and index id.
*/
protected long selectBestIndex(
protected static long selectBestIndex(
List<MaterializedIndex> candidates,
LogicalOlapScan scan,
Set<Expression> predicates) {
Expand Down Expand Up @@ -212,7 +212,7 @@ protected long selectBestIndex(
return CollectionUtils.isEmpty(sortedIndexIds) ? scan.getTable().getBaseIndexId() : sortedIndexIds.get(0);
}

protected List<MaterializedIndex> matchPrefixMost(
protected static List<MaterializedIndex> matchPrefixMost(
LogicalOlapScan scan,
List<MaterializedIndex> candidate,
Set<Expression> predicates,
Expand All @@ -238,7 +238,7 @@ protected List<MaterializedIndex> matchPrefixMost(
* Filter the input conjuncts those can use prefix and split into 2 groups: is equal-to or non-equal-to predicate
* when comparing the key column.
*/
private Map<Boolean, Set<String>> filterCanUsePrefixIndexAndSplitByEquality(
private static Map<Boolean, Set<String>> filterCanUsePrefixIndexAndSplitByEquality(
Set<Expression> conjuncts, Map<ExprId, String> exprIdToColName) {
return conjuncts.stream()
.map(expr -> PredicateChecker.canUsePrefixIndex(expr, exprIdToColName))
Expand Down Expand Up @@ -332,7 +332,7 @@ private Optional<ExprId> check(Expression maybeSlot, Expression maybeConst) {
///////////////////////////////////////////////////////////////////////////
// Matching key prefix
///////////////////////////////////////////////////////////////////////////
private List<MaterializedIndex> matchKeyPrefixMost(
private static List<MaterializedIndex> matchKeyPrefixMost(
OlapTable table,
List<MaterializedIndex> indexes,
Set<String> equalColumns,
Expand All @@ -350,7 +350,7 @@ private List<MaterializedIndex> matchKeyPrefixMost(
return collect.descendingMap().firstEntry().getValue();
}

private int indexKeyPrefixMatchCount(
private static int indexKeyPrefixMatchCount(
OlapTable table,
MaterializedIndex index,
Set<String> equalColNames,
Expand All @@ -370,7 +370,7 @@ private int indexKeyPrefixMatchCount(
return matchCount;
}

protected boolean preAggEnabledByHint(LogicalOlapScan olapScan) {
protected static boolean preAggEnabledByHint(LogicalOlapScan olapScan) {
return olapScan.getHints().stream().anyMatch("PREAGGOPEN"::equalsIgnoreCase);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.rewrite.RewriteRuleFactory;
import org.apache.doris.nereids.rules.rewrite.mv.AbstractSelectMaterializedIndexRule.SlotContext;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.ExprId;
Expand Down Expand Up @@ -266,6 +267,20 @@ public List<Rule> buildRules() {

LogicalOlapScan mvPlan = createLogicalOlapScan(scan, result);
SlotContext slotContext = generateBaseScanExprToMvExpr(mvPlan);
if (result.indexId == scan.getTable().getBaseIndexId()) {
LogicalOlapScan mvPlanWithoutAgg = SelectMaterializedIndexWithoutAggregate.select(scan,
project::getInputSlots, filter::getConjuncts,
Stream.concat(filter.getExpressions().stream(),
project.getExpressions().stream())
.collect(ImmutableSet.toImmutableSet()));
SlotContext slotContextWithoutAgg = generateBaseScanExprToMvExpr(mvPlanWithoutAgg);

return agg.withChildren(new LogicalProject(
generateProjectsAlias(project.getOutput(), slotContextWithoutAgg),
new ReplaceExpressions(slotContextWithoutAgg).replace(
project.withChildren(filter.withChildren(mvPlanWithoutAgg)),
mvPlanWithoutAgg)));
}

if (result.exprRewriteMap.isEmpty()) {
return new LogicalProject<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public List<Rule> buildRules() {
* @param predicatesSupplier Supplier to get pushdown predicates.
* @return Result scan node.
*/
private LogicalOlapScan select(
public static LogicalOlapScan select(
LogicalOlapScan scan,
Supplier<Set<Slot>> requiredScanOutputSupplier,
Supplier<Set<Expression>> predicatesSupplier,
Expand Down Expand Up @@ -237,7 +237,7 @@ private LogicalOlapScan select(
}
}

private boolean isSameDataType(LogicalOlapScan scan, long selectIndex, Set<Slot> slots) {
private static boolean isSameDataType(LogicalOlapScan scan, long selectIndex, Set<Slot> slots) {
if (selectIndex != scan.getTable().getBaseIndexId()) {
Map<String, PrimitiveType> columnTypes =
scan.getTable().getSchemaByIndexId(selectIndex).stream().collect(Collectors
Expand All @@ -252,7 +252,7 @@ private boolean isSameDataType(LogicalOlapScan scan, long selectIndex, Set<Slot>
return true;
}

private boolean indexHasAggregate(MaterializedIndex index, LogicalOlapScan scan) {
private static boolean indexHasAggregate(MaterializedIndex index, LogicalOlapScan scan) {
return scan.getTable().getSchemaByIndexId(index.getId())
.stream()
.anyMatch(Column::isAggregated);
Expand Down
3 changes: 3 additions & 0 deletions regression-test/data/mv_p0/k1s2m3/k1s2m3.out
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
2 4
3 \N

-- !select_mv --
0

-- !select_star --
\N 4 \N d
-4 -4 -4 d
Expand Down
7 changes: 7 additions & 0 deletions regression-test/suites/mv_p0/k1s2m3/k1s2m3.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,12 @@ suite ("k1s2m3") {
}
qt_select_mv "select k1,sum(k2*k3) from d_table group by k1 order by k1;"

createMV("create materialized view kdup321 as select k3,k2,k1 from d_table;")
explain {
sql("select count(k2) from d_table where k3 = 1;")
contains "(kdup321)"
}
qt_select_mv "select count(k2) from d_table where k3 = 1;"

qt_select_star "select * from d_table order by k1;"
}