-
Notifications
You must be signed in to change notification settings - Fork 2.5k
RelNodeVisitCoverageTest for test RelShuttle visit coverage #4641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package org.apache.calcite.test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.github.classgraph.ClassGraph; | ||
| import io.github.classgraph.ClassInfo; | ||
| import io.github.classgraph.ScanResult; | ||
|
|
||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.RelShuttle; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Modifier; | ||
| import java.util.Comparator; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Discovers every concrete {@link RelNode} implementation on the classpath and | ||
| * verifies {@link RelShuttle} declares a compatible {@code visit} method. | ||
| */ | ||
| class RelNodeVisitCoverageTest { | ||
|
|
||
| @Test void relShuttleCoversAllRelNodes() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so what's the result of the test?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a work in progress. The current logic can obtain the set of all classes that inherit from RelNode, as well as the set of all classes visited in RelShuttle. The current test only compares whether these two sets are equal. We might also need a criterion to determine which classes should be included in the visit and which classes should be excluded. |
||
| Set<Class<? extends RelNode>> relNodeClasses = findRelNodeClasses(); | ||
| Set<Class<?>> visitParameters = visitParameterTypes(); | ||
|
|
||
| Set<Class<? extends RelNode>> missing = relNodeClasses.stream() | ||
| .filter(relClass -> visitParameters.stream() | ||
| .noneMatch(relClass::isAssignableFrom)) | ||
| .collect(Collectors.toCollection(() -> | ||
| new TreeSet<>(Comparator.comparing(Class::getName)))); | ||
|
|
||
| assertTrue(missing.isEmpty(), | ||
| () -> "RelShuttle.visit(...) is missing for: " + missing); | ||
| } | ||
|
|
||
| private Set<Class<?>> visitParameterTypes() { | ||
| Set<Class<?>> visitParameters = new HashSet<>(); | ||
| for (Method method : RelShuttle.class.getMethods()) { | ||
| if ("visit".equals(method.getName()) && method.getParameterCount() == 1) { | ||
| visitParameters.add(method.getParameterTypes()[0]); | ||
| } | ||
| } | ||
| return visitParameters; | ||
| } | ||
|
|
||
| private Set<Class<? extends RelNode>> findRelNodeClasses() { | ||
| Set<Class<? extends RelNode>> result = | ||
| new TreeSet<>(Comparator.comparing(Class::getName)); | ||
| try (ScanResult scan = new ClassGraph() | ||
| .enableClassInfo() | ||
| .ignoreClassVisibility() | ||
| .acceptPackages(RelNode.class.getPackageName()) | ||
| .scan()) { | ||
| for (ClassInfo classInfo : scan.getClassesImplementing(RelNode.class.getName())) { | ||
| Class<? extends RelNode> clazz = classInfo.loadClass(RelNode.class); | ||
| if (isConcreteRelNode(clazz)) { | ||
| result.add(clazz); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private boolean isConcreteRelNode(Class<?> clazz) { | ||
| return RelNode.class.isAssignableFrom(clazz) | ||
| && !clazz.isInterface() | ||
| && !Modifier.isAbstract(clazz.getModifiers()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verifies whether