hello-stephen commented on code in PR #71440:
URL: https://github.com/apache/doris/pull/71440#discussion_r4807240047
##########
fe/fe-core/src/test/java/org/apache/doris/mtmv/ivm/IvmAggDeltaHandlerTest.java:
##########
@@ -916,6 +923,139 @@ void
testGroupedAggFullKeysWithSameNamedGroupKeysKeepsDistinctDeltaSlots() {
Assertions.assertTrue(deltaSides.contains(deltaKeySlots.get(1).getExprId()));
}
+ private void markMvKeyColumns(MTMV mtmv, String... keyColNames) {
+ Set<String> keySet = new HashSet<>(Arrays.asList(keyColNames));
+ for (Column col : mtmv.getBaseSchema(true)) {
+ if (keySet.contains(col.getName())) {
+ col.setIsKey(true);
+ }
+ }
+ }
+
+ private AggRewriteResult rewriteAggPlanWithMvKeys(Plan plan, List<Slot>
identityKeys, String... mvKeyCols) {
+ ConnectContext connectContext = newConnectContext();
+ JobContext jobContext = newJobContextForRoot(plan, connectContext);
+ Plan normalizedPlan = new IvmNormalizeUniqKeysMTMV().normalize(plan,
jobContext);
+ IvmRewriteResult rewriteResult = publishNormalizeResult(jobContext,
normalizedPlan);
+ PlanBundle bundle = new PlanBundle(connectContext, normalizedPlan,
rewriteResult);
+ if (identityKeys != null) {
+ bundle.rewriteResult.setIdentityKeySlots(identityKeys);
+ }
+ MTMV mtmv = buildMtmvFromPlan(bundle.normalizedPlan.getOutput());
+ markMvKeyColumns(mtmv, mvKeyCols);
+ mtmv.getIvmInfo().advanceSequencePrefix();
+ mtmv.getIvmInfo().advanceSequencePrefix();
+ Plan rewritten = new IvmDeltaRewriter().generateIncrRefreshPlan(
+ bundle.normalizedPlan, bundle.rewriteResult,
+ IvmRewriteContext.incremental(mtmv), bundle.connectContext);
+ Assertions.assertNotNull(rewritten);
+ InsertIntoTableCommand command = new IvmIncrRefreshManager()
+ .buildInsertCommand((LogicalPlan) rewritten, mtmv);
+ UnboundTableSink<?> sink = getSink(command);
+ return new AggRewriteResult(bundle, mtmv, sink, (LogicalProject<?>)
sink.child());
+ }
+
+ @Test
+ void testApplyJoinAddsVisibleMvKeyConjunctsWithoutFullKeys() {
Review Comment:
[P3] Cover the new Bucket Shuffle path in a physical refresh test
`buildMtmvFromPlan` gives this MV `RandomDistributionInfo`, and `getJoin`
inspects a logical join. The test would still pass if an actual MV distributed
by `HASH(k)` shuffled both apply inputs. Please add a physical incremental
refresh plan assertion for a single-partition grouped MV distributed on `k`,
with full keys off, that checks for `BUCKET_SHUFFLE` on the apply join.
##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmAggDeltaHandler.java:
##########
@@ -436,15 +444,143 @@ private ImmutableList<Expression>
buildApplyJoinConjuncts(IvmIncrRefreshContext
if (deltaKey == null) {
continue;
}
+ addedMvSlots.add(mvKey.getExprId());
conjuncts.add(new NullSafeEqual(mvKey, deltaKey));
}
}
+ // When an aggregate MV defines explicit KEY, PARTITION BY, or HASH
distribution columns,
+ // those visible columns precede __DORIS_IVM_ROW_ID_COL__ in the
physical schema. Adding
+ // redundant equality conjuncts on any visible key column that
directly passes through a
+ // group key unlocks Bucket Shuffle Join (and runtime-filter / prefix
pruning when both
+ // sides are non-nullable and use EqualTo) even without
ivm_use_full_keys.
+ Set<String> visibleMvKeyNames =
collectVisibleMvKeyColumnNames(ctx.getMtmv());
+ if (!visibleMvKeyNames.isEmpty()) {
+ Set<String> targetLowerNames = visibleMvKeyNames.stream()
+ .map(name -> name.toLowerCase(Locale.ROOT))
+ .collect(Collectors.toSet());
+ Map<String, Integer> directGroupKeyByMvColName =
resolveDirectGroupKeyMapping(
+ rewriteState.normalizedRootPlan(), normalizedAgg, aggMeta,
targetLowerNames);
+ for (String keyColName : visibleMvKeyNames) {
+ Integer groupKeyIndex =
directGroupKeyByMvColName.get(keyColName.toLowerCase(Locale.ROOT));
+ if (groupKeyIndex == null) {
+ continue;
+ }
+ Slot mvKey =
helper.findSlotByNameOrNull(rawMvScan.getOutput(), keyColName);
+ Slot deltaKey = delta.deltaGroupKeySlots.get(groupKeyIndex);
+ if (mvKey == null ||
!mvKey.getDataType().equals(deltaKey.getDataType())
+ || !addedMvSlots.add(mvKey.getExprId())) {
+ continue;
+ }
+ conjuncts.add(!mvKey.nullable() && !deltaKey.nullable()
+ ? new EqualTo(mvKey, deltaKey)
+ : new NullSafeEqual(mvKey, deltaKey));
+ }
+ }
// Row-id may be NULL (single key used directly as largeint), so match
with null-safe
// equality: a NULL row-id matches the MV row of the NULL-key group.
- conjuncts.add(new NullSafeEqual(mvRowId, delta.rowIdSlot));
+ if (addedMvSlots.add(mvRowId.getExprId())) {
+ conjuncts.add(new NullSafeEqual(mvRowId, delta.rowIdSlot));
+ }
return conjuncts.build();
}
+ private Set<String> collectVisibleMvKeyColumnNames(MTMV mtmv) {
+ Set<String> keyNames = new LinkedHashSet<>();
+ for (Column col : mtmv.getKeyColumnsByIndexId(mtmv.getBaseIndexId())) {
+ if (col.isVisible() && !IvmUtil.isIvmHiddenColumn(col.getName())) {
+ keyNames.add(col.getName());
+ }
+ }
+ for (Column col : mtmv.getPartitionColumns()) {
+ if (col.isVisible() && !IvmUtil.isIvmHiddenColumn(col.getName())) {
+ keyNames.add(col.getName());
+ }
+ }
+ // getDistributionColumnNames() returns lowercased names, so resolve
via catalog column
+ // to check visibility and preserve original case for
IvmUtil.isIvmHiddenColumn().
+ for (String colName : mtmv.getDistributionColumnNames()) {
+ Column col = mtmv.getColumn(colName);
+ if (col != null && col.isVisible() &&
!IvmUtil.isIvmHiddenColumn(col.getName())) {
+ keyNames.add(col.getName());
+ }
+ }
+ return keyNames;
+ }
+
+ /**
+ * Maps each requested visible MV output column name (lowercased) to its
group-key index when
+ * the column is a direct passthrough (or plain rename) of a group key
slot of
+ * {@code normalizedAgg}. Columns that apply any expression or cast above
the aggregate are
+ * omitted so we never pair a transformed MV value against an
untransformed delta group key.
+ */
+ private Map<String, Integer> resolveDirectGroupKeyMapping(Plan rootPlan,
+ LogicalAggregate<?> normalizedAgg, IvmAggMeta aggMeta, Set<String>
targetLowerNames) {
+ if (rootPlan == null || aggMeta.isScalarAgg() ||
targetLowerNames.isEmpty()) {
+ return Collections.emptyMap();
+ }
+ List<LogicalProject<?>> projectsAboveAgg = new ArrayList<>();
+ Plan current = rootPlan;
+ while (current != normalizedAgg) {
+ if (current instanceof LogicalProject) {
+ projectsAboveAgg.add((LogicalProject<?>) current);
+ current = current.child(0);
+ } else if (current instanceof LogicalSubQueryAlias || current
instanceof LogicalFilter) {
+ current = current.child(0);
+ } else {
+ return Collections.emptyMap();
+ }
+ }
+ Map<String, Integer> result = new HashMap<>();
+ Set<String> ambiguousNames = new HashSet<>();
+ for (Slot rootSlot : rootPlan.getOutput()) {
+ String colName = rootSlot.getName();
+ String normalizedName = colName.toLowerCase(Locale.ROOT);
Review Comment:
[P2] Keep distinct Unicode column names separate when resolving apply keys
The catalog treats `i̇` (U+0069 U+0307) and `İ` (U+0130) as distinct names,
but `toLowerCase(Locale.ROOT)` maps both to `i̇`. Consider a grouped MV with ``
KEY(`i̇`) `` and this output:
```text
Project(CAST(x + 1 AS INT) AS `i̇`, x AS `İ`, cnt)
Aggregate(GROUP BY x, COUNT(*) AS cnt)
```
`resolveDirectGroupKeyMapping` skips the transformed `i̇`, then registers
the direct alias `İ` under the same lowercase name. `buildApplyJoinConjuncts`
adds `mv.i̇ = delta.x` alongside the matching row-id. For `x = 7`, the extra
predicate compares 8 with 7, so the apply step treats the old group as absent.
Starting from a stored count of 1, a second input row for that group writes 1
instead of 2. Use the catalog's `String.CASE_INSENSITIVE_ORDER` on the original
spellings for the candidate set, lineage map, and ambiguity tracking, and add a
regression for these two aliases.
--
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]