hello-stephen commented on code in PR #71368:
URL: https://github.com/apache/doris/pull/71368#discussion_r4786328252
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewUtils.java:
##########
@@ -205,15 +206,33 @@ public Void visitLogicalCTEProducer(LogicalCTEProducer<?
extends Plan> cteProduc
}
/**
- * Check the partition expression date_trunc num is valid or not
+ * Check that each partition expression has at most one date_trunc, all
tracked base partition
+ * columns with date_trunc use the same time unit, and multiple references
to the same base table
+ * agree on whether and how they truncate.
*/
private static boolean
checkPartitionRefExpression(Collection<RelatedTableColumnInfo> refExpressions) {
+ String expectedDateTruncUnit = null;
+ Map<BaseTableInfo, Optional<String>> tableUnits = new HashMap<>();
for (RelatedTableColumnInfo tableColumnInfo : refExpressions) {
- if (tableColumnInfo.getPartitionExpression().isPresent()) {
- // If partition ref up expression is empty, return false
directly
- List<DateTrunc> dateTruncs =
-
tableColumnInfo.getPartitionExpression().get().collectToList(DateTrunc.class::isInstance);
- if (dateTruncs.size() > 1) {
+ Optional<String> unit =
PartitionIncrementMaintainer.extractDateTruncUnit(
+ tableColumnInfo.getPartitionExpression());
+ if (unit == null) {
+ return false;
+ }
+ if (!tableColumnInfo.isFromTablePartitionColumn()) {
+ continue;
+ }
+ if (unit.isPresent()) {
+ if (expectedDateTruncUnit == null) {
+ expectedDateTruncUnit = unit.get();
+ } else if (!expectedDateTruncUnit.equals(unit.get())) {
+ return false;
+ }
+ }
+ BaseTableInfo tableInfo = tableColumnInfo.getTableInfo();
+ if (tableInfo != null) {
+ Optional<String> previousUnit =
tableUnits.putIfAbsent(tableInfo, unit);
+ if (previousUnit != null && !previousUnit.equals(unit)) {
Review Comment:
[P2] Do not reject equivalent same-table mappings after separate UNION checks
With DATETIME tables `t` and `u` partitioned into day-aligned RANGE(dt)
partitions, consider `PARTITION BY (q)` over:
```sql
SELECT a.q, b.v
FROM (
SELECT date_trunc(dt, 'day') AS q FROM t
UNION ALL SELECT date_trunc(dt, 'day') AS q FROM u
) a
JOIN (
SELECT dt, v FROM t
UNION ALL SELECT dt, v FROM u
) b ON a.q = b.dt
```
The join propagates tracking from `a.q` to `b.dt`. Each UNION independently
passes the new branch check (DAY/DAY on the left, bare/bare on the right). Each
branch uses independent scan slots, so their context merges do not collide. The
global `tableUnits` check nevertheless sees DAY and bare entries for each of
the same two `BaseTableInfo` keys and rejects the MV before table deduplication.
The join guarantees that all contributing rows on both sides come from the
day represented by `q`; bare and DAY rollup produce identical daily bounds and
the refresh filter is valid for all occurrences of each table. Fixing the check
inside a single UNION arm would not fix this separate global rejection. Retain
information about the proven partition equivalence when validating repeated
table references.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/PartitionIncrementMaintainer.java:
##########
@@ -810,6 +860,45 @@ public Map<NamedExpression, RelatedTableColumnInfo>
getPartitionAndRefExpression
return partitionAndRefExpressionMap;
}
+ /**
+ * Record a tracked partition expression for a slot, failing fast if
an existing entry for the
+ * same slot has a conflicting date_trunc unit.
+ */
+ public boolean putPartitionAndRefExpression(NamedExpression key,
RelatedTableColumnInfo value) {
+ RelatedTableColumnInfo existing =
partitionAndRefExpressionMap.get(key);
+ if (existing != null) {
+ Optional<String> existingUnit =
extractDateTruncUnit(existing.getPartitionExpression());
+ Optional<String> newUnit =
extractDateTruncUnit(value.getPartitionExpression());
+ if (existingUnit == null || newUnit == null ||
!existingUnit.equals(newUnit)) {
Review Comment:
[P2] Preserve proven equal partition keys when merging join lineage
On two DATETIME tables `a` and `b` with day-aligned RANGE(dt) partitions,
consider an MV partitioned by `q` with:
```sql
SELECT a.q, a.dt, b.v
FROM (SELECT date_trunc(dt, 'day') AS q, dt FROM a) a
JOIN b ON a.q = b.dt AND a.dt = b.dt
```
In the reduced Project → InnerJoin → Project/Scan plan, `checkPartition`
retains the original output slot `q` with no explicit rollup and adds `a.dt ->
DAY(a.dt)` after tracing its lineage. Both hash equalities put `q`, `a.dt` and
`b.dt` in the same equal set. In `visitLogicalJoin`, propagation adds a bare
entry for `a.dt` from `q`, and `DAY(q)` for `q` from `a.dt`. At this guard
those entries conflict with the existing ones, regardless of map iteration
order.
Every matching row has `q = a.dt = b.dt` at a day boundary, and either bare
or DAY mapping gives the same daily partition bounds. The old merge accepted
this partition-local join; the new check fails CREATE even with no UNION or
CTE. Compare canonical/effective partition lineage with the proven equality
taken into account rather than treating an unexpanded output placeholder and
its equivalent expression as conflicting.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]