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 @@ -116,7 +116,8 @@ private RelatedTableColumnInfo(NamedExpression partitionNamedExpression,
*/
public BaseTableInfo getTableInfo() {
if (!(partitionNamedExpression instanceof SlotReference)
|| !partitionNamedExpression.isColumnFromTable()) {
|| !partitionNamedExpression.isColumnFromTable()
|| !((SlotReference) partitionNamedExpression).getOriginalTable().isPresent()) {
return null;
}
return new BaseTableInfo(((SlotReference) partitionNamedExpression).getOriginalTable().get());
Expand All @@ -135,7 +136,8 @@ public String getColumnStr() {
*/
public Column getColumn() {
if (!(partitionNamedExpression instanceof SlotReference)
|| !partitionNamedExpression.isColumnFromTable()) {
|| !partitionNamedExpression.isColumnFromTable()
|| !((SlotReference) partitionNamedExpression).getOriginalTable().isPresent()) {
return null;
}
return extractColumn(this.partitionNamedExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.doris.analysis.StringLiteral;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.PartitionType;
import org.apache.doris.common.Pair;
import org.apache.doris.datasource.mvcc.MvccUtil;
import org.apache.doris.mtmv.BaseColInfo;
import org.apache.doris.mtmv.BaseTableInfo;
Expand All @@ -51,6 +52,7 @@

import com.google.common.collect.Lists;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -125,8 +127,13 @@ private void fillPctInfos(NereidsPlanner planner, String partitionColName,
if (partitionExpression.isPresent() && partitionExpression.get().getExpressionName()
.equalsIgnoreCase(PARTITION_BY_FUNCTION_NAME)) {
DateTrunc dateTrunc = (DateTrunc) partitionExpression.get();
List<Pair<Integer, Expr>> paramPairs = convertDateTruncToLegacyArguments(dateTrunc.children());
List<Expr> params = paramPairs.stream()
.sorted(Comparator.comparingInt(Pair::key))
.map(Pair::value)
.collect(Collectors.toList());
mtmvPartitionInfo.setExpr(new FunctionCallExpr(dateTrunc.getName(),
new FunctionParams(convertToLegacyArguments(dateTrunc.children()))));
new FunctionParams(params)));
mtmvPartitionInfo.setPartitionType(MTMVPartitionType.EXPR);
this.partitionType = MTMVPartitionType.EXPR;
}
Expand Down Expand Up @@ -168,15 +175,15 @@ private void fillPctInfos(NereidsPlanner planner, String partitionColName,
mtmvPartitionInfo.setPctInfos(pctInfos);
}

private static List<Expr> convertToLegacyArguments(List<Expression> children) {
private static List<Pair<Integer, Expr>> convertDateTruncToLegacyArguments(List<Expression> children) {
return children.stream().map(MTMVPartitionDefinition::convertToLegacyRecursion).collect(Collectors.toList());
}

private static Expr convertToLegacyRecursion(Expression expression) {
private static Pair<Integer, Expr> convertToLegacyRecursion(Expression expression) {
if (expression instanceof Slot) {
return new SlotRef(null, ((Slot) expression).getName());
return Pair.of(1, new SlotRef(null, ((Slot) expression).getName()));
} else if (expression instanceof Literal) {
return new StringLiteral(((Literal) expression).getStringValue());
return Pair.of(2, new StringLiteral(((Literal) expression).getStringValue()));
} else if (expression instanceof Cast) {
// mv partition roll up only need the slot in cast
return convertToLegacyRecursion(((Cast) expression).child());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ protected void runBeforeAll() throws Exception {
+ " PROPERTIES (\n"
+ " \"replication_num\" = \"1\"\n"
+ " )");

createView("CREATE VIEW lineitem_daily_summary_view AS\n"
+ "SELECT \n"
+ " DATE_TRUNC('day', L_SHIPDATE) AS ship_date,\n"
+ " L_RETURNFLAG,\n"
+ " L_LINESTATUS,\n"
+ " COUNT(*) AS order_count,\n"
+ " SUM(L_QUANTITY) AS total_quantity,\n"
+ " SUM(L_EXTENDEDPRICE) AS total_price,\n"
+ " AVG(L_DISCOUNT) AS avg_discount\n"
+ "FROM lineitem\n"
+ "WHERE L_SHIPDATE IS NOT NULL\n"
+ "GROUP BY ship_date, L_RETURNFLAG, L_LINESTATUS;");

// Should not make scan to empty relation when the table used by materialized view has no data
connectContext.getSessionVariable().setDisableNereidsRules("OLAP_SCAN_PARTITION_PRUNE,PRUNE_EMPTY_PARTITION");
}
Expand Down Expand Up @@ -1165,6 +1179,31 @@ public void test41() {
});
}

// test with view which contains date_trunc
@Test
public void test42() {
PlanChecker.from(connectContext)
.checkExplain("SELECT\n"
+ " ship_date,\n"
+ " L_RETURNFLAG,\n"
+ " SUM(order_count) AS total_orders,\n"
+ " SUM(total_quantity) AS sum_quantity,\n"
+ " SUM(total_price) AS sum_price,\n"
+ " AVG(avg_discount) AS average_discount\n"
+ "FROM lineitem_daily_summary_view\n"
+ "GROUP BY ship_date, L_RETURNFLAG\n"
+ "ORDER BY ship_date, L_RETURNFLAG, total_orders, sum_quantity, sum_price;",
nereidsPlanner -> {
Plan rewrittenPlan = nereidsPlanner.getRewrittenPlan();
RelatedTableInfo relatedTableInfo =
MaterializedViewUtils.getRelatedTableInfos("ship_date", null,
rewrittenPlan, nereidsPlanner.getCascadesContext());
successWith(relatedTableInfo,
ImmutableSet.of(ImmutableList.of("lineitem", "l_shipdate", "true", "true")),
"day");
});
}

private static void successWith(RelatedTableInfo relatedTableInfo,
Set<List<String>> expectTableColumnPairSet, String timeUnit) {
Assertions.assertFalse(relatedTableInfo.getTableColumnInfos().isEmpty());
Expand All @@ -1180,9 +1219,9 @@ private static void successWith(RelatedTableInfo relatedTableInfo,
}
if (StringUtils.isNotEmpty(timeUnit) && partitionExpression.isPresent()) {
List<DateTrunc> dateTruncs = partitionExpression.get().collectToList(DateTrunc.class::isInstance);
anyFoundDateTrunc = anyFoundDateTrunc
|| (dateTruncs.size() == 1
&& Objects.equals("'" + timeUnit + "'", dateTruncs.get(0).getArgument(1).toString().toLowerCase()));
anyFoundDateTrunc = anyFoundDateTrunc || (dateTruncs.size() == 1
&& (Objects.equals("'" + timeUnit + "'", dateTruncs.get(0).getArgument(0).toString().toLowerCase())
|| Objects.equals("'" + timeUnit + "'", dateTruncs.get(0).getArgument(1).toString().toLowerCase())));
}
try {
relatedTableColumnPairs.add(
Expand Down
Loading