Skip to content

Commit cad4abb

Browse files
authored
[fix](nereids) Fix dead visitor dispatch of PhysicalLazyMaterializeTVFScan (#68871)
### What problem does this PR solve? Issue Number: N/A Problem Summary: `PhysicalLazyMaterializeTVFScan.accept()` dispatched to `visitPhysicalTVFRelation`, so the dedicated `PhysicalPlanTranslator.visitPhysicalLazyMaterializeTVFScan` was unreachable dead code and every TVF scan under TopN lazy materialization was translated by the parent method. This PR dispatches `accept()` to `visitPhysicalLazyMaterializeTVFScan`, consistent with `PhysicalLazyMaterializeOlapScan`, so the dedicated translation path stays reachable. No behavior change today since both translator methods are identical, and the added unit test pins the dispatch. ### Release note None ### Check List (For Author) - Test - [ ] Regression test - [x] Unit Test - `PhysicalLazyMaterializeTVFScanVisitorTest` (new), plus `TopnLazyMaterializeTest`, `MaterializeProbeVisitorTest`, `LazyMaterializeTopNTest`, `TopNRuntimeFilterTest` (27 tests, all pass) - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - Behavior changed: - [x] No. - [ ] Yes. - Does this need documentation? - [x] No. - [ ] Yes. ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label
1 parent 358e87c commit cad4abb

2 files changed

Lines changed: 182 additions & 1 deletion

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/physical/PhysicalLazyMaterializeTVFScan.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public String toString() {
8585

8686
@Override
8787
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
88-
return visitor.visitPhysicalTVFRelation(this, context);
88+
return visitor.visitPhysicalLazyMaterializeTVFScan(this, context);
8989
}
9090

9191
@Override
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.plans.physical;
19+
20+
import org.apache.doris.nereids.trees.plans.Plan;
21+
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
22+
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Assumptions;
25+
import org.junit.jupiter.api.Test;
26+
import org.objectweb.asm.ClassReader;
27+
import org.objectweb.asm.ClassVisitor;
28+
import org.objectweb.asm.MethodVisitor;
29+
import org.objectweb.asm.Opcodes;
30+
31+
import java.io.IOException;
32+
import java.io.InputStream;
33+
import java.lang.reflect.Method;
34+
import java.lang.reflect.Modifier;
35+
import java.net.URL;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.nio.file.Paths;
39+
import java.util.ArrayList;
40+
import java.util.LinkedHashSet;
41+
import java.util.List;
42+
import java.util.Set;
43+
import java.util.stream.Collectors;
44+
import java.util.stream.Stream;
45+
46+
/**
47+
* Consistency check of the physical plan visitor dispatch.
48+
*
49+
* <p>Every plan node that implements {@code accept()} itself hard codes the {@code visitXxx} method of
50+
* {@link PlanVisitor} that handles it. When such a node has a dedicated hook in {@link PlanVisitor},
51+
* the dispatch has to reach that hook: dispatching to a parent type silently turns the dedicated hook
52+
* into unreachable dead code, and nothing catches it - the compiler is happy and no runtime test fails.
53+
*
54+
* <p>The check reads the invoked method out of the compiled {@code accept()} body, so it covers every
55+
* physical plan node instead of a hand picked one. Nodes that inherit {@code accept()} from a parent
56+
* plan are out of scope by construction.
57+
*/
58+
public class PhysicalPlanVisitorDispatchTest {
59+
60+
/** Guards against the class path scan silently finding nothing and making this test vacuous. */
61+
private static final int MIN_CHECKED_PLAN_CLASSES = 30;
62+
63+
private static final String PLAN_PACKAGE = "org.apache.doris.nereids.trees.plans.physical";
64+
65+
private static final String ACCEPT_DESCRIPTOR =
66+
"(Lorg/apache/doris/nereids/trees/plans/visitor/PlanVisitor;Ljava/lang/Object;)Ljava/lang/Object;";
67+
68+
@Test
69+
public void acceptDispatchesToItsOwnMostSpecificVisitorMethod() throws Exception {
70+
Path classesRoot = classesRoot();
71+
Path packageRoot = classesRoot.resolve(PLAN_PACKAGE.replace('.', '/'));
72+
List<String> violations = new ArrayList<>();
73+
int checked = 0;
74+
75+
for (Path classFile : planClassFiles(packageRoot)) {
76+
Class<?> planClass = Class.forName(toClassName(packageRoot, classFile), false,
77+
PhysicalPlanVisitorDispatchTest.class.getClassLoader());
78+
if (!Plan.class.isAssignableFrom(planClass) || Modifier.isAbstract(planClass.getModifiers())
79+
|| !declaresAccept(planClass)) {
80+
continue;
81+
}
82+
String dispatched = dispatchedVisitorMethod(classFile);
83+
if (dispatched == null) {
84+
// accept() does not dispatch through a visit method, nothing to verify.
85+
continue;
86+
}
87+
checked++;
88+
String expected = mostSpecificVisitorMethod(planClass);
89+
if (!dispatched.equals(expected)) {
90+
violations.add(planClass.getSimpleName() + ".accept() dispatches to " + dispatched
91+
+ "(), but the most specific visitor method is " + expected + "()");
92+
}
93+
}
94+
95+
Assertions.assertTrue(violations.isEmpty(),
96+
"accept() dispatches to the wrong visitor method:\n" + String.join("\n", violations));
97+
Assertions.assertTrue(checked >= MIN_CHECKED_PLAN_CLASSES,
98+
"only " + checked + " physical plan classes were checked, the class path scan is broken");
99+
}
100+
101+
private static Path classesRoot() throws Exception {
102+
URL location = PlanVisitor.class.getProtectionDomain().getCodeSource().getLocation();
103+
Path root = Paths.get(location.toURI());
104+
Assumptions.assumeTrue(Files.isDirectory(root),
105+
"plan classes are not available as a directory: " + root);
106+
return root;
107+
}
108+
109+
private static List<Path> planClassFiles(Path packageRoot) throws IOException {
110+
Assumptions.assumeTrue(Files.isDirectory(packageRoot), "no plans directory: " + packageRoot);
111+
try (Stream<Path> files = Files.walk(packageRoot)) {
112+
return files.filter(path -> path.toString().endsWith(".class"))
113+
.filter(path -> !path.toString().contains("$"))
114+
.filter(path -> !path.getFileName().toString().equals("package-info.class"))
115+
.collect(Collectors.toList());
116+
}
117+
}
118+
119+
private static String toClassName(Path packageRoot, Path classFile) {
120+
String relative = packageRoot.relativize(classFile).toString();
121+
return PLAN_PACKAGE + "."
122+
+ relative.substring(0, relative.length() - ".class".length()).replace('/', '.');
123+
}
124+
125+
private static boolean declaresAccept(Class<?> planClass) {
126+
try {
127+
planClass.getDeclaredMethod("accept", PlanVisitor.class, Object.class);
128+
return true;
129+
} catch (NoSuchMethodException e) {
130+
return false;
131+
}
132+
}
133+
134+
/** Reads the visitor method invoked inside {@code accept()} from the compiled class file. */
135+
private static String dispatchedVisitorMethod(Path classFile) throws IOException {
136+
Set<String> invoked = new LinkedHashSet<>();
137+
byte[] bytes;
138+
try (InputStream in = Files.newInputStream(classFile)) {
139+
bytes = in.readAllBytes();
140+
}
141+
new ClassReader(bytes).accept(new ClassVisitor(Opcodes.ASM9) {
142+
@Override
143+
public MethodVisitor visitMethod(int access, String name, String descriptor,
144+
String signature, String[] exceptions) {
145+
if (!"accept".equals(name) || !ACCEPT_DESCRIPTOR.equals(descriptor)) {
146+
return null;
147+
}
148+
return new MethodVisitor(Opcodes.ASM9) {
149+
@Override
150+
public void visitMethodInsn(int opcode, String owner, String methodName,
151+
String methodDescriptor, boolean isInterface) {
152+
if (methodName.startsWith("visit")) {
153+
invoked.add(methodName);
154+
}
155+
}
156+
};
157+
}
158+
}, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
159+
return invoked.size() == 1 ? invoked.iterator().next() : null;
160+
}
161+
162+
/**
163+
* The visit method whose visited parameter is the closest super type of {@code planClass}, i.e. the
164+
* one declared for this exact plan node when such a hook exists.
165+
*/
166+
private static String mostSpecificVisitorMethod(Class<?> planClass) {
167+
Method mostSpecific = null;
168+
for (Method method : PlanVisitor.class.getMethods()) {
169+
if (!method.getName().startsWith("visit") || method.getParameterCount() != 2) {
170+
continue;
171+
}
172+
Class<?> visited = method.getParameterTypes()[0];
173+
if (visited.isAssignableFrom(planClass)
174+
&& (mostSpecific == null
175+
|| mostSpecific.getParameterTypes()[0].isAssignableFrom(visited))) {
176+
mostSpecific = method;
177+
}
178+
}
179+
return mostSpecific == null ? null : mostSpecific.getName();
180+
}
181+
}

0 commit comments

Comments
 (0)