Skip to content

Commit

Permalink
[BugFix] Fix partition prune for str2date materialized views with in …
Browse files Browse the repository at this point in the history
…predicate (#35001)

Signed-off-by: shuming.li <ming.moriarty@gmail.com>
  • Loading branch information
LiShuMing authored Nov 15, 2023
1 parent 24da46d commit 77fb6ae
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,11 @@ public static boolean isConvertToDate(Column partitionColumn, PartitionColumnFil
if (partitionColumnFilter == null || partitionColumn == null) {
return false;
}
LiteralExpr lowerBound = partitionColumnFilter.getLowerBound();
LiteralExpr upperBound = partitionColumnFilter.getUpperBound();
LiteralExpr literalExpr = (lowerBound == null) ? upperBound : lowerBound;
if (literalExpr == null) {
Type filterType = partitionColumnFilter.getFilterType();
if (filterType == null) {
return false;
}
return isConvertToDate(partitionColumn.getType(), literalExpr.getType());
return isConvertToDate(partitionColumn.getType(), filterType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@
import com.google.common.collect.Range;
import com.starrocks.analysis.InPredicate;
import com.starrocks.analysis.LiteralExpr;
import com.starrocks.connector.PartitionUtil;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.PartitionValue;
import com.starrocks.catalog.Column;
import com.starrocks.catalog.PartitionKey;
import com.starrocks.catalog.Type;
import com.starrocks.common.AnalysisException;
import com.starrocks.connector.PartitionUtil;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.PartitionValue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -169,6 +170,22 @@ public Range<PartitionKey> getRange(List<Column> columns) {
return null;
}

public Type getFilterType() {
if (lowerBound != null) {
return lowerBound.getType();
}
if (upperBound != null) {
return upperBound.getType();
}
if (inPredicate != null) {
return inPredicate.getChild(0).getType();
}
if (inPredicateLiterals != null && !inPredicateLiterals.isEmpty()) {
return inPredicateLiterals.get(0).getType();
}
return null;
}

@Override
public String toString() {
String str = "";
Expand All @@ -189,6 +206,11 @@ public String toString() {
} else {
str += "\ninPredicate is " + inPredicate;
}
if (null == inPredicateLiterals || inPredicateLiterals.isEmpty()) {
str += "\ninPredicateLiterals is UNSET";
} else {
str += "\ninPredicateLiterals is " + inPredicateLiterals;
}
return str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

package com.starrocks.planner;

import com.google.common.base.Preconditions;
import com.google.common.collect.BoundType;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
Expand Down Expand Up @@ -113,9 +114,10 @@ private List<Long> prune(RangeMap<PartitionKey, Long> rangeMap,
}

boolean isConvertToDate = PartitionUtil.isConvertToDate(keyColumn, filter);
if (null == inPredicateLiterals || inPredicateLiterals.size() * complex > inPredicateMaxLen) {
LiteralExpr lowerBound = filter.getLowerBound();
LiteralExpr upperBound = filter.getUpperBound();
LiteralExpr lowerBound = filter.getLowerBound();
LiteralExpr upperBound = filter.getUpperBound();
if (null == inPredicateLiterals || ((lowerBound != null || upperBound != null) &&
inPredicateLiterals.size() * complex > inPredicateMaxLen)) {
if (filter.lowerBoundInclusive && filter.upperBoundInclusive
&& lowerBound != null && upperBound != null
&& 0 == lowerBound.compareLiteral(upperBound)) {
Expand Down Expand Up @@ -195,8 +197,16 @@ private List<Long> prune(RangeMap<PartitionKey, Long> rangeMap,
Set<Long> resultSet = Sets.newHashSet();
int newComplex = inPredicateLiterals.size() * complex;
for (LiteralExpr expr : inPredicateLiterals) {
minKey.pushColumn(expr, keyColumn.getPrimitiveType());
maxKey.pushColumn(expr, keyColumn.getPrimitiveType());
if (isConvertToDate) {
LiteralExpr convertExpr = PartitionUtil.convertToDateLiteral(expr);
Preconditions.checkState(convertExpr.getType().equals(keyColumn.getType()));
minKey.pushColumn(convertExpr, keyColumn.getPrimitiveType());
maxKey.pushColumn(convertExpr, keyColumn.getPrimitiveType());
} else {
Preconditions.checkState(expr.getType().equals(keyColumn.getType()));
minKey.pushColumn(expr, keyColumn.getPrimitiveType());
maxKey.pushColumn(expr, keyColumn.getPrimitiveType());
}
Collection<Long> subList = prune(rangeMap, columnIdx + 1, minKey, maxKey, newComplex);
resultSet.addAll(subList);
minKey.popColumn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ private static Pair<ScalarOperator, List<ScalarOperator>> prunePartitionPredicat
if (null != lowerBound) {
lowerBind = false;
PartitionKey min = new PartitionKey();
// TODO: take care `isConvertToDate` for olap table
min.pushColumn(pcf.getLowerBound(), column.getPrimitiveType());
int cmp = minRange.compareTo(min);
if (cmp > 0 || (0 == cmp && pcf.lowerBoundInclusive)) {
Expand All @@ -190,7 +189,6 @@ private static Pair<ScalarOperator, List<ScalarOperator>> prunePartitionPredicat
if (null != upperBound) {
upperBind = false;
PartitionKey max = new PartitionKey();
// TODO: take care `isConvertToDate` for olap table
max.pushColumn(upperBound, column.getPrimitiveType());
int cmp = maxRange.compareTo(max);
if (cmp < 0 || (0 == cmp && pcf.upperBoundInclusive)) {
Expand Down
Loading

0 comments on commit 77fb6ae

Please sign in to comment.