Skip to content

Commit 6295f40

Browse files
liutang123dataroaring
authored andcommitted
[fix](mtmv) Choose a valid partition column when there are both valid and invalid expressions (#38367)
Choose a partition column when there are both valid and invalid expressions.
1 parent e679879 commit 6295f40

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,8 @@ private SlotReference getContextPartitionColumn(IncrementCheckerContext context)
580580
private static boolean checkPartition(Collection<? extends Expression> expressionsToCheck, Plan plan,
581581
IncrementCheckerContext context) {
582582
NamedExpression partitionColumn = context.getMvPartitionColumn();
583-
for (Expression projectSlot : expressionsToCheck) {
583+
584+
OUTER_CHECK: for (Expression projectSlot : expressionsToCheck) {
584585
if (projectSlot.isColumnFromTable() && projectSlot.equals(partitionColumn.toSlot())) {
585586
continue;
586587
}
@@ -612,7 +613,7 @@ private static boolean checkPartition(Collection<? extends Expression> expressio
612613
String.format("partition expression use more than one slot reference, invalid "
613614
+ "expressionToCheckColumns is %s, partitionColumnDateColumns is %s",
614615
expressionToCheckColumns, partitionColumns));
615-
return false;
616+
continue;
616617
}
617618
List<Expression> expressions = expressionToCheck.collectToList(Expression.class::isInstance);
618619
for (Expression expression : expressions) {
@@ -621,7 +622,7 @@ private static boolean checkPartition(Collection<? extends Expression> expressio
621622
context.addFailReason(
622623
String.format("column to check use invalid implicit expression, invalid "
623624
+ "expression is %s", expression));
624-
return false;
625+
continue OUTER_CHECK;
625626
}
626627
}
627628
List<Expression> partitionExpressions = partitionExpression.collectToList(
@@ -632,7 +633,7 @@ private static boolean checkPartition(Collection<? extends Expression> expressio
632633
context.addFailReason(
633634
String.format("partition column use invalid implicit expression, invalid "
634635
+ "expression is %s", expression));
635-
return false;
636+
continue OUTER_CHECK;
636637
}
637638
}
638639
List<DateTrunc> expressionToCheckDataTruncList =
@@ -643,16 +644,17 @@ private static boolean checkPartition(Collection<? extends Expression> expressio
643644
// mv time unit level is little then query
644645
context.addFailReason("partition column time unit level should be "
645646
+ "greater than sql select column");
646-
return false;
647+
continue;
647648
}
648649
if (!partitionColumn.isColumnFromTable()) {
649650
context.setMvPartitionColumn(partitionColumns.iterator().next());
650651
}
651652
if (!context.getPartitionExpression().isPresent()) {
652653
context.setPartitionExpression(partitionExpression);
653654
}
655+
return true;
654656
}
655-
return true;
657+
return context.getMvPartitionColumn().isColumnFromTable();
656658
}
657659
}
658660

fe/fe-core/src/test/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtilsTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ protected void runBeforeAll() throws Exception {
234234
+ " \"replication_num\" = \"1\"\n"
235235
+ ");\n"
236236
);
237+
createTable("CREATE TABLE `test3` (\n"
238+
+ " `id` VARCHAR(36) NOT NULL COMMENT 'id',\n"
239+
+ " `created_time` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''\n"
240+
+ ") ENGINE=OLAP\n"
241+
+ "DUPLICATE KEY(`id`)\n"
242+
+ "COMMENT ''\n"
243+
+ "PARTITION BY RANGE(`created_time`)\n"
244+
+ "(PARTITION P_2024071713 VALUES [('2024-07-17 13:00:00'), ('2024-07-17 14:00:00')),\n"
245+
+ "PARTITION P_2024071714 VALUES [('2024-07-17 14:00:00'), ('2024-07-17 15:00:00')))\n"
246+
+ "DISTRIBUTED BY HASH(`id`) BUCKETS AUTO\n"
247+
+ "PROPERTIES (\n"
248+
+ "\"replication_allocation\" = \"tag.location.default: 1\"\n"
249+
+ ");\n");
237250
// Should not make scan to empty relation when the table used by materialized view has no data
238251
connectContext.getSessionVariable().setDisableNereidsRules("OLAP_SCAN_PARTITION_PRUNE,PRUNE_EMPTY_PARTITION");
239252
}
@@ -809,6 +822,42 @@ public void containTableQueryOperatorWithoutOperatorTest() {
809822
});
810823
}
811824

825+
@Test
826+
public void getRelatedTableInfoWhenMultiPartitionExprs() {
827+
PlanChecker.from(connectContext)
828+
.checkExplain("select id, date_trunc(created_time, 'minute') as created_time_minute,"
829+
+ " min(created_time) as start_time,"
830+
+ " if(count(id) > 0, 1, 0) as status\n"
831+
+ " from test3 \n"
832+
+ " group by id, date_trunc(created_time, 'minute')",
833+
nereidsPlanner -> {
834+
Plan rewrittenPlan = nereidsPlanner.getRewrittenPlan();
835+
RelatedTableInfo relatedTableInfo =
836+
MaterializedViewUtils.getRelatedTableInfo("created_time_minute",
837+
"day", rewrittenPlan, nereidsPlanner.getCascadesContext());
838+
checkRelatedTableInfo(relatedTableInfo,
839+
"test3",
840+
"created_time",
841+
true);
842+
});
843+
PlanChecker.from(connectContext)
844+
.checkExplain("select id, date_trunc(created_time, 'hour') as created_time_hour,"
845+
+ " min(created_time) as start_time\n"
846+
+ " from test3 \n"
847+
+ " group by id, date_trunc(created_time, 'minute'),"
848+
+ " date_trunc(created_time, 'hour');",
849+
nereidsPlanner -> {
850+
Plan rewrittenPlan = nereidsPlanner.getRewrittenPlan();
851+
RelatedTableInfo relatedTableInfo =
852+
MaterializedViewUtils.getRelatedTableInfo("created_time_hour",
853+
"day", rewrittenPlan, nereidsPlanner.getCascadesContext());
854+
checkRelatedTableInfo(relatedTableInfo,
855+
"test3",
856+
"created_time",
857+
true);
858+
});
859+
}
860+
812861
private void checkRelatedTableInfo(RelatedTableInfo relatedTableInfo,
813862
String expectTableName,
814863
String expectColumnName,

0 commit comments

Comments
 (0)